aboutsummaryrefslogtreecommitdiff
path: root/src/test/cuckoocache_tests.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/cuckoocache_tests.cpp')
-rw-r--r--src/test/cuckoocache_tests.cpp44
1 files changed, 26 insertions, 18 deletions
diff --git a/src/test/cuckoocache_tests.cpp b/src/test/cuckoocache_tests.cpp
index fc22daeb57..bb4c8c7093 100644
--- a/src/test/cuckoocache_tests.cpp
+++ b/src/test/cuckoocache_tests.cpp
@@ -29,11 +29,11 @@
* using BOOST_CHECK_CLOSE to fail.
*
*/
-BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
+BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup);
/* Test that no values not inserted into the cache are read out of it.
*
- * There are no repeats in the first 200000 InsecureRand256() calls
+ * There are no repeats in the first 200000 m_rng.rand256() calls
*/
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
{
@@ -42,18 +42,19 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
size_t megabytes = 4;
cc.setup_bytes(megabytes << 20);
for (int x = 0; x < 100000; ++x) {
- cc.insert(InsecureRand256());
+ cc.insert(m_rng.rand256());
}
for (int x = 0; x < 100000; ++x) {
- BOOST_CHECK(!cc.contains(InsecureRand256(), false));
+ BOOST_CHECK(!cc.contains(m_rng.rand256(), false));
}
};
+struct HitRateTest : BasicTestingSetup {
/** This helper returns the hit rate when megabytes*load worth of entries are
* inserted into a megabytes sized cache
*/
template <typename Cache>
-static double test_cache(size_t megabytes, double load)
+double test_cache(size_t megabytes, double load)
{
SeedRandomForTest(SeedRand::ZEROS);
std::vector<uint256> hashes;
@@ -65,7 +66,7 @@ static double test_cache(size_t megabytes, double load)
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)hashes[i].begin();
for (uint8_t j = 0; j < 8; ++j)
- *(ptr++) = InsecureRand32();
+ *(ptr++) = m_rng.rand32();
}
/** We make a copy of the hashes because future optimizations of the
* cuckoocache may overwrite the inserted element, so the test is
@@ -104,9 +105,10 @@ static double normalize_hit_rate(double hits, double load)
{
return hits * std::max(load, 1.0);
}
+}; // struct HitRateTest
/** Check the hit rate on loads ranging from 0.1 to 1.6 */
-BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
+BOOST_FIXTURE_TEST_CASE(cuckoocache_hit_rate_ok, HitRateTest)
{
/** Arbitrarily selected Hit Rate threshold that happens to work for this test
* as a lower bound on performance.
@@ -120,10 +122,11 @@ BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
}
+struct EraseTest : BasicTestingSetup {
/** This helper checks that erased elements are preferentially inserted onto and
* that the hit rate of "fresher" keys is reasonable*/
template <typename Cache>
-static void test_cache_erase(size_t megabytes)
+void test_cache_erase(size_t megabytes)
{
double load = 1;
SeedRandomForTest(SeedRand::ZEROS);
@@ -136,7 +139,7 @@ static void test_cache_erase(size_t megabytes)
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)hashes[i].begin();
for (uint8_t j = 0; j < 8; ++j)
- *(ptr++) = InsecureRand32();
+ *(ptr++) = m_rng.rand32();
}
/** We make a copy of the hashes because future optimizations of the
* cuckoocache may overwrite the inserted element, so the test is
@@ -178,15 +181,17 @@ static void test_cache_erase(size_t megabytes)
// erased elements.
BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
}
+}; // struct EraseTest
-BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
+BOOST_FIXTURE_TEST_CASE(cuckoocache_erase_ok, EraseTest)
{
size_t megabytes = 4;
test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
}
+struct EraseParallelTest : BasicTestingSetup {
template <typename Cache>
-static void test_cache_erase_parallel(size_t megabytes)
+void test_cache_erase_parallel(size_t megabytes)
{
double load = 1;
SeedRandomForTest(SeedRand::ZEROS);
@@ -199,7 +204,7 @@ static void test_cache_erase_parallel(size_t megabytes)
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)hashes[i].begin();
for (uint8_t j = 0; j < 8; ++j)
- *(ptr++) = InsecureRand32();
+ *(ptr++) = m_rng.rand32();
}
/** We make a copy of the hashes because future optimizations of the
* cuckoocache may overwrite the inserted element, so the test is
@@ -268,15 +273,17 @@ static void test_cache_erase_parallel(size_t megabytes)
// erased elements.
BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
}
-BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
+}; // struct EraseParallelTest
+BOOST_FIXTURE_TEST_CASE(cuckoocache_erase_parallel_ok, EraseParallelTest)
{
size_t megabytes = 4;
test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
}
+struct GenerationsTest : BasicTestingSetup {
template <typename Cache>
-static void test_cache_generations()
+void test_cache_generations()
{
// This test checks that for a simulation of network activity, the fresh hit
// rate is never below 99%, and the number of times that it is worse than
@@ -302,7 +309,7 @@ static void test_cache_generations()
// immediately and never uses the other half.
struct block_activity {
std::vector<uint256> reads;
- block_activity(uint32_t n_insert, Cache& c) : reads()
+ block_activity(uint32_t n_insert, FastRandomContext& rng, Cache& c)
{
std::vector<uint256> inserts;
inserts.resize(n_insert);
@@ -310,7 +317,7 @@ static void test_cache_generations()
for (uint32_t i = 0; i < n_insert; ++i) {
uint32_t* ptr = (uint32_t*)inserts[i].begin();
for (uint8_t j = 0; j < 8; ++j)
- *(ptr++) = InsecureRand32();
+ *(ptr++) = rng.rand32();
}
for (uint32_t i = 0; i < n_insert / 4; ++i)
reads.push_back(inserts[i]);
@@ -344,7 +351,7 @@ static void test_cache_generations()
for (uint32_t i = 0; i < total; ++i) {
if (last_few.size() == WINDOW_SIZE)
last_few.pop_front();
- last_few.emplace_back(BLOCK_SIZE, set);
+ last_few.emplace_back(BLOCK_SIZE, m_rng, set);
uint32_t count = 0;
for (auto& act : last_few)
for (uint32_t k = 0; k < POP_AMOUNT; ++k) {
@@ -365,7 +372,8 @@ static void test_cache_generations()
// max_rate_less_than_tight_hit_rate of the time
BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate);
}
-BOOST_AUTO_TEST_CASE(cuckoocache_generations)
+}; // struct GenerationsTest
+BOOST_FIXTURE_TEST_CASE(cuckoocache_generations, GenerationsTest)
{
test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
}