core_func_common.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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. //#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/ext.hpp>
  14. #include <glm/gtx/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::equalEpsilon(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::equalEpsilon(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. int test_mix()
  129. {
  130. int Error = 0;
  131. {
  132. float A = glm::mix(0.f, 1.f, true);
  133. Error += A == 1.f ? 0 : 1;
  134. float B = glm::mix(0.f, 1.f, false);
  135. Error += B == 0.f ? 0 : 1;
  136. }
  137. {
  138. float A = glm::mix(0.f, 1.f, 1.f);
  139. Error += A == 1.f ? 0 : 1;
  140. float B = glm::mix(0.f, 1.f, 0.f);
  141. Error += B == 0.f ? 0 : 1;
  142. }
  143. return Error;
  144. }
  145. int test_round()
  146. {
  147. int Error = 0;
  148. {
  149. float A = glm::round(0.0f);
  150. Error += A == 0.0f ? 0 : 1;
  151. float B = glm::round(0.5f);
  152. Error += B == 1.0f ? 0 : 1;
  153. float C = glm::round(1.0f);
  154. Error += C == 1.0f ? 0 : 1;
  155. float D = glm::round(0.1f);
  156. Error += D == 0.0f ? 0 : 1;
  157. float E = glm::round(0.9f);
  158. Error += E == 1.0f ? 0 : 1;
  159. float F = glm::round(1.5f);
  160. Error += F == 2.0f ? 0 : 1;
  161. float G = glm::round(1.9f);
  162. Error += G == 2.0f ? 0 : 1;
  163. }
  164. {
  165. float A = glm::round(-0.0f);
  166. Error += A == 0.0f ? 0 : 1;
  167. float B = glm::round(-0.5f);
  168. Error += B == -1.0f ? 0 : 1;
  169. float C = glm::round(-1.0f);
  170. Error += C == -1.0f ? 0 : 1;
  171. float D = glm::round(-0.1f);
  172. Error += D == 0.0f ? 0 : 1;
  173. float E = glm::round(-0.9f);
  174. Error += E == -1.0f ? 0 : 1;
  175. float F = glm::round(-1.5f);
  176. Error += F == -2.0f ? 0 : 1;
  177. float G = glm::round(-1.9f);
  178. Error += G == -2.0f ? 0 : 1;
  179. }
  180. return Error;
  181. }
  182. int test_roundEven()
  183. {
  184. int Error = 0;
  185. {
  186. float A = glm::roundEven(-1.5f);
  187. Error += glm::equalEpsilon(A, -2.0f, 0.0001f) ? 0 : 1;
  188. Error += 0;
  189. }
  190. {
  191. float A = glm::roundEven(1.5f);
  192. Error += glm::equalEpsilon(A, 2.0f, 0.0001f) ? 0 : 1;
  193. Error += 0;
  194. }
  195. {
  196. float A = glm::roundEven(-3.5f);
  197. Error += glm::equalEpsilon(A, -4.0f, 0.0001f) ? 0 : 1;
  198. Error += 0;
  199. }
  200. {
  201. float A = glm::roundEven(3.5f);
  202. Error += glm::equalEpsilon(A, 4.0f, 0.0001f) ? 0 : 1;
  203. Error += 0;
  204. }
  205. {
  206. float A = glm::roundEven(-2.5f);
  207. Error += glm::equalEpsilon(A, -2.0f, 0.0001f) ? 0 : 1;
  208. Error += 0;
  209. }
  210. {
  211. float A = glm::roundEven(2.5f);
  212. Error += glm::equalEpsilon(A, 2.0f, 0.0001f) ? 0 : 1;
  213. Error += 0;
  214. }
  215. {
  216. float A = glm::roundEven(-2.4f);
  217. Error += glm::equalEpsilon(A, -2.0f, 0.0001f) ? 0 : 1;
  218. Error += 0;
  219. }
  220. {
  221. float A = glm::roundEven(2.4f);
  222. Error += glm::equalEpsilon(A, 2.0f, 0.0001f) ? 0 : 1;
  223. Error += 0;
  224. }
  225. {
  226. float A = glm::roundEven(-2.6f);
  227. Error += glm::equalEpsilon(A, -3.0f, 0.0001f) ? 0 : 1;
  228. Error += 0;
  229. }
  230. {
  231. float A = glm::roundEven(2.6f);
  232. Error += glm::equalEpsilon(A, 3.0f, 0.0001f) ? 0 : 1;
  233. Error += 0;
  234. }
  235. {
  236. float A = glm::roundEven(-2.0f);
  237. Error += glm::equalEpsilon(A, -2.0f, 0.0001f) ? 0 : 1;
  238. Error += 0;
  239. }
  240. {
  241. float A = glm::roundEven(2.0f);
  242. Error += glm::equalEpsilon(A, 2.0f, 0.0001f) ? 0 : 1;
  243. Error += 0;
  244. }
  245. {
  246. float A = glm::roundEven(0.0f);
  247. Error += A == 0.0f ? 0 : 1;
  248. float B = glm::roundEven(0.5f);
  249. Error += B == 0.0f ? 0 : 1;
  250. float C = glm::roundEven(1.0f);
  251. Error += C == 1.0f ? 0 : 1;
  252. float D = glm::roundEven(0.1f);
  253. Error += D == 0.0f ? 0 : 1;
  254. float E = glm::roundEven(0.9f);
  255. Error += E == 1.0f ? 0 : 1;
  256. float F = glm::roundEven(1.5f);
  257. Error += F == 2.0f ? 0 : 1;
  258. float G = glm::roundEven(1.9f);
  259. Error += G == 2.0f ? 0 : 1;
  260. }
  261. {
  262. float A = glm::roundEven(-0.0f);
  263. Error += A == 0.0f ? 0 : 1;
  264. float B = glm::roundEven(-0.5f);
  265. Error += B == -0.0f ? 0 : 1;
  266. float C = glm::roundEven(-1.0f);
  267. Error += C == -1.0f ? 0 : 1;
  268. float D = glm::roundEven(-0.1f);
  269. Error += D == 0.0f ? 0 : 1;
  270. float E = glm::roundEven(-0.9f);
  271. Error += E == -1.0f ? 0 : 1;
  272. float F = glm::roundEven(-1.5f);
  273. Error += F == -2.0f ? 0 : 1;
  274. float G = glm::roundEven(-1.9f);
  275. Error += G == -2.0f ? 0 : 1;
  276. }
  277. {
  278. float A = glm::roundEven(1.5f);
  279. Error += A == 2.0f ? 0 : 1;
  280. float B = glm::roundEven(2.5f);
  281. Error += B == 2.0f ? 0 : 1;
  282. float C = glm::roundEven(3.5f);
  283. Error += C == 4.0f ? 0 : 1;
  284. float D = glm::roundEven(4.5f);
  285. Error += D == 4.0f ? 0 : 1;
  286. float E = glm::roundEven(5.5f);
  287. Error += E == 6.0f ? 0 : 1;
  288. float F = glm::roundEven(6.5f);
  289. Error += F == 6.0f ? 0 : 1;
  290. float G = glm::roundEven(7.5f);
  291. Error += G == 8.0f ? 0 : 1;
  292. }
  293. {
  294. float A = glm::roundEven(-1.5f);
  295. Error += A == -2.0f ? 0 : 1;
  296. float B = glm::roundEven(-2.5f);
  297. Error += B == -2.0f ? 0 : 1;
  298. float C = glm::roundEven(-3.5f);
  299. Error += C == -4.0f ? 0 : 1;
  300. float D = glm::roundEven(-4.5f);
  301. Error += D == -4.0f ? 0 : 1;
  302. float E = glm::roundEven(-5.5f);
  303. Error += E == -6.0f ? 0 : 1;
  304. float F = glm::roundEven(-6.5f);
  305. Error += F == -6.0f ? 0 : 1;
  306. float G = glm::roundEven(-7.5f);
  307. Error += G == -8.0f ? 0 : 1;
  308. }
  309. return Error;
  310. }
  311. int test_isnan()
  312. {
  313. int Error = 0;
  314. float Zero_f = 0.0;
  315. double Zero_d = 0.0;
  316. {
  317. Error += true == glm::isnan(0.0/Zero_d) ? 0 : 1;
  318. Error += true == glm::any(glm::isnan(glm::dvec2(0.0f/Zero_f))) ? 0 : 1;
  319. Error += true == glm::any(glm::isnan(glm::dvec3(0.0f/Zero_f))) ? 0 : 1;
  320. Error += true == glm::any(glm::isnan(glm::dvec4(0.0f/Zero_f))) ? 0 : 1;
  321. }
  322. {
  323. Error += true == glm::isnan(0.0f/Zero_f) ? 0 : 1;
  324. Error += true == glm::any(glm::isnan(glm::vec2(0.0f/Zero_f))) ? 0 : 1;
  325. Error += true == glm::any(glm::isnan(glm::vec3(0.0f/Zero_f))) ? 0 : 1;
  326. Error += true == glm::any(glm::isnan(glm::vec4(0.0f/Zero_f))) ? 0 : 1;
  327. }
  328. return Error;
  329. }
  330. int test_isinf()
  331. {
  332. int Error = 0;
  333. float Zero_f = 0.0;
  334. double Zero_d = 0.0;
  335. {
  336. Error += true == glm::isinf( 1.0/Zero_d) ? 0 : 1;
  337. Error += true == glm::isinf(-1.0/Zero_d) ? 0 : 1;
  338. Error += true == glm::any(glm::isinf(glm::dvec2( 1.0/Zero_d))) ? 0 : 1;
  339. Error += true == glm::any(glm::isinf(glm::dvec2(-1.0/Zero_d))) ? 0 : 1;
  340. Error += true == glm::any(glm::isinf(glm::dvec3( 1.0/Zero_d))) ? 0 : 1;
  341. Error += true == glm::any(glm::isinf(glm::dvec3(-1.0/Zero_d))) ? 0 : 1;
  342. Error += true == glm::any(glm::isinf(glm::dvec4( 1.0/Zero_d))) ? 0 : 1;
  343. Error += true == glm::any(glm::isinf(glm::dvec4(-1.0/Zero_d))) ? 0 : 1;
  344. }
  345. {
  346. Error += true == glm::isinf( 1.0f/Zero_f) ? 0 : 1;
  347. Error += true == glm::isinf(-1.0f/Zero_f) ? 0 : 1;
  348. Error += true == glm::any(glm::isinf(glm::vec2( 1.0f/Zero_f))) ? 0 : 1;
  349. Error += true == glm::any(glm::isinf(glm::vec2(-1.0f/Zero_f))) ? 0 : 1;
  350. Error += true == glm::any(glm::isinf(glm::vec3( 1.0f/Zero_f))) ? 0 : 1;
  351. Error += true == glm::any(glm::isinf(glm::vec3(-1.0f/Zero_f))) ? 0 : 1;
  352. Error += true == glm::any(glm::isinf(glm::vec4( 1.0f/Zero_f))) ? 0 : 1;
  353. Error += true == glm::any(glm::isinf(glm::vec4(-1.0f/Zero_f))) ? 0 : 1;
  354. }
  355. return Error;
  356. }
  357. int main()
  358. {
  359. int Error(0);
  360. Error += test_modf();
  361. Error += test_floatBitsToInt();
  362. Error += test_floatBitsToUint();
  363. Error += test_mix();
  364. Error += test_round();
  365. Error += test_roundEven();
  366. Error += test_isnan();
  367. Error += test_isinf();
  368. return Error;
  369. }