]> git.localhorst.tv Git - blank.git/commitdiff
test random ints of various widths
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 10 Nov 2016 11:38:02 +0000 (12:38 +0100)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 10 Nov 2016 11:38:02 +0000 (12:38 +0100)
src/rand/GaloisLFSR.hpp
tst/rand/StabilityTest.cpp

index f5de7ad0bb650f15d9d6870d82e3bd6f3ee97b70..81652d85f54a64cd628c22a95d3493b6de6476cd 100644 (file)
@@ -42,6 +42,11 @@ public:
                return out = static_cast<T>(state);
        }
 
+       /// special case for randrom(boolean), since static_cast<bool>(0b10) == true
+       bool operator ()(bool &out) noexcept {
+               return out = operator ()();
+       }
+
        template<class T>
        T Next() noexcept {
                T next;
index 928cdd6a464f428bda83d503fd3c7edf345ed09f..579433752bfc779f03512248e02deb15c5bd8062 100644 (file)
@@ -118,9 +118,9 @@ void StabilityTest::testRNG() {
                "unexpected value #18 from RNG",
                uint16_t(0xB000), value
        );
-       random(value);
+       value = random.Next<uint16_t>();
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "unexpected value #19 from RNG",
+               "unexpected value #19 from RNG (using Next())",
                uint16_t(0x0B0B), value
        );
        random(value);
@@ -153,9 +153,9 @@ void StabilityTest::testRNG() {
                "unexpected value #25 from RNG",
                uint16_t(0x0000), value
        );
-       random(value);
+       value = random.Next<uint16_t>();
        CPPUNIT_ASSERT_EQUAL_MESSAGE(
-               "unexpected value #26 from RNG",
+               "unexpected value #26 from RNG (using Next())",
                uint16_t(0xC970), value
        );
        random(value);
@@ -198,6 +198,36 @@ void StabilityTest::testRNG() {
                "RNG with seeds 0 and 1 differ",
                value, value1
        );
+
+       GaloisLFSR random_bool(0);
+       bool value_bool;
+       for (int i = 0; i < (16 * 32); ++i) {
+               random_bool(value_bool);
+       }
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "unexpected result for bool",
+               false, value_bool
+       );
+
+       GaloisLFSR random8(0);
+       uint8_t value8;
+       for (int i = 0; i < 31; ++i) {
+               random8(value8);
+       }
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "unexpected result for uint8",
+               uint8_t(0x10), value8
+       );
+
+       GaloisLFSR random32(0);
+       uint32_t value32;
+       for (int i = 0; i < 16; ++i) {
+               random32(value32);
+       }
+       CPPUNIT_ASSERT_EQUAL_MESSAGE(
+               "unexpected result for uint32",
+               uint32_t(0xB0000000), value32
+       );
 }
 
 void StabilityTest::testSimplex() {