Platformer in OpenGL
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
9.6 KiB

5 years ago
  1. #define GLM_FORCE_DEFAULT_ALIGNED_GENTYPES
  2. #include <glm/gtc/random.hpp>
  3. #include <glm/gtc/epsilon.hpp>
  4. #include <glm/gtc/type_precision.hpp>
  5. #if GLM_LANG & GLM_LANG_CXX0X_FLAG
  6. # include <array>
  7. #endif
  8. std::size_t const TestSamples = 10000;
  9. int test_linearRand()
  10. {
  11. int Error = 0;
  12. glm::int32 const Min = 16;
  13. glm::int32 const Max = 32;
  14. {
  15. glm::u8vec2 AMin(std::numeric_limits<glm::u8>::max());
  16. glm::u8vec2 AMax(std::numeric_limits<glm::u8>::min());
  17. {
  18. for(std::size_t i = 0; i < TestSamples; ++i)
  19. {
  20. glm::u8vec2 A = glm::linearRand(glm::u8vec2(Min), glm::u8vec2(Max));
  21. AMin = glm::min(AMin, A);
  22. AMax = glm::max(AMax, A);
  23. if(!glm::all(glm::lessThanEqual(A, glm::u8vec2(Max))))
  24. ++Error;
  25. if(!glm::all(glm::greaterThanEqual(A, glm::u8vec2(Min))))
  26. ++Error;
  27. assert(!Error);
  28. }
  29. Error += glm::all(glm::equal(AMin, glm::u8vec2(Min))) ? 0 : 1;
  30. Error += glm::all(glm::equal(AMax, glm::u8vec2(Max))) ? 0 : 1;
  31. assert(!Error);
  32. }
  33. glm::u16vec2 BMin(std::numeric_limits<glm::u16>::max());
  34. glm::u16vec2 BMax(std::numeric_limits<glm::u16>::min());
  35. {
  36. for(std::size_t i = 0; i < TestSamples; ++i)
  37. {
  38. glm::u16vec2 B = glm::linearRand(glm::u16vec2(Min), glm::u16vec2(Max));
  39. BMin = glm::min(BMin, B);
  40. BMax = glm::max(BMax, B);
  41. if(!glm::all(glm::lessThanEqual(B, glm::u16vec2(Max))))
  42. ++Error;
  43. if(!glm::all(glm::greaterThanEqual(B, glm::u16vec2(Min))))
  44. ++Error;
  45. assert(!Error);
  46. }
  47. Error += glm::all(glm::equal(BMin, glm::u16vec2(Min))) ? 0 : 1;
  48. Error += glm::all(glm::equal(BMax, glm::u16vec2(Max))) ? 0 : 1;
  49. assert(!Error);
  50. }
  51. glm::u32vec2 CMin(std::numeric_limits<glm::u32>::max());
  52. glm::u32vec2 CMax(std::numeric_limits<glm::u32>::min());
  53. {
  54. for(std::size_t i = 0; i < TestSamples; ++i)
  55. {
  56. glm::u32vec2 C = glm::linearRand(glm::u32vec2(Min), glm::u32vec2(Max));
  57. CMin = glm::min(CMin, C);
  58. CMax = glm::max(CMax, C);
  59. if(!glm::all(glm::lessThanEqual(C, glm::u32vec2(Max))))
  60. ++Error;
  61. if(!glm::all(glm::greaterThanEqual(C, glm::u32vec2(Min))))
  62. ++Error;
  63. assert(!Error);
  64. }
  65. Error += glm::all(glm::equal(CMin, glm::u32vec2(Min))) ? 0 : 1;
  66. Error += glm::all(glm::equal(CMax, glm::u32vec2(Max))) ? 0 : 1;
  67. assert(!Error);
  68. }
  69. glm::u64vec2 DMin(std::numeric_limits<glm::u64>::max());
  70. glm::u64vec2 DMax(std::numeric_limits<glm::u64>::min());
  71. {
  72. for(std::size_t i = 0; i < TestSamples; ++i)
  73. {
  74. glm::u64vec2 D = glm::linearRand(glm::u64vec2(Min), glm::u64vec2(Max));
  75. DMin = glm::min(DMin, D);
  76. DMax = glm::max(DMax, D);
  77. if(!glm::all(glm::lessThanEqual(D, glm::u64vec2(Max))))
  78. ++Error;
  79. if(!glm::all(glm::greaterThanEqual(D, glm::u64vec2(Min))))
  80. ++Error;
  81. assert(!Error);
  82. }
  83. Error += glm::all(glm::equal(DMin, glm::u64vec2(Min))) ? 0 : 1;
  84. Error += glm::all(glm::equal(DMax, glm::u64vec2(Max))) ? 0 : 1;
  85. assert(!Error);
  86. }
  87. }
  88. {
  89. glm::i8vec2 AMin(std::numeric_limits<glm::i8>::max());
  90. glm::i8vec2 AMax(std::numeric_limits<glm::i8>::min());
  91. {
  92. for(std::size_t i = 0; i < TestSamples; ++i)
  93. {
  94. glm::i8vec2 A = glm::linearRand(glm::i8vec2(Min), glm::i8vec2(Max));
  95. AMin = glm::min(AMin, A);
  96. AMax = glm::max(AMax, A);
  97. if(!glm::all(glm::lessThanEqual(A, glm::i8vec2(Max))))
  98. ++Error;
  99. if(!glm::all(glm::greaterThanEqual(A, glm::i8vec2(Min))))
  100. ++Error;
  101. assert(!Error);
  102. }
  103. Error += glm::all(glm::equal(AMin, glm::i8vec2(Min))) ? 0 : 1;
  104. Error += glm::all(glm::equal(AMax, glm::i8vec2(Max))) ? 0 : 1;
  105. assert(!Error);
  106. }
  107. glm::i16vec2 BMin(std::numeric_limits<glm::i16>::max());
  108. glm::i16vec2 BMax(std::numeric_limits<glm::i16>::min());
  109. {
  110. for(std::size_t i = 0; i < TestSamples; ++i)
  111. {
  112. glm::i16vec2 B = glm::linearRand(glm::i16vec2(Min), glm::i16vec2(Max));
  113. BMin = glm::min(BMin, B);
  114. BMax = glm::max(BMax, B);
  115. if(!glm::all(glm::lessThanEqual(B, glm::i16vec2(Max))))
  116. ++Error;
  117. if(!glm::all(glm::greaterThanEqual(B, glm::i16vec2(Min))))
  118. ++Error;
  119. assert(!Error);
  120. }
  121. Error += glm::all(glm::equal(BMin, glm::i16vec2(Min))) ? 0 : 1;
  122. Error += glm::all(glm::equal(BMax, glm::i16vec2(Max))) ? 0 : 1;
  123. assert(!Error);
  124. }
  125. glm::i32vec2 CMin(std::numeric_limits<glm::i32>::max());
  126. glm::i32vec2 CMax(std::numeric_limits<glm::i32>::min());
  127. {
  128. for(std::size_t i = 0; i < TestSamples; ++i)
  129. {
  130. glm::i32vec2 C = glm::linearRand(glm::i32vec2(Min), glm::i32vec2(Max));
  131. CMin = glm::min(CMin, C);
  132. CMax = glm::max(CMax, C);
  133. if(!glm::all(glm::lessThanEqual(C, glm::i32vec2(Max))))
  134. ++Error;
  135. if(!glm::all(glm::greaterThanEqual(C, glm::i32vec2(Min))))
  136. ++Error;
  137. assert(!Error);
  138. }
  139. Error += glm::all(glm::equal(CMin, glm::i32vec2(Min))) ? 0 : 1;
  140. Error += glm::all(glm::equal(CMax, glm::i32vec2(Max))) ? 0 : 1;
  141. assert(!Error);
  142. }
  143. glm::i64vec2 DMin(std::numeric_limits<glm::i64>::max());
  144. glm::i64vec2 DMax(std::numeric_limits<glm::i64>::min());
  145. {
  146. for(std::size_t i = 0; i < TestSamples; ++i)
  147. {
  148. glm::i64vec2 D = glm::linearRand(glm::i64vec2(Min), glm::i64vec2(Max));
  149. DMin = glm::min(DMin, D);
  150. DMax = glm::max(DMax, D);
  151. if(!glm::all(glm::lessThanEqual(D, glm::i64vec2(Max))))
  152. ++Error;
  153. if(!glm::all(glm::greaterThanEqual(D, glm::i64vec2(Min))))
  154. ++Error;
  155. assert(!Error);
  156. }
  157. Error += glm::all(glm::equal(DMin, glm::i64vec2(Min))) ? 0 : 1;
  158. Error += glm::all(glm::equal(DMax, glm::i64vec2(Max))) ? 0 : 1;
  159. assert(!Error);
  160. }
  161. }
  162. for(std::size_t i = 0; i < TestSamples; ++i)
  163. {
  164. glm::f32vec2 const A(glm::linearRand(glm::f32vec2(static_cast<float>(Min)), glm::f32vec2(static_cast<float>(Max))));
  165. if(!glm::all(glm::lessThanEqual(A, glm::f32vec2(static_cast<float>(Max)))))
  166. ++Error;
  167. if(!glm::all(glm::greaterThanEqual(A, glm::f32vec2(static_cast<float>(Min)))))
  168. ++Error;
  169. glm::f64vec2 const B(glm::linearRand(glm::f64vec2(Min), glm::f64vec2(Max)));
  170. if(!glm::all(glm::lessThanEqual(B, glm::f64vec2(Max))))
  171. ++Error;
  172. if(!glm::all(glm::greaterThanEqual(B, glm::f64vec2(Min))))
  173. ++Error;
  174. assert(!Error);
  175. }
  176. {
  177. float ResultFloat = 0.0f;
  178. double ResultDouble = 0.0;
  179. for(std::size_t i = 0; i < TestSamples; ++i)
  180. {
  181. ResultFloat += glm::linearRand(-1.0f, 1.0f);
  182. ResultDouble += glm::linearRand(-1.0, 1.0);
  183. }
  184. Error += glm::epsilonEqual(ResultFloat, 0.0f, 0.0001f);
  185. Error += glm::epsilonEqual(ResultDouble, 0.0, 0.0001);
  186. assert(!Error);
  187. }
  188. return Error;
  189. }
  190. int test_circularRand()
  191. {
  192. int Error = 0;
  193. {
  194. std::size_t Max = TestSamples;
  195. float ResultFloat = 0.0f;
  196. double ResultDouble = 0.0;
  197. double Radius = 2.0;
  198. for(std::size_t i = 0; i < Max; ++i)
  199. {
  200. ResultFloat += glm::length(glm::circularRand(1.0f));
  201. ResultDouble += glm::length(glm::circularRand(Radius));
  202. }
  203. Error += glm::epsilonEqual(ResultFloat, float(Max), 0.01f) ? 0 : 1;
  204. Error += glm::epsilonEqual(ResultDouble, double(Max) * double(Radius), 0.01) ? 0 : 1;
  205. assert(!Error);
  206. }
  207. return Error;
  208. }
  209. int test_sphericalRand()
  210. {
  211. int Error = 0;
  212. {
  213. std::size_t Max = TestSamples;
  214. float ResultFloatA = 0.0f;
  215. float ResultFloatB = 0.0f;
  216. float ResultFloatC = 0.0f;
  217. double ResultDoubleA = 0.0;
  218. double ResultDoubleB = 0.0;
  219. double ResultDoubleC = 0.0;
  220. for(std::size_t i = 0; i < Max; ++i)
  221. {
  222. ResultFloatA += glm::length(glm::sphericalRand(1.0f));
  223. ResultDoubleA += glm::length(glm::sphericalRand(1.0));
  224. ResultFloatB += glm::length(glm::sphericalRand(2.0f));
  225. ResultDoubleB += glm::length(glm::sphericalRand(2.0));
  226. ResultFloatC += glm::length(glm::sphericalRand(3.0f));
  227. ResultDoubleC += glm::length(glm::sphericalRand(3.0));
  228. }
  229. Error += glm::epsilonEqual(ResultFloatA, float(Max), 0.01f) ? 0 : 1;
  230. Error += glm::epsilonEqual(ResultDoubleA, double(Max), 0.0001) ? 0 : 1;
  231. Error += glm::epsilonEqual(ResultFloatB, float(Max * 2), 0.01f) ? 0 : 1;
  232. Error += glm::epsilonEqual(ResultDoubleB, double(Max * 2), 0.0001) ? 0 : 1;
  233. Error += glm::epsilonEqual(ResultFloatC, float(Max * 3), 0.01f) ? 0 : 1;
  234. Error += glm::epsilonEqual(ResultDoubleC, double(Max * 3), 0.01) ? 0 : 1;
  235. assert(!Error);
  236. }
  237. return Error;
  238. }
  239. int test_diskRand()
  240. {
  241. int Error = 0;
  242. {
  243. float ResultFloat = 0.0f;
  244. double ResultDouble = 0.0;
  245. for(std::size_t i = 0; i < TestSamples; ++i)
  246. {
  247. ResultFloat += glm::length(glm::diskRand(2.0f));
  248. ResultDouble += glm::length(glm::diskRand(2.0));
  249. }
  250. Error += ResultFloat < float(TestSamples) * 2.f ? 0 : 1;
  251. Error += ResultDouble < double(TestSamples) * 2.0 ? 0 : 1;
  252. assert(!Error);
  253. }
  254. return Error;
  255. }
  256. int test_ballRand()
  257. {
  258. int Error = 0;
  259. {
  260. float ResultFloat = 0.0f;
  261. double ResultDouble = 0.0;
  262. for(std::size_t i = 0; i < TestSamples; ++i)
  263. {
  264. ResultFloat += glm::length(glm::ballRand(2.0f));
  265. ResultDouble += glm::length(glm::ballRand(2.0));
  266. }
  267. Error += ResultFloat < float(TestSamples) * 2.f ? 0 : 1;
  268. Error += ResultDouble < double(TestSamples) * 2.0 ? 0 : 1;
  269. assert(!Error);
  270. }
  271. return Error;
  272. }
  273. /*
  274. #if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
  275. int test_grid()
  276. {
  277. int Error = 0;
  278. typedef std::array<int, 8> colors;
  279. typedef std::array<int, 8 * 8> grid;
  280. grid Grid;
  281. colors Colors;
  282. grid GridBest;
  283. colors ColorsBest;
  284. while(true)
  285. {
  286. for(std::size_t i = 0; i < Grid.size(); ++i)
  287. Grid[i] = int(glm::linearRand(0.0, 8.0 * 8.0 * 8.0 - 1.0) / 64.0);
  288. for(std::size_t i = 0; i < Grid.size(); ++i)
  289. ++Colors[Grid[i]];
  290. bool Exit = true;
  291. for(std::size_t i = 0; i < Colors.size(); ++i)
  292. {
  293. if(Colors[i] == 8)
  294. continue;
  295. Exit = false;
  296. break;
  297. }
  298. if(Exit == true)
  299. break;
  300. }
  301. return Error;
  302. }
  303. #endif
  304. */
  305. int main()
  306. {
  307. int Error = 0;
  308. Error += test_linearRand();
  309. Error += test_circularRand();
  310. Error += test_sphericalRand();
  311. Error += test_diskRand();
  312. Error += test_ballRand();
  313. /*
  314. #if(GLM_LANG & GLM_LANG_CXX0X_FLAG)
  315. Error += test_grid();
  316. #endif
  317. */
  318. return Error;
  319. }