TestHashTable.cpp (5360B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/CompactPair.h" 8 #include "mozilla/HashTable.h" 9 #include "mozilla/PairHash.h" 10 11 #include <utility> 12 13 void TestMoveConstructor() { 14 using namespace mozilla; 15 16 HashMap<int, int> map; 17 MOZ_RELEASE_ASSERT(map.putNew(3, 32)); 18 MOZ_RELEASE_ASSERT(map.putNew(4, 42)); 19 MOZ_RELEASE_ASSERT(map.count() == 2); 20 MOZ_RELEASE_ASSERT(!map.empty()); 21 MOZ_RELEASE_ASSERT(!map.lookup(2)); 22 MOZ_RELEASE_ASSERT(map.lookup(3)->value() == 32); 23 MOZ_RELEASE_ASSERT(map.lookup(4)->value() == 42); 24 25 HashMap<int, int> moved = std::move(map); 26 MOZ_RELEASE_ASSERT(moved.count() == 2); 27 MOZ_RELEASE_ASSERT(!moved.empty()); 28 MOZ_RELEASE_ASSERT(!moved.lookup(2)); 29 MOZ_RELEASE_ASSERT(moved.lookup(3)->value() == 32); 30 MOZ_RELEASE_ASSERT(moved.lookup(4)->value() == 42); 31 32 MOZ_RELEASE_ASSERT(map.empty()); 33 MOZ_RELEASE_ASSERT(!map.count()); 34 } 35 36 void CheckSwapMap1(const mozilla::HashMap<int, int>& map1) { 37 MOZ_RELEASE_ASSERT(map1.count() == 2); 38 MOZ_RELEASE_ASSERT(!map1.empty()); 39 MOZ_RELEASE_ASSERT(!map1.lookup(3)); 40 MOZ_RELEASE_ASSERT(!map1.lookup(4)); 41 MOZ_RELEASE_ASSERT(map1.lookup(1)->value() == 10); 42 MOZ_RELEASE_ASSERT(map1.lookup(2)->value() == 20); 43 } 44 45 void CheckSwapMap2(const mozilla::HashMap<int, int>& map2) { 46 MOZ_RELEASE_ASSERT(map2.count() == 2); 47 MOZ_RELEASE_ASSERT(!map2.empty()); 48 MOZ_RELEASE_ASSERT(!map2.lookup(1)); 49 MOZ_RELEASE_ASSERT(!map2.lookup(2)); 50 MOZ_RELEASE_ASSERT(map2.lookup(3)->value() == 30); 51 MOZ_RELEASE_ASSERT(map2.lookup(4)->value() == 40); 52 } 53 54 void TestSwap() { 55 using namespace mozilla; 56 57 HashMap<int, int> map1; 58 MOZ_RELEASE_ASSERT(map1.putNew(1, 10)); 59 MOZ_RELEASE_ASSERT(map1.putNew(2, 20)); 60 CheckSwapMap1(map1); 61 62 HashMap<int, int> map2; 63 MOZ_RELEASE_ASSERT(map2.putNew(3, 30)); 64 MOZ_RELEASE_ASSERT(map2.putNew(4, 40)); 65 CheckSwapMap2(map2); 66 67 map1.swap(map2); 68 CheckSwapMap2(map1); 69 CheckSwapMap1(map2); 70 } 71 72 enum SimpleEnum { SIMPLE_1, SIMPLE_2 }; 73 74 enum class ClassEnum : int { 75 CLASS_ENUM_1, 76 CLASS_ENUM_2, 77 }; 78 79 void TestEnumHash() { 80 using namespace mozilla; 81 82 HashMap<SimpleEnum, int> map; 83 MOZ_RELEASE_ASSERT(map.put(SIMPLE_1, 1)); 84 MOZ_RELEASE_ASSERT(map.put(SIMPLE_2, 2)); 85 86 MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_1)->value() == 1); 87 MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_2)->value() == 2); 88 89 HashMap<ClassEnum, int> map2; 90 MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_1, 1)); 91 MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_2, 2)); 92 93 MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_1)->value() == 1); 94 MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_2)->value() == 2); 95 } 96 97 void TestHashPair() { 98 using namespace mozilla; 99 100 // Test with std::pair 101 { 102 HashMap<std::pair<int, bool>, int, PairHasher<int, bool>> map; 103 std::pair<int, bool> key1 = std::make_pair(1, true); 104 MOZ_RELEASE_ASSERT(map.putNew(key1, 1)); 105 MOZ_RELEASE_ASSERT(map.has(key1)); 106 std::pair<int, bool> key2 = std::make_pair(1, false); 107 MOZ_RELEASE_ASSERT(map.putNew(key2, 1)); 108 std::pair<int, bool> key3 = std::make_pair(2, false); 109 MOZ_RELEASE_ASSERT(map.putNew(key3, 2)); 110 MOZ_RELEASE_ASSERT(map.has(key3)); 111 112 MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1); 113 MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1); 114 MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2); 115 } 116 // Test wtih compact pair 117 { 118 HashMap<mozilla::CompactPair<int, bool>, int, CompactPairHasher<int, bool>> 119 map; 120 mozilla::CompactPair<int, bool> key1 = mozilla::MakeCompactPair(1, true); 121 MOZ_RELEASE_ASSERT(map.putNew(key1, 1)); 122 MOZ_RELEASE_ASSERT(map.has(key1)); 123 mozilla::CompactPair<int, bool> key2 = mozilla::MakeCompactPair(1, false); 124 MOZ_RELEASE_ASSERT(map.putNew(key2, 1)); 125 mozilla::CompactPair<int, bool> key3 = mozilla::MakeCompactPair(2, false); 126 MOZ_RELEASE_ASSERT(map.putNew(key3, 2)); 127 MOZ_RELEASE_ASSERT(map.has(key3)); 128 129 MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1); 130 MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1); 131 MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2); 132 } 133 } 134 135 void TestCapacityAfterRemove() { 136 mozilla::HashMap<int, int> map; 137 MOZ_RELEASE_ASSERT(map.count() == 0); 138 MOZ_RELEASE_ASSERT(map.capacity() == 0); 139 140 MOZ_RELEASE_ASSERT(map.putNew(1, 1)); 141 MOZ_RELEASE_ASSERT(map.count() == 1); 142 MOZ_RELEASE_ASSERT(map.capacity() != 0); 143 144 map.remove(1); 145 MOZ_RELEASE_ASSERT(map.count() == 0); 146 MOZ_RELEASE_ASSERT(map.capacity() != 0); 147 148 map.compact(); 149 MOZ_RELEASE_ASSERT(map.count() == 0); 150 MOZ_RELEASE_ASSERT(map.capacity() == 0); 151 152 MOZ_RELEASE_ASSERT(map.putNew(1, 1)); 153 MOZ_RELEASE_ASSERT(map.count() == 1); 154 MOZ_RELEASE_ASSERT(map.capacity() != 0); 155 156 { 157 auto iter = map.modIter(); 158 MOZ_RELEASE_ASSERT(!iter.done()); 159 iter.remove(); 160 } 161 MOZ_RELEASE_ASSERT(map.count() == 0); 162 MOZ_RELEASE_ASSERT(map.capacity() != 0); 163 164 map.compact(); 165 MOZ_RELEASE_ASSERT(map.count() == 0); 166 MOZ_RELEASE_ASSERT(map.capacity() == 0); 167 } 168 169 int main() { 170 TestMoveConstructor(); 171 TestEnumHash(); 172 TestHashPair(); 173 TestCapacityAfterRemove(); 174 return 0; 175 }