unordered_set_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*-
  2. * Copyright 2012-2018 Matthew Endsley
  3. * All rights reserved
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted providing that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  20. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  21. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  22. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  23. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  24. * POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "test.h"
  27. #include <tinystl/unordered_set.h>
  28. #include <tinystl/string.h>
  29. #include <utility>
  30. template<typename T>
  31. static void comparesets(const tinystl::unordered_set<T>& s, const tinystl::unordered_set<T>& expected) {
  32. CHECK( s.size() == expected.size() );
  33. typedef typename tinystl::unordered_set<T>::const_iterator iterator;
  34. for (iterator it = expected.begin(), end = expected.end(); it != end; ++it) {
  35. CHECK( s.find(*it) != s.end() );
  36. }
  37. }
  38. TEST(uoset_constructor) {
  39. typedef tinystl::unordered_set<int> unordered_set;
  40. unordered_set baseline;
  41. comparesets(baseline, baseline); // test on empty
  42. baseline.insert(5);
  43. baseline.insert(6);
  44. CHECK( 2 == baseline.size() );
  45. CHECK( baseline.find(5) != baseline.end() );
  46. CHECK( baseline.find(6) != baseline.end() );
  47. comparesets(baseline, baseline);
  48. {
  49. unordered_set s;
  50. CHECK( s.empty() );
  51. CHECK( s.size() == 0 );
  52. }
  53. {
  54. unordered_set s = baseline;
  55. comparesets(s, baseline);
  56. }
  57. {
  58. unordered_set other = baseline;
  59. unordered_set s = std::move(other);
  60. comparesets(s, baseline);
  61. CHECK( other.empty() );
  62. }
  63. }
  64. TEST(uoset_assign) {
  65. typedef tinystl::unordered_set<int> unordered_set;
  66. unordered_set baseline;
  67. baseline.insert(5);
  68. baseline.insert(6);
  69. CHECK( 2 == baseline.size() );
  70. CHECK( baseline.find(5) != baseline.end() );
  71. CHECK( baseline.find(6) != baseline.end() );
  72. comparesets(baseline, baseline);
  73. {
  74. unordered_set s;
  75. s = baseline;
  76. comparesets(s, baseline);
  77. }
  78. {
  79. unordered_set s;
  80. for (int ii = 0; ii != 10; ++ii)
  81. s.insert(ii);
  82. s = baseline;
  83. comparesets(s, baseline);
  84. }
  85. {
  86. unordered_set other = baseline;
  87. unordered_set s;
  88. s = std::move(other);
  89. comparesets(s, baseline);
  90. CHECK( other.empty() );
  91. }
  92. {
  93. unordered_set other = baseline;
  94. unordered_set s;
  95. for (int ii = 0; ii != 10; ++ii)
  96. s.insert(ii);
  97. s = std::move(other);
  98. comparesets(s, baseline);
  99. CHECK( other.empty() );
  100. }
  101. }
  102. TEST(uoset_insert) {
  103. typedef tinystl::unordered_set<tinystl::string> unordered_set;
  104. typedef tinystl::pair<unordered_set::iterator, bool> pair;
  105. {
  106. unordered_set s;
  107. s.insert("hello");
  108. CHECK( s.find("hello") != s.end() );
  109. }
  110. {
  111. unordered_set s;
  112. pair p1 = s.insert("hello");
  113. CHECK( p1.second );
  114. CHECK( (*p1.first) == tinystl::string("hello") );
  115. pair p2 = s.insert("hello");
  116. CHECK( !p2.second );
  117. CHECK( p2.first == p1.first );
  118. }
  119. {
  120. unordered_set s;
  121. s.emplace("hello");
  122. CHECK( s.find("hello") != s.end() );
  123. }
  124. {
  125. unordered_set s;
  126. pair p1 = s.emplace("hello");
  127. CHECK( p1.second );
  128. CHECK( (*p1.first) == tinystl::string("hello") );
  129. pair p2 = s.emplace("hello");
  130. CHECK( !p2.second );
  131. CHECK( p2.first == p1.first );
  132. }
  133. {
  134. unordered_set s;
  135. tinystl::string key("hello");
  136. s.emplace(std::move(key));
  137. CHECK( s.find("hello") != s.end() );
  138. CHECK( key.size() == 0 );
  139. }
  140. }
  141. TEST(uoset_iterate) {
  142. typedef tinystl::unordered_set<int> unordered_set;
  143. {
  144. unordered_set s;
  145. for (size_t i = 0; i < 1000; ++i) {
  146. CHECK( s.size() == i );
  147. size_t count = 0;
  148. for (auto it = s.begin(); it != s.end(); ++it) {
  149. count++;
  150. }
  151. CHECK( count == i );
  152. s.insert(int(17 * i));
  153. }
  154. }
  155. }