#### /deps/glm-0.9.4.0/test/core/core_type_vec3.cpp

https://bitbucket.org/sturmh/dirtygraphics
C++ | 439 lines | 315 code | 89 blank | 35 comment | 278 complexity | 04b97c86e8d5deb9a79185e1072263ed MD5 | raw file
```  1///////////////////////////////////////////////////////////////////////////////////////////////////
2// OpenGL Mathematics Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
3///////////////////////////////////////////////////////////////////////////////////////////////////
4// Created : 2008-08-31
5// Updated : 2011-09-19
6// Licence : This source is under MIT License
7// File    : test/core/type_vec3.cpp
8///////////////////////////////////////////////////////////////////////////////////////////////////
9
10#define GLM_SWIZZLE
11#include <glm/glm.hpp>
12#include <glm/gtc/half_float.hpp>
13#include <cstdio>
14#include <vector>
15
16int test_vec3_ctor()
17{
18	int Error = 0;
19
20	{
21		glm::vec3 A(1);
22		glm::vec3 B(1, 1, 1);
23
24		Error += A == B ? 0 : 1;
25	}
26
27	{
28		std::vector<glm::vec3> Tests;
29		Tests.push_back(glm::vec3(glm::vec2(1, 2), 3));
30		Tests.push_back(glm::vec3(1, glm::vec2(2, 3)));
31		Tests.push_back(glm::vec3(1, 2, 3));
32		Tests.push_back(glm::vec3(glm::vec4(1, 2, 3, 4)));
33
34		for(std::size_t i = 0; i < Tests.size(); ++i)
35			Error += Tests[i] == glm::vec3(1, 2, 3) ? 0 : 1;
36	}
37
38	return Error;
39}
40
41int test_vec3_operators()
42{
43	int Error = 0;
44
45	{
46		glm::vec3 A(1.0f);
47		glm::vec3 B(1.0f);
48		bool R = A != B;
49		bool S = A == B;
50
51		Error += (S && !R) ? 0 : 1;
52	}
53
54	{
55		glm::vec3 A(1.0f, 2.0f, 3.0f);
56		glm::vec3 B(4.0f, 5.0f, 6.0f);
57
58		glm::vec3 C = A + B;
59		Error += C == glm::vec3(5, 7, 9) ? 0 : 1;
60
61		glm::vec3 D = B - A;
62		Error += D == glm::vec3(3, 3, 3) ? 0 : 1;
63
64		glm::vec3 E = A * B;
65		Error += E == glm::vec3(4, 10, 18) ? 0 : 1;
66
67		glm::vec3 F = B / A;
68		Error += F == glm::vec3(4, 2.5, 2) ? 0 : 1;
69
70		glm::vec3 G = A + 1.0f;
71		Error += G == glm::vec3(2, 3, 4) ? 0 : 1;
72
73		glm::vec3 H = B - 1.0f;
74		Error += H == glm::vec3(3, 4, 5) ? 0 : 1;
75
76		glm::vec3 I = A * 2.0f;
77		Error += I == glm::vec3(2, 4, 6) ? 0 : 1;
78
79		glm::vec3 J = B / 2.0f;
80		Error += J == glm::vec3(2, 2.5, 3) ? 0 : 1;
81
82		glm::vec3 K = 1.0f + A;
83		Error += K == glm::vec3(2, 3, 4) ? 0 : 1;
84
85		glm::vec3 L = 1.0f - B;
86		Error += L == glm::vec3(-3, -4, -5) ? 0 : 1;
87
88		glm::vec3 M = 2.0f * A;
89		Error += M == glm::vec3(2, 4, 6) ? 0 : 1;
90
91		glm::vec3 N = 2.0f / B;
92		Error += N == glm::vec3(0.5, 2.0 / 5.0, 2.0 / 6.0) ? 0 : 1;
93	}
94
95	{
96		glm::vec3 A(1.0f, 2.0f, 3.0f);
97		glm::vec3 B(4.0f, 5.0f, 6.0f);
98
99		A += B;
100		Error += A == glm::vec3(5, 7, 9) ? 0 : 1;
101
102		A += 1.0f;
103		Error += A == glm::vec3(6, 8, 10) ? 0 : 1;
104	}
105	{
106		glm::vec3 A(1.0f, 2.0f, 3.0f);
107		glm::vec3 B(4.0f, 5.0f, 6.0f);
108
109		B -= A;
110		Error += B == glm::vec3(3, 3, 3) ? 0 : 1;
111
112		B -= 1.0f;
113		Error += B == glm::vec3(2, 2, 2) ? 0 : 1;
114	}
115	{
116		glm::vec3 A(1.0f, 2.0f, 3.0f);
117		glm::vec3 B(4.0f, 5.0f, 6.0f);
118
119		A *= B;
120		Error += A == glm::vec3(4, 10, 18) ? 0 : 1;
121
122		A *= 2.0f;
123		Error += A == glm::vec3(8, 20, 36) ? 0 : 1;
124	}
125	{
126		glm::vec3 A(1.0f, 2.0f, 3.0f);
127		glm::vec3 B(4.0f, 5.0f, 6.0f);
128
129		B /= A;
130		Error += B == glm::vec3(4, 2.5, 2) ? 0 : 1;
131
132		B /= 2.0f;
133		Error += B == glm::vec3(2, 1.25, 1) ? 0 : 1;
134	}
135
136	{
137		glm::vec3 A(1.0f, 2.0f, 3.0f);
138		glm::vec3 B = -A;
139		Error += B == glm::vec3(-1.0f, -2.0f, -3.0f) ? 0 : 1;
140	}
141
142	{
143		glm::vec3 A(1.0f, 2.0f, 3.0f);
144		glm::vec3 B = --A;
145		Error += B == glm::vec3(0.0f, 1.0f, 2.0f) ? 0 : 1;
146	}
147
148	{
149		glm::vec3 A(1.0f, 2.0f, 3.0f);
150		glm::vec3 B = A--;
151		Error += B == glm::vec3(0.0f, 1.0f, 2.0f) ? 0 : 1;
152	}
153
154	{
155		glm::vec3 A(1.0f, 2.0f, 3.0f);
156		glm::vec3 B = ++A;
157		Error += B == glm::vec3(2.0f, 3.0f, 4.0f) ? 0 : 1;
158	}
159
160	{
161		glm::vec3 A(1.0f, 2.0f, 3.0f);
162		glm::vec3 B = A++;
163		Error += B == glm::vec3(2.0f, 3.0f, 4.0f) ? 0 : 1;
164	}
165
166	return Error;
167}
168
169int test_vec3_size()
170{
171	int Error = 0;
172
173	Error += sizeof(glm::vec3) == sizeof(glm::mediump_vec3) ? 0 : 1;
174	Error += 12 == sizeof(glm::mediump_vec3) ? 0 : 1;
175	Error += sizeof(glm::dvec3) == sizeof(glm::highp_vec3) ? 0 : 1;
176	Error += 24 == sizeof(glm::highp_vec3) ? 0 : 1;
177	Error += glm::vec3().length() == 3 ? 0 : 1;
178	Error += glm::dvec3().length() == 3 ? 0 : 1;
179
180	return Error;
181}
182
183int test_vec3_swizzle3_2()
184{
185    int Error = 0;
186
187    glm::vec3 v(1, 2, 3);
188    glm::vec2 u;
189
190    // Can not assign a vec3 swizzle to a vec2
191    //u = v.xyz;    //Illegal
192    //u = v.rgb;    //Illegal
193    //u = v.stp;    //Illegal
194
195#if(GLM_SUPPORT_SWIZZLE_OPERATOR())
196	u = v.xx;       Error += (u.x == 1.0f && u.y == 1.0f) ? 0 : 1;
197    u = v.xy;       Error += (u.x == 1.0f && u.y == 2.0f) ? 0 : 1;
198    u = v.xz;       Error += (u.x == 1.0f && u.y == 3.0f) ? 0 : 1;
199    u = v.yx;       Error += (u.x == 2.0f && u.y == 1.0f) ? 0 : 1;
200    u = v.yy;       Error += (u.x == 2.0f && u.y == 2.0f) ? 0 : 1;
201    u = v.yz;       Error += (u.x == 2.0f && u.y == 3.0f) ? 0 : 1;
202    u = v.zx;       Error += (u.x == 3.0f && u.y == 1.0f) ? 0 : 1;
203    u = v.zy;       Error += (u.x == 3.0f && u.y == 2.0f) ? 0 : 1;
204    u = v.zz;       Error += (u.x == 3.0f && u.y == 3.0f) ? 0 : 1;
205
206    u = v.rr;       Error += (u.r == 1.0f && u.g == 1.0f) ? 0 : 1;
207    u = v.rg;       Error += (u.r == 1.0f && u.g == 2.0f) ? 0 : 1;
208    u = v.rb;       Error += (u.r == 1.0f && u.g == 3.0f) ? 0 : 1;
209    u = v.gr;       Error += (u.r == 2.0f && u.g == 1.0f) ? 0 : 1;
210    u = v.gg;       Error += (u.r == 2.0f && u.g == 2.0f) ? 0 : 1;
211    u = v.gb;       Error += (u.r == 2.0f && u.g == 3.0f) ? 0 : 1;
212    u = v.br;       Error += (u.r == 3.0f && u.g == 1.0f) ? 0 : 1;
213    u = v.bg;       Error += (u.r == 3.0f && u.g == 2.0f) ? 0 : 1;
214    u = v.bb;       Error += (u.r == 3.0f && u.g == 3.0f) ? 0 : 1;
215
216    u = v.ss;       Error += (u.s == 1.0f && u.t == 1.0f) ? 0 : 1;
217    u = v.st;       Error += (u.s == 1.0f && u.t == 2.0f) ? 0 : 1;
218    u = v.sp;       Error += (u.s == 1.0f && u.t == 3.0f) ? 0 : 1;
219    u = v.ts;       Error += (u.s == 2.0f && u.t == 1.0f) ? 0 : 1;
220    u = v.tt;       Error += (u.s == 2.0f && u.t == 2.0f) ? 0 : 1;
221    u = v.tp;       Error += (u.s == 2.0f && u.t == 3.0f) ? 0 : 1;
222    u = v.ps;       Error += (u.s == 3.0f && u.t == 1.0f) ? 0 : 1;
223    u = v.pt;       Error += (u.s == 3.0f && u.t == 2.0f) ? 0 : 1;
224    u = v.pp;       Error += (u.s == 3.0f && u.t == 3.0f) ? 0 : 1;
225    // Mixed member aliases are not valid
226    //u = v.rx;     //Illegal
227    //u = v.sy;     //Illegal
228
229
230    u = glm::vec2(1, 2);
231    v = glm::vec3(1, 2, 3);
232    //v.xx = u;     //Illegal
233    v.xy = u;       Error += (v.x == 1.0f && v.y == 2.0f && v.z == 3.0f) ? 0 : 1;
234    v.xz = u;       Error += (v.x == 1.0f && v.y == 2.0f && v.z == 2.0f) ? 0 : 1;
235    v.yx = u;       Error += (v.x == 2.0f && v.y == 1.0f && v.z == 2.0f) ? 0 : 1;
236    //v.yy = u;     //Illegal
237    v.yz = u;       Error += (v.x == 2.0f && v.y == 1.0f && v.z == 2.0f) ? 0 : 1;
238    v.zx = u;       Error += (v.x == 2.0f && v.y == 1.0f && v.z == 1.0f) ? 0 : 1;
239    v.zy = u;       Error += (v.x == 2.0f && v.y == 2.0f && v.z == 1.0f) ? 0 : 1;
240    //v.zz = u;     //Illegal
241#endif//(GLM_SUPPORT_SWIZZLE_OPERATOR())
242
243    return Error;
244}
245
246int test_vec3_swizzle3_3()
247{
248    int Error = 0;
249
250    glm::vec3 v(1, 2, 3);
251    glm::vec3 u;
252
253    u = v;          Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
254
255#if(GLM_SUPPORT_SWIZZLE_OPERATOR())
256	u = v.xyz;      Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
257    u = v.zyx;      Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
258    u.zyx = v;      Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
259
260    u = v.rgb;      Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
261    u = v.bgr;      Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
262    u.bgr = v;      Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
263
264    u = v.stp;      Error += (u.x == 1.0f && u.y == 2.0f && u.z == 3.0f) ? 0 : 1;
265    u = v.pts;      Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
266    u.pts = v;      Error += (u.x == 3.0f && u.y == 2.0f && u.z == 1.0f) ? 0 : 1;
267#endif//(GLM_SUPPORT_SWIZZLE_OPERATOR())
268    return Error;
269}
270
271int test_vec3_swizzle_half()
272{
273    int Error = 0;
274
275    glm::half a1(1);
276    glm::half b1(2);
277    glm::half c1(3);
278    glm::hvec3 v(a1, b1, c1);
279    glm::hvec3 u;
280
281    u = v;
282
283    Error += (u.x == glm::half(1.0f) && u.y == glm::half(2.0f) && u.z == glm::half(3.0f)) ? 0 : 1;
284
285#if(GLM_SUPPORT_SWIZZLE_OPERATOR())
286	u = v.xyz;
287    Error += (u.x == glm::half(1.0f) && u.y == glm::half(2.0f) && u.z == glm::half(3.0f)) ? 0 : 1;
288    u = v.zyx;
289    Error += (u.x == glm::half(3.0f) && u.y == glm::half(2.0f) && u.z == glm::half(1.0f)) ? 0 : 1;
290    u.zyx = v;
291    Error += (u.x == glm::half(3.0f) && u.y == glm::half(2.0f) && u.z == glm::half(1.0f)) ? 0 : 1;
292
293    u = v.rgb;
294    Error += (u.x == glm::half(1.0f) && u.y == glm::half(2.0f) && u.z == glm::half(3.0f)) ? 0 : 1;
295    u = v.bgr;
296    Error += (u.x == glm::half(3.0f) && u.y == glm::half(2.0f) && u.z == glm::half(1.0f)) ? 0 : 1;
297    u.bgr = v;
298    Error += (u.x == glm::half(3.0f) && u.y == glm::half(2.0f) && u.z == glm::half(1.0f)) ? 0 : 1;
299
300    u = v.stp;
301    Error += (u.x == glm::half(1.0f) && u.y == glm::half(2.0f) && u.z == glm::half(3.0f)) ? 0 : 1;
302    u = v.pts;
303    Error += (u.x == glm::half(3.0f) && u.y == glm::half(2.0f) && u.z == glm::half(1.0f)) ? 0 : 1;
304    u.pts = v;
305    Error += (u.x == glm::half(3.0f) && u.y == glm::half(2.0f) && u.z == glm::half(1.0f)) ? 0 : 1;
306#endif//(GLM_SUPPORT_SWIZZLE_OPERATOR())
307
308    return Error;
309}
310
311int test_vec3_swizzle_operators()
312{
313    int Error = 0;
314
315    glm::vec3 q, u, v;
316
317    u = glm::vec3(1, 2, 3);
318    v = glm::vec3(10, 20, 30);
319
320#if(GLM_SUPPORT_SWIZZLE_OPERATOR())
321    // Swizzle, swizzle binary operators
322    q = u.xyz + v.xyz;          Error += (q == (u + v)) ? 0 : 1;
323    q = (u.zyx + v.zyx).zyx;    Error += (q == (u + v)) ? 0 : 1;
324    q = (u.xyz - v.xyz);        Error += (q == (u - v)) ? 0 : 1;
325    q = (u.xyz * v.xyz);        Error += (q == (u * v)) ? 0 : 1;
326    q = (u.xxx * v.xxx);        Error += (q == glm::vec3(u.x * v.x)) ? 0 : 1;
327    q = (u.xyz / v.xyz);        Error += (q == (u / v)) ? 0 : 1;
328
329    // vec, swizzle binary operators
330    q = u + v.xyz;              Error += (q == (u + v)) ? 0 : 1;
331    q = (u - v.xyz);            Error += (q == (u - v)) ? 0 : 1;
332    q = (u * v.xyz);            Error += (q == (u * v)) ? 0 : 1;
333    q = (u * v.xxx);            Error += (q == v.x * u) ? 0 : 1;
334    q = (u / v.xyz);            Error += (q == (u / v)) ? 0 : 1;
335
336    // swizzle,vec binary operators
337    q = u.xyz + v;              Error += (q == (u + v)) ? 0 : 1;
338    q = (u.xyz - v);            Error += (q == (u - v)) ? 0 : 1;
339    q = (u.xyz * v);            Error += (q == (u * v)) ? 0 : 1;
340    q = (u.xxx * v);            Error += (q == u.x * v) ? 0 : 1;
341    q = (u.xyz / v);            Error += (q == (u / v)) ? 0 : 1;
342#endif//(GLM_SUPPORT_SWIZZLE_OPERATOR())
343
344    // Compile errors
345    //q = (u.yz * v.xyz);
346    //q = (u * v.xy);
347
348    return Error;
349}
350
351int test_vec3_swizzle_functions()
352{
353    int Error = 0;
354
355    //
356    // NOTE: template functions cannot pick up the implicit conversion from
357    // a swizzle to the unswizzled type, therefore the operator() must be
358    // used.  E.g.:
359    //
360    // glm::dot(u.xy, v.xy);        <--- Compile error
361    // glm::dot(u.xy(), v.xy());    <--- Compiles correctly
362    //
363
364#if(GLM_SUPPORT_SWIZZLE_OPERATOR())
365    float r;
366
367	// vec2
368	glm::vec2 a(1, 2);
369	glm::vec2 b(10, 20);
370	r = glm::dot(a, b);                 Error += (int(r) == 50) ? 0 : 1;
371	r = glm::dot(glm::vec2(a.xy()), glm::vec2(b.xy()));       Error += (int(r) == 50) ? 0 : 1;
372	r = glm::dot(glm::vec2(a.xy()), glm::vec2(b.yy()));       Error += (int(r) == 60) ? 0 : 1;
373
374	// vec3
375	glm::vec3 q, u, v;
376	u = glm::vec3(1, 2, 3);
377	v = glm::vec3(10, 20, 30);
378	r = glm::dot(u, v);                 Error += (int(r) == 140) ? 0 : 1;
379	r = glm::dot(u.xyz(), v.zyz());     Error += (int(r) == 160) ? 0 : 1;
380	r = glm::dot(u, v.zyx());           Error += (int(r) == 100) ? 0 : 1;
381	r = glm::dot(u.xyz(), v);           Error += (int(r) == 140) ? 0 : 1;
382	r = glm::dot(u.xy(), v.xy());       Error += (int(r) == 50) ? 0 : 1;
383
384	// vec4
385	glm::vec4 s, t;
386	s = glm::vec4(1, 2, 3, 4);
387	t = glm::vec4(10, 20, 30, 40);
388	r = glm::dot(s, t);                 Error += (int(r) == 300) ? 0 : 1;
389	r = glm::dot(s.xyzw(), t.xyzw());   Error += (int(r) == 300) ? 0 : 1;
390	r = glm::dot(s.xyz(), t.xyz());     Error += (int(r) == 140) ? 0 : 1;
391#endif//(GLM_SUPPORT_SWIZZLE_OPERATOR())
392
393    return Error;
394}
395
396int test_vec3_swizzle_partial()
397{
398	int Error = 0;
399
400#if(GLM_SUPPORT_SWIZZLE_OPERATOR())
401	glm::vec3 A(1, 2, 3);
402
403	{
404		glm::vec3 B(A.xy, 3.0f);
405		Error += A == B ? 0 : 1;
406	}
407
408	{
409		glm::vec3 B(1.0f, A.yz);
410		Error += A == B ? 0 : 1;
411	}
412
413	{
414		glm::vec3 B(A.xyz);
415		Error += A == B ? 0 : 1;
416	}
417#endif//(GLM_SUPPORT_SWIZZLE_OPERATOR())
418
419	return Error;
420}
421
422int main()
423{
424	int Error = 0;
425
426	Error += test_vec3_ctor();
427	Error += test_vec3_operators();
428	Error += test_vec3_size();
429    Error += test_vec3_swizzle3_2();
430    Error += test_vec3_swizzle3_3();
431    Error += test_vec3_swizzle_half();
432	Error += test_vec3_swizzle_partial();
433    Error += test_vec3_swizzle_operators();
434    Error += test_vec3_swizzle_functions();
435
436	printf("Errors: %d\n", Error);
437
438	return Error;
439}
```