浏览代码

test: more complete PRNG unit tests.

legacy
Sam Hocevar sam 11 年前
父节点
当前提交
affdedd835
共有 1 个文件被更改,包括 64 次插入3 次删除
  1. +64
    -3
      test/unit/rand.cpp

+ 64
- 3
test/unit/rand.cpp 查看文件

@@ -33,7 +33,10 @@ LOLUNIT_FIXTURE(RandTest)

for (int i = 0; i < rolls; ++i)
{
uint32_t r = rand<int>();
int32_t r = rand<int32_t>();

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<int16_t>();

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<int8_t>();

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);
}
}


正在加载...
取消
保存