TestHashMap.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/TestSuite.h>
  27. #include <AK/HashMap.h>
  28. #include <AK/String.h>
  29. TEST_CASE(construct)
  30. {
  31. using IntIntMap = HashMap<int, int>;
  32. EXPECT(IntIntMap().is_empty());
  33. EXPECT_EQ(IntIntMap().size(), 0u);
  34. }
  35. TEST_CASE(construct_from_initializer_list)
  36. {
  37. HashMap<int, String> number_to_string {
  38. { 1, "One" },
  39. { 2, "Two" },
  40. { 3, "Three" },
  41. };
  42. EXPECT_EQ(number_to_string.is_empty(), false);
  43. EXPECT_EQ(number_to_string.size(), 3u);
  44. }
  45. TEST_CASE(populate)
  46. {
  47. HashMap<int, String> number_to_string;
  48. number_to_string.set(1, "One");
  49. number_to_string.set(2, "Two");
  50. number_to_string.set(3, "Three");
  51. EXPECT_EQ(number_to_string.is_empty(), false);
  52. EXPECT_EQ(number_to_string.size(), 3u);
  53. }
  54. TEST_CASE(range_loop)
  55. {
  56. HashMap<int, String> number_to_string;
  57. EXPECT_EQ(number_to_string.set(1, "One"), AK::HashSetResult::InsertedNewEntry);
  58. EXPECT_EQ(number_to_string.set(2, "Two"), AK::HashSetResult::InsertedNewEntry);
  59. EXPECT_EQ(number_to_string.set(3, "Three"), AK::HashSetResult::InsertedNewEntry);
  60. int loop_counter = 0;
  61. for (auto& it : number_to_string) {
  62. EXPECT_EQ(it.value.is_null(), false);
  63. ++loop_counter;
  64. }
  65. EXPECT_EQ(loop_counter, 3);
  66. }
  67. TEST_CASE(map_remove)
  68. {
  69. HashMap<int, String> number_to_string;
  70. EXPECT_EQ(number_to_string.set(1, "One"), AK::HashSetResult::InsertedNewEntry);
  71. EXPECT_EQ(number_to_string.set(2, "Two"), AK::HashSetResult::InsertedNewEntry);
  72. EXPECT_EQ(number_to_string.set(3, "Three"), AK::HashSetResult::InsertedNewEntry);
  73. EXPECT_EQ(number_to_string.remove(1), true);
  74. EXPECT_EQ(number_to_string.size(), 2u);
  75. EXPECT(number_to_string.find(1) == number_to_string.end());
  76. EXPECT_EQ(number_to_string.remove(3), true);
  77. EXPECT_EQ(number_to_string.size(), 1u);
  78. EXPECT(number_to_string.find(3) == number_to_string.end());
  79. EXPECT(number_to_string.find(2) != number_to_string.end());
  80. }
  81. TEST_CASE(case_insensitive)
  82. {
  83. HashMap<String, int, CaseInsensitiveStringTraits> casemap;
  84. EXPECT_EQ(String("nickserv").to_lowercase(), String("NickServ").to_lowercase());
  85. EXPECT_EQ(casemap.set("nickserv", 3), AK::HashSetResult::InsertedNewEntry);
  86. EXPECT_EQ(casemap.set("NickServ", 3), AK::HashSetResult::ReplacedExistingEntry);
  87. EXPECT_EQ(casemap.size(), 1u);
  88. }
  89. TEST_CASE(hashmap_of_nonnullownptr_get)
  90. {
  91. struct Object {
  92. Object(const String& s)
  93. : string(s)
  94. {
  95. }
  96. String string;
  97. };
  98. HashMap<int, NonnullOwnPtr<Object>> objects;
  99. objects.set(1, make<Object>("One"));
  100. objects.set(2, make<Object>("Two"));
  101. objects.set(3, make<Object>("Three"));
  102. {
  103. auto x = objects.get(2);
  104. EXPECT_EQ(x.has_value(), true);
  105. EXPECT_EQ(x.value()->string, "Two");
  106. }
  107. {
  108. // Do it again to make sure that peeking into the map above didn't
  109. // remove the value from the map.
  110. auto x = objects.get(2);
  111. EXPECT_EQ(x.has_value(), true);
  112. EXPECT_EQ(x.value()->string, "Two");
  113. }
  114. EXPECT_EQ(objects.size(), 3u);
  115. }
  116. TEST_CASE(many_strings)
  117. {
  118. HashMap<String, int> strings;
  119. for (int i = 0; i < 999; ++i) {
  120. EXPECT_EQ(strings.set(String::number(i), i), AK::HashSetResult::InsertedNewEntry);
  121. }
  122. EXPECT_EQ(strings.size(), 999u);
  123. for (auto& it : strings) {
  124. EXPECT_EQ(it.key.to_int().value(), it.value);
  125. }
  126. for (int i = 0; i < 999; ++i) {
  127. EXPECT_EQ(strings.remove(String::number(i)), true);
  128. }
  129. EXPECT_EQ(strings.is_empty(), true);
  130. }
  131. TEST_CASE(basic_remove)
  132. {
  133. HashMap<int, int> map;
  134. map.set(1, 10);
  135. map.set(2, 20);
  136. map.set(3, 30);
  137. EXPECT_EQ(map.remove(3), true);
  138. EXPECT_EQ(map.remove(3), false);
  139. EXPECT_EQ(map.size(), 2u);
  140. EXPECT_EQ(map.remove(1), true);
  141. EXPECT_EQ(map.remove(1), false);
  142. EXPECT_EQ(map.size(), 1u);
  143. EXPECT_EQ(map.remove(2), true);
  144. EXPECT_EQ(map.remove(2), false);
  145. EXPECT_EQ(map.size(), 0u);
  146. }
  147. TEST_CASE(basic_contains)
  148. {
  149. HashMap<int, int> map;
  150. map.set(1, 10);
  151. map.set(2, 20);
  152. map.set(3, 30);
  153. EXPECT_EQ(map.contains(1), true);
  154. EXPECT_EQ(map.contains(2), true);
  155. EXPECT_EQ(map.contains(3), true);
  156. EXPECT_EQ(map.contains(4), false);
  157. EXPECT_EQ(map.remove(3), true);
  158. EXPECT_EQ(map.contains(3), false);
  159. EXPECT_EQ(map.contains(1), true);
  160. EXPECT_EQ(map.contains(2), true);
  161. EXPECT_EQ(map.remove(2), true);
  162. EXPECT_EQ(map.contains(2), false);
  163. EXPECT_EQ(map.contains(3), false);
  164. EXPECT_EQ(map.contains(1), true);
  165. EXPECT_EQ(map.remove(1), true);
  166. EXPECT_EQ(map.contains(1), false);
  167. }
  168. TEST_MAIN(HashMap)