LCOV - code coverage report
Current view: top level - src/test - cachemultimap_tests.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 79 88 89.8 %
Date: 2026-06-25 07:23:43 Functions: 8 8 100.0 %

          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 <cachemultimap.h>
       6             : #include <streams.h>
       7             : #include <version.h>
       8             : 
       9             : #include <algorithm>
      10             : #include <iostream>
      11             : 
      12             : #include <boost/test/unit_test.hpp>
      13             : 
      14         146 : BOOST_AUTO_TEST_SUITE(cachemultimap_tests)
      15             : 
      16           3 : static bool Compare(const CacheMultiMap<int,int>& cmmap1, const CacheMultiMap<int,int>& cmmap2)
      17             : {
      18           3 :     if(cmmap1.GetMaxSize() != cmmap2.GetMaxSize()) {
      19           0 :         std::cout << "Compare returning false: max size mismatch" << std::endl;
      20           0 :         return false;
      21             :     }
      22             : 
      23           3 :     if(cmmap1.GetSize() != cmmap2.GetSize()) {
      24           0 :         std::cout << "Compare returning false: size mismatch" << std::endl;
      25           0 :         return false;
      26             :     }
      27             : 
      28           3 :     const CacheMultiMap<int,int>::list_t& items1 = cmmap1.GetItemList();
      29           3 :     const CacheMultiMap<int,int>::list_t& items2 = cmmap2.GetItemList();
      30           3 :     CacheMultiMap<int,int>::list_cit it2 = items2.begin();
      31          33 :     for(CacheMultiMap<int,int>::list_cit it1 = items1.begin(); it1 != items1.end(); ++it1) {
      32          30 :         const CacheItem<int,int>& item1 = *it1;
      33          30 :         const CacheItem<int,int>& item2 = *it2;
      34          30 :         if(item1.key != item2.key) {
      35           0 :             return false;
      36             :         }
      37          30 :         if(item1.value != item2.value) {
      38           0 :             return false;
      39             :         }
      40          30 :         ++it2;
      41          30 :     }
      42             : 
      43           3 :     return true;
      44           3 : }
      45             : 
      46           1 : static bool CheckExpected(const CacheMultiMap<int,int>& cmmap, int* expected, CacheMultiMap<int,int>::size_type nSize)
      47             : {
      48           1 :     if(cmmap.GetSize() != nSize) {
      49           0 :         return false;
      50             :     }
      51          10 :     for(CacheMultiMap<int,int>::size_type i = 0; i < nSize; ++i) {
      52           9 :         int nVal = 0;
      53           9 :         int eVal = expected[i];
      54           9 :         if(!cmmap.Get(eVal, nVal)) {
      55           0 :             return false;
      56             :         }
      57           9 :         if(nVal != eVal) {
      58           0 :             return false;
      59             :         }
      60           9 :     }
      61           1 :     return true;
      62           1 : }
      63             : 
      64         148 : BOOST_AUTO_TEST_CASE(cachemultimap_test)
      65             : {
      66             :     // create a CacheMultiMap limited to 10 items
      67           1 :     CacheMultiMap<int,int> cmmapTest1(10);
      68             : 
      69             :     // check that the max size is 10
      70           1 :     BOOST_CHECK(cmmapTest1.GetMaxSize() == 10);
      71             : 
      72             :     // check that the size is 0
      73           1 :     BOOST_CHECK(cmmapTest1.GetSize() == 0);
      74             : 
      75             :     // insert (-1, -1)
      76           1 :     cmmapTest1.Insert(-1, -1);
      77             : 
      78             :     // make sure that the size is updated
      79           1 :     BOOST_CHECK(cmmapTest1.GetSize() == 1);
      80             : 
      81             :     // make sure the map contains the key
      82           1 :     BOOST_CHECK(cmmapTest1.HasKey(-1) == true);
      83             : 
      84             :     // add 10 items
      85          11 :     for(int i = 0; i < 10; ++i) {
      86          10 :         cmmapTest1.Insert(i, i);
      87          10 :     }
      88             : 
      89             :     // check that the size is 10
      90           1 :     BOOST_CHECK(cmmapTest1.GetSize() == 10);
      91             : 
      92             :     // check that the map contains the expected items
      93          11 :     for(int i = 0; i < 10; ++i) {
      94          10 :         int nVal = 0;
      95          10 :         BOOST_CHECK(cmmapTest1.Get(i, nVal) == true);
      96          10 :         BOOST_CHECK(nVal == i);
      97          10 :     }
      98             : 
      99             :     // check that the map no longer contains the first item
     100           1 :     BOOST_CHECK(cmmapTest1.HasKey(-1) == false);
     101             : 
     102             :     // erase an item
     103           1 :     cmmapTest1.Erase(5);
     104             : 
     105             :     // check the size
     106           1 :     BOOST_CHECK(cmmapTest1.GetSize() == 9);
     107             : 
     108             :     // check that the map no longer contains the item
     109           1 :     BOOST_CHECK(cmmapTest1.HasKey(5) == false);
     110             : 
     111             :     // check that the map contains the expected items
     112           1 :     int expected[] = { 0, 1, 2, 3, 4, 6, 7, 8, 9 };
     113           1 :     BOOST_CHECK(CheckExpected(cmmapTest1, expected, 9 ) == true);
     114             : 
     115             :     // add multiple items for the same key
     116           1 :     cmmapTest1.Insert(5, 2);
     117           1 :     cmmapTest1.Insert(5, 1);
     118           1 :     cmmapTest1.Insert(5, 4);
     119             : 
     120             :     // check the size
     121           1 :     BOOST_CHECK(cmmapTest1.GetSize() == 10);
     122             : 
     123             :     // check that 2 keys have been removed
     124           1 :     BOOST_CHECK(cmmapTest1.HasKey(0) == false);
     125           1 :     BOOST_CHECK(cmmapTest1.HasKey(1) == false);
     126           1 :     BOOST_CHECK(cmmapTest1.HasKey(2) == true);
     127             : 
     128             :     // check multiple values
     129           1 :     std::vector<int> vecVals;
     130           1 :     BOOST_CHECK(cmmapTest1.GetAll(5, vecVals) == true);
     131           1 :     BOOST_CHECK(vecVals.size() == 3);
     132           1 :     BOOST_CHECK(vecVals[0] == 1);
     133           1 :     BOOST_CHECK(vecVals[1] == 2);
     134           1 :     BOOST_CHECK(vecVals[2] == 4);
     135             : 
     136             : //    std::cout << "cmmapTest1 dump:" << std::endl;
     137             : //    DumpMap(cmmapTest1);
     138             : 
     139             :     // test serialization
     140           1 :     CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
     141           1 :     ss << cmmapTest1;
     142             : 
     143           1 :     CacheMultiMap<int,int> cmmapTest2;
     144           1 :     ss >> cmmapTest2;
     145             : 
     146             : //    std::cout << "cmmapTest2 dump:" << std::endl;
     147             : //    DumpMap(cmmapTest2);
     148             : 
     149             :     // check multiple values
     150           1 :     std::vector<int> vecVals2;
     151           1 :     BOOST_CHECK(cmmapTest2.GetAll(5, vecVals2) == true);
     152           1 :     BOOST_CHECK(vecVals2.size() == 3);
     153           1 :     BOOST_CHECK(vecVals2[0] == 1);
     154           1 :     BOOST_CHECK(vecVals2[1] == 2);
     155           1 :     BOOST_CHECK(vecVals2[2] == 4);
     156             : 
     157           1 :     BOOST_CHECK(Compare(cmmapTest1, cmmapTest2));
     158             : 
     159             :     // test copy constructor
     160           1 :     CacheMultiMap<int,int> cmmapTest3(cmmapTest1);
     161           1 :     BOOST_CHECK(Compare(cmmapTest1, cmmapTest3));
     162             : 
     163             :     // test assignment operator
     164           1 :     CacheMultiMap<int,int> mapTest4;
     165           1 :     mapTest4 = cmmapTest1;
     166           1 :     BOOST_CHECK(Compare(cmmapTest1, mapTest4));
     167           1 : }
     168             : 
     169         146 : BOOST_AUTO_TEST_SUITE_END()

Generated by: LCOV version 1.16