text
stringlengths
0
2.2M
ASSERT(cnt < (expected * 1.3));
ASSERT(cnt > (expected * 0.7));
} break;
case 1: {
// --------------------------------------------------------------------
// BREATHING TEST
//
// Concerns:
// Exercise a broad cross-section of the mechanism of reading random
// numbers from the system. Probe that functionality systematically
// and incrementally to discover basic errors in isolation.
//
// Plan:
// Request a large pool of random 'ints' from each of the system's
// random number generators. Verify the uniqueness, and the
// distribution.
//
// Testing:
// BREATHING TEST
// --------------------------------------------------------------------
const int NUM_ITERATIONS = 8;
if (verbose) cout << endl
<< "BREATHING TEST" << endl
<< "==============" << endl;
int rand;
int numbers [NUM_ITERATIONS];
// fill buffer with random bytes
for (int i = 0; i < NUM_ITERATIONS; ++i) {
unsigned char *p = reinterpret_cast<unsigned char *>(&rand);
ASSERT(0 == Util::getRandomBytes(p, sizeof rand));
numbers[i] = rand;
if (veryVerbose) { P_(i) P(rand) }
}
// verify uniqueness
for (int i = 0; i < NUM_ITERATIONS; ++i) {
for (int j = i + 1; j < NUM_ITERATIONS; ++j) {
LOOP2_ASSERT(i, j, numbers[i] != numbers[j]);
}
}
} break;
case -1: {
// --------------------------------------------------------------------
// 'int getRandomBytes(void *buf, int numBytes) TEST'
//
// Concerns:
// 1) If a number is passed, that many bytes are set.
// 2) The random bytes are distributed uniform (probabilistic)
//
// Plan:
// Request a large pool and random bytes from non-blocking random
// number generator. Verify that each is unique. Verify that the
// numbers approximate a uniform distribution.
//
// Testing:
// static int getRandomBytes(unsigned char *buf, size_t numB);
// --------------------------------------------------------------------
const int NUM_ITERATIONS = 25;
int cnt = 0;
int numbers[NUM_ITERATIONS] = { };
const unsigned NUM_BYTES = sizeof numbers;
if (verbose)
cout << endl
<< "'int getRandomBytes(void *buf, int numBytes) TEST'"
<< endl
<< "=================================================="
<< endl;
if (veryVerbose) {
cout << "\nTesting the number of bytes set." << endl;
}
// 2) If a number is passed, that many bytes are set.
for (unsigned i = 0; i < 5; ++i) {
unsigned j;
unsigned char *p = reinterpret_cast<unsigned char *>(numbers);
memset(numbers, 0, NUM_BYTES);
if (veryVerbose) { P(i) }
ASSERT(0 == Util::getRandomBytesNonBlocking(p, i));
for (j = 0; j < i; ++j) {
LOOP3_ASSERT(i, j, p[j], 0 != p[j]);
}
for (; j < NUM_BYTES; ++j) {
LOOP3_ASSERT(i, j, p[j], 0 == p[j]);
}
}
if (veryVerbose) {
cout << "\nTesting the distribution of rand." << endl;
}
// 3) The random bytes are distributed uniform (probabilistic)
for (int i = 0; i < NUM_ITERATIONS; ++i) {
int rand_int = 0;
unsigned char *p1 = reinterpret_cast<unsigned char *>(&rand_int);
if (veryVerbose) { P(i); }
ASSERT(0 == Util::getRandomBytesNonBlocking(p1, sizeof rand_int));
numbers[i] = rand_int;
for (int j = 0; j < i; ++j) {