Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(111)

Side by Side Diff: services/media/common/test/timeline_rate_test.cc

Issue 1952673003: Motown: Rename Ratio and LinearFunction classes (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « services/media/common/test/timeline_function_test.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <limits>
6
7 #include "mojo/services/media/common/cpp/timeline_rate.h"
8 #include "services/media/common/test/test_base.h"
9
10 namespace mojo {
11 namespace media {
12 namespace {
13
14 class TimelineRateTest : public TestBase {
15 static uint32_t gcd(uint32_t a, uint32_t b) {
16 while (b != 0) {
17 uint32_t t = a;
18 a = b;
19 b = t % b;
20 }
21 return a;
22 }
23
24 public:
25 // Verifies TimelineRate::Reduce and the constructor, ensuring that the ratio
26 // subject_delta * common_factor / reference_delta * common_factor is reduced
27 // to subject_delta / reference_delta. subject_delta and reference_delta need
28 // to be relatively prime for this to work.
29 void VerifyReduce(uint32_t subject_delta,
30 uint32_t reference_delta,
31 uint32_t common_factor) {
32 // Make sure subject_delta and reference_delta are relatively prime.
33 EXPECT_EQ(1u, gcd(subject_delta, reference_delta));
34
35 uint32_t test_subject_delta = subject_delta * common_factor;
36 uint32_t test_reference_delta = reference_delta * common_factor;
37
38 // Make sure the constructor reduces.
39 TimelineRate rate(test_subject_delta, test_reference_delta);
40 EXPECT_EQ(subject_delta, rate.subject_delta());
41 EXPECT_EQ(reference_delta, rate.reference_delta());
42
43 // Test the static method.
44 TimelineRate::Reduce(&test_subject_delta, &test_reference_delta);
45 EXPECT_EQ(subject_delta, test_subject_delta);
46 EXPECT_EQ(reference_delta, test_reference_delta);
47 }
48
49 // Verifies the TimelineRate::Scale methods by scaling value by subject_delta
50 // /
51 // reference_delta and verifying the result.
52 void VerifyScale(int64_t value,
53 uint32_t subject_delta,
54 uint32_t reference_delta,
55 int64_t result) {
56 // Test the instance method.
57 EXPECT_EQ(result,
58 TimelineRate(subject_delta, reference_delta).Scale(value));
59
60 // Test the static method.
61 EXPECT_EQ(result,
62 TimelineRate::Scale(value, subject_delta, reference_delta));
63
64 // Test the operators.
65 EXPECT_EQ(result, value * TimelineRate(subject_delta, reference_delta));
66 EXPECT_EQ(result, TimelineRate(subject_delta, reference_delta) * value);
67 if (subject_delta != 0) {
68 EXPECT_EQ(result, value / TimelineRate(reference_delta, subject_delta));
69 }
70 }
71
72 // Verifies the TimelineRate::Product methods by multiplying the given a and b
73 // rates and checking the result against the expected rate.
74 void VerifyProduct(uint32_t a_subject_delta,
75 uint32_t a_reference_delta,
76 uint32_t b_subject_delta,
77 uint32_t b_reference_delta,
78 uint32_t expected_subject_delta,
79 uint32_t expected_reference_delta,
80 bool exact) {
81 // Test the first static method.
82 uint32_t actual_subject_delta;
83 uint32_t actual_reference_delta;
84 TimelineRate::Product(a_subject_delta, a_reference_delta, b_subject_delta,
85 b_reference_delta, &actual_subject_delta,
86 &actual_reference_delta, exact);
87 EXPECT_EQ(expected_subject_delta, actual_subject_delta);
88 EXPECT_EQ(expected_reference_delta, actual_reference_delta);
89
90 // Test the second static method.
91 EXPECT_EQ(TimelineRate(expected_subject_delta, expected_reference_delta),
92 TimelineRate::Product(
93 TimelineRate(a_subject_delta, a_reference_delta),
94 TimelineRate(b_subject_delta, b_reference_delta), exact));
95
96 // Test the operator
97 if (exact) {
98 EXPECT_EQ(TimelineRate(expected_subject_delta, expected_reference_delta),
99 TimelineRate(a_subject_delta, a_reference_delta) *
100 TimelineRate(b_subject_delta, b_reference_delta));
101 }
102 }
103
104 // Verifies the TimelineRaten::Inverse method using the given rate.
105 void VerifyInverse(uint32_t subject_delta, uint32_t reference_delta) {
106 TimelineRate rate(subject_delta, reference_delta);
107 TimelineRate inverse(rate.Inverse());
108 EXPECT_EQ(rate.reference_delta(), inverse.subject_delta());
109 EXPECT_EQ(rate.subject_delta(), inverse.reference_delta());
110 }
111 };
112
113 // Tests TimelineRate::Reduce and that the TimelineRate constructor reduces.
114 TEST_F(TimelineRateTest, Reduce) {
115 VerifyReduce(0, 1, 1);
116 VerifyReduce(1, 1, 1);
117 VerifyReduce(1234, 1, 1);
118 VerifyReduce(1, 1234, 14);
119 VerifyReduce(1, 1, 1234);
120 VerifyReduce(10, 1, 1234);
121 VerifyReduce(1, 10, 1234);
122 VerifyReduce(49, 81, 1);
123 VerifyReduce(49, 81, 10);
124 VerifyReduce(49, 81, 100);
125 VerifyReduce(1, 8, 65536);
126 VerifyReduce(8, 1, 65536);
127 }
128
129 // Tests TimelineRate::Scale, static, instance and operator versions.
130 TEST_F(TimelineRateTest, Scale) {
131 const int64_t int64_min = std::numeric_limits<int64_t>::min();
132 VerifyScale(0, 0, 1, 0);
133 VerifyScale(1, 0, 1, 0);
134 VerifyScale(0, 1, 1, 0);
135 VerifyScale(1, 1, 1, 1);
136 VerifyScale(1, 2, 1, 2);
137 VerifyScale(1, 1, 2, 0);
138 VerifyScale(-1, 1, 2, -1);
139 VerifyScale(1000, 1, 2, 500);
140 VerifyScale(1001, 1, 2, 500);
141 VerifyScale(-1000, 1, 2, -500);
142 VerifyScale(-1001, 1, 2, -501);
143 VerifyScale(1000, 2, 1, 2000);
144 VerifyScale(1001, 2, 1, 2002);
145 VerifyScale(-1000, 2, 1, -2000);
146 VerifyScale(-1001, 2, 1, -2002);
147 VerifyScale(1ll << 32, 1, 1, 1ll << 32);
148 VerifyScale(1ll << 32, 1, 2, 1ll << 31);
149 VerifyScale(1ll << 32, 2, 1, 1ll << 33);
150 VerifyScale(1234ll << 30, 1, 1, 1234ll << 30);
151 VerifyScale(1234ll << 30, 1, 2, 1234ll << 29);
152 VerifyScale(1234ll << 30, 2, 1, 1234ll << 31);
153 VerifyScale(1234ll << 30, 1 << 31, 1, TimelineRate::kOverflow);
154 VerifyScale(1234ll << 30, 1ll << 31, (1ll << 31) - 2,
155 (1234ll << 30) + 1234ll);
156 VerifyScale(int64_min, 1, 1, int64_min);
157 VerifyScale(int64_min, 1, 2, int64_min / 2);
158 VerifyScale(int64_min / 2, 2, 1, int64_min);
159 VerifyScale(int64_min, 1000001, 1000000, TimelineRate::kOverflow);
160 }
161
162 // Tests TimelineRate::Product, static and operator versions.
163 TEST_F(TimelineRateTest, Product) {
164 VerifyProduct(0, 1, 0, 1, 0, 1, true);
165 VerifyProduct(1, 1, 1, 1, 1, 1, true);
166 VerifyProduct(10, 1, 1, 10, 1, 1, true);
167 VerifyProduct(4321, 1234, 617, 4321, 1, 2, true);
168 VerifyProduct(1234, 4321, 4321, 617, 2, 1, true);
169 VerifyProduct(1ll << 31, (1ll << 31) - 1, (1ll << 31) - 1, 1ll << 31, 1, 1,
170 true);
171 VerifyProduct(1ll << 31, (1ll << 31) - 1, (1ll << 31) - 2, 1ll << 31,
172 0x7ffffffe, 0x7fffffff, false);
173 }
174
175 // Tests TimelineRate::Inverse.
176 TEST_F(TimelineRateTest, Inverse) {
177 VerifyInverse(1, 1);
178 VerifyInverse(2, 1);
179 VerifyInverse(1, 2);
180 VerifyInverse(1000000, 1234);
181 VerifyInverse(1234, 1000000);
182 }
183
184 } // namespace
185 } // namespace media
186 } // namespace mojo
OLDNEW
« no previous file with comments | « services/media/common/test/timeline_function_test.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698