|
17 | 17 |
|
18 | 18 | using namespace std;
|
19 | 19 |
|
20 |
| -class mrutester |
21 |
| -{ |
22 |
| -private: |
23 |
| - mruset<int> mru; |
24 |
| - std::set<int> set; |
25 |
| - |
26 |
| -public: |
27 |
| - mrutester() : mru(MAX_SIZE) {} |
28 |
| - int size() const { return set.size(); } |
29 |
| - |
30 |
| - void insert(int n) |
31 |
| - { |
32 |
| - mru.insert(n); |
33 |
| - set.insert(n); |
34 |
| - BOOST_CHECK(mru == set); |
35 |
| - } |
36 |
| -}; |
37 |
| - |
38 | 20 | BOOST_FIXTURE_TEST_SUITE(mruset_tests, BasicTestingSetup)
|
39 | 21 |
|
40 |
| -// Test that an mruset behaves like a set, as long as no more than MAX_SIZE elements are in it |
41 |
| -BOOST_AUTO_TEST_CASE(mruset_like_set) |
42 |
| -{ |
43 |
| - |
44 |
| - for (int nTest=0; nTest<NUM_TESTS; nTest++) |
45 |
| - { |
46 |
| - mrutester tester; |
47 |
| - while (tester.size() < MAX_SIZE) |
48 |
| - tester.insert(GetRandInt(2 * MAX_SIZE)); |
49 |
| - } |
50 |
| - |
51 |
| -} |
52 |
| - |
53 |
| -// Test that an mruset's size never exceeds its max_size |
54 |
| -BOOST_AUTO_TEST_CASE(mruset_limited_size) |
| 22 | +BOOST_AUTO_TEST_CASE(mruset_test) |
55 | 23 | {
|
56 |
| - for (int nTest=0; nTest<NUM_TESTS; nTest++) |
57 |
| - { |
58 |
| - mruset<int> mru(MAX_SIZE); |
59 |
| - for (int nAction=0; nAction<3*MAX_SIZE; nAction++) |
60 |
| - { |
61 |
| - int n = GetRandInt(2 * MAX_SIZE); |
62 |
| - mru.insert(n); |
63 |
| - BOOST_CHECK(mru.size() <= MAX_SIZE); |
| 24 | + // The mruset being tested. |
| 25 | + mruset<int> mru(5000); |
| 26 | + |
| 27 | + // Run the test 10 times. |
| 28 | + for (int test = 0; test < 10; test++) { |
| 29 | + // Reset mru. |
| 30 | + 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 | + for (int j=0; j<10000; j++) { |
| 38 | + int add = GetRandInt(15000); |
| 39 | + mru.insert(add); |
| 40 | + |
| 41 | + // Add the number to rep/all as well. |
| 42 | + if (all.count(add) == 0) { |
| 43 | + all.insert(add); |
| 44 | + rep.push_back(add); |
| 45 | + if (all.size() == 5001) { |
| 46 | + all.erase(rep.front()); |
| 47 | + rep.pop_front(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // Do a full comparison between mru and the simulated mru every 1000 and every 5001 elements. |
| 52 | + if (j % 1000 == 0 || j % 5001 == 0) { |
| 53 | + mruset<int> mru2 = mru; // Also try making a copy |
| 54 | + |
| 55 | + // Check that all elements that should be in there, are in there. |
| 56 | + BOOST_FOREACH(int x, rep) { |
| 57 | + BOOST_CHECK(mru.count(x)); |
| 58 | + BOOST_CHECK(mru2.count(x)); |
| 59 | + } |
| 60 | + |
| 61 | + // Check that all elements that are in there, should be in there. |
| 62 | + BOOST_FOREACH(int x, mru) { |
| 63 | + BOOST_CHECK(all.count(x)); |
| 64 | + } |
| 65 | + |
| 66 | + // Check that all elements that are in there, should be in there. |
| 67 | + BOOST_FOREACH(int x, mru2) { |
| 68 | + BOOST_CHECK(all.count(x)); |
| 69 | + } |
| 70 | + |
| 71 | + for (int t = 0; t < 10; t++) { |
| 72 | + int r = GetRandInt(15000); |
| 73 | + BOOST_CHECK(all.count(r) == mru.count(r)); |
| 74 | + BOOST_CHECK(all.count(r) == mru2.count(r)); |
| 75 | + } |
| 76 | + } |
64 | 77 | }
|
65 | 78 | }
|
66 | 79 | }
|
67 | 80 |
|
68 |
| -// 16-bit permutation function |
69 |
| -int static permute(int n) |
70 |
| -{ |
71 |
| - // hexadecimals of pi; verified to be linearly independent |
72 |
| - static const int table[16] = {0x243F, 0x6A88, 0x85A3, 0x08D3, 0x1319, 0x8A2E, 0x0370, 0x7344, |
73 |
| - 0xA409, 0x3822, 0x299F, 0x31D0, 0x082E, 0xFA98, 0xEC4E, 0x6C89}; |
74 |
| - |
75 |
| - int ret = 0; |
76 |
| - for (int bit=0; bit<16; bit++) |
77 |
| - if (n & (1<<bit)) |
78 |
| - ret ^= table[bit]; |
79 |
| - |
80 |
| - return ret; |
81 |
| -} |
82 |
| - |
83 |
| -// Test that an mruset acts like a moving window, if no duplicate elements are added |
84 |
| -BOOST_AUTO_TEST_CASE(mruset_window) |
85 |
| -{ |
86 |
| - mruset<int> mru(MAX_SIZE); |
87 |
| - for (int n=0; n<10*MAX_SIZE; n++) |
88 |
| - { |
89 |
| - mru.insert(permute(n)); |
90 |
| - |
91 |
| - set<int> tester; |
92 |
| - for (int m=max(0,n-MAX_SIZE+1); m<=n; m++) |
93 |
| - tester.insert(permute(m)); |
94 |
| - |
95 |
| - BOOST_CHECK(mru == tester); |
96 |
| - } |
97 |
| -} |
98 |
| - |
99 | 81 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments