RandomTests.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "../Tests.hpp"
  2. #include "core/data/Array.hpp"
  3. #include "core/utils/Random.hpp"
  4. static void testAverage(bool light) {
  5. int limit = light ? 100000 : 1000000;
  6. Core::Random r(553);
  7. float sum = 0;
  8. for(int i = 0; i < limit; i++) {
  9. sum += static_cast<float>(r.next(2, 10));
  10. }
  11. sum /= static_cast<float>(limit);
  12. CORE_TEST_FLOAT(6.0f, sum, 0.01f);
  13. }
  14. static void testCoin(bool light) {
  15. int limit = light ? 100000 : 1000000;
  16. Core::Random r(5533);
  17. Core::Array<int, 2> c(0);
  18. for(int i = 0; i < limit; i++) {
  19. c[r.nextBool()]++;
  20. }
  21. CORE_TEST_FLOAT(0.0f,
  22. static_cast<float>(c[0] - c[1]) / static_cast<float>(limit),
  23. 0.003f);
  24. }
  25. static void testDistribution(bool light) {
  26. size_t limit = light ? 100000 : 1000000;
  27. Core::Random r(553);
  28. Core::Array<int, 102> c(0);
  29. for(size_t i = 0; i < limit; i++) {
  30. c[r.nextSize(1, c.getLength() - 2)]++;
  31. }
  32. CORE_TEST_EQUAL(0, c[0]);
  33. CORE_TEST_EQUAL(0, c[c.getLength() - 1]);
  34. for(size_t i = 1; i < c.getLength() - 1; i++) {
  35. CORE_TEST_FLOAT(0.01f,
  36. static_cast<float>(c[i]) / static_cast<float>(limit),
  37. 0.001f);
  38. }
  39. }
  40. static void testFloatAverage(bool light) {
  41. int limit = light ? 100000 : 1000000;
  42. Core::Random r(553);
  43. float sum = 0;
  44. for(int i = 0; i < limit; i++) {
  45. sum += r.nextFloat();
  46. }
  47. sum /= static_cast<float>(limit);
  48. CORE_TEST_FLOAT(0.5f, sum, 0.001f);
  49. }
  50. static void testFloatCoin(bool light) {
  51. int limit = light ? 100000 : 1000000;
  52. Core::Random r(5534);
  53. Core::Array<int, 2> c(0);
  54. for(int i = 0; i < limit; i++) {
  55. c[r.nextFloat() < 0.5f]++;
  56. }
  57. CORE_TEST_FLOAT(0.0f,
  58. static_cast<float>(c[0] - c[1]) / static_cast<float>(limit),
  59. 0.003f);
  60. }
  61. static void testFloatDistribution(bool light) {
  62. int limit = light ? 100000 : 1000000;
  63. Core::Random r(553);
  64. Core::Array<int, 102> c(0);
  65. for(int i = 0; i < limit; i++) {
  66. c[static_cast<size_t>(r.nextFloat(1.0f, c.getLength() - 1))]++;
  67. }
  68. CORE_TEST_EQUAL(0, c[0]);
  69. CORE_TEST_EQUAL(0, c[c.getLength() - 1]);
  70. for(size_t i = 1; i < c.getLength() - 1; i++) {
  71. CORE_TEST_FLOAT(0.01f,
  72. static_cast<float>(c[i]) / static_cast<float>(limit),
  73. 0.003f);
  74. }
  75. }
  76. static void testRandomI32() {
  77. Core::Random r(56346);
  78. Core::Array<int, 7> c;
  79. c.fill(0);
  80. for(int i = 0; i < 10'000; i++) {
  81. i32 index = r.nextI32(-2, 2) + 3;
  82. if(CORE_TEST_TRUE(index >= 0)) {
  83. c[static_cast<size_t>(index)]++;
  84. }
  85. }
  86. for(size_t i = 0; i < c.getLength(); i++) {
  87. CORE_TEST_EQUAL(i != 0 && i != c.getLength() - 1, c[i] > 0);
  88. }
  89. }
  90. void Core::testRandom(bool light) {
  91. testAverage(light);
  92. testCoin(light);
  93. testDistribution(light);
  94. testFloatAverage(light);
  95. testFloatCoin(light);
  96. testFloatDistribution(light);
  97. testRandomI32();
  98. }