RandomTests.cpp 2.8 KB

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