aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2017-06-07 11:34:55 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2017-06-07 11:34:55 -0700
commit3ecabae36364e905e7821fba3e60aa7f8418de6c (patch)
tree04b22d1cf440fcedacdea019f6421c629d766b44 /src/test
parentefee1db21a652019e0ab18fffc233d91bb7f1816 (diff)
Replace more rand() % NUM by randranges
Diffstat (limited to 'src/test')
-rw-r--r--src/test/coins_tests.cpp6
-rw-r--r--src/test/crypto_tests.cpp2
-rw-r--r--src/test/merkle_tests.cpp2
-rw-r--r--src/test/pmt_tests.cpp2
-rw-r--r--src/test/prevector_tests.cpp14
-rw-r--r--src/test/sighash_tests.cpp4
-rw-r--r--src/test/skiplist_tests.cpp8
7 files changed, 19 insertions, 19 deletions
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<uint256> newBranch = BlockMerkleBranch(block, mtx);
std::vector<uint256> 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<int>(0, std::min<int>(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<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);
}
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<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);
}
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; 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) {
@@ -138,7 +138,7 @@ BOOST_AUTO_TEST_CASE(sighash_test)
RandomTransaction(txTo, (nHashType & 0x1f) == SIGHASH_SINGLE);
CScript scriptCode;
RandomScript(scriptCode);
- int nIn = insecure_rand() % txTo.vin.size();
+ int nIn = insecure_randrange(txTo.vin.size());
uint256 sh, sho;
sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType);
diff --git a/src/test/skiplist_tests.cpp b/src/test/skiplist_tests.cpp
index 00ad5c1aa8..36f29a486b 100644
--- a/src/test/skiplist_tests.cpp
+++ b/src/test/skiplist_tests.cpp
@@ -34,8 +34,8 @@ BOOST_AUTO_TEST_CASE(skiplist_test)
}
for (int i=0; i < 1000; i++) {
- int from = insecure_rand() % (SKIPLIST_LENGTH - 1);
- int to = insecure_rand() % (from + 1);
+ int from = insecure_randrange(SKIPLIST_LENGTH - 1);
+ int to = insecure_randrange(from + 1);
BOOST_CHECK(vIndex[SKIPLIST_LENGTH - 1].GetAncestor(from) == &vIndex[from]);
BOOST_CHECK(vIndex[from].GetAncestor(to) == &vIndex[to]);
@@ -115,7 +115,7 @@ BOOST_AUTO_TEST_CASE(findearliestatleast_test)
} else {
// randomly choose something in the range [MTP, MTP*2]
int64_t medianTimePast = vBlocksMain[i].GetMedianTimePast();
- int r = insecure_rand() % medianTimePast;
+ int r = insecure_randrange(medianTimePast);
vBlocksMain[i].nTime = r + medianTimePast;
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.
for (unsigned int i=0; i<10000; ++i) {
// 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;
CBlockIndex *ret = chain.FindEarliestAtLeast(test_time);
BOOST_CHECK(ret->nTimeMax >= test_time);