Line data Source code
1 : // Copyright (c) 2012-2013 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 "mruset.h"
6 :
7 : #include "random.h"
8 : #include "util.h"
9 : #include "test/test_bitcoin.h"
10 :
11 : #include <set>
12 :
13 : #include <boost/test/unit_test.hpp>
14 :
15 : #define NUM_TESTS 16
16 : #define MAX_SIZE 100
17 :
18 : using namespace std;
19 :
20 1 : BOOST_FIXTURE_TEST_SUITE(mruset_tests, BasicTestingSetup)
21 :
22 6 : BOOST_AUTO_TEST_CASE(mruset_test)
23 : {
24 : // The mruset being tested.
25 1 : mruset<int> mru(5000);
26 :
27 : // Run the test 10 times.
28 10 : for (int test = 0; test < 10; test++) {
29 : // Reset mru.
30 10 : mru.clear();
31 :
32 : // A deque + set to simulate the mruset.
33 : std::deque<int> rep;
34 : std::set<int> all;
35 :
36 : // Insert 10000 random integers below 15000.
37 100010 : for (int j=0; j<10000; j++) {
38 100000 : int add = GetRandInt(15000);
39 100000 : mru.insert(add);
40 :
41 : // Add the number to rep/all as well.
42 100000 : if (all.count(add) == 0) {
43 : all.insert(add);
44 76174 : rep.push_back(add);
45 76174 : if (all.size() == 5001) {
46 26174 : all.erase(rep.front());
47 26174 : rep.pop_front();
48 : }
49 : }
50 :
51 : // Do a full comparison between mru and the simulated mru every 1000 and every 5001 elements.
52 100000 : if (j % 1000 == 0 || j % 5001 == 0) {
53 110 : mruset<int> mru2 = mru; // Also try making a copy
54 :
55 : // Check that all elements that should be in there, are in there.
56 1878910 : BOOST_FOREACH(int x, rep) {
57 3005200 : BOOST_CHECK(mru.count(x));
58 3005200 : BOOST_CHECK(mru2.count(x));
59 : }
60 :
61 : // Check that all elements that are in there, should be in there.
62 2254560 : BOOST_FOREACH(int x, mru) {
63 3005200 : BOOST_CHECK(all.count(x));
64 : }
65 :
66 : // Check that all elements that are in there, should be in there.
67 2254560 : BOOST_FOREACH(int x, mru2) {
68 3005200 : BOOST_CHECK(all.count(x));
69 : }
70 :
71 1210 : for (int t = 0; t < 10; t++) {
72 1100 : int r = GetRandInt(15000);
73 9900 : BOOST_CHECK(all.count(r) == mru.count(r));
74 9900 : BOOST_CHECK(all.count(r) == mru2.count(r));
75 110 : }
76 : }
77 : }
78 1 : }
79 1 : }
80 :
81 3 : BOOST_AUTO_TEST_SUITE_END()
|