2019-06-11 13:46:07 +02:00
|
|
|
// Copyright (c) 2014-2019 The Dash Core developers
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
#include "cachemap.h"
|
|
|
|
|
|
|
|
#include "test/test_dash.h"
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(cachemap_tests, BasicTestingSetup)
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
bool Compare(const CacheMap<int,int>& cmap1, const CacheMap<int,int>& cmap2)
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
2018-02-06 12:08:43 +01:00
|
|
|
if(cmap1.GetMaxSize() != cmap2.GetMaxSize()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
if(cmap1.GetSize() != cmap2.GetSize()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
const CacheMap<int,int>::list_t& items1 = cmap1.GetItemList();
|
2016-11-13 18:52:34 +01:00
|
|
|
for(CacheMap<int,int>::list_cit it = items1.begin(); it != items1.end(); ++it) {
|
2018-02-06 12:08:43 +01:00
|
|
|
if(!cmap2.HasKey(it->key)) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int val = 0;
|
2018-02-06 12:08:43 +01:00
|
|
|
if(!cmap2.Get(it->key, val)) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(it->value != val) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
const CacheMap<int,int>::list_t& items2 = cmap2.GetItemList();
|
2016-11-13 18:52:34 +01:00
|
|
|
for(CacheMap<int,int>::list_cit it = items2.begin(); it != items2.end(); ++it) {
|
2018-02-06 12:08:43 +01:00
|
|
|
if(!cmap1.HasKey(it->key)) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int val = 0;
|
2018-02-06 12:08:43 +01:00
|
|
|
if(!cmap1.Get(it->key, val)) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(it->value != val) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(cachemap_test)
|
|
|
|
{
|
|
|
|
// create a CacheMap limited to 10 items
|
2018-02-06 12:08:43 +01:00
|
|
|
CacheMap<int,int> cmapTest1(10);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check that the max size is 10
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.GetMaxSize() == 10);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check that the size is 0
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.GetSize() == 0);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// insert (-1, -1)
|
2018-02-06 12:08:43 +01:00
|
|
|
cmapTest1.Insert(-1, -1);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// make sure that the size is updated
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.GetSize() == 1);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// make sure the map contains the key
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.HasKey(-1) == true);
|
|
|
|
|
|
|
|
// make sure that insert fails to update already existing key
|
|
|
|
BOOST_CHECK(cmapTest1.Insert(-1, -2) == false);
|
|
|
|
int nValRet = 0;
|
|
|
|
BOOST_CHECK(cmapTest1.Get(-1, nValRet) == true);
|
|
|
|
BOOST_CHECK(nValRet == -1);
|
|
|
|
|
|
|
|
// make sure that the size is still the same
|
|
|
|
BOOST_CHECK(cmapTest1.GetSize() == 1);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// add 10 items
|
|
|
|
for(int i = 0; i < 10; ++i) {
|
2018-02-06 12:08:43 +01:00
|
|
|
cmapTest1.Insert(i, i);
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// check that the size is 10
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.GetSize() == 10);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check that the map contains the expected items
|
|
|
|
for(int i = 0; i < 10; ++i) {
|
|
|
|
int nVal = 0;
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.Get(i, nVal) == true);
|
2016-11-13 18:52:34 +01:00
|
|
|
BOOST_CHECK(nVal == i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check that the map no longer contains the first item
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.HasKey(-1) == false);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// erase an item
|
2018-02-06 12:08:43 +01:00
|
|
|
cmapTest1.Erase(5);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check the size
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.GetSize() == 9);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check that the map no longer contains the item
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.HasKey(5) == false);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check that the map contains the expected items
|
|
|
|
int expected[] = { 0, 1, 2, 3, 4, 6, 7, 8, 9 };
|
|
|
|
for(size_t i = 0; i < 9; ++i) {
|
|
|
|
int nVal = 0;
|
|
|
|
int eVal = expected[i];
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmapTest1.Get(eVal, nVal) == true);
|
2016-11-13 18:52:34 +01:00
|
|
|
BOOST_CHECK(nVal == eVal);
|
|
|
|
}
|
|
|
|
|
|
|
|
// test serialization
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
2018-02-06 12:08:43 +01:00
|
|
|
ss << cmapTest1;
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
CacheMap<int,int> mapTest2;
|
|
|
|
ss >> mapTest2;
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(Compare(cmapTest1, mapTest2));
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// test copy constructor
|
2018-02-06 12:08:43 +01:00
|
|
|
CacheMap<int,int> mapTest3(cmapTest1);
|
|
|
|
BOOST_CHECK(Compare(cmapTest1, mapTest3));
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// test assignment operator
|
|
|
|
CacheMap<int,int> mapTest4;
|
2018-02-06 12:08:43 +01:00
|
|
|
mapTest4 = cmapTest1;
|
|
|
|
BOOST_CHECK(Compare(cmapTest1, mapTest4));
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|