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 "VecTest"
18
19 #include <math.h>
20 #include <stdlib.h>
21
22 #include <math/vec4.h>
23
24 #include <gtest/gtest.h>
25
26 namespace android {
27
28 class VecTest : public testing::Test {
29 };
30
TEST_F(VecTest,Basics)31 TEST_F(VecTest, Basics) {
32 vec4 v4;
33 vec3& v3(v4.xyz);
34
35 EXPECT_EQ(sizeof(vec4), sizeof(float)*4);
36 EXPECT_EQ(sizeof(vec3), sizeof(float)*3);
37 EXPECT_EQ(sizeof(vec2), sizeof(float)*2);
38 EXPECT_EQ(reinterpret_cast<void*>(&v3), reinterpret_cast<void*>(&v4));
39 }
40
TEST_F(VecTest,Constructors)41 TEST_F(VecTest, Constructors) {
42 vec4 v0;
43 EXPECT_EQ(v0.x, 0);
44 EXPECT_EQ(v0.y, 0);
45 EXPECT_EQ(v0.z, 0);
46 EXPECT_EQ(v0.w, 0);
47
48 vec4 v1(1);
49 EXPECT_EQ(v1.x, 1);
50 EXPECT_EQ(v1.y, 1);
51 EXPECT_EQ(v1.z, 1);
52 EXPECT_EQ(v1.w, 1);
53
54 vec4 v2(1, 2, 3, 4);
55 EXPECT_EQ(v2.x, 1);
56 EXPECT_EQ(v2.y, 2);
57 EXPECT_EQ(v2.z, 3);
58 EXPECT_EQ(v2.w, 4);
59
60 vec4 v3(v2);
61 EXPECT_EQ(v3.x, 1);
62 EXPECT_EQ(v3.y, 2);
63 EXPECT_EQ(v3.z, 3);
64 EXPECT_EQ(v3.w, 4);
65
66 vec4 v4(v3.xyz, 42);
67 EXPECT_EQ(v4.x, 1);
68 EXPECT_EQ(v4.y, 2);
69 EXPECT_EQ(v4.z, 3);
70 EXPECT_EQ(v4.w, 42);
71
72 vec4 v5(vec3(v2.xy, 42), 24);
73 EXPECT_EQ(v5.x, 1);
74 EXPECT_EQ(v5.y, 2);
75 EXPECT_EQ(v5.z, 42);
76 EXPECT_EQ(v5.w, 24);
77
78 float4 vf(2);
79 EXPECT_EQ(vf.x, 2);
80 EXPECT_EQ(vf.y, 2);
81 EXPECT_EQ(vf.z, 2);
82 EXPECT_EQ(vf.w, 2);
83 }
84
TEST_F(VecTest,Access)85 TEST_F(VecTest, Access) {
86 vec4 v0(1, 2, 3, 4);
87
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 v1 = -v1;
167 EXPECT_EQ(v1.x, -10);
168 EXPECT_EQ(v1.y, -20);
169 EXPECT_EQ(v1.z, -30);
170 EXPECT_EQ(v1.w, -40);
171
172 float4 fv(1, 2, 3, 4);
173 v1 += fv;
174 EXPECT_EQ(v1.x, -9);
175 EXPECT_EQ(v1.y, -18);
176 EXPECT_EQ(v1.z, -27);
177 EXPECT_EQ(v1.w, -36);
178 }
179
TEST_F(VecTest,ComparisonOps)180 TEST_F(VecTest, ComparisonOps) {
181 vec4 v0(1, 2, 3, 4);
182 vec4 v1(10, 20, 30, 40);
183
184 EXPECT_TRUE(v0 == v0);
185 EXPECT_TRUE(v0 != v1);
186 EXPECT_FALSE(v0 != v0);
187 EXPECT_FALSE(v0 == v1);
188 }
189
TEST_F(VecTest,ComparisonFunctions)190 TEST_F(VecTest, ComparisonFunctions) {
191 vec4 v0(1, 2, 3, 4);
192 vec4 v1(10, 20, 30, 40);
193
194 EXPECT_TRUE(all(equal(v0, v0)));
195 EXPECT_TRUE(all(notEqual(v0, v1)));
196 EXPECT_FALSE(any(notEqual(v0, v0)));
197 EXPECT_FALSE(any(equal(v0, v1)));
198
199 EXPECT_FALSE(all(lessThan(v0, v0)));
200 EXPECT_TRUE(all(lessThanEqual(v0, v0)));
201 EXPECT_FALSE(all(greaterThan(v0, v0)));
202 EXPECT_TRUE(all(greaterThanEqual(v0, v0)));
203 EXPECT_TRUE(all(lessThan(v0, v1)));
204 EXPECT_TRUE(all(greaterThan(v1, v0)));
205 }
206
TEST_F(VecTest,ArithmeticOps)207 TEST_F(VecTest, ArithmeticOps) {
208 vec4 v0(1, 2, 3, 4);
209 vec4 v1(10, 20, 30, 40);
210
211 vec4 v2(v0 + v1);
212 EXPECT_EQ(v2.x, 11);
213 EXPECT_EQ(v2.y, 22);
214 EXPECT_EQ(v2.z, 33);
215 EXPECT_EQ(v2.w, 44);
216
217 v0 = v1 * 2;
218 EXPECT_EQ(v0.x, 20);
219 EXPECT_EQ(v0.y, 40);
220 EXPECT_EQ(v0.z, 60);
221 EXPECT_EQ(v0.w, 80);
222
223 v0 = 2 * v1;
224 EXPECT_EQ(v0.x, 20);
225 EXPECT_EQ(v0.y, 40);
226 EXPECT_EQ(v0.z, 60);
227 EXPECT_EQ(v0.w, 80);
228
229 float4 vf(2);
230 v0 = v1 * vf;
231 EXPECT_EQ(v0.x, 20);
232 EXPECT_EQ(v0.y, 40);
233 EXPECT_EQ(v0.z, 60);
234 EXPECT_EQ(v0.w, 80);
235 }
236
TEST_F(VecTest,ArithmeticFunc)237 TEST_F(VecTest, ArithmeticFunc) {
238 vec3 east(1, 0, 0);
239 vec3 north(0, 1, 0);
240 vec3 up(cross(east, north));
241 EXPECT_EQ(up, vec3(0, 0, 1));
242 EXPECT_EQ(dot(east, north), 0);
243 EXPECT_EQ(length(east), 1);
244 EXPECT_EQ(distance(east, north), sqrtf(2));
245
246 vec3 v0(1, 2, 3);
247 vec3 vn(normalize(v0));
248 EXPECT_FLOAT_EQ(1, length(vn));
249 EXPECT_FLOAT_EQ(length(v0), dot(v0, vn));
250
251 float3 vf(east);
252 EXPECT_EQ(length(vf), 1);
253
254 EXPECT_TRUE(any(vec3(0, 0, 1)));
255 EXPECT_FALSE(any(vec3(0, 0, 0)));
256
257 EXPECT_TRUE(all(vec3(1, 1, 1)));
258 EXPECT_FALSE(all(vec3(0, 0, 1)));
259
260 EXPECT_TRUE(any(bool3(false, false, true)));
261 EXPECT_FALSE(any(bool3(false)));
262
263 EXPECT_TRUE(all(bool3(true)));
264 EXPECT_FALSE(all(bool3(false, false, true)));
265
266 std::function<bool(float)> p = [](auto v) -> bool { return v > 0.0f; };
267 EXPECT_TRUE(all(map(vec3(1, 2, 3), p)));
268 }
269
270 }; // namespace android
271