text
stringlengths 0
2.2M
|
---|
//..
|
// Finally, we observe that we obtain different numbers:
|
//..
|
ASSERT(unseededR1 != rand());
|
ASSERT(unseededR2 != rand());
|
//..
|
} break;
|
case 2: {
|
// --------------------------------------------------------------------
|
// 'int getRandomBytesNonBlocking(buf, numB)' TEST
|
//
|
// Concerns:
|
//: 1 If a number is passed, that many bytes are set.
|
//: 2 The random bytes are distributed uniformly (probabilistic).
|
//
|
// Plan:
|
//: 1 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 getRandomBytesNonBlocking(buf, numB);
|
// --------------------------------------------------------------------
|
const int NUM_ITERATIONS = 25;
|
const int NUM_TRIALS = 10;
|
int cnt = 0;
|
unsigned char buffer[NUM_ITERATIONS * 4] = { };
|
unsigned char or_buffer[NUM_ITERATIONS * 4] = { };
|
unsigned char and_buffer[NUM_ITERATIONS * 4] = { };
|
const unsigned NUM_BYTES = sizeof buffer;
|
if (verbose)
|
cout << endl
|
<< "'int getRandomBytesNonBlocking(buf, numB)' TEST" << endl
|
<< "===============================================" << endl;
|
// 1) If a number is passed, that many bytes are set.
|
if (veryVerbose) {
|
cout << "\nTesting the number of bytes set." << endl;
|
}
|
for (unsigned i = 0; i < 5; ++i) {
|
memset(buffer, 0, NUM_BYTES);
|
memset(or_buffer, 0, NUM_BYTES);
|
memset(and_buffer, 0xf, NUM_BYTES);
|
// Repeat the accession of random bytes 'NUM_TRIALS' times to
|
// prevent false negatives
|
for (int j = 0; j < NUM_TRIALS; ++j) {
|
if (veryVerbose) { P(j) }
|
ASSERT(0 == Util::getRandomBytesNonBlocking(buffer, i));
|
// '|' and '&' the bytes
|
for (unsigned k = 0; k < i; ++k) {
|
or_buffer[k] |= buffer[k];
|
and_buffer[k] &= buffer[k];
|
}
|
}
|
// Check that the bytes set are non-zero and non-0xf. Since
|
// 'NUM_TRIALS > 4', the is less than a 2^(-32) chance of these
|
// tests failing due to randomness. They are much more likely to
|
// fail on '1 == i' than later.
|
if (0 < i) {
|
int or_sum = 0, and_sum = 0;
|
for (unsigned j = 0; j < i; ++j) {
|
or_sum += or_buffer[j];
|
and_sum += ~and_buffer[j];
|
}
|
LOOP2_ASSERT(i, or_sum, 0 != or_sum);
|
LOOP2_ASSERT(i, and_sum, 0 != and_sum);
|
}
|
// check that remaining bytes are still unset.
|
for (unsigned j = i; j < NUM_BYTES; ++j) {
|
LOOP3_ASSERT(i, j, int(buffer[j]), 0 == buffer[j]);
|
}
|
}
|
if (veryVerbose) {
|
cout << "\nTesting the distribution of rand." << endl;
|
}
|
// 3) The random bytes are uniformly distributed (probabilistic)
|
int numbers[NUM_ITERATIONS] = { };
|
for (int i = 0; i< NUM_ITERATIONS; ++i) {
|
int rand;
|
if (veryVerbose) { P(i) }
|
ASSERT(0 == Util::getRandomBytesNonBlocking(
|
reinterpret_cast<unsigned char *>(&rand),
|
sizeof rand));
|
numbers[i] = rand;
|
if (veryVerbose) { P_(i) P(rand) }
|
for (int j = 0; j < i; ++j) {
|
ASSERT(numbers[j] != rand);
|
if (veryVerbose) { P_(j) P(numbers[j]) }
|
}
|
for (int b = 0; b < 15; ++b) {
|
cnt += rand & 1;
|
rand >>= 1;
|
if (veryVerbose) { P(cnt) }
|
}
|
}
|
double expected = (NUM_ITERATIONS * 15) / 2;
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.