From 3ecabae36364e905e7821fba3e60aa7f8418de6c Mon Sep 17 00:00:00 2001 From: Pieter Wuille Date: Wed, 7 Jun 2017 11:34:55 -0700 Subject: Replace more rand() % NUM by randranges --- src/test/coins_tests.cpp | 6 +++--- src/test/crypto_tests.cpp | 2 +- src/test/merkle_tests.cpp | 2 +- src/test/pmt_tests.cpp | 2 +- src/test/prevector_tests.cpp | 14 +++++++------- src/test/sighash_tests.cpp | 4 ++-- src/test/skiplist_tests.cpp | 8 ++++---- 7 files changed, 19 insertions(+), 19 deletions(-) (limited to 'src/test') diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp index d7b3d194d8..c923f8df28 100644 --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -145,7 +145,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test) for (unsigned int i = 0; i < NUM_SIMULATION_ITERATIONS; i++) { // 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)]; const Coin& entry = (insecure_randrange(500) == 0) ? AccessByTxid(*stack.back(), txid) : stack.back()->AccessCoin(COutPoint(txid, 0)); BOOST_CHECK(coin == entry); @@ -201,7 +201,7 @@ BOOST_AUTO_TEST_CASE(coins_cache_simulation_test) if (insecure_randrange(100) == 0) { // Every 100 iterations, flush an intermediate cache 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(); } } @@ -434,7 +434,7 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test) if (insecure_randrange(100) == 0) { // Every 100 iterations, flush an intermediate cache 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(); } } diff --git a/src/test/crypto_tests.cpp b/src/test/crypto_tests.cpp index 4b76cc1021..ab3e6fc1fd 100644 --- a/src/test/crypto_tests.cpp +++ b/src/test/crypto_tests.cpp @@ -38,7 +38,7 @@ void TestVector(const Hasher &h, const In &in, const Out &out) { Hasher hasher(h); size_t pos = 0; 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); pos += len; if (pos > 0 && pos + 2 * out.size() > in.size() && pos < in.size()) { diff --git a/src/test/merkle_tests.cpp b/src/test/merkle_tests.cpp index 785864520d..3108da8596 100644 --- a/src/test/merkle_tests.cpp +++ b/src/test/merkle_tests.cpp @@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(merkle_test) // If ntx <= 16, try all branches. Otherwise, try 16 random ones. int mtx = loop; if (ntx > 16) { - mtx = insecure_rand() % ntx; + mtx = insecure_randrange(ntx); } std::vector newBranch = BlockMerkleBranch(block, mtx); std::vector oldBranch = BlockGetMerkleBranch(block, merkleTree, mtx); diff --git a/src/test/pmt_tests.cpp b/src/test/pmt_tests.cpp index 3572505af3..d07a9f3959 100644 --- a/src/test/pmt_tests.cpp +++ b/src/test/pmt_tests.cpp @@ -21,7 +21,7 @@ class CPartialMerkleTreeTester : public CPartialMerkleTree public: // flip one bit in one of the hashes - this should break the authentication void Damage() { - unsigned int n = insecure_rand() % vHash.size(); + unsigned int n = insecure_randrange(vHash.size()); int bit = insecure_randrange(256); *(vHash[n].begin() + (bit>>3)) ^= 1<<(bit&7); } diff --git a/src/test/prevector_tests.cpp b/src/test/prevector_tests.cpp index 28f72a9b91..d951a6c3c2 100644 --- a/src/test/prevector_tests.cpp +++ b/src/test/prevector_tests.cpp @@ -200,21 +200,21 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt) for (int i = 0; i < 2048; i++) { int r = insecure_rand(); 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) { - test.erase(insecure_rand() % test.size()); + test.erase(insecure_randrange(test.size())); } if (((r >> 4) % 8) == 2) { int new_size = std::max(0, std::min(30, test.size() + (insecure_randrange(5)) - 2)); test.resize(new_size); } 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) { int del = std::min(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); } if (((r >> 13) % 16) == 5) { @@ -229,11 +229,11 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt) for (int k = 0; k < num; k++) { 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) { int del = std::min(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); } r = insecure_rand(); @@ -244,7 +244,7 @@ BOOST_AUTO_TEST_CASE(PrevectorTestInt) test.shrink_to_fit(); } 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) { test.clear(); diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp index 564fc6b944..805c09c1a4 100644 --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -91,7 +91,7 @@ void static RandomScript(CScript &script) { script = CScript(); int ops = (insecure_randrange(10)); for (int i=0; inTimeMax >= test_time); -- cgit v1.2.3