PageRenderTime 45ms CodeModel.GetById 2ms app.highlight 38ms RepoModel.GetById 1ms app.codeStats 0ms

/deps/glm-0.9.4.0/test/core/core_func_common.cpp

https://bitbucket.org/sturmh/dirtygraphics
C++ | 425 lines | 356 code | 57 blank | 12 comment | 104 complexity | 3b7fb4778b54ee2380bf2f24bd4d6726 MD5 | raw file
  1///////////////////////////////////////////////////////////////////////////////////////////////////
  2// OpenGL Mathematics Copyright (c) 2005 - 2012 G-Truc Creation (www.g-truc.net)
  3///////////////////////////////////////////////////////////////////////////////////////////////////
  4// Created : 2011-01-15
  5// Updated : 2011-09-13
  6// Licence : This source is under MIT licence
  7// File    : test/core/func_common.cpp
  8///////////////////////////////////////////////////////////////////////////////////////////////////
  9
 10//#include <boost/array.hpp>
 11//#include <boost/date_time/posix_time/posix_time.hpp>
 12//#include <boost/thread/thread.hpp>
 13#include <glm/glm.hpp>
 14#include <glm/gtc/epsilon.hpp>
 15#include <cstdio>
 16
 17int test_modf()
 18{
 19	int Error(0);
 20
 21	{
 22		float X(1.5f);
 23		float I(0.0f);
 24		float A = glm::modf(X, I);
 25
 26		Error += I == 1.0f ? 0 : 1;
 27		Error += A == 0.5f ? 0 : 1;
 28	}
 29
 30	{
 31		glm::vec4 X(1.1f, 1.2f, 1.5f, 1.7f);
 32		glm::vec4 I(0.0f);
 33		glm::vec4 A = glm::modf(X, I);
 34
 35		Error += I == glm::vec4(1.0f) ? 0 : 1;
 36		Error += glm::all(glm::epsilonEqual(A, glm::vec4(0.1f, 0.2f, 0.5f, 0.7f), 0.00001f)) ? 0 : 1;
 37	}
 38
 39	{
 40		glm::dvec4 X(1.1, 1.2, 1.5, 1.7);
 41		glm::dvec4 I(0.0);
 42		glm::dvec4 A = glm::modf(X, I);
 43
 44		Error += I == glm::dvec4(1.0) ? 0 : 1;
 45		Error += glm::all(glm::epsilonEqual(A, glm::dvec4(0.1, 0.2, 0.5, 0.7), 0.000000001)) ? 0 : 1;
 46	}
 47
 48	{
 49		double X(1.5);
 50		double I(0.0);
 51		double A = glm::modf(X, I);
 52
 53		Error += I == 1.0 ? 0 : 1;
 54		Error += A == 0.5 ? 0 : 1;
 55	}
 56
 57	return Error;
 58}
 59
 60int test_floatBitsToInt()
 61{
 62	int Error = 0;
 63	
 64	{
 65		float A = 1.0f;
 66		int B = glm::floatBitsToInt(A);
 67		float C = glm::intBitsToFloat(B);
 68		int D = *(int*)&A;
 69		Error += B == D ? 0 : 1;
 70		Error += A == C ? 0 : 1;
 71	}
 72
 73	{
 74		glm::vec2 A(1.0f, 2.0f);
 75		glm::ivec2 B = glm::floatBitsToInt(A);
 76		glm::vec2 C = glm::intBitsToFloat(B);
 77		Error += B.x == *(int*)&(A.x) ? 0 : 1;
 78		Error += B.y == *(int*)&(A.y) ? 0 : 1;
 79		Error += A == C? 0 : 1;
 80	}
 81
 82	{
 83		glm::vec3 A(1.0f, 2.0f, 3.0f);
 84		glm::ivec3 B = glm::floatBitsToInt(A);
 85		glm::vec3 C = glm::intBitsToFloat(B);
 86		Error += B.x == *(int*)&(A.x) ? 0 : 1;
 87		Error += B.y == *(int*)&(A.y) ? 0 : 1;
 88		Error += B.z == *(int*)&(A.z) ? 0 : 1;
 89		Error += A == C? 0 : 1;
 90	}
 91	
 92	{
 93		glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
 94		glm::ivec4 B = glm::floatBitsToInt(A);
 95		glm::vec4 C = glm::intBitsToFloat(B);
 96		Error += B.x == *(int*)&(A.x) ? 0 : 1;
 97		Error += B.y == *(int*)&(A.y) ? 0 : 1;
 98		Error += B.z == *(int*)&(A.z) ? 0 : 1;
 99		Error += B.w == *(int*)&(A.w) ? 0 : 1;
100		Error += A == C? 0 : 1;
101	}
102	
103	return Error;
104}
105
106int test_floatBitsToUint()
107{
108	int Error = 0;
109	
110	{
111		float A = 1.0f;
112		glm::uint B = glm::floatBitsToUint(A);
113		float C = glm::intBitsToFloat(B);
114		Error += B == *(glm::uint*)&A ? 0 : 1;
115		Error += A == C? 0 : 1;
116	}
117	
118	{
119		glm::vec2 A(1.0f, 2.0f);
120		glm::uvec2 B = glm::floatBitsToUint(A);
121		glm::vec2 C = glm::uintBitsToFloat(B);
122		Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
123		Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
124		Error += A == C ? 0 : 1;
125	}
126	
127	{
128		glm::vec3 A(1.0f, 2.0f, 3.0f);
129		glm::uvec3 B = glm::floatBitsToUint(A);
130		glm::vec3 C = glm::uintBitsToFloat(B);
131		Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
132		Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
133		Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
134		Error += A == C? 0 : 1;
135	}
136	
137	{
138		glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
139		glm::uvec4 B = glm::floatBitsToUint(A);
140		glm::vec4 C = glm::uintBitsToFloat(B);
141		Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
142		Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
143		Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
144		Error += B.w == *(glm::uint*)&(A.w) ? 0 : 1;
145		Error += A == C? 0 : 1;
146	}
147	
148	return Error;
149}
150
151int test_mix()
152{
153	int Error = 0;
154
155	{
156		float A = glm::mix(0.f, 1.f, true);
157		Error += A == 1.f ? 0 : 1;
158		float B = glm::mix(0.f, 1.f, false);
159		Error += B == 0.f ? 0 : 1;
160	}
161
162	{
163		float A = glm::mix(0.f, 1.f, 1.f);
164		Error += A == 1.f ? 0 : 1;
165		float B = glm::mix(0.f, 1.f, 0.f);
166		Error += B == 0.f ? 0 : 1;
167	}
168
169	return Error;
170}
171
172int test_round()
173{
174	int Error = 0;
175	
176	{
177		float A = glm::round(0.0f);
178		Error += A == 0.0f ? 0 : 1;
179		float B = glm::round(0.5f);
180		Error += B == 1.0f ? 0 : 1;
181		float C = glm::round(1.0f);
182		Error += C == 1.0f ? 0 : 1;
183		float D = glm::round(0.1f);
184		Error += D == 0.0f ? 0 : 1;
185		float E = glm::round(0.9f);
186		Error += E == 1.0f ? 0 : 1;
187		float F = glm::round(1.5f);
188		Error += F == 2.0f ? 0 : 1;
189		float G = glm::round(1.9f);
190		Error += G == 2.0f ? 0 : 1;
191	}
192	
193	{
194		float A = glm::round(-0.0f);
195		Error += A ==  0.0f ? 0 : 1;
196		float B = glm::round(-0.5f);
197		Error += B == -1.0f ? 0 : 1;
198		float C = glm::round(-1.0f);
199		Error += C == -1.0f ? 0 : 1;
200		float D = glm::round(-0.1f);
201		Error += D ==  0.0f ? 0 : 1;
202		float E = glm::round(-0.9f);
203		Error += E == -1.0f ? 0 : 1;
204		float F = glm::round(-1.5f);
205		Error += F == -2.0f ? 0 : 1;
206		float G = glm::round(-1.9f);
207		Error += G == -2.0f ? 0 : 1;
208	}
209	
210	return Error;
211}
212
213int test_roundEven()
214{
215	int Error = 0;
216
217	{
218		float A = glm::roundEven(-1.5f);
219		Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
220		Error += 0;
221	}
222	{
223		float A = glm::roundEven(1.5f);
224		Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
225		Error += 0;
226	}
227
228	{
229		float A = glm::roundEven(-3.5f);
230		Error += glm::epsilonEqual(A, -4.0f, 0.0001f) ? 0 : 1;
231		Error += 0;
232	}
233	{
234		float A = glm::roundEven(3.5f);
235		Error += glm::epsilonEqual(A, 4.0f, 0.0001f) ? 0 : 1;
236		Error += 0;
237	}
238
239	{
240		float A = glm::roundEven(-2.5f);
241		Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
242		Error += 0;
243	}
244	{
245		float A = glm::roundEven(2.5f);
246		Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
247		Error += 0;
248	}
249
250	{
251		float A = glm::roundEven(-2.4f);
252		Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
253		Error += 0;
254	}
255	{
256		float A = glm::roundEven(2.4f);
257		Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
258		Error += 0;
259	}
260
261	{
262		float A = glm::roundEven(-2.6f);
263		Error += glm::epsilonEqual(A, -3.0f, 0.0001f) ? 0 : 1;
264		Error += 0;
265	}
266	{
267		float A = glm::roundEven(2.6f);
268		Error += glm::epsilonEqual(A, 3.0f, 0.0001f) ? 0 : 1;
269		Error += 0;
270	}
271
272	{
273		float A = glm::roundEven(-2.0f);
274		Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
275		Error += 0;
276	}
277	{
278		float A = glm::roundEven(2.0f);
279		Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
280		Error += 0;
281	}
282
283	{
284		float A = glm::roundEven(0.0f);
285		Error += A == 0.0f ? 0 : 1;
286		float B = glm::roundEven(0.5f);
287		Error += B == 0.0f ? 0 : 1;
288		float C = glm::roundEven(1.0f);
289		Error += C == 1.0f ? 0 : 1;
290		float D = glm::roundEven(0.1f);
291		Error += D == 0.0f ? 0 : 1;
292		float E = glm::roundEven(0.9f);
293		Error += E == 1.0f ? 0 : 1;
294		float F = glm::roundEven(1.5f);
295		Error += F == 2.0f ? 0 : 1;
296		float G = glm::roundEven(1.9f);
297		Error += G == 2.0f ? 0 : 1;
298	}
299
300	{
301		float A = glm::roundEven(-0.0f);
302		Error += A ==  0.0f ? 0 : 1;
303		float B = glm::roundEven(-0.5f);
304		Error += B == -0.0f ? 0 : 1;
305		float C = glm::roundEven(-1.0f);
306		Error += C == -1.0f ? 0 : 1;
307		float D = glm::roundEven(-0.1f);
308		Error += D ==  0.0f ? 0 : 1;
309		float E = glm::roundEven(-0.9f);
310		Error += E == -1.0f ? 0 : 1;
311		float F = glm::roundEven(-1.5f);
312		Error += F == -2.0f ? 0 : 1;
313		float G = glm::roundEven(-1.9f);
314		Error += G == -2.0f ? 0 : 1;
315	}
316
317	{
318		float A = glm::roundEven(1.5f);
319		Error += A == 2.0f ? 0 : 1;
320		float B = glm::roundEven(2.5f);
321		Error += B == 2.0f ? 0 : 1;
322		float C = glm::roundEven(3.5f);
323		Error += C == 4.0f ? 0 : 1;
324		float D = glm::roundEven(4.5f);
325		Error += D == 4.0f ? 0 : 1;
326		float E = glm::roundEven(5.5f);
327		Error += E == 6.0f ? 0 : 1;
328		float F = glm::roundEven(6.5f);
329		Error += F == 6.0f ? 0 : 1;
330		float G = glm::roundEven(7.5f);
331		Error += G == 8.0f ? 0 : 1;
332	}
333	
334	{
335		float A = glm::roundEven(-1.5f);
336		Error += A == -2.0f ? 0 : 1;
337		float B = glm::roundEven(-2.5f);
338		Error += B == -2.0f ? 0 : 1;
339		float C = glm::roundEven(-3.5f);
340		Error += C == -4.0f ? 0 : 1;
341		float D = glm::roundEven(-4.5f);
342		Error += D == -4.0f ? 0 : 1;
343		float E = glm::roundEven(-5.5f);
344		Error += E == -6.0f ? 0 : 1;
345		float F = glm::roundEven(-6.5f);
346		Error += F == -6.0f ? 0 : 1;
347		float G = glm::roundEven(-7.5f);
348		Error += G == -8.0f ? 0 : 1;
349	}
350
351	return Error;
352}
353
354int test_isnan()
355{
356	int Error = 0;
357
358	float Zero_f = 0.0;
359	double Zero_d = 0.0;
360
361	{
362 		Error += true == glm::isnan(0.0/Zero_d) ? 0 : 1;
363 		Error += true == glm::any(glm::isnan(glm::dvec2(0.0 / Zero_d))) ? 0 : 1;
364 		Error += true == glm::any(glm::isnan(glm::dvec3(0.0 / Zero_d))) ? 0 : 1;
365 		Error += true == glm::any(glm::isnan(glm::dvec4(0.0 / Zero_d))) ? 0 : 1;
366	}
367
368	{
369 		Error += true == glm::isnan(0.0f/Zero_f) ? 0 : 1;
370 		Error += true == glm::any(glm::isnan(glm::vec2(0.0f/Zero_f))) ? 0 : 1;
371 		Error += true == glm::any(glm::isnan(glm::vec3(0.0f/Zero_f))) ? 0 : 1;
372 		Error += true == glm::any(glm::isnan(glm::vec4(0.0f/Zero_f))) ? 0 : 1;
373	}
374
375	return Error;
376}
377 
378int test_isinf()
379{
380	int Error = 0;
381 
382	float Zero_f = 0.0;
383	double Zero_d = 0.0;
384
385	{
386 		Error += true == glm::isinf( 1.0/Zero_d) ? 0 : 1;
387		Error += true == glm::isinf(-1.0/Zero_d) ? 0 : 1;
388 		Error += true == glm::any(glm::isinf(glm::dvec2( 1.0/Zero_d))) ? 0 : 1;
389 		Error += true == glm::any(glm::isinf(glm::dvec2(-1.0/Zero_d))) ? 0 : 1;
390 		Error += true == glm::any(glm::isinf(glm::dvec3( 1.0/Zero_d))) ? 0 : 1;
391 		Error += true == glm::any(glm::isinf(glm::dvec3(-1.0/Zero_d))) ? 0 : 1;
392 		Error += true == glm::any(glm::isinf(glm::dvec4( 1.0/Zero_d))) ? 0 : 1;
393 		Error += true == glm::any(glm::isinf(glm::dvec4(-1.0/Zero_d))) ? 0 : 1;
394	}
395 
396	{
397 		Error += true == glm::isinf( 1.0f/Zero_f) ? 0 : 1;
398 		Error += true == glm::isinf(-1.0f/Zero_f) ? 0 : 1;
399 		Error += true == glm::any(glm::isinf(glm::vec2( 1.0f/Zero_f))) ? 0 : 1;
400 		Error += true == glm::any(glm::isinf(glm::vec2(-1.0f/Zero_f))) ? 0 : 1;
401 		Error += true == glm::any(glm::isinf(glm::vec3( 1.0f/Zero_f))) ? 0 : 1;
402 		Error += true == glm::any(glm::isinf(glm::vec3(-1.0f/Zero_f))) ? 0 : 1;
403 		Error += true == glm::any(glm::isinf(glm::vec4( 1.0f/Zero_f))) ? 0 : 1;
404 		Error += true == glm::any(glm::isinf(glm::vec4(-1.0f/Zero_f))) ? 0 : 1;
405	}
406 
407	return Error;
408}
409
410int main()
411{
412	int Error(0);
413
414	Error += test_modf();
415	Error += test_floatBitsToInt();
416	Error += test_floatBitsToUint();
417	Error += test_mix();
418	Error += test_round();
419	Error += test_roundEven();
420	Error += test_isnan();
421	//Error += test_isinf();
422
423	return Error;
424}
425