summaryrefslogtreecommitdiff
path: root/durt
Commit message (Expand)AuthorAge
* State at 2024-11-17autocommitter2024-11-17
* State at 2024-11-03autocommitter2024-11-03
* State at 2024-11-02autocommitter2024-11-02
* State at 2024-11-01autocommitter2024-11-01
* State at 2024-10-31autocommitter2024-10-31
* State at 2024-10-28autocommitter2024-10-28
* State at 2024-10-15autocommitter2024-10-15
* State at 2024-10-15autocommitter2024-10-15
* State at 2023-04-13autocommitter2024-04-22
* State at 2023-03-27autocommitter2024-04-22
* State at 2023-03-26autocommitter2024-04-22
* State at 2022-07-10autocommitter2024-04-22
* State at 2022-07-09autocommitter2024-04-22
* State at 2022-07-05autocommitter2024-04-22
* State at 2022-07-04autocommitter2024-04-22
* State at 2022-06-28autocommitter2024-04-22
* State at 2022-06-27autocommitter2024-04-22
* State at 2022-06-25autocommitter2024-04-22
* State at 2022-06-24autocommitter2024-04-22
* State at 2022-06-22autocommitter2024-04-22
* State at 2022-06-20autocommitter2024-04-22
* State at 2022-06-19autocommitter2024-04-22
* State at 2022-06-15autocommitter2024-04-22
* State at 2022-05-15autocommitter2024-04-22
* State at 2022-05-14autocommitter2024-04-22
* State at 2022-05-10autocommitter2024-04-22
* State at 2022-05-09autocommitter2024-04-22
* State at 2022-04-26autocommitter2024-04-22
* State at 2022-04-25autocommitter2024-04-22
* State at 2022-04-19autocommitter2024-04-22
* State at 2022-04-15autocommitter2024-04-22
* State at 2022-04-14autocommitter2024-04-22
* State at 2022-04-05autocommitter2024-04-22
* State at 2022-04-04autocommitter2024-04-22
* State at 2022-04-03autocommitter2024-04-22
* State at 2022-03-29autocommitter2024-04-22
* State at 2022-03-28autocommitter2024-04-22
* State at 2022-03-20autocommitter2024-04-22
* State at 2022-03-09autocommitter2024-04-22
* State at 2022-03-06autocommitter2024-04-22
* State at 2022-03-04autocommitter2024-04-22
* State at 2022-03-02autocommitter2024-04-22
* State at 2022-02-26autocommitter2024-04-22
* State at 2022-01-13autocommitter2024-04-22
* State at 2022-01-05autocommitter2024-04-22
* State at 2021-12-29autocommitter2024-04-22
* State at 2021-12-21autocommitter2024-04-22
* State at 2021-12-20autocommitter2024-04-22
* State at 2021-12-10autocommitter2024-04-22
* State at 2021-11-27autocommitter2024-04-22
/span> void testPseudoRandomRange(); void testPcgRandom(); void testPcgRandomRange(); void testPcgRandomBytes(); void testPcgRandomNormalDist(); static const int expected_pseudorandom_results[256]; static const u32 expected_pcgrandom_results[256]; static const u8 expected_pcgrandom_bytes_result[24]; static const u8 expected_pcgrandom_bytes_result2[24]; }; static TestRandom g_test_instance; void TestRandom::runTests(IGameDef *gamedef) { TEST(testPseudoRandom); TEST(testPseudoRandomRange); TEST(testPcgRandom); TEST(testPcgRandomRange); TEST(testPcgRandomBytes); TEST(testPcgRandomNormalDist); } //////////////////////////////////////////////////////////////////////////////// void TestRandom::testPseudoRandom() { PseudoRandom pr(814538); for (u32 i = 0; i != 256; i++) UASSERTEQ(int, pr.next(), expected_pseudorandom_results[i]); } void TestRandom::testPseudoRandomRange() { PseudoRandom pr((int)time(NULL)); EXCEPTION_CHECK(PrngException, pr.range(2000, 6000)); EXCEPTION_CHECK(PrngException, pr.range(5, 1)); for (u32 i = 0; i != 32768; i++) { int min = (pr.next() % 3000) - 500; int max = (pr.next() % 3000) - 500; if (min > max) SWAP(int, min, max); int randval = pr.range(min, max); UASSERT(randval >= min); UASSERT(randval <= max); } } void TestRandom::testPcgRandom() { PcgRandom pr(814538, 998877); for (u32 i = 0; i != 256; i++) UASSERTEQ(u32, pr.next(), expected_pcgrandom_results[i]); } void TestRandom::testPcgRandomRange() { PcgRandom pr((int)time(NULL)); EXCEPTION_CHECK(PrngException, pr.range(5, 1)); // Regression test for bug 3027 pr.range(pr.RANDOM_MIN, pr.RANDOM_MAX); for (u32 i = 0; i != 32768; i++) { int min = (pr.next() % 3000) - 500; int max = (pr.next() % 3000) - 500; if (min > max) SWAP(int, min, max); int randval = pr.range(min, max); UASSERT(randval >= min); UASSERT(randval <= max); } } void TestRandom::testPcgRandomBytes() { char buf[32]; PcgRandom r(1538, 877); memset(buf, 0, sizeof(buf)); r.bytes(buf + 5, 23); UASSERT(memcmp(buf + 5, expected_pcgrandom_bytes_result, sizeof(expected_pcgrandom_bytes_result)) == 0); memset(buf, 0, sizeof(buf)); r.bytes(buf, 17); UASSERT(memcmp(buf, expected_pcgrandom_bytes_result2, sizeof(expected_pcgrandom_bytes_result2)) == 0); } void TestRandom::testPcgRandomNormalDist() { static const int max = 120; static const int min = -120; static const int num_trials = 20; static const u32 num_samples = 61000; s32 bins[max - min + 1]; memset(bins, 0, sizeof(bins)); PcgRandom r(486179 + (int)time(NULL)); for (u32 i = 0; i != num_samples; i++) { s32 randval = r.randNormalDist(min, max, num_trials); UASSERT(randval <= max); UASSERT(randval >= min); bins[randval - min]++; } // Note that here we divide variance by the number of trials; // this is because variance is a biased estimator. int range = (max - min + 1); float mean = (max + min) / 2; float variance = ((range * range - 1) / 12) / num_trials; float stddev = std::sqrt(variance); static const float prediction_intervals[] = { 0.68269f, // 1.0 0.86639f, // 1.5 0.95450f, // 2.0 0.98758f, // 2.5 0.99730f, // 3.0 }; //// Simple normality test using the 68-95-99.7% rule for (u32 i = 0; i != ARRLEN(prediction_intervals); i++) { float deviations = i / 2.f + 1.f; int lbound = myround(mean - deviations * stddev); int ubound = myround(mean + deviations * stddev); UASSERT(lbound >= min); UASSERT(ubound <= max); int accum = 0; for (int j = lbound; j != ubound; j++) accum += bins[j - min]; float actual = (float)accum / num_samples; UASSERT(std::fabs(actual - prediction_intervals[i]) < 0.02f); } } const int TestRandom::expected_pseudorandom_results[256] = { 0x02fa, 0x60d5, 0x6c10, 0x606b, 0x098b, 0x5f1e, 0x4f56, 0x3fbd, 0x77af, 0x4fe9, 0x419a, 0x6fe1, 0x177b, 0x6858, 0x36f8, 0x6d83, 0x14fc, 0x2d62, 0x1077, 0x23e2, 0x041b, 0x7a7e, 0x5b52, 0x215d, 0x682b, 0x4716, 0x47e3, 0x08c0, 0x1952, 0x56ae, 0x146d, 0x4b4f, 0x239f, 0x3fd0, 0x6794, 0x7796, 0x7be2, 0x75b7, 0x5691, 0x28ee, 0x2656, 0x40c0, 0x133c, 0x63cd, 0x2aeb, 0x518f, 0x7dbc, 0x6ad8, 0x736e, 0x5b05, 0x160b, 0x589f, 0x6f64, 0x5edc, 0x092c, 0x0a39, 0x199e, 0x1927, 0x562b, 0x2689, 0x3ba3, 0x366f, 0x46da, 0x4e49, 0x0abb, 0x40a1, 0x3846, 0x40db, 0x7adb, 0x6ec1, 0x6efa, 0x01cc, 0x6335, 0x4352, 0x72fb, 0x4b2d, 0x509a, 0x257e, 0x2f7d, 0x5891, 0x2195, 0x6107, 0x5269, 0x56e3, 0x4849, 0x38f7, 0x2791, 0x04f2, 0x4e05, 0x78ff, 0x6bae, 0x50b3, 0x74ad, 0x31af, 0x531e, 0x7d56, 0x11c9, 0x0b5e, 0x405e, 0x1e15, 0x7f6a, 0x5bd3, 0x6649, 0x71b4, 0x3ec2, 0x6ab4, 0x520e, 0x6ad6, 0x287e, 0x10b8, 0x18f2, 0x7107, 0x46ea, 0x1d85, 0x25cc, 0x2689, 0x35c1, 0x3065, 0x6237, 0x3edd, 0x23d9, 0x6fb5, 0x37a1, 0x3211, 0x526a, 0x4b09, 0x23f1, 0x58cc, 0x2e42, 0x341f, 0x5e16, 0x3d1a, 0x5e8c, 0x7a82, 0x4635, 0x2bf8, 0x6577, 0x3603, 0x1daf, 0x539f, 0x2e91, 0x6bd8, 0x42d3, 0x7a93, 0x26e3, 0x5a91, 0x6c67, 0x1b66, 0x3ac7, 0x18bf, 0x20d8, 0x7153, 0x558d, 0x7262, 0x653d, 0x417d, 0x3ed3, 0x3117, 0x600d, 0x6d04, 0x719c, 0x3afd, 0x6ba5, 0x17c5, 0x4935, 0x346c, 0x5479, 0x6ff6, 0x1fcc, 0x1054, 0x3f14, 0x6266, 0x3acc, 0x3b77, 0x71d8, 0x478b, 0x20fa, 0x4e46, 0x7e77, 0x5554, 0x3652, 0x719c, 0x072b, 0x61ad, 0x399f, 0x621d, 0x1bba, 0x41d0, 0x7fdc, 0x3e6c, 0x6a2a, 0x5253, 0x094e, 0x0c10, 0x3f43, 0x73eb, 0x4c5f, 0x1f23, 0x12c9, 0x0902, 0x5238, 0x50c0, 0x1b77, 0x3ffd, 0x0124, 0x302a, 0x26b9, 0x3648, 0x30a6, 0x1abc, 0x3031, 0x4029, 0x6358, 0x6696, 0x74e8, 0x6142, 0x4284, 0x0c00, 0x7e50, 0x41e3, 0x3782, 0x79a5, 0x60fe, 0x2d15, 0x3ed2, 0x7f70, 0x2b27, 0x6366, 0x5100, 0x7c44, 0x3ee0, 0x4e76, 0x7d34, 0x3a60,