#include "../Tests.h" #include "core/Random.h" static void testAverage(bool light) { int limit = light ? 100000 : 1000000; CoreRandom r; coreInitRandom(&r, 553); float sum = 0; for(int i = 0; i < limit; i++) { u32 u = coreRandomU32(&r, 2, 11); sum += (float)u; } sum /= (float)limit; CORE_TEST_FLOAT(6.0f, sum, 0.01f); } static void testCoin(bool light) { int limit = light ? 100000 : 1000000; CoreRandom r; coreInitRandom(&r, 5533); int c[2] = {0, 0}; for(int i = 0; i < limit; i++) { c[coreRandomBool(&r)]++; } CORE_TEST_FLOAT(0.0f, (float)(c[0] - c[1]) / (float)limit, 0.003f); } static void testDistribution(bool light) { size_t limit = light ? 100000 : 1000000; CoreRandom r; coreInitRandom(&r, 553); int c[102] = {0}; for(size_t i = 0; i < limit; i++) { c[coreRandomSize(&r, 1, 101)]++; } CORE_TEST_INT(0, c[0]); CORE_TEST_INT(0, c[101]); for(size_t i = 1; i < 101; i++) { CORE_TEST_FLOAT(0.01f, (float)c[i] / (float)limit, 0.001f); } } static void testFloatAverage(bool light) { int limit = light ? 100000 : 1000000; CoreRandom r; coreInitRandom(&r, 553); float sum = 0; for(int i = 0; i < limit; i++) { sum += coreRandomFloat(&r); } sum /= (float)limit; CORE_TEST_FLOAT(0.5f, sum, 0.001f); } static void testFloatCoin(bool light) { int limit = light ? 100000 : 1000000; CoreRandom r; coreInitRandom(&r, 5534); int c[2] = {0, 0}; for(int i = 0; i < limit; i++) { c[coreRandomFloat(&r) < 0.5f]++; } CORE_TEST_FLOAT(0.0f, (float)(c[0] - c[1]) / (float)limit, 0.003f); } static void testFloatDistribution(bool light) { int limit = light ? 100000 : 1000000; CoreRandom r; coreInitRandom(&r, 553); int c[102] = {0}; for(int i = 0; i < limit; i++) { c[(size_t)(1.0f + coreRandomFloat(&r) * 100.0f)]++; } CORE_TEST_INT(0, c[0]); CORE_TEST_INT(0, c[101]); for(size_t i = 1; i < 101; i++) { CORE_TEST_FLOAT(0.01f, (float)c[i] / (float)limit, 0.003f); } } static void testRandomI32() { CoreRandom r; coreInitRandom(&r, 56346); int c[7] = {0}; for(int i = 0; i < 10000; i++) { i32 index = coreRandomI32(&r, -2, 3) + 3; if(CORE_TEST_TRUE(index >= 0 && index < 7)) { c[index]++; } } for(size_t i = 0; i < 7; i++) { CORE_TEST_BOOL(i != 0 && i != 6, c[i] > 0); } } void coreTestRandom(bool light) { testAverage(light); testCoin(light); testDistribution(light); testFloatAverage(light); testFloatCoin(light); testFloatDistribution(light); testRandomI32(); }