unordered_map_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_map.h>
  28. #include <tinystl/string.h>
  29. #include <utility>
  30. template<typename K, typename V>
  31. static void comparemaps(const tinystl::unordered_map<K, V>& m, const tinystl::unordered_map<K, V>& expected) {
  32. CHECK( m.size() == expected.size() );
  33. typedef typename tinystl::unordered_map<K, V>::const_iterator iterator;
  34. for (iterator it = expected.begin(), end = expected.end(); it != end; ++it) {
  35. iterator found = m.find((*it).first);
  36. CHECK( found != m.end() );
  37. CHECK( (*found).second == (*it).second );
  38. }
  39. }
  40. TEST(uomap_constructor) {
  41. typedef tinystl::unordered_map<int, int> unordered_map;
  42. using tinystl::make_pair;
  43. unordered_map baseline;
  44. comparemaps(baseline, baseline); // test with empty maps
  45. baseline.insert(make_pair(5, 1));
  46. baseline.insert(make_pair(6, 2));
  47. CHECK( 2 == baseline.size() );
  48. CHECK( baseline.find(5) != baseline.end() );
  49. CHECK( baseline[5] == 1 );
  50. CHECK( baseline.find(6) != baseline.end() );
  51. CHECK( baseline[6] == 2 );
  52. comparemaps(baseline, baseline);
  53. {
  54. unordered_map m;
  55. CHECK( m.empty() );
  56. CHECK( m.size() == 0 );
  57. }
  58. {
  59. unordered_map m = baseline;
  60. comparemaps(m, baseline);
  61. }
  62. {
  63. unordered_map other = baseline;
  64. unordered_map m = std::move(other);
  65. comparemaps(m, baseline);
  66. CHECK( other.empty() );
  67. }
  68. }
  69. TEST(uomap_assign) {
  70. typedef tinystl::unordered_map<int, int> unordered_map;
  71. using tinystl::make_pair;
  72. unordered_map baseline;
  73. baseline.insert(make_pair(5, 1));
  74. baseline.insert(make_pair(6, 2));
  75. CHECK( 2 == baseline.size() );
  76. CHECK( baseline.find(5) != baseline.end() );
  77. CHECK( baseline[5] == 1 );
  78. CHECK( baseline.find(6) != baseline.end() );
  79. CHECK( baseline[6] == 2 );
  80. comparemaps(baseline, baseline);
  81. {
  82. unordered_map m;
  83. m = baseline;
  84. comparemaps(m, baseline);
  85. }
  86. {
  87. unordered_map m;
  88. for (int ii = 0; ii != 10; ++ii)
  89. m.insert(make_pair(ii, 10*ii));
  90. m = baseline;
  91. comparemaps(m, baseline);
  92. }
  93. {
  94. unordered_map other = baseline;
  95. unordered_map m;
  96. m = std::move(other);
  97. comparemaps(m, baseline);
  98. CHECK( other.empty() );
  99. }
  100. {
  101. unordered_map other = baseline;
  102. unordered_map m;
  103. for (int ii = 0; ii != 10; ++ii)
  104. m.insert(make_pair(ii, 10*ii));
  105. m = std::move(other);
  106. comparemaps(m, baseline);
  107. CHECK( other.empty() );
  108. }
  109. }
  110. TEST(uomap_insert) {
  111. using tinystl::string;
  112. using tinystl::pair;
  113. typedef tinystl::unordered_map<string, string> unordered_map;
  114. typedef pair<unordered_map::iterator, bool> inspair;
  115. {
  116. unordered_map m;
  117. m.insert(make_pair(string("hello"), string("world")));
  118. CHECK( m.find("hello") != m.end() );
  119. }
  120. {
  121. const pair<string, string> p("hello", "world");
  122. unordered_map m;
  123. inspair p1 = m.insert(p);
  124. CHECK( p1.second );
  125. CHECK( (*p1.first).first == tinystl::string("hello") );
  126. CHECK( (*p1.first).second == tinystl::string("world") );
  127. inspair p2 = m.insert(p);
  128. CHECK( !p2.second );
  129. CHECK( p2.first == p1.first );
  130. }
  131. {
  132. unordered_map m;
  133. m.emplace(pair<string, string>("hello", "world"));
  134. CHECK( m.find("hello") != m.end() );
  135. }
  136. {
  137. unordered_map m;
  138. inspair p1 = m.emplace(pair<string, string>("hello", "world"));
  139. CHECK( p1.second );
  140. CHECK( (*p1.first).first == tinystl::string("hello") );
  141. CHECK( (*p1.first).second == tinystl::string("world") );
  142. inspair p2 = m.emplace(pair<string, string>("hello", "world"));
  143. CHECK( !p2.second );
  144. CHECK( p2.first == p1.first );
  145. }
  146. {
  147. unordered_map m;
  148. pair<string, string> p("hello", "world");
  149. m.emplace(std::move(p));
  150. CHECK( m.find("hello") != m.end() );
  151. CHECK( p.first.size() == 0 );
  152. CHECK( p.second.size() == 0 );
  153. }
  154. }
  155. TEST(uomap_iterate) {
  156. typedef tinystl::unordered_map<size_t, size_t> unordered_map;
  157. {
  158. unordered_map m;
  159. for (size_t i = 0; i < 1000; ++i) {
  160. CHECK( m.size() == i );
  161. size_t count = 0;
  162. for (auto it = m.begin(); it != m.end(); ++it) {
  163. count++;
  164. }
  165. CHECK( count == i );
  166. m.insert(tinystl::make_pair(17 * i, 101 * i));
  167. }
  168. }
  169. }