2022-06-08 01:36:46 +02:00
|
|
|
// Copyright (c) 2014-2022 The Dash Core developers
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <cachemultimap.h>
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2022-02-25 18:19:25 +01:00
|
|
|
#include <test/util/setup_common.h>
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
|
|
|
BOOST_FIXTURE_TEST_SUITE(cachemultimap_tests, BasicTestingSetup)
|
|
|
|
|
2018-05-04 22:42:39 +02:00
|
|
|
static bool Compare(const CacheMultiMap<int,int>& cmmap1, const CacheMultiMap<int,int>& cmmap2)
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
2018-02-06 12:08:43 +01:00
|
|
|
if(cmmap1.GetMaxSize() != cmmap2.GetMaxSize()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
std::cout << "Compare returning false: max size mismatch" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
if(cmmap1.GetSize() != cmmap2.GetSize()) {
|
2016-11-13 18:52:34 +01:00
|
|
|
std::cout << "Compare returning false: size mismatch" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
const CacheMultiMap<int,int>::list_t& items1 = cmmap1.GetItemList();
|
|
|
|
const CacheMultiMap<int,int>::list_t& items2 = cmmap2.GetItemList();
|
2016-11-13 18:52:34 +01:00
|
|
|
CacheMultiMap<int,int>::list_cit it2 = items2.begin();
|
|
|
|
for(CacheMultiMap<int,int>::list_cit it1 = items1.begin(); it1 != items1.end(); ++it1) {
|
|
|
|
const CacheItem<int,int>& item1 = *it1;
|
|
|
|
const CacheItem<int,int>& item2 = *it2;
|
|
|
|
if(item1.key != item2.key) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(item1.value != item2.value) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
++it2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-04 22:42:39 +02:00
|
|
|
static bool CheckExpected(const CacheMultiMap<int,int>& cmmap, int* expected, CacheMultiMap<int,int>::size_type nSize)
|
2016-11-13 18:52:34 +01:00
|
|
|
{
|
2018-02-06 12:08:43 +01:00
|
|
|
if(cmmap.GetSize() != nSize) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for(CacheMultiMap<int,int>::size_type i = 0; i < nSize; ++i) {
|
|
|
|
int nVal = 0;
|
|
|
|
int eVal = expected[i];
|
2018-02-06 12:08:43 +01:00
|
|
|
if(!cmmap.Get(eVal, nVal)) {
|
2016-11-13 18:52:34 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(nVal != eVal) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(cachemultimap_test)
|
|
|
|
{
|
|
|
|
// create a CacheMultiMap limited to 10 items
|
2018-02-06 12:08:43 +01:00
|
|
|
CacheMultiMap<int,int> cmmapTest1(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(cmmapTest1.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(cmmapTest1.GetSize() == 0);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// insert (-1, -1)
|
2018-02-06 12:08:43 +01:00
|
|
|
cmmapTest1.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(cmmapTest1.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(cmmapTest1.HasKey(-1) == true);
|
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
|
|
|
cmmapTest1.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(cmmapTest1.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(cmmapTest1.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(cmmapTest1.HasKey(-1) == false);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// erase an item
|
2018-02-06 12:08:43 +01:00
|
|
|
cmmapTest1.Erase(5);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check the size
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmmapTest1.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(cmmapTest1.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 };
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(CheckExpected(cmmapTest1, expected, 9 ) == true);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// add multiple items for the same key
|
2018-02-06 12:08:43 +01:00
|
|
|
cmmapTest1.Insert(5, 2);
|
|
|
|
cmmapTest1.Insert(5, 1);
|
|
|
|
cmmapTest1.Insert(5, 4);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check the size
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmmapTest1.GetSize() == 10);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check that 2 keys have been removed
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmmapTest1.HasKey(0) == false);
|
|
|
|
BOOST_CHECK(cmmapTest1.HasKey(1) == false);
|
|
|
|
BOOST_CHECK(cmmapTest1.HasKey(2) == true);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check multiple values
|
|
|
|
std::vector<int> vecVals;
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmmapTest1.GetAll(5, vecVals) == true);
|
2016-11-13 18:52:34 +01:00
|
|
|
BOOST_CHECK(vecVals.size() == 3);
|
|
|
|
BOOST_CHECK(vecVals[0] == 1);
|
|
|
|
BOOST_CHECK(vecVals[1] == 2);
|
|
|
|
BOOST_CHECK(vecVals[2] == 4);
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
// std::cout << "cmmapTest1 dump:" << std::endl;
|
|
|
|
// DumpMap(cmmapTest1);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// test serialization
|
|
|
|
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
|
2018-02-06 12:08:43 +01:00
|
|
|
ss << cmmapTest1;
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
CacheMultiMap<int,int> cmmapTest2;
|
|
|
|
ss >> cmmapTest2;
|
2016-11-13 18:52:34 +01:00
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
// std::cout << "cmmapTest2 dump:" << std::endl;
|
|
|
|
// DumpMap(cmmapTest2);
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// check multiple values
|
|
|
|
std::vector<int> vecVals2;
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(cmmapTest2.GetAll(5, vecVals2) == true);
|
2016-11-13 18:52:34 +01:00
|
|
|
BOOST_CHECK(vecVals2.size() == 3);
|
|
|
|
BOOST_CHECK(vecVals2[0] == 1);
|
|
|
|
BOOST_CHECK(vecVals2[1] == 2);
|
|
|
|
BOOST_CHECK(vecVals2[2] == 4);
|
|
|
|
|
2018-02-06 12:08:43 +01:00
|
|
|
BOOST_CHECK(Compare(cmmapTest1, cmmapTest2));
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// test copy constructor
|
2018-02-06 12:08:43 +01:00
|
|
|
CacheMultiMap<int,int> cmmapTest3(cmmapTest1);
|
|
|
|
BOOST_CHECK(Compare(cmmapTest1, cmmapTest3));
|
2016-11-13 18:52:34 +01:00
|
|
|
|
|
|
|
// test assignment operator
|
|
|
|
CacheMultiMap<int,int> mapTest4;
|
2018-02-06 12:08:43 +01:00
|
|
|
mapTest4 = cmmapTest1;
|
|
|
|
BOOST_CHECK(Compare(cmmapTest1, mapTest4));
|
2016-11-13 18:52:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|