/Src/Dependencies/Boost/libs/unordered/test/unordered/simple_tests.cpp

http://hadesmem.googlecode.com/ · C++ · 132 lines · 102 code · 26 blank · 4 comment · 17 complexity · 72d9854f392f97b1d88e2f812579ba8e MD5 · raw file

  1. // Copyright 2006-2009 Daniel James.
  2. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  3. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. // This test checks the runtime requirements of containers.
  5. #include "../helpers/prefix.hpp"
  6. #include <boost/unordered_set.hpp>
  7. #include <boost/unordered_map.hpp>
  8. #include "../helpers/test.hpp"
  9. #include <cstdlib>
  10. #include <algorithm>
  11. #include "../helpers/equivalent.hpp"
  12. template <class X>
  13. void simple_test(X const& a)
  14. {
  15. test::unordered_equivalence_tester<X> equivalent(a);
  16. {
  17. X u;
  18. BOOST_TEST(u.size() == 0);
  19. BOOST_TEST(X().size() == 0);
  20. }
  21. {
  22. BOOST_TEST(equivalent(X(a)));
  23. }
  24. {
  25. X u(a);
  26. BOOST_TEST(equivalent(u));
  27. }
  28. {
  29. X u = a;
  30. BOOST_TEST(equivalent(u));
  31. }
  32. {
  33. X b(a);
  34. BOOST_TEST(b.begin() == const_cast<X const&>(b).cbegin());
  35. BOOST_TEST(b.end() == const_cast<X const&>(b).cend());
  36. }
  37. {
  38. X b(a);
  39. X c;
  40. BOOST_TEST(equivalent(b));
  41. BOOST_TEST(c.empty());
  42. b.swap(c);
  43. BOOST_TEST(b.empty());
  44. BOOST_TEST(equivalent(c));
  45. b.swap(c);
  46. BOOST_TEST(c.empty());
  47. BOOST_TEST(equivalent(b));
  48. }
  49. {
  50. X u;
  51. X& r = u;
  52. BOOST_TEST(&(r = r) == &r);
  53. BOOST_TEST(r.empty());
  54. BOOST_TEST(&(r = a) == &r);
  55. BOOST_TEST(equivalent(r));
  56. BOOST_TEST(&(r = r) == &r);
  57. BOOST_TEST(equivalent(r));
  58. }
  59. {
  60. BOOST_TEST(a.size() ==
  61. static_cast<BOOST_DEDUCED_TYPENAME X::size_type>(
  62. std::distance(a.begin(), a.end())));
  63. }
  64. {
  65. BOOST_TEST(a.empty() == (a.size() == 0));
  66. }
  67. {
  68. BOOST_TEST(a.empty() == (a.begin() == a.end()));
  69. X u;
  70. BOOST_TEST(u.begin() == u.end());
  71. }
  72. }
  73. UNORDERED_AUTO_TEST(simple_tests)
  74. {
  75. using namespace std;
  76. srand(14878);
  77. std::cout<<"Test unordered_set.\n";
  78. boost::unordered_set<int> set;
  79. simple_test(set);
  80. set.insert(1); set.insert(2); set.insert(1456);
  81. simple_test(set);
  82. std::cout<<"Test unordered_multiset.\n";
  83. boost::unordered_multiset<int> multiset;
  84. simple_test(multiset);
  85. for(int i1 = 0; i1 < 1000; ++i1) {
  86. int count = rand() % 10, index = rand();
  87. for(int j = 0; j < count; ++j)
  88. multiset.insert(index);
  89. }
  90. simple_test(multiset);
  91. std::cout<<"Test unordered_map.\n";
  92. boost::unordered_map<int, int> map;
  93. for(int i2 = 0; i2 < 1000; ++i2) {
  94. map.insert(std::pair<const int, int>(rand(), rand()));
  95. }
  96. simple_test(map);
  97. std::cout<<"Test unordered_multimap.\n";
  98. boost::unordered_multimap<int, int> multimap;
  99. for(int i3 = 0; i3 < 1000; ++i3) {
  100. int count = rand() % 10, index = rand();
  101. for(int j = 0; j < count; ++j)
  102. multimap.insert(std::pair<const int, int>(index, rand()));
  103. }
  104. simple_test(multimap);
  105. }
  106. RUN_TESTS()