core_func_common.cpp 12 KB

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