diff --git a/test/unit/rand.cpp b/test/unit/rand.cpp index ccc4b7fc..d7508846 100644 --- a/test/unit/rand.cpp +++ b/test/unit/rand.cpp @@ -33,7 +33,10 @@ LOLUNIT_FIXTURE(RandTest) for (int i = 0; i < rolls; ++i) { - uint32_t r = rand(); + int32_t r = rand(); + + LOLUNIT_ASSERT_GEQUAL(r, 0); + for (int k = 0; k < 31; k++) { bits[k] += r & 1; @@ -44,8 +47,66 @@ LOLUNIT_FIXTURE(RandTest) for (int k = 0; k < 31; k++) { LOLUNIT_SET_CONTEXT(k); - LOLUNIT_ASSERT_GREATER(bits[k], rolls / 3); - LOLUNIT_ASSERT_LESS(bits[k], rolls * 2 / 3); + LOLUNIT_ASSERT_GEQUAL(bits[k], rolls / 3); + LOLUNIT_ASSERT_LEQUAL(bits[k], rolls * 2 / 3); + LOLUNIT_UNSET_CONTEXT(k); + } + } + + LOLUNIT_TEST(Int16Bits) + { + int const rolls = 2000; + + int bits[16]; + memset(bits, 0, sizeof(bits)); + + for (int i = 0; i < rolls; ++i) + { + int16_t r = rand(); + + LOLUNIT_ASSERT_GEQUAL(r, 0); + + for (int k = 0; k < 15; k++) + { + bits[k] += r & 1; + r >>= 1; + } + } + + for (int k = 0; k < 15; k++) + { + LOLUNIT_SET_CONTEXT(k); + LOLUNIT_ASSERT_GEQUAL(bits[k], rolls / 3); + LOLUNIT_ASSERT_LEQUAL(bits[k], rolls * 2 / 3); + LOLUNIT_UNSET_CONTEXT(k); + } + } + + LOLUNIT_TEST(Int8Bits) + { + int const rolls = 2000; + + int bits[8]; + memset(bits, 0, sizeof(bits)); + + for (int i = 0; i < rolls; ++i) + { + int8_t r = rand(); + + LOLUNIT_ASSERT_GEQUAL(r, 0); + + for (int k = 0; k < 7; k++) + { + bits[k] += r & 1; + r >>= 1; + } + } + + for (int k = 0; k < 7; k++) + { + LOLUNIT_SET_CONTEXT(k); + LOLUNIT_ASSERT_GEQUAL(bits[k], rolls / 3); + LOLUNIT_ASSERT_LEQUAL(bits[k], rolls * 2 / 3); LOLUNIT_UNSET_CONTEXT(k); } }