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

Side by Side Diff: services/media/common/test/ratio_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
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/ratio.h"
8 #include "services/media/common/test/test_base.h"
9
10 namespace mojo {
11 namespace media {
12 namespace {
13
14 class RatioTest : 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 Ratio::Reduce and the constructor, ensuring that the ratio
26 // numerator * common_factor / denominator * common_factor is reduced to
27 // numerator / denominator. numerator and denominator need to be relatively
28 // prime for this to work.
29 void VerifyReduce(uint32_t numerator,
30 uint32_t denominator,
31 uint32_t common_factor) {
32 // Make sure numerator and denominator are relatively prime.
33 EXPECT_EQ(1u, gcd(numerator, denominator));
34
35 uint32_t test_numerator = numerator * common_factor;
36 uint32_t test_denominator = denominator * common_factor;
37
38 // Make sure the constructor reduces.
39 Ratio ratio(test_numerator, test_denominator);
40 EXPECT_EQ(numerator, ratio.numerator());
41 EXPECT_EQ(denominator, ratio.denominator());
42
43 // Test the static method.
44 Ratio::Reduce(&test_numerator, &test_denominator);
45 EXPECT_EQ(numerator, test_numerator);
46 EXPECT_EQ(denominator, test_denominator);
47 }
48
49 // Verifies the Ratio::Scale methods by scaling value by numerator /
50 // denominator and verifying the result.
51 void VerifyScale(int64_t value,
52 uint32_t numerator,
53 uint32_t denominator,
54 int64_t result) {
55 // Test the instance method.
56 EXPECT_EQ(result, Ratio(numerator, denominator).Scale(value));
57
58 // Test the static method.
59 EXPECT_EQ(result, Ratio::Scale(value, numerator, denominator));
60
61 // Test the operators.
62 EXPECT_EQ(result, value * Ratio(numerator, denominator));
63 EXPECT_EQ(result, Ratio(numerator, denominator) * value);
64 if (numerator != 0) {
65 EXPECT_EQ(result, value / Ratio(denominator, numerator));
66 }
67 }
68
69 // Verifies the Ratio::Product methods by multiplying the given a and b
70 // ratios and checking the result against the expected ratio.
71 void VerifyProduct(uint32_t a_numerator,
72 uint32_t a_denominator,
73 uint32_t b_numerator,
74 uint32_t b_denominator,
75 uint32_t expected_numerator,
76 uint32_t expected_denominator,
77 bool exact) {
78 // Test the first static method.
79 uint32_t actual_numerator;
80 uint32_t actual_denominator;
81 Ratio::Product(a_numerator, a_denominator, b_numerator, b_denominator,
82 &actual_numerator, &actual_denominator, exact);
83 EXPECT_EQ(expected_numerator, actual_numerator);
84 EXPECT_EQ(expected_denominator, actual_denominator);
85
86 // Test the second static method.
87 EXPECT_EQ(Ratio(expected_numerator, expected_denominator),
88 Ratio::Product(Ratio(a_numerator, a_denominator),
89 Ratio(b_numerator, b_denominator), exact));
90
91 // Test the operator
92 if (exact) {
93 EXPECT_EQ(Ratio(expected_numerator, expected_denominator),
94 Ratio(a_numerator, a_denominator) *
95 Ratio(b_numerator, b_denominator));
96 }
97 }
98
99 // Verifies the Ration::Inverse method using the given ratio.
100 void VerifyInverse(uint32_t numerator, uint32_t denominator) {
101 Ratio ratio(numerator, denominator);
102 Ratio inverse(ratio.Inverse());
103 EXPECT_EQ(ratio.denominator(), inverse.numerator());
104 EXPECT_EQ(ratio.numerator(), inverse.denominator());
105 }
106 };
107
108 // Tests Ratio::Reduce and that the Ratio constructor reduces.
109 TEST_F(RatioTest, Reduce) {
110 VerifyReduce(0, 1, 1);
111 VerifyReduce(1, 1, 1);
112 VerifyReduce(1234, 1, 1);
113 VerifyReduce(1, 1234, 14);
114 VerifyReduce(1, 1, 1234);
115 VerifyReduce(10, 1, 1234);
116 VerifyReduce(1, 10, 1234);
117 VerifyReduce(49, 81, 1);
118 VerifyReduce(49, 81, 10);
119 VerifyReduce(49, 81, 100);
120 VerifyReduce(1, 8, 65536);
121 VerifyReduce(8, 1, 65536);
122 }
123
124 // Tests Ratio::Scale, static, instance and operator versions.
125 TEST_F(RatioTest, Scale) {
126 const int64_t int64_min = std::numeric_limits<int64_t>::min();
127 VerifyScale(0, 0, 1, 0);
128 VerifyScale(1, 0, 1, 0);
129 VerifyScale(0, 1, 1, 0);
130 VerifyScale(1, 1, 1, 1);
131 VerifyScale(1, 2, 1, 2);
132 VerifyScale(1, 1, 2, 0);
133 VerifyScale(-1, 1, 2, -1);
134 VerifyScale(1000, 1, 2, 500);
135 VerifyScale(1001, 1, 2, 500);
136 VerifyScale(-1000, 1, 2, -500);
137 VerifyScale(-1001, 1, 2, -501);
138 VerifyScale(1000, 2, 1, 2000);
139 VerifyScale(1001, 2, 1, 2002);
140 VerifyScale(-1000, 2, 1, -2000);
141 VerifyScale(-1001, 2, 1, -2002);
142 VerifyScale(1ll << 32, 1, 1, 1ll << 32);
143 VerifyScale(1ll << 32, 1, 2, 1ll << 31);
144 VerifyScale(1ll << 32, 2, 1, 1ll << 33);
145 VerifyScale(1234ll << 30, 1, 1, 1234ll << 30);
146 VerifyScale(1234ll << 30, 1, 2, 1234ll << 29);
147 VerifyScale(1234ll << 30, 2, 1, 1234ll << 31);
148 VerifyScale(1234ll << 30, 1 << 31, 1, Ratio::kOverflow);
149 VerifyScale(1234ll << 30, 1ll << 31, (1ll << 31) - 2,
150 (1234ll << 30) + 1234ll);
151 VerifyScale(int64_min, 1, 1, int64_min);
152 VerifyScale(int64_min, 1, 2, int64_min / 2);
153 VerifyScale(int64_min / 2, 2, 1, int64_min);
154 VerifyScale(int64_min, 1000001, 1000000, Ratio::kOverflow);
155 }
156
157 // Tests Ratio::Product, static and operator versions.
158 TEST_F(RatioTest, Product) {
159 VerifyProduct(0, 1, 0, 1, 0, 1, true);
160 VerifyProduct(1, 1, 1, 1, 1, 1, true);
161 VerifyProduct(10, 1, 1, 10, 1, 1, true);
162 VerifyProduct(4321, 1234, 617, 4321, 1, 2, true);
163 VerifyProduct(1234, 4321, 4321, 617, 2, 1, true);
164 VerifyProduct(1ll << 31, (1ll << 31) - 1, (1ll << 31) - 1, 1ll << 31, 1, 1,
165 true);
166 VerifyProduct(1ll << 31, (1ll << 31) - 1, (1ll << 31) - 2, 1ll << 31,
167 0x7ffffffe, 0x7fffffff, false);
168 }
169
170 // Tests Ratio::Inverse.
171 TEST_F(RatioTest, Inverse) {
172 VerifyInverse(1, 1);
173 VerifyInverse(2, 1);
174 VerifyInverse(1, 2);
175 VerifyInverse(1000000, 1234);
176 VerifyInverse(1234, 1000000);
177 }
178
179 } // namespace
180 } // namespace media
181 } // namespace mojo
OLDNEW
« no previous file with comments | « services/media/common/test/linear_function_test.cc ('k') | services/media/common/test/timeline_function_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698