Commit 6546dece authored by jan.koester's avatar jan.koester
Browse files

test

parent 08d8f4bb
Loading
Loading
Loading
Loading
+35 −0
Changes for src/test/e2e/random_test.cpp: 35 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -10,6 +10,7 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>

#define CHECK(cond, msg) \
    do { if (!(cond)) { printf("FAIL: %s (errno=%d)\n", msg, errno); return 1; } } while (0)
@@ -64,6 +65,40 @@ int main() {
        CHECK(r >= -1.0 && r < 1.0, "uniform_real_distribution(-1,1) stays in range");
    }

    /* Bounded distribution on a full-width engine (mt19937_64): gen_span+1
     * wrapped to 0 and every draw was rejected -- an endless loop, hit by
     * proxyplus' random load balancing. alarm() turns a regression into a
     * failure instead of a hang. Each of 3 buckets must get roughly 1/3. */
    alarm(20);
    std::mt19937_64 mt64b(7);
    std::uniform_int_distribution<size_t> three(0, 2);
    int counts[3] = {0, 0, 0};
    for (int i = 0; i < 30000; i++) {
        size_t r = three(mt64b);
        CHECK(r <= 2, "uniform_int_distribution<size_t>(0,2) on mt19937_64 stays in range");
        counts[r]++;
    }
    for (int i = 0; i < 3; i++)
        CHECK(counts[i] > 9000 && counts[i] < 11000, "mt19937_64 bounded draws are roughly uniform");

    /* Range wider than the engine (mt19937 for 2^40 values): also looped. */
    std::mt19937 mt4(11);
    std::uniform_int_distribution<unsigned long long> wide(0, (1ULL << 40) - 1);
    bool high = false;
    for (int i = 0; i < 200; i++) {
        unsigned long long r = wide(mt4);
        CHECK(r < (1ULL << 40), "40-bit range on a 32-bit engine stays in range");
        if (r >= (1ULL << 39)) high = true;
    }
    CHECK(high, "40-bit range on a 32-bit engine reaches the upper half (more than 32 bits used)");

    /* Engine whose span is not 2^k-1 (minstd_rand: 1..2^31-2), wide range. */
    std::minstd_rand ms(5);
    std::uniform_int_distribution<unsigned long long> wide2(0, (1ULL << 50) - 1);
    for (int i = 0; i < 200; i++)
        CHECK(wide2(ms) < (1ULL << 50), "50-bit range on minstd_rand stays in range");
    alarm(0);

    puts("random: all checks passed");
    return 0;
}