Line data Source code
1 : // Copyright (c) 2014-2025 The Dash Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <cachemap.h> 6 : #include <streams.h> 7 : #include <version.h> 8 : 9 : #include <boost/test/unit_test.hpp> 10 : 11 146 : BOOST_AUTO_TEST_SUITE(cachemap_tests) 12 : 13 3 : static bool Compare(const CacheMap<int,int>& cmap1, const CacheMap<int,int>& cmap2) 14 : { 15 3 : if(cmap1.GetMaxSize() != cmap2.GetMaxSize()) { 16 0 : return false; 17 : } 18 : 19 3 : if(cmap1.GetSize() != cmap2.GetSize()) { 20 0 : return false; 21 : } 22 : 23 3 : const CacheMap<int,int>::list_t& items1 = cmap1.GetItemList(); 24 30 : for(CacheMap<int,int>::list_cit it = items1.begin(); it != items1.end(); ++it) { 25 27 : if(!cmap2.HasKey(it->key)) { 26 0 : return false; 27 : } 28 27 : int val = 0; 29 27 : if(!cmap2.Get(it->key, val)) { 30 0 : return false; 31 : } 32 27 : if(it->value != val) { 33 0 : return false; 34 : } 35 27 : } 36 : 37 3 : const CacheMap<int,int>::list_t& items2 = cmap2.GetItemList(); 38 30 : for(CacheMap<int,int>::list_cit it = items2.begin(); it != items2.end(); ++it) { 39 27 : if(!cmap1.HasKey(it->key)) { 40 0 : return false; 41 : } 42 27 : int val = 0; 43 27 : if(!cmap1.Get(it->key, val)) { 44 0 : return false; 45 : } 46 27 : if(it->value != val) { 47 0 : return false; 48 : } 49 27 : } 50 : 51 3 : return true; 52 3 : } 53 : 54 148 : BOOST_AUTO_TEST_CASE(cachemap_test) 55 : { 56 : // create a CacheMap limited to 10 items 57 1 : CacheMap<int,int> cmapTest1(10); 58 : 59 : // check that the max size is 10 60 1 : BOOST_CHECK(cmapTest1.GetMaxSize() == 10); 61 : 62 : // check that the size is 0 63 1 : BOOST_CHECK(cmapTest1.GetSize() == 0); 64 : 65 : // insert (-1, -1) 66 1 : cmapTest1.Insert(-1, -1); 67 : 68 : // make sure that the size is updated 69 1 : BOOST_CHECK(cmapTest1.GetSize() == 1); 70 : 71 : // make sure the map contains the key 72 1 : BOOST_CHECK(cmapTest1.HasKey(-1) == true); 73 : 74 : // make sure that insert fails to update already existing key 75 1 : BOOST_CHECK(cmapTest1.Insert(-1, -2) == false); 76 1 : int nValRet = 0; 77 1 : BOOST_CHECK(cmapTest1.Get(-1, nValRet) == true); 78 1 : BOOST_CHECK(nValRet == -1); 79 : 80 : // make sure that the size is still the same 81 1 : BOOST_CHECK(cmapTest1.GetSize() == 1); 82 : 83 : // add 10 items 84 11 : for(int i = 0; i < 10; ++i) { 85 10 : cmapTest1.Insert(i, i); 86 10 : } 87 : 88 : // check that the size is 10 89 1 : BOOST_CHECK(cmapTest1.GetSize() == 10); 90 : 91 : // check that the map contains the expected items 92 11 : for(int i = 0; i < 10; ++i) { 93 10 : int nVal = 0; 94 10 : BOOST_CHECK(cmapTest1.Get(i, nVal) == true); 95 10 : BOOST_CHECK(nVal == i); 96 10 : } 97 : 98 : // check that the map no longer contains the first item 99 1 : BOOST_CHECK(cmapTest1.HasKey(-1) == false); 100 : 101 : // erase an item 102 1 : cmapTest1.Erase(5); 103 : 104 : // check the size 105 1 : BOOST_CHECK(cmapTest1.GetSize() == 9); 106 : 107 : // check that the map no longer contains the item 108 1 : BOOST_CHECK(cmapTest1.HasKey(5) == false); 109 : 110 : // check that the map contains the expected items 111 1 : int expected[] = { 0, 1, 2, 3, 4, 6, 7, 8, 9 }; 112 10 : for(size_t i = 0; i < 9; ++i) { 113 9 : int nVal = 0; 114 9 : int eVal = expected[i]; 115 9 : BOOST_CHECK(cmapTest1.Get(eVal, nVal) == true); 116 9 : BOOST_CHECK(nVal == eVal); 117 9 : } 118 : 119 : // test serialization 120 1 : CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); 121 1 : ss << cmapTest1; 122 : 123 1 : CacheMap<int,int> mapTest2; 124 1 : ss >> mapTest2; 125 : 126 1 : BOOST_CHECK(Compare(cmapTest1, mapTest2)); 127 : 128 : // test copy constructor 129 1 : CacheMap<int,int> mapTest3(cmapTest1); 130 1 : BOOST_CHECK(Compare(cmapTest1, mapTest3)); 131 : 132 : // test assignment operator 133 1 : CacheMap<int,int> mapTest4; 134 1 : mapTest4 = cmapTest1; 135 1 : BOOST_CHECK(Compare(cmapTest1, mapTest4)); 136 1 : } 137 : 138 146 : BOOST_AUTO_TEST_SUITE_END()