1 /*
2 * Copyright 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "RegionTest"
18
19 #include <stdlib.h>
20 #include <ui/Region.h>
21 #include <ui/Rect.h>
22 #include <gtest/gtest.h>
23
24 #include <ui/vec4.h>
25
26 namespace android {
27
28 class VecTest : public testing::Test {
29 protected:
30 };
31
TEST_F(VecTest,Basics)32 TEST_F(VecTest, Basics) {
33 vec4 v4;
34 vec3& v3(v4.xyz);
35
36 EXPECT_EQ(sizeof(vec4), sizeof(float)*4);
37 EXPECT_EQ(sizeof(vec3), sizeof(float)*3);
38 EXPECT_EQ(sizeof(vec2), sizeof(float)*2);
39 EXPECT_EQ((void*)&v3, (void*)&v4);
40 }
41
TEST_F(VecTest,Constructors)42 TEST_F(VecTest, Constructors) {
43 vec4 v0;
44 EXPECT_EQ(v0.x, 0);
45 EXPECT_EQ(v0.y, 0);
46 EXPECT_EQ(v0.z, 0);
47 EXPECT_EQ(v0.w, 0);
48
49 vec4 v1(1);
50 EXPECT_EQ(v1.x, 1);
51 EXPECT_EQ(v1.y, 1);
52 EXPECT_EQ(v1.z, 1);
53 EXPECT_EQ(v1.w, 1);
54
55 vec4 v2(1,2,3,4);
56 EXPECT_EQ(v2.x, 1);
57 EXPECT_EQ(v2.y, 2);
58 EXPECT_EQ(v2.z, 3);
59 EXPECT_EQ(v2.w, 4);
60
61 vec4 v3(v2);
62 EXPECT_EQ(v3.x, 1);
63 EXPECT_EQ(v3.y, 2);
64 EXPECT_EQ(v3.z, 3);
65 EXPECT_EQ(v3.w, 4);
66
67 vec4 v4(v3.xyz, 42);
68 EXPECT_EQ(v4.x, 1);
69 EXPECT_EQ(v4.y, 2);
70 EXPECT_EQ(v4.z, 3);
71 EXPECT_EQ(v4.w, 42);
72
73 vec4 v5(vec3(v2.xy, 42), 24);
74 EXPECT_EQ(v5.x, 1);
75 EXPECT_EQ(v5.y, 2);
76 EXPECT_EQ(v5.z, 42);
77 EXPECT_EQ(v5.w, 24);
78
79 tvec4<double> vd(2);
80 EXPECT_EQ(vd.x, 2);
81 EXPECT_EQ(vd.y, 2);
82 EXPECT_EQ(vd.z, 2);
83 EXPECT_EQ(vd.w, 2);
84 }
85
TEST_F(VecTest,Access)86 TEST_F(VecTest, Access) {
87 vec4 v0(1,2,3,4);
88 v0.x = 10;
89 v0.y = 20;
90 v0.z = 30;
91 v0.w = 40;
92 EXPECT_EQ(v0.x, 10);
93 EXPECT_EQ(v0.y, 20);
94 EXPECT_EQ(v0.z, 30);
95 EXPECT_EQ(v0.w, 40);
96
97 v0[0] = 100;
98 v0[1] = 200;
99 v0[2] = 300;
100 v0[3] = 400;
101 EXPECT_EQ(v0.x, 100);
102 EXPECT_EQ(v0.y, 200);
103 EXPECT_EQ(v0.z, 300);
104 EXPECT_EQ(v0.w, 400);
105
106 v0.xyz = vec3(1,2,3);
107 EXPECT_EQ(v0.x, 1);
108 EXPECT_EQ(v0.y, 2);
109 EXPECT_EQ(v0.z, 3);
110 EXPECT_EQ(v0.w, 400);
111 }
112
TEST_F(VecTest,UnaryOps)113 TEST_F(VecTest, UnaryOps) {
114 vec4 v0(1,2,3,4);
115
116 v0 += 1;
117 EXPECT_EQ(v0.x, 2);
118 EXPECT_EQ(v0.y, 3);
119 EXPECT_EQ(v0.z, 4);
120 EXPECT_EQ(v0.w, 5);
121
122 v0 -= 1;
123 EXPECT_EQ(v0.x, 1);
124 EXPECT_EQ(v0.y, 2);
125 EXPECT_EQ(v0.z, 3);
126 EXPECT_EQ(v0.w, 4);
127
128 v0 *= 2;
129 EXPECT_EQ(v0.x, 2);
130 EXPECT_EQ(v0.y, 4);
131 EXPECT_EQ(v0.z, 6);
132 EXPECT_EQ(v0.w, 8);
133
134 v0 /= 2;
135 EXPECT_EQ(v0.x, 1);
136 EXPECT_EQ(v0.y, 2);
137 EXPECT_EQ(v0.z, 3);
138 EXPECT_EQ(v0.w, 4);
139
140 vec4 v1(10, 20, 30, 40);
141
142 v0 += v1;
143 EXPECT_EQ(v0.x, 11);
144 EXPECT_EQ(v0.y, 22);
145 EXPECT_EQ(v0.z, 33);
146 EXPECT_EQ(v0.w, 44);
147
148 v0 -= v1;
149 EXPECT_EQ(v0.x, 1);
150 EXPECT_EQ(v0.y, 2);
151 EXPECT_EQ(v0.z, 3);
152 EXPECT_EQ(v0.w, 4);
153
154 v0 *= v1;
155 EXPECT_EQ(v0.x, 10);
156 EXPECT_EQ(v0.y, 40);
157 EXPECT_EQ(v0.z, 90);
158 EXPECT_EQ(v0.w, 160);
159
160 v0 /= v1;
161 EXPECT_EQ(v0.x, 1);
162 EXPECT_EQ(v0.y, 2);
163 EXPECT_EQ(v0.z, 3);
164 EXPECT_EQ(v0.w, 4);
165
166 ++v0;
167 EXPECT_EQ(v0.x, 2);
168 EXPECT_EQ(v0.y, 3);
169 EXPECT_EQ(v0.z, 4);
170 EXPECT_EQ(v0.w, 5);
171
172 ++++v0;
173 EXPECT_EQ(v0.x, 4);
174 EXPECT_EQ(v0.y, 5);
175 EXPECT_EQ(v0.z, 6);
176 EXPECT_EQ(v0.w, 7);
177
178 --v1;
179 EXPECT_EQ(v1.x, 9);
180 EXPECT_EQ(v1.y, 19);
181 EXPECT_EQ(v1.z, 29);
182 EXPECT_EQ(v1.w, 39);
183
184 v1 = -v1;
185 EXPECT_EQ(v1.x, -9);
186 EXPECT_EQ(v1.y, -19);
187 EXPECT_EQ(v1.z, -29);
188 EXPECT_EQ(v1.w, -39);
189
190 tvec4<double> dv(1,2,3,4);
191 v1 += dv;
192 EXPECT_EQ(v1.x, -8);
193 EXPECT_EQ(v1.y, -17);
194 EXPECT_EQ(v1.z, -26);
195 EXPECT_EQ(v1.w, -35);
196 }
197
TEST_F(VecTest,ComparisonOps)198 TEST_F(VecTest, ComparisonOps) {
199 vec4 v0(1,2,3,4);
200 vec4 v1(10,20,30,40);
201
202 EXPECT_TRUE(v0 == v0);
203 EXPECT_TRUE(v0 != v1);
204 EXPECT_FALSE(v0 != v0);
205 EXPECT_FALSE(v0 == v1);
206 }
207
TEST_F(VecTest,ArithmeticOps)208 TEST_F(VecTest, ArithmeticOps) {
209 vec4 v0(1,2,3,4);
210 vec4 v1(10,20,30,40);
211
212 vec4 v2(v0 + v1);
213 EXPECT_EQ(v2.x, 11);
214 EXPECT_EQ(v2.y, 22);
215 EXPECT_EQ(v2.z, 33);
216 EXPECT_EQ(v2.w, 44);
217
218 v0 = v1 * 2;
219 EXPECT_EQ(v0.x, 20);
220 EXPECT_EQ(v0.y, 40);
221 EXPECT_EQ(v0.z, 60);
222 EXPECT_EQ(v0.w, 80);
223
224 v0 = 2 * v1;
225 EXPECT_EQ(v0.x, 20);
226 EXPECT_EQ(v0.y, 40);
227 EXPECT_EQ(v0.z, 60);
228 EXPECT_EQ(v0.w, 80);
229
230 tvec4<double> vd(2);
231 v0 = v1 * vd;
232 EXPECT_EQ(v0.x, 20);
233 EXPECT_EQ(v0.y, 40);
234 EXPECT_EQ(v0.z, 60);
235 EXPECT_EQ(v0.w, 80);
236 }
237
TEST_F(VecTest,ArithmeticFunc)238 TEST_F(VecTest, ArithmeticFunc) {
239 vec3 east(1, 0, 0);
240 vec3 north(0, 1, 0);
241 vec3 up( cross(east, north) );
242 EXPECT_EQ(up, vec3(0,0,1));
243 EXPECT_EQ(dot(east, north), 0);
244 EXPECT_EQ(length(east), 1);
245 EXPECT_EQ(distance(east, north), sqrtf(2));
246
247 vec3 v0(1,2,3);
248 vec3 vn(normalize(v0));
249 EXPECT_FLOAT_EQ(1, length(vn));
250 EXPECT_FLOAT_EQ(length(v0), dot(v0, vn));
251
252 tvec3<double> vd(east);
253 EXPECT_EQ(length(vd), 1);
254 }
255
256 }; // namespace android
257