Replace more rand() % NUM by randranges

This commit is contained in:
Pieter Wuille 2017-06-07 11:34:55 -07:00
parent efee1db21a
commit 3ecabae363
7 changed files with 19 additions and 19 deletions

View File

@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) {
// Do a random modification. // Do a random modification.
{ {
uint256 txid = txids[insecure_rand() % txids.size()]; // txid we're going to modify in this iteration. uint256 txid = txids[insecure_randrange(txids.size())]; // txid we're going to modify in this iteration.
Coin& coin = result[COutPoint(txid, 0)]; Coin& coin = result[COutPoint(txid, 0)];
const Coin& entry = (insecure_randrange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0)); const Coin& entry = (insecure_randrange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0));
BOOST_CHECK(coin == entry); BOOST_CHECK(coin == entry);
@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test)
if (insecure_randrange(100) == 0) { if (insecure_randrange(100) == 0) {
// Every 100 iterations, flush an intermediate cache // Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && insecure_randrange(2) == 0) { if (stack.size() > 1 && insecure_randrange(2) == 0) {
unsigned int flushIndex = insecure_rand() % (stack.size() - 1); unsigned int flushIndex = insecure_randrange(stack.size() - 1);
stack[flushIndex]->Flush(); stack[flushIndex]->Flush();
} }
} }
@ -434,7 +434,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
if (insecure_randrange(100) == 0) { if (insecure_randrange(100) == 0) {
// Every 100 iterations, flush an intermediate cache // Every 100 iterations, flush an intermediate cache
if (stack.size() > 1 && insecure_randrange(2) == 0) { if (stack.size() > 1 && insecure_randrange(2) == 0) {
unsigned int flushIndex = insecure_rand() % (stack.size() - 1); unsigned int flushIndex = insecure_randrange(stack.size() - 1);
stack[flushIndex]->Flush(); stack[flushIndex]->Flush();
} }
} }

View File

@ -38,7 +38,7 @@ void TestVector(const Hasher &h, const In &in, const Out &out) {
Hasher hasher(h); Hasher hasher(h);
size_t pos = 0; size_t pos = 0;
while (pos < in.size()) { while (pos < in.size()) {
size_t len = insecure_rand() % ((in.size() - pos + 1) / 2 + 1); size_t len = insecure_randrange((in.size() - pos + 1) / 2 + 1);
hasher.Write((unsigned char*)&in[pos], len); hasher.Write((unsigned char*)&in[pos], len);
pos += len; pos += len;
if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) { if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) {

View File

@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(merkle_test)
// If ntx <= 16, try all branches. Otherwise, try 16 random ones. // If ntx <= 16, try all branches. Otherwise, try 16 random ones.
int mtx = loop; int mtx = loop;
if (ntx > 16) { if (ntx > 16) {
mtx = insecure_rand() % ntx; mtx = insecure_randrange(ntx);
} }
std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx); std::vector<uint256> newBranch = BlockMerkleBranch(block, mtx);
std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx); std::vector<uint256> oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx);

View File

@ -21,7 +21,7 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree
public: public:
// flip one bit in one of the hashes - this should break the authentication // flip one bit in one of the hashes - this should break the authentication
void Damage() { void Damage() {
unsigned int n = insecure_rand() % vHash.size(); unsigned int n = insecure_randrange(vHash.size());
int bit = insecure_randrange(256); int bit = insecure_randrange(256);
*(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7); *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7);
} }

View File

@ -200,21 +200,21 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
for (int i = 0; i < 2048; i++) { for (int i = 0; i < 2048; i++) {
int r = insecure_rand(); int r = insecure_rand();
if ((r % 4) == 0) { if ((r % 4) == 0) {
test.insert(insecure_rand() % (test.size() + 1), insecure_rand()); test.insert(insecure_randrange(test.size() + 1), insecure_rand());
} }
if (test.size() > 0 && ((r >> 2) % 4) == 1) { if (test.size() > 0 && ((r >> 2) % 4) == 1) {
test.erase(insecure_rand() % test.size()); test.erase(insecure_randrange(test.size()));
} }
if (((r >> 4) % 8) == 2) { if (((r >> 4) % 8) == 2) {
int new_size = std::max<int>(0, std::min<int>(30, test.size() + (insecure_randrange(5)) - 2)); int new_size = std::max<int>(0, std::min<int>(30, test.size() + (insecure_randrange(5)) - 2));
test.resize(new_size); test.resize(new_size);
} }
if (((r >> 7) % 8) == 3) { if (((r >> 7) % 8) == 3) {
test.insert(insecure_rand() % (test.size() + 1), 1 + (insecure_randrange(2)), insecure_rand()); test.insert(insecure_randrange(test.size() + 1), 1 + insecure_randrange(2), insecure_rand());
} }
if (((r >> 10) % 8) == 4) { if (((r >> 10) % 8) == 4) {
int del = std::min<int>(test.size(), 1 + (insecure_randrange(2))); int del = std::min<int>(test.size(), 1 + (insecure_randrange(2)));
int beg = insecure_rand() % (test.size() + 1 - del); int beg = insecure_randrange(test.size() + 1 - del);
test.erase(beg, beg + del); test.erase(beg, beg + del);
} }
if (((r >> 13) % 16) == 5) { if (((r >> 13) % 16) == 5) {
@ -229,11 +229,11 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
for (int k = 0; k < num; k++) { for (int k = 0; k < num; k++) {
values[k] = insecure_rand(); values[k] = insecure_rand();
} }
test.insert_range(insecure_rand() % (test.size() + 1), values, values + num); test.insert_range(insecure_randrange(test.size() + 1), values, values + num);
} }
if (((r >> 26) % 32) == 8) { if (((r >> 26) % 32) == 8) {
int del = std::min<int>(test.size(), 1 + (insecure_randrange(4))); int del = std::min<int>(test.size(), 1 + (insecure_randrange(4)));
int beg = insecure_rand() % (test.size() + 1 - del); int beg = insecure_randrange(test.size() + 1 - del);
test.erase(beg, beg + del); test.erase(beg, beg + del);
} }
r = insecure_rand(); r = insecure_rand();
@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt)
test.shrink_to_fit(); test.shrink_to_fit();
} }
if (test.size() > 0) { if (test.size() > 0) {
test.update(insecure_rand() % test.size(), insecure_rand()); test.update(insecure_randrange(test.size()), insecure_rand());
} }
if (((r >> 11) % 1024) == 11) { if (((r >> 11) % 1024) == 11) {
test.clear(); test.clear();

View File

@ -91,7 +91,7 @@ void static RandomScript(CScript &script) {
script = CScript(); script = CScript();
int ops = (insecure_randrange(10)); int ops = (insecure_randrange(10));
for (int i=0; i<ops; i++) for (int i=0; i<ops; i++)
script << oplist[insecure_rand() % (sizeof(oplist)/sizeof(oplist[0]))]; script << oplist[insecure_randrange(sizeof(oplist)/sizeof(oplist[0]))];
} }
void static RandomTransaction(CMutableTransaction &tx, bool fSingle) { void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {
@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(sighash_test)
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE); RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
CScript scriptCode; CScript scriptCode;
RandomScript(scriptCode); RandomScript(scriptCode);
int nIn = insecure_rand() % txTo.vin.size(); int nIn = insecure_randrange(txTo.vin.size());
uint256 sh, sho; uint256 sh, sho;
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType); sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);

View File

@ -34,8 +34,8 @@ BOOST_AUTO_TEST_CASE(skiplist_test)
} }
for (int i=0; i < 1000; i++) { for (int i=0; i < 1000; i++) {
int from = insecure_rand() % (SKIPLIST_LENGTH - 1); int from = insecure_randrange(SKIPLIST_LENGTH - 1);
int to = insecure_rand() % (from + 1); int to = insecure_randrange(from + 1);
BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]); BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]); BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
} else { } else {
// randomly choose something in the range [MTP, MTP*2] // randomly choose something in the range [MTP, MTP*2]
int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast(); int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
int r = insecure_rand() % medianTimePast; int r = insecure_randrange(medianTimePast);
vBlocksMain[i].nTime = r + medianTimePast; vBlocksMain[i].nTime = r + medianTimePast;
vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax); vBlocksMain[i].nTimeMax = std::max(vBlocksMain[i].nTime, vBlocksMain[i-1].nTimeMax);
} }
@ -134,7 +134,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
// Verify that FindEarliestAtLeast is correct. // Verify that FindEarliestAtLeast is correct.
for (unsigned int i=0; i<10000; ++i) { for (unsigned int i=0; i<10000; ++i) {
// Pick a random element in vBlocksMain. // Pick a random element in vBlocksMain.
int r = insecure_rand() % vBlocksMain.size(); int r = insecure_randrange(vBlocksMain.size());
int64_t test_time = vBlocksMain[r].nTime; int64_t test_time = vBlocksMain[r].nTime;
CBlockIndex *ret = chain.FindEarliestAtLeast(test_time); CBlockIndex *ret = chain.FindEarliestAtLeast(test_time);
BOOST_CHECK(ret->nTimeMax >= test_time); BOOST_CHECK(ret->nTimeMax >= test_time);