You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mruset_tests.cpp 2.3KB

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