OLD | NEW |
| (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 "mojo/services/media/common/cpp/linear_function.h" | |
6 #include "services/media/common/test/test_base.h" | |
7 | |
8 namespace mojo { | |
9 namespace media { | |
10 namespace { | |
11 | |
12 class LinearFunctionTest : public TestBase { | |
13 public: | |
14 // Verifies that a LinearFunction instantiated in three different ways with | |
15 // the given arguments has the expected properties. | |
16 void VerifyBasics(int64_t domain_basis, | |
17 int64_t range_basis, | |
18 uint32_t domain_delta, | |
19 uint32_t range_delta) { | |
20 LinearFunction under_test_1(domain_basis, range_basis, domain_delta, | |
21 range_delta); | |
22 VerifyBasics(under_test_1, domain_basis, range_basis, domain_delta, | |
23 range_delta); | |
24 | |
25 LinearFunction under_test_2(domain_basis, range_basis, | |
26 Ratio(range_delta, domain_delta)); | |
27 VerifyBasics(under_test_2, domain_basis, range_basis, domain_delta, | |
28 range_delta); | |
29 | |
30 LinearFunction under_test_3(Ratio(range_delta, domain_delta)); | |
31 VerifyBasics(under_test_3, 0, 0, domain_delta, range_delta); | |
32 | |
33 EXPECT_EQ(under_test_1, under_test_1); | |
34 EXPECT_EQ(under_test_1, under_test_2); | |
35 EXPECT_EQ(under_test_2, under_test_1); | |
36 EXPECT_EQ(under_test_2, under_test_2); | |
37 | |
38 if (domain_basis == 0 && range_basis == 0) { | |
39 EXPECT_EQ(under_test_1, under_test_3); | |
40 EXPECT_EQ(under_test_2, under_test_3); | |
41 EXPECT_EQ(under_test_3, under_test_1); | |
42 EXPECT_EQ(under_test_3, under_test_2); | |
43 } else { | |
44 EXPECT_NE(under_test_1, under_test_3); | |
45 EXPECT_NE(under_test_2, under_test_3); | |
46 EXPECT_NE(under_test_3, under_test_1); | |
47 EXPECT_NE(under_test_3, under_test_2); | |
48 } | |
49 } | |
50 | |
51 // Verifies that the given LinearFunction instantiated with the given | |
52 // arguments has the expected properties. | |
53 void VerifyBasics(const LinearFunction& under_test, | |
54 int64_t domain_basis, | |
55 int64_t range_basis, | |
56 uint32_t domain_delta, | |
57 uint32_t range_delta) { | |
58 Ratio::Reduce(&range_delta, &domain_delta); | |
59 EXPECT_EQ(domain_basis, under_test.domain_basis()); | |
60 EXPECT_EQ(range_basis, under_test.range_basis()); | |
61 EXPECT_EQ(domain_delta, under_test.domain_delta()); | |
62 EXPECT_EQ(range_delta, under_test.range_delta()); | |
63 EXPECT_EQ(domain_delta, under_test.slope().denominator()); | |
64 EXPECT_EQ(range_delta, under_test.slope().numerator()); | |
65 } | |
66 | |
67 // Verifies that the inverse of a LinearFunction instantiated in three | |
68 // different ways with the given arguments has the expected properties. | |
69 void VerifyInverse(int64_t domain_basis, | |
70 int64_t range_basis, | |
71 uint32_t domain_delta, | |
72 uint32_t range_delta) { | |
73 LinearFunction under_test_1(domain_basis, range_basis, domain_delta, | |
74 range_delta); | |
75 VerifyBasics(under_test_1.Inverse(), range_basis, domain_basis, range_delta, | |
76 domain_delta); | |
77 | |
78 LinearFunction under_test_2(domain_basis, range_basis, | |
79 Ratio(range_delta, domain_delta)); | |
80 VerifyBasics(under_test_2.Inverse(), range_basis, domain_basis, range_delta, | |
81 domain_delta); | |
82 | |
83 LinearFunction under_test_3(Ratio(range_delta, domain_delta)); | |
84 VerifyBasics(under_test_3.Inverse(), 0, 0, range_delta, domain_delta); | |
85 } | |
86 | |
87 // Verifies that LinearFunction::Apply, in its various forms, works as | |
88 // expected for the given arguments. | |
89 void VerifyApply(int64_t domain_basis, | |
90 int64_t range_basis, | |
91 uint32_t domain_delta, | |
92 uint32_t range_delta, | |
93 int64_t domain_input, | |
94 int64_t expected_result) { | |
95 // Verify the static method. | |
96 EXPECT_EQ( | |
97 expected_result, | |
98 LinearFunction::Apply(domain_basis, range_basis, | |
99 Ratio(range_delta, domain_delta), domain_input)); | |
100 | |
101 // Verify the instance method. | |
102 LinearFunction under_test(domain_basis, range_basis, domain_delta, | |
103 range_delta); | |
104 EXPECT_EQ(expected_result, under_test.Apply(domain_input)); | |
105 | |
106 // Verify the operator. | |
107 EXPECT_EQ(expected_result, under_test(domain_input)); | |
108 } | |
109 | |
110 // Verifies that LinearFunction::ApplyInverse, in its various forms, works as | |
111 // expected for the given arguments. | |
112 void VerifyApplyInverse(int64_t domain_basis, | |
113 int64_t range_basis, | |
114 uint32_t domain_delta, | |
115 uint32_t range_delta, | |
116 int64_t range_input, | |
117 int64_t expected_result) { | |
118 // Verify the static method. | |
119 EXPECT_EQ(expected_result, | |
120 LinearFunction::ApplyInverse(domain_basis, range_basis, | |
121 Ratio(range_delta, domain_delta), | |
122 range_input)); | |
123 | |
124 // Verify the instance method. | |
125 LinearFunction under_test(domain_basis, range_basis, domain_delta, | |
126 range_delta); | |
127 EXPECT_EQ(expected_result, under_test.ApplyInverse(range_input)); | |
128 } | |
129 | |
130 // Verifies that LinearFunction::Compose works as expected with the given | |
131 // inputs. | |
132 void VerifyCompose(const LinearFunction& a, | |
133 const LinearFunction& b, | |
134 bool exact, | |
135 const LinearFunction& expected_result) { | |
136 // Verify the static method. | |
137 EXPECT_EQ(expected_result, LinearFunction::Compose(a, b, exact)); | |
138 } | |
139 }; | |
140 | |
141 // Tests LinearFunction basics for various instantiation arguments. | |
142 TEST_F(LinearFunctionTest, Basics) { | |
143 VerifyBasics(0, 0, 1, 0); | |
144 VerifyBasics(0, 0, 1, 1); | |
145 VerifyBasics(1, 1, 10, 10); | |
146 VerifyBasics(1234, 5678, 4321, 8765); | |
147 VerifyBasics(-1234, 5678, 4321, 8765); | |
148 VerifyBasics(-1234, -5678, 4321, 8765); | |
149 VerifyBasics(1234, -5678, 4321, 8765); | |
150 } | |
151 | |
152 // Tests LinearFunction::Inverse. | |
153 TEST_F(LinearFunctionTest, Inverse) { | |
154 VerifyInverse(0, 0, 1, 1); | |
155 VerifyInverse(1, 1, 10, 10); | |
156 VerifyInverse(1234, 5678, 4321, 8765); | |
157 VerifyInverse(-1234, 5678, 4321, 8765); | |
158 VerifyInverse(-1234, -5678, 4321, 8765); | |
159 VerifyInverse(1234, -5678, 4321, 8765); | |
160 } | |
161 | |
162 // Tests LinearFunction::Apply in its variations. | |
163 TEST_F(LinearFunctionTest, Apply) { | |
164 VerifyApply(0, 0, 1, 0, 0, 0); | |
165 VerifyApply(0, 0, 1, 0, 1000, 0); | |
166 VerifyApply(0, 1234, 1, 0, 0, 1234); | |
167 VerifyApply(0, 1234, 1, 0, 1000, 1234); | |
168 VerifyApply(0, 1234, 1, 0, -1000, 1234); | |
169 VerifyApply(0, -1234, 1, 0, 0, -1234); | |
170 VerifyApply(0, -1234, 1, 0, 1000, -1234); | |
171 VerifyApply(0, -1234, 1, 0, -1000, -1234); | |
172 VerifyApply(0, 0, 1, 1, 0, 0); | |
173 VerifyApply(0, 0, 1, 1, 1000, 1000); | |
174 VerifyApply(0, 1234, 1, 1, 0, 1234); | |
175 VerifyApply(0, 1234, 1, 1, 1000, 2234); | |
176 VerifyApply(0, 1234, 1, 1, -1000, 234); | |
177 VerifyApply(0, -1234, 1, 1, 0, -1234); | |
178 VerifyApply(0, -1234, 1, 1, 1000, -234); | |
179 VerifyApply(0, -1234, 1, 1, -1000, -2234); | |
180 VerifyApply(10, 0, 1, 0, 0, 0); | |
181 VerifyApply(10, 0, 1, 1, 0, -10); | |
182 VerifyApply(-10, 0, 1, 0, 0, 0); | |
183 VerifyApply(-10, 0, 1, 1, 0, 10); | |
184 VerifyApply(0, 1234, 2, 1, 0, 1234); | |
185 VerifyApply(0, 1234, 2, 1, 1234, 1234 + 1234 / 2); | |
186 VerifyApply(0, 1234, 1, 2, 1234, 1234 + 1234 * 2); | |
187 } | |
188 | |
189 // Tests LinearFunction::Apply in its variations. | |
190 TEST_F(LinearFunctionTest, ApplyInverse) { | |
191 VerifyApplyInverse(0, 0, 1, 1, 0, 0); | |
192 VerifyApplyInverse(0, 0, 1, 1, 1000, 1000); | |
193 VerifyApplyInverse(0, 1234, 1, 1, 1234, 0); | |
194 VerifyApplyInverse(0, 1234, 1, 1, 2234, 1000); | |
195 VerifyApplyInverse(0, 1234, 1, 1, 234, -1000); | |
196 VerifyApplyInverse(0, -1234, 1, 1, -1234, 0); | |
197 VerifyApplyInverse(0, -1234, 1, 1, -234, 1000); | |
198 VerifyApplyInverse(0, -1234, 1, 1, -2234, -1000); | |
199 VerifyApplyInverse(10, 0, 1, 1, -10, 0); | |
200 VerifyApplyInverse(-10, 0, 1, 1, 10, 0); | |
201 VerifyApplyInverse(0, 1234, 2, 1, 1234, 0); | |
202 VerifyApplyInverse(0, 1234, 2, 1, 1234 + 1234 / 2, 1234); | |
203 VerifyApplyInverse(0, 1234, 1, 2, 1234 + 1234 * 2, 1234); | |
204 } | |
205 | |
206 // Tests LinearFunction::Compose. | |
207 TEST_F(LinearFunctionTest, Compose) { | |
208 VerifyCompose(LinearFunction(0, 0, 1, 0), LinearFunction(0, 0, 1, 0), true, | |
209 LinearFunction(0, 0, 1, 0)); | |
210 VerifyCompose(LinearFunction(0, 0, 1, 1), LinearFunction(0, 0, 1, 1), true, | |
211 LinearFunction(0, 0, 1, 1)); | |
212 VerifyCompose(LinearFunction(1, 0, 1, 1), LinearFunction(0, 0, 1, 1), true, | |
213 LinearFunction(0, -1, 1, 1)); | |
214 VerifyCompose(LinearFunction(10, 10, 1, 1), LinearFunction(0, 0, 1, 1), true, | |
215 LinearFunction(0, 0, 1, 1)); | |
216 VerifyCompose(LinearFunction(0, 0, 1, 2), LinearFunction(0, 0, 1, 2), true, | |
217 LinearFunction(0, 0, 1, 4)); | |
218 VerifyCompose(LinearFunction(0, 0, 2, 1), LinearFunction(0, 0, 2, 1), true, | |
219 LinearFunction(0, 0, 4, 1)); | |
220 VerifyCompose(LinearFunction(0, 0, 2, 1), LinearFunction(0, 0, 1, 2), true, | |
221 LinearFunction(0, 0, 1, 1)); | |
222 } | |
223 | |
224 } // namespace | |
225 } // namespace media | |
226 } // namespace mojo | |
OLD | NEW |