Line data Source code
1 : // Copyright (c) 2012-2015 The Bitcoin 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 <limitedmap.h> 6 : 7 : #include <test/util/setup_common.h> 8 : 9 : #include <boost/test/unit_test.hpp> 10 : 11 146 : BOOST_FIXTURE_TEST_SUITE(limitedmap_tests, BasicTestingSetup) 12 : 13 149 : BOOST_AUTO_TEST_CASE(limitedmap_test) 14 : { 15 : // create a limitedmap capped at 10 items 16 1 : unordered_limitedmap<int, int> map(10); 17 : 18 : // check that the max size is 10 19 1 : BOOST_CHECK(map.max_size() == 10); 20 : 21 : // check that it's empty 22 1 : BOOST_CHECK(map.size() == 0); 23 : 24 : // insert (-1, -1) 25 1 : map.insert(std::pair<int, int>(-1, -1)); 26 : 27 : // make sure that the size is updated 28 1 : BOOST_CHECK(map.size() == 1); 29 : 30 : // make sure that the new item is in the map 31 1 : BOOST_CHECK(map.count(-1) == 1); 32 : 33 : // insert 10 new items 34 11 : for (int i = 0; i < 10; i++) { 35 10 : map.insert(std::pair<int, int>(i, i + 1)); 36 10 : } 37 : 38 : // make sure that the map now contains 10 items... 39 1 : BOOST_CHECK(map.size() == 10); 40 : 41 : // ...and that the first item has been discarded 42 1 : BOOST_CHECK(map.count(-1) == 0); 43 : 44 : // iterate over the map, both with an index and an iterator 45 1 : unordered_limitedmap<int, int>::const_iterator it = map.begin(); 46 11 : for (int i = 0; i < 10; i++) { 47 : // make sure the item is present 48 10 : BOOST_CHECK(map.count(i) == 1); 49 : 50 : // use the iterator to check for the expected key and value 51 : //BOOST_CHECK(it->first == i); 52 : //BOOST_CHECK(it->second == i + 1); 53 : 54 : // use find to check for the value 55 10 : BOOST_CHECK(map.find(i)->second == i + 1); 56 : 57 : // update and recheck 58 10 : auto jt = map.find(i); 59 10 : map.update(jt, i + 2); 60 10 : BOOST_CHECK(map.find(i)->second == i + 2); 61 : 62 10 : it++; 63 10 : } 64 : 65 : // check that we've exhausted the iterator 66 1 : BOOST_CHECK(it == map.end()); 67 : 68 : // resize the map to 5 items 69 1 : map.max_size(5); 70 : 71 : // check that the max size and size are now 5 72 1 : BOOST_CHECK(map.max_size() == 5); 73 1 : BOOST_CHECK(map.size() == 5); 74 : 75 : // check that items less than 5 have been discarded 76 : // and items greater than 5 are retained 77 11 : for (int i = 0; i < 10; i++) { 78 10 : if (i < 5) { 79 5 : BOOST_CHECK(map.count(i) == 0); 80 5 : } else { 81 5 : BOOST_CHECK(map.count(i) == 1); 82 : } 83 10 : } 84 : 85 : // erase some items not in the map 86 10 : for (int i = 100; i < 1000; i += 100) { 87 9 : map.erase(i); 88 9 : } 89 : 90 : // check that the size is unaffected 91 1 : BOOST_CHECK(map.size() == 5); 92 : 93 : // erase the remaining elements 94 6 : for (int i = 5; i < 10; i++) { 95 5 : map.erase(i); 96 5 : } 97 : 98 : // check that the map is now empty 99 1 : BOOST_CHECK(map.empty()); 100 1 : } 101 : 102 146 : BOOST_AUTO_TEST_SUITE_END()