core_cpp_constexpr.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #include <glm/glm.hpp>
  2. #if GLM_USE_CONSTEXP == GLM_ENABLE
  3. #include <glm/gtc/constants.hpp>
  4. #include <glm/gtc/quaternion.hpp>
  5. #include <glm/ext/vector_relational.hpp>
  6. #include <glm/ext/vec1.hpp>
  7. static int test_vec1()
  8. {
  9. int Error = 0;
  10. {
  11. constexpr glm::ivec1 O(glm::ivec1(1));
  12. static_assert(glm::ivec1(1) == O, "GLM: Failed constexpr");
  13. constexpr glm::ivec1 P(1);
  14. static_assert(glm::ivec1(1) == P, "GLM: Failed constexpr");
  15. }
  16. {
  17. constexpr glm::ivec1 L(glm::ivec2(1, 2));
  18. static_assert(glm::ivec1(1) == L, "GLM: Failed constexpr");
  19. constexpr glm::ivec1 M(glm::ivec3(1, 2, 3));
  20. static_assert(glm::ivec1(1) == M, "GLM: Failed constexpr");
  21. constexpr glm::ivec1 N(glm::ivec4(1, 2, 3, 4));
  22. static_assert(glm::ivec1(1) == N, "GLM: Failed constexpr");
  23. }
  24. {
  25. constexpr glm::ivec1 A(1);
  26. static_assert(A[0] == 1, "GLM: Failed constexpr");
  27. static_assert(glm::vec1(1.0f).x > 0.0f, "GLM: Failed constexpr");
  28. static_assert(glm::vec1::length() == 1, "GLM: Failed constexpr");
  29. }
  30. {
  31. constexpr glm::bvec1 A1(true);
  32. constexpr glm::bvec1 A2(true);
  33. constexpr glm::bvec1 B1(false);
  34. constexpr glm::bvec1 B2(false);
  35. static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr");
  36. static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr");
  37. }
  38. return Error;
  39. }
  40. static int test_vec2()
  41. {
  42. int Error = 0;
  43. {
  44. constexpr glm::ivec2 O(glm::ivec1(1));
  45. static_assert(glm::ivec2(1) == O, "GLM: Failed constexpr");
  46. constexpr glm::ivec2 A(1);
  47. static_assert(glm::ivec2(1) == A, "GLM: Failed constexpr");
  48. }
  49. {
  50. constexpr glm::ivec2 F(glm::ivec1(1), glm::ivec1(2));
  51. static_assert(glm::ivec2(1, 2) == F, "GLM: Failed constexpr");
  52. constexpr glm::ivec2 G(1, glm::ivec1(2));
  53. static_assert(glm::ivec2(1, 2) == G, "GLM: Failed constexpr");
  54. constexpr glm::ivec2 H(glm::ivec1(1), 2);
  55. static_assert(glm::ivec2(1, 2) == H, "GLM: Failed constexpr");
  56. constexpr glm::ivec2 I(1, 2);
  57. static_assert(glm::ivec2(1, 2) == I, "GLM: Failed constexpr");
  58. }
  59. {
  60. constexpr glm::ivec2 L(glm::ivec2(1, 2));
  61. static_assert(glm::ivec2(1, 2) == L, "GLM: Failed constexpr");
  62. constexpr glm::ivec2 M(glm::ivec3(1, 2, 3));
  63. static_assert(glm::ivec2(1, 2) == M, "GLM: Failed constexpr");
  64. constexpr glm::ivec2 N(glm::ivec4(1, 2, 3, 4));
  65. static_assert(glm::ivec2(1, 2) == N, "GLM: Failed constexpr");
  66. }
  67. {
  68. constexpr glm::ivec2 A(1);
  69. static_assert(A[0] == 1, "GLM: Failed constexpr");
  70. static_assert(glm::vec2(1.0f).x > 0.0f, "GLM: Failed constexpr");
  71. static_assert(glm::vec2(1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr");
  72. static_assert(glm::vec2(1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr");
  73. static_assert(glm::vec2::length() == 2, "GLM: Failed constexpr");
  74. }
  75. {
  76. constexpr glm::bvec2 A1(true);
  77. constexpr glm::bvec2 A2(true);
  78. constexpr glm::bvec2 B1(false);
  79. constexpr glm::bvec2 B2(false);
  80. static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr");
  81. static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr");
  82. }
  83. {
  84. constexpr glm::ivec2 A(1);
  85. constexpr glm::ivec2 B = A + 1;
  86. constexpr glm::ivec2 C(3);
  87. static_assert(A + B == C, "GLM: Failed constexpr");
  88. constexpr glm::ivec2 D = +A;
  89. static_assert(D == A, "GLM: Failed constexpr");
  90. }
  91. {
  92. constexpr glm::ivec2 A(3);
  93. constexpr glm::ivec2 B = A - 1;
  94. constexpr glm::ivec2 C(1);
  95. static_assert(A - B == C, "GLM: Failed constexpr");
  96. constexpr glm::ivec2 D = -A;
  97. static_assert(-D == A, "GLM: Failed constexpr");
  98. }
  99. {
  100. constexpr glm::ivec2 A(3);
  101. constexpr glm::ivec2 B = A * 1;
  102. static_assert(A == B, "GLM: Failed constexpr");
  103. constexpr glm::ivec2 C(1);
  104. static_assert(B * C == A, "GLM: Failed constexpr");
  105. }
  106. {
  107. constexpr glm::ivec2 A(3);
  108. constexpr glm::ivec2 B = A / 1;
  109. static_assert(A == B, "GLM: Failed constexpr");
  110. constexpr glm::ivec2 C(1);
  111. static_assert(B / C == A, "GLM: Failed constexpr");
  112. }
  113. {
  114. constexpr glm::ivec2 A(3);
  115. constexpr glm::ivec2 B = A % 2;
  116. constexpr glm::ivec2 C(1);
  117. static_assert(B == C, "GLM: Failed constexpr");
  118. constexpr glm::ivec1 D(2);
  119. static_assert(A % D == C, "GLM: Failed constexpr");
  120. }
  121. {
  122. constexpr glm::ivec2 A(1);
  123. constexpr glm::ivec2 B = A & 1;
  124. static_assert(A == B, "GLM: Failed constexpr");
  125. constexpr glm::ivec1 C(1);
  126. static_assert(A == (A & C), "GLM: Failed constexpr");
  127. }
  128. {
  129. constexpr glm::ivec2 A(1);
  130. constexpr glm::ivec2 B = A | 1;
  131. static_assert(A == B, "GLM: Failed constexpr");
  132. constexpr glm::ivec1 C(1);
  133. static_assert(A == (A | C), "GLM: Failed constexpr");
  134. }
  135. {
  136. constexpr glm::ivec2 A(1);
  137. constexpr glm::ivec2 B = A ^ 0;
  138. static_assert(A == B, "GLM: Failed constexpr");
  139. constexpr glm::ivec1 C(0);
  140. static_assert(A == (A ^ C), "GLM: Failed constexpr");
  141. }
  142. {
  143. constexpr glm::ivec2 A(1);
  144. constexpr glm::ivec2 B = A << 1;
  145. static_assert(B == glm::ivec2(2), "GLM: Failed constexpr");
  146. constexpr glm::ivec1 C(1);
  147. static_assert(B == (A << C), "GLM: Failed constexpr");
  148. }
  149. {
  150. constexpr glm::ivec2 A(2);
  151. constexpr glm::ivec2 B = A >> 1;
  152. static_assert(B == glm::ivec2(1), "GLM: Failed constexpr");
  153. constexpr glm::ivec1 C(1);
  154. static_assert(B == A >> C, "GLM: Failed constexpr");
  155. }
  156. {
  157. constexpr glm::ivec2 A(~0);
  158. constexpr glm::ivec2 B = ~A;
  159. static_assert(A == ~B, "GLM: Failed constexpr");
  160. }
  161. return Error;
  162. }
  163. static int test_vec3()
  164. {
  165. int Error = 0;
  166. {
  167. constexpr glm::ivec3 O(glm::ivec1(1));
  168. static_assert(glm::ivec3(1) == O, "GLM: Failed constexpr");
  169. constexpr glm::ivec3 A(1);
  170. static_assert(glm::ivec3(1) == A, "GLM: Failed constexpr");
  171. }
  172. {
  173. constexpr glm::ivec3 B(glm::ivec2(1, 2), 3);
  174. static_assert(glm::ivec3(1, 2, 3) == B, "GLM: Failed constexpr");
  175. constexpr glm::ivec3 C(1, glm::ivec2(2, 3));
  176. static_assert(glm::ivec3(1, 2, 3) == C, "GLM: Failed constexpr");
  177. constexpr glm::ivec3 D(glm::ivec1(1), glm::ivec2(2, 3));
  178. static_assert(glm::ivec3(1, 2, 3) == D, "GLM: Failed constexpr");
  179. constexpr glm::ivec3 E(glm::ivec2(1, 2), glm::ivec1(3));
  180. static_assert(glm::ivec3(1, 2, 3) == E, "GLM: Failed constexpr");
  181. }
  182. {
  183. constexpr glm::ivec3 F(glm::ivec1(1), glm::ivec1(2), glm::ivec1(3));
  184. static_assert(glm::ivec3(1, 2, 3) == F, "GLM: Failed constexpr");
  185. constexpr glm::ivec3 G(1, glm::ivec1(2), glm::ivec1(3));
  186. static_assert(glm::ivec3(1, 2, 3) == G, "GLM: Failed constexpr");
  187. constexpr glm::ivec3 H(glm::ivec1(1), 2, glm::ivec1(3));
  188. static_assert(glm::ivec3(1, 2, 3) == H, "GLM: Failed constexpr");
  189. constexpr glm::ivec3 I(1, 2, glm::ivec1(3));
  190. static_assert(glm::ivec3(1, 2, 3) == I, "GLM: Failed constexpr");
  191. constexpr glm::ivec3 J(glm::ivec1(1), glm::ivec1(2), 3);
  192. static_assert(glm::ivec3(1, 2, 3) == J, "GLM: Failed constexpr");
  193. constexpr glm::ivec3 K(1, glm::ivec1(2), 3);
  194. static_assert(glm::ivec3(1, 2, 3) == K, "GLM: Failed constexpr");
  195. constexpr glm::ivec3 L(glm::ivec1(1), 2, 3);
  196. static_assert(glm::ivec3(1, 2, 3) == L, "GLM: Failed constexpr");
  197. constexpr glm::ivec3 M(1, 2, 3);
  198. static_assert(glm::ivec3(1, 2, 3) == M, "GLM: Failed constexpr");
  199. }
  200. {
  201. constexpr glm::ivec3 N(glm::ivec4(1, 2, 3, 4));
  202. static_assert(glm::ivec3(1, 2, 3) == N, "GLM: Failed constexpr");
  203. }
  204. {
  205. constexpr glm::ivec3 const A(1);
  206. static_assert(A[0] == 1, "GLM: Failed constexpr");
  207. static_assert(glm::vec3(1.0f).x > 0.0f, "GLM: Failed constexpr");
  208. static_assert(glm::vec3(1.0f, -1.0f, -1.0f).x > 0.0f, "GLM: Failed constexpr");
  209. static_assert(glm::vec3(1.0f, -1.0f, -1.0f).y < 0.0f, "GLM: Failed constexpr");
  210. static_assert(glm::vec3::length() == 3, "GLM: Failed constexpr");
  211. }
  212. {
  213. constexpr glm::bvec3 A1(true);
  214. constexpr glm::bvec3 A2(true);
  215. constexpr glm::bvec3 B1(false);
  216. constexpr glm::bvec3 B2(false);
  217. static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr");
  218. static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr");
  219. }
  220. return Error;
  221. }
  222. static int test_vec4()
  223. {
  224. int Error = 0;
  225. {
  226. constexpr glm::ivec4 O(glm::ivec4(1));
  227. static_assert(glm::ivec4(1) == O, "GLM: Failed constexpr");
  228. constexpr glm::ivec4 A(1);
  229. static_assert(glm::ivec4(1) == A, "GLM: Failed constexpr");
  230. constexpr glm::ivec4 N(glm::ivec4(1, 2, 3, 4));
  231. static_assert(glm::ivec4(1, 2, 3, 4) == N, "GLM: Failed constexpr");
  232. }
  233. {
  234. constexpr glm::ivec4 A(glm::ivec3(1, 2, 3), 4);
  235. static_assert(glm::ivec4(1, 2, 3, 4) == A, "GLM: Failed constexpr");
  236. constexpr glm::ivec4 B(glm::ivec2(1, 2), glm::ivec2(3, 4));
  237. static_assert(glm::ivec4(1, 2, 3, 4) == B, "GLM: Failed constexpr");
  238. constexpr glm::ivec4 C(1, glm::ivec3(2, 3, 4));
  239. static_assert(glm::ivec4(1, 2, 3, 4) == C, "GLM: Failed constexpr");
  240. constexpr glm::ivec4 D(glm::ivec1(1), glm::ivec2(2, 3), glm::ivec1(4));
  241. static_assert(glm::ivec4(1, 2, 3, 4) == D, "GLM: Failed constexpr");
  242. constexpr glm::ivec4 E(glm::ivec2(1, 2), glm::ivec1(3), glm::ivec1(4));
  243. static_assert(glm::ivec4(1, 2, 3, 4) == E, "GLM: Failed constexpr");
  244. constexpr glm::ivec4 F(glm::ivec1(1), glm::ivec1(2), glm::ivec2(3, 4));
  245. static_assert(glm::ivec4(1, 2, 3, 4) == F, "GLM: Failed constexpr");
  246. }
  247. {
  248. constexpr glm::ivec4 A(1);
  249. static_assert(A[0] == 1, "GLM: Failed constexpr");
  250. static_assert(glm::ivec4(1).x > 0, "GLM: Failed constexpr");
  251. static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).x > 0, "GLM: Failed constexpr");
  252. static_assert(glm::ivec4(1.0f, -1.0f, -1.0f, 1.0f).y < 0, "GLM: Failed constexpr");
  253. static_assert(glm::ivec4::length() == 4, "GLM: Failed constexpr");
  254. }
  255. {
  256. constexpr glm::bvec4 A1(true);
  257. constexpr glm::bvec4 A2(true);
  258. constexpr glm::bvec4 B1(false);
  259. constexpr glm::bvec4 B2(false);
  260. static_assert(A1 == A2 && B1 == B2, "GLM: Failed constexpr");
  261. static_assert(A1 == A2 || B1 == B2, "GLM: Failed constexpr");
  262. }
  263. return Error;
  264. }
  265. static int test_quat()
  266. {
  267. int Error = 0;
  268. {
  269. static_assert(glm::quat::length() == 4, "GLM: Failed constexpr");
  270. static_assert(glm::quat(1.0f, glm::vec3(0.0f)).w > 0.0f, "GLM: Failed constexpr");
  271. static_assert(glm::quat(1.0f, 0.0f, 0.0f, 0.0f).w > 0.0f, "GLM: Failed constexpr");
  272. glm::quat constexpr Q = glm::identity<glm::quat>();
  273. static_assert(Q.x - glm::quat(1.0f, glm::vec3(0.0f)).x <= glm::epsilon<float>(), "GLM: Failed constexpr");
  274. static_assert(Q[0] == 0, "GLM: Failed constexpr");
  275. }
  276. return Error;
  277. }
  278. static int test_mat2x2()
  279. {
  280. int Error = 0;
  281. static_assert(glm::mat2x2::length() == 2, "GLM: Failed constexpr");
  282. return Error;
  283. }
  284. #endif//GLM_USE_CONSTEXP == GLM_ENABLE
  285. int main()
  286. {
  287. int Error = 0;
  288. # if GLM_USE_CONSTEXP == GLM_ENABLE
  289. Error += test_vec1();
  290. Error += test_vec2();
  291. Error += test_vec3();
  292. Error += test_vec4();
  293. Error += test_quat();
  294. Error += test_mat2x2();
  295. # endif//GLM_USE_CONSTEXP == GLM_ENABLE
  296. return Error;
  297. }