RandomTests.c 2.5 KB

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