PageRenderTime 30ms CodeModel.GetById 47ms RepoModel.GetById 12ms 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. //#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/epsilon.hpp>
  14. #include <cstdio>
  15. int test_modf()
  16. {
  17. int Error(0);
  18. {
  19. float X(1.5f);
  20. float I(0.0f);
  21. float A = glm::modf(X, I);
  22. Error += I == 1.0f ? 0 : 1;
  23. Error += A == 0.5f ? 0 : 1;
  24. }
  25. {
  26. glm::vec4 X(1.1f, 1.2f, 1.5f, 1.7f);
  27. glm::vec4 I(0.0f);
  28. glm::vec4 A = glm::modf(X, I);
  29. Error += I == glm::vec4(1.0f) ? 0 : 1;
  30. Error += glm::all(glm::epsilonEqual(A, glm::vec4(0.1f, 0.2f, 0.5f, 0.7f), 0.00001f)) ? 0 : 1;
  31. }
  32. {
  33. glm::dvec4 X(1.1, 1.2, 1.5, 1.7);
  34. glm::dvec4 I(0.0);
  35. glm::dvec4 A = glm::modf(X, I);
  36. Error += I == glm::dvec4(1.0) ? 0 : 1;
  37. Error += glm::all(glm::epsilonEqual(A, glm::dvec4(0.1, 0.2, 0.5, 0.7), 0.000000001)) ? 0 : 1;
  38. }
  39. {
  40. double X(1.5);
  41. double I(0.0);
  42. double A = glm::modf(X, I);
  43. Error += I == 1.0 ? 0 : 1;
  44. Error += A == 0.5 ? 0 : 1;
  45. }
  46. return Error;
  47. }
  48. int test_floatBitsToInt()
  49. {
  50. int Error = 0;
  51. {
  52. float A = 1.0f;
  53. int B = glm::floatBitsToInt(A);
  54. float C = glm::intBitsToFloat(B);
  55. int D = *(int*)&A;
  56. Error += B == D ? 0 : 1;
  57. Error += A == C ? 0 : 1;
  58. }
  59. {
  60. glm::vec2 A(1.0f, 2.0f);
  61. glm::ivec2 B = glm::floatBitsToInt(A);
  62. glm::vec2 C = glm::intBitsToFloat(B);
  63. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  64. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  65. Error += A == C? 0 : 1;
  66. }
  67. {
  68. glm::vec3 A(1.0f, 2.0f, 3.0f);
  69. glm::ivec3 B = glm::floatBitsToInt(A);
  70. glm::vec3 C = glm::intBitsToFloat(B);
  71. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  72. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  73. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  74. Error += A == C? 0 : 1;
  75. }
  76. {
  77. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  78. glm::ivec4 B = glm::floatBitsToInt(A);
  79. glm::vec4 C = glm::intBitsToFloat(B);
  80. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  81. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  82. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  83. Error += B.w == *(int*)&(A.w) ? 0 : 1;
  84. Error += A == C? 0 : 1;
  85. }
  86. return Error;
  87. }
  88. int test_floatBitsToUint()
  89. {
  90. int Error = 0;
  91. {
  92. float A = 1.0f;
  93. glm::uint B = glm::floatBitsToUint(A);
  94. float C = glm::intBitsToFloat(B);
  95. Error += B == *(glm::uint*)&A ? 0 : 1;
  96. Error += A == C? 0 : 1;
  97. }
  98. {
  99. glm::vec2 A(1.0f, 2.0f);
  100. glm::uvec2 B = glm::floatBitsToUint(A);
  101. glm::vec2 C = glm::uintBitsToFloat(B);
  102. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  103. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  104. Error += A == C ? 0 : 1;
  105. }
  106. {
  107. glm::vec3 A(1.0f, 2.0f, 3.0f);
  108. glm::uvec3 B = glm::floatBitsToUint(A);
  109. glm::vec3 C = glm::uintBitsToFloat(B);
  110. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  111. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  112. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  113. Error += A == C? 0 : 1;
  114. }
  115. {
  116. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  117. glm::uvec4 B = glm::floatBitsToUint(A);
  118. glm::vec4 C = glm::uintBitsToFloat(B);
  119. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  120. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  121. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  122. Error += B.w == *(glm::uint*)&(A.w) ? 0 : 1;
  123. Error += A == C? 0 : 1;
  124. }
  125. return Error;
  126. }
  127. int test_mix()
  128. {
  129. int Error = 0;
  130. {
  131. float A = glm::mix(0.f, 1.f, true);
  132. Error += A == 1.f ? 0 : 1;
  133. float B = glm::mix(0.f, 1.f, false);
  134. Error += B == 0.f ? 0 : 1;
  135. }
  136. {
  137. float A = glm::mix(0.f, 1.f, 1.f);
  138. Error += A == 1.f ? 0 : 1;
  139. float B = glm::mix(0.f, 1.f, 0.f);
  140. Error += B == 0.f ? 0 : 1;
  141. }
  142. return Error;
  143. }
  144. int test_round()
  145. {
  146. int Error = 0;
  147. {
  148. float A = glm::round(0.0f);
  149. Error += A == 0.0f ? 0 : 1;
  150. float B = glm::round(0.5f);
  151. Error += B == 1.0f ? 0 : 1;
  152. float C = glm::round(1.0f);
  153. Error += C == 1.0f ? 0 : 1;
  154. float D = glm::round(0.1f);
  155. Error += D == 0.0f ? 0 : 1;
  156. float E = glm::round(0.9f);
  157. Error += E == 1.0f ? 0 : 1;
  158. float F = glm::round(1.5f);
  159. Error += F == 2.0f ? 0 : 1;
  160. float G = glm::round(1.9f);
  161. Error += G == 2.0f ? 0 : 1;
  162. }
  163. {
  164. float A = glm::round(-0.0f);
  165. Error += A == 0.0f ? 0 : 1;
  166. float B = glm::round(-0.5f);
  167. Error += B == -1.0f ? 0 : 1;
  168. float C = glm::round(-1.0f);
  169. Error += C == -1.0f ? 0 : 1;
  170. float D = glm::round(-0.1f);
  171. Error += D == 0.0f ? 0 : 1;
  172. float E = glm::round(-0.9f);
  173. Error += E == -1.0f ? 0 : 1;
  174. float F = glm::round(-1.5f);
  175. Error += F == -2.0f ? 0 : 1;
  176. float G = glm::round(-1.9f);
  177. Error += G == -2.0f ? 0 : 1;
  178. }
  179. return Error;
  180. }
  181. int test_roundEven()
  182. {
  183. int Error = 0;
  184. {
  185. float A = glm::roundEven(-1.5f);
  186. Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
  187. Error += 0;
  188. }
  189. {
  190. float A = glm::roundEven(1.5f);
  191. Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
  192. Error += 0;
  193. }
  194. {
  195. float A = glm::roundEven(-3.5f);
  196. Error += glm::epsilonEqual(A, -4.0f, 0.0001f) ? 0 : 1;
  197. Error += 0;
  198. }
  199. {
  200. float A = glm::roundEven(3.5f);
  201. Error += glm::epsilonEqual(A, 4.0f, 0.0001f) ? 0 : 1;
  202. Error += 0;
  203. }
  204. {
  205. float A = glm::roundEven(-2.5f);
  206. Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
  207. Error += 0;
  208. }
  209. {
  210. float A = glm::roundEven(2.5f);
  211. Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
  212. Error += 0;
  213. }
  214. {
  215. float A = glm::roundEven(-2.4f);
  216. Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
  217. Error += 0;
  218. }
  219. {
  220. float A = glm::roundEven(2.4f);
  221. Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
  222. Error += 0;
  223. }
  224. {
  225. float A = glm::roundEven(-2.6f);
  226. Error += glm::epsilonEqual(A, -3.0f, 0.0001f) ? 0 : 1;
  227. Error += 0;
  228. }
  229. {
  230. float A = glm::roundEven(2.6f);
  231. Error += glm::epsilonEqual(A, 3.0f, 0.0001f) ? 0 : 1;
  232. Error += 0;
  233. }
  234. {
  235. float A = glm::roundEven(-2.0f);
  236. Error += glm::epsilonEqual(A, -2.0f, 0.0001f) ? 0 : 1;
  237. Error += 0;
  238. }
  239. {
  240. float A = glm::roundEven(2.0f);
  241. Error += glm::epsilonEqual(A, 2.0f, 0.0001f) ? 0 : 1;
  242. Error += 0;
  243. }
  244. {
  245. float A = glm::roundEven(0.0f);
  246. Error += A == 0.0f ? 0 : 1;
  247. float B = glm::roundEven(0.5f);
  248. Error += B == 0.0f ? 0 : 1;
  249. float C = glm::roundEven(1.0f);
  250. Error += C == 1.0f ? 0 : 1;
  251. float D = glm::roundEven(0.1f);
  252. Error += D == 0.0f ? 0 : 1;
  253. float E = glm::roundEven(0.9f);
  254. Error += E == 1.0f ? 0 : 1;
  255. float F = glm::roundEven(1.5f);
  256. Error += F == 2.0f ? 0 : 1;
  257. float G = glm::roundEven(1.9f);
  258. Error += G == 2.0f ? 0 : 1;
  259. }
  260. {
  261. float A = glm::roundEven(-0.0f);
  262. Error += A == 0.0f ? 0 : 1;
  263. float B = glm::roundEven(-0.5f);
  264. Error += B == -0.0f ? 0 : 1;
  265. float C = glm::roundEven(-1.0f);
  266. Error += C == -1.0f ? 0 : 1;
  267. float D = glm::roundEven(-0.1f);
  268. Error += D == 0.0f ? 0 : 1;
  269. float E = glm::roundEven(-0.9f);
  270. Error += E == -1.0f ? 0 : 1;
  271. float F = glm::roundEven(-1.5f);
  272. Error += F == -2.0f ? 0 : 1;
  273. float G = glm::roundEven(-1.9f);
  274. Error += G == -2.0f ? 0 : 1;
  275. }
  276. {
  277. float A = glm::roundEven(1.5f);
  278. Error += A == 2.0f ? 0 : 1;
  279. float B = glm::roundEven(2.5f);
  280. Error += B == 2.0f ? 0 : 1;
  281. float C = glm::roundEven(3.5f);
  282. Error += C == 4.0f ? 0 : 1;
  283. float D = glm::roundEven(4.5f);
  284. Error += D == 4.0f ? 0 : 1;
  285. float E = glm::roundEven(5.5f);
  286. Error += E == 6.0f ? 0 : 1;
  287. float F = glm::roundEven(6.5f);
  288. Error += F == 6.0f ? 0 : 1;
  289. float G = glm::roundEven(7.5f);
  290. Error += G == 8.0f ? 0 : 1;
  291. }
  292. {
  293. float A = glm::roundEven(-1.5f);
  294. Error += A == -2.0f ? 0 : 1;
  295. float B = glm::roundEven(-2.5f);
  296. Error += B == -2.0f ? 0 : 1;
  297. float C = glm::roundEven(-3.5f);
  298. Error += C == -4.0f ? 0 : 1;
  299. float D = glm::roundEven(-4.5f);
  300. Error += D == -4.0f ? 0 : 1;
  301. float E = glm::roundEven(-5.5f);
  302. Error += E == -6.0f ? 0 : 1;
  303. float F = glm::roundEven(-6.5f);
  304. Error += F == -6.0f ? 0 : 1;
  305. float G = glm::roundEven(-7.5f);
  306. Error += G == -8.0f ? 0 : 1;
  307. }
  308. return Error;
  309. }
  310. int test_isnan()
  311. {
  312. int Error = 0;
  313. float Zero_f = 0.0;
  314. double Zero_d = 0.0;
  315. {
  316. Error += true == glm::isnan(0.0/Zero_d) ? 0 : 1;
  317. Error += true == glm::any(glm::isnan(glm::dvec2(0.0 / Zero_d))) ? 0 : 1;
  318. Error += true == glm::any(glm::isnan(glm::dvec3(0.0 / Zero_d))) ? 0 : 1;
  319. Error += true == glm::any(glm::isnan(glm::dvec4(0.0 / Zero_d))) ? 0 : 1;
  320. }
  321. {
  322. Error += true == glm::isnan(0.0f/Zero_f) ? 0 : 1;
  323. Error += true == glm::any(glm::isnan(glm::vec2(0.0f/Zero_f))) ? 0 : 1;
  324. Error += true == glm::any(glm::isnan(glm::vec3(0.0f/Zero_f))) ? 0 : 1;
  325. Error += true == glm::any(glm::isnan(glm::vec4(0.0f/Zero_f))) ? 0 : 1;
  326. }
  327. return Error;
  328. }
  329. int test_isinf()
  330. {
  331. int Error = 0;
  332. float Zero_f = 0.0;
  333. double Zero_d = 0.0;
  334. {
  335. Error += true == glm::isinf( 1.0/Zero_d) ? 0 : 1;
  336. Error += true == glm::isinf(-1.0/Zero_d) ? 0 : 1;
  337. Error += true == glm::any(glm::isinf(glm::dvec2( 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::dvec3( 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::dvec4( 1.0/Zero_d))) ? 0 : 1;
  342. Error += true == glm::any(glm::isinf(glm::dvec4(-1.0/Zero_d))) ? 0 : 1;
  343. }
  344. {
  345. Error += true == glm::isinf( 1.0f/Zero_f) ? 0 : 1;
  346. Error += true == glm::isinf(-1.0f/Zero_f) ? 0 : 1;
  347. Error += true == glm::any(glm::isinf(glm::vec2( 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::vec3( 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::vec4( 1.0f/Zero_f))) ? 0 : 1;
  352. Error += true == glm::any(glm::isinf(glm::vec4(-1.0f/Zero_f))) ? 0 : 1;
  353. }
  354. return Error;
  355. }
  356. int main()
  357. {
  358. int Error(0);
  359. Error += test_modf();
  360. Error += test_floatBitsToInt();
  361. Error += test_floatBitsToUint();
  362. Error += test_mix();
  363. Error += test_round();
  364. Error += test_roundEven();
  365. Error += test_isnan();
  366. //Error += test_isinf();
  367. return Error;
  368. }