core_func_common.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Mathematics Copyright (c) 2005 - 2011 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 <glm/glm.hpp>
  10. int test_floatBitsToInt()
  11. {
  12. int Error = 0;
  13. {
  14. float A = 1.0f;
  15. int B = glm::floatBitsToInt(A);
  16. float C = glm::intBitsToFloat(B);
  17. int D = *(int*)&A;
  18. Error += B == D ? 0 : 1;
  19. Error += A == C ? 0 : 1;
  20. }
  21. {
  22. glm::vec2 A(1.0f, 2.0f);
  23. glm::ivec2 B = glm::floatBitsToInt(A);
  24. glm::vec2 C = glm::intBitsToFloat(B);
  25. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  26. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  27. Error += A == C? 0 : 1;
  28. }
  29. {
  30. glm::vec3 A(1.0f, 2.0f, 3.0f);
  31. glm::ivec3 B = glm::floatBitsToInt(A);
  32. glm::vec3 C = glm::intBitsToFloat(B);
  33. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  34. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  35. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  36. Error += A == C? 0 : 1;
  37. }
  38. {
  39. glm::vec4 A(1.0f, 2.0f, 3.0f, 4.0f);
  40. glm::ivec4 B = glm::floatBitsToInt(A);
  41. glm::vec4 C = glm::intBitsToFloat(B);
  42. Error += B.x == *(int*)&(A.x) ? 0 : 1;
  43. Error += B.y == *(int*)&(A.y) ? 0 : 1;
  44. Error += B.z == *(int*)&(A.z) ? 0 : 1;
  45. Error += B.w == *(int*)&(A.w) ? 0 : 1;
  46. Error += A == C? 0 : 1;
  47. }
  48. return Error;
  49. }
  50. int test_floatBitsToUint()
  51. {
  52. int Error = 0;
  53. {
  54. float A = 1.0f;
  55. glm::uint B = glm::floatBitsToUint(A);
  56. float C = glm::intBitsToFloat(B);
  57. Error += B == *(glm::uint*)&A ? 0 : 1;
  58. Error += A == C? 0 : 1;
  59. }
  60. {
  61. glm::vec2 A(1.0f, 2.0f);
  62. glm::uvec2 B = glm::floatBitsToUint(A);
  63. glm::vec2 C = glm::uintBitsToFloat(B);
  64. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  65. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  66. Error += A == C ? 0 : 1;
  67. }
  68. {
  69. glm::vec3 A(1.0f, 2.0f, 3.0f);
  70. glm::uvec3 B = glm::floatBitsToUint(A);
  71. glm::vec3 C = glm::uintBitsToFloat(B);
  72. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  73. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  74. Error += B.z == *(glm::uint*)&(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::uvec4 B = glm::floatBitsToUint(A);
  80. glm::vec4 C = glm::uintBitsToFloat(B);
  81. Error += B.x == *(glm::uint*)&(A.x) ? 0 : 1;
  82. Error += B.y == *(glm::uint*)&(A.y) ? 0 : 1;
  83. Error += B.z == *(glm::uint*)&(A.z) ? 0 : 1;
  84. Error += B.w == *(glm::uint*)&(A.w) ? 0 : 1;
  85. Error += A == C? 0 : 1;
  86. }
  87. return Error;
  88. }
  89. int test_mix()
  90. {
  91. int Error = 0;
  92. {
  93. float A = glm::mix(0.f, 1.f, true);
  94. Error += A == 1.f ? 0 : 1;
  95. float B = glm::mix(0.f, 1.f, false);
  96. Error += B == 0.f ? 0 : 1;
  97. }
  98. {
  99. float A = glm::mix(0.f, 1.f, 1.f);
  100. Error += A == 1.f ? 0 : 1;
  101. float B = glm::mix(0.f, 1.f, 0.f);
  102. Error += B == 0.f ? 0 : 1;
  103. }
  104. return Error;
  105. }
  106. int test_round()
  107. {
  108. int Error = 0;
  109. {
  110. float A = glm::round(0.0f);
  111. Error += A == 0.0f ? 0 : 1;
  112. float B = glm::round(0.5f);
  113. Error += B == 1.0f ? 0 : 1;
  114. float C = glm::round(1.0f);
  115. Error += C == 1.0f ? 0 : 1;
  116. float D = glm::round(0.1f);
  117. Error += D == 0.0f ? 0 : 1;
  118. float E = glm::round(0.9f);
  119. Error += E == 1.0f ? 0 : 1;
  120. float F = glm::round(1.9f);
  121. Error += F == 2.0f ? 0 : 1;
  122. }
  123. {
  124. float A = glm::round(-0.0f);
  125. Error += A == 0.0f ? 0 : 1;
  126. float B = glm::round(-0.5f);
  127. Error += B == -1.0f ? 0 : 1;
  128. float C = glm::round(-1.0f);
  129. Error += C == -1.0f ? 0 : 1;
  130. float D = glm::round(-0.1f);
  131. Error += D == 0.0f ? 0 : 1;
  132. float E = glm::round(-0.9f);
  133. Error += E == -1.0f ? 0 : 1;
  134. float F = glm::round(-1.9f);
  135. Error += F == -2.0f ? 0 : 1;
  136. }
  137. return Error;
  138. }
  139. int main()
  140. {
  141. int Error = 0;
  142. Error += test_floatBitsToInt();
  143. Error += test_floatBitsToUint();
  144. Error += test_mix();
  145. Error += test_round();
  146. return Error;
  147. }