| 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/HashTable.h" |
| 8 |
#include "mozilla/PairHash.h" |
| 9 |
|
| 10 |
#include <utility> |
| 11 |
|
| 12 |
void TestMoveConstructor() { |
| 13 |
using namespace mozilla; |
| 14 |
|
| 15 |
HashMap<int, int> map; |
| 16 |
MOZ_RELEASE_ASSERT(map.putNew(3, 32)); |
| 17 |
MOZ_RELEASE_ASSERT(map.putNew(4, 42)); |
| 18 |
MOZ_RELEASE_ASSERT(map.count() == 2); |
| 19 |
MOZ_RELEASE_ASSERT(!map.empty()); |
| 20 |
MOZ_RELEASE_ASSERT(!map.lookup(2)); |
| 21 |
MOZ_RELEASE_ASSERT(map.lookup(3)->value() == 32); |
| 22 |
MOZ_RELEASE_ASSERT(map.lookup(4)->value() == 42); |
| 23 |
|
| 24 |
HashMap<int, int> moved = std::move(map); |
| 25 |
MOZ_RELEASE_ASSERT(moved.count() == 2); |
| 26 |
MOZ_RELEASE_ASSERT(!moved.empty()); |
| 27 |
MOZ_RELEASE_ASSERT(!moved.lookup(2)); |
| 28 |
MOZ_RELEASE_ASSERT(moved.lookup(3)->value() == 32); |
| 29 |
MOZ_RELEASE_ASSERT(moved.lookup(4)->value() == 42); |
| 30 |
|
| 31 |
MOZ_RELEASE_ASSERT(map.empty()); |
| 32 |
MOZ_RELEASE_ASSERT(!map.count()); |
| 33 |
} |
| 34 |
|
| 35 |
void CheckSwapMap1(const mozilla::HashMap<int, int>& map1) { |
| 36 |
MOZ_RELEASE_ASSERT(map1.count() == 2); |
| 37 |
MOZ_RELEASE_ASSERT(!map1.empty()); |
| 38 |
MOZ_RELEASE_ASSERT(!map1.lookup(3)); |
| 39 |
MOZ_RELEASE_ASSERT(!map1.lookup(4)); |
| 40 |
MOZ_RELEASE_ASSERT(map1.lookup(1)->value() == 10); |
| 41 |
MOZ_RELEASE_ASSERT(map1.lookup(2)->value() == 20); |
| 42 |
} |
| 43 |
|
| 44 |
void CheckSwapMap2(const mozilla::HashMap<int, int>& map2) { |
| 45 |
MOZ_RELEASE_ASSERT(map2.count() == 2); |
| 46 |
MOZ_RELEASE_ASSERT(!map2.empty()); |
| 47 |
MOZ_RELEASE_ASSERT(!map2.lookup(1)); |
| 48 |
MOZ_RELEASE_ASSERT(!map2.lookup(2)); |
| 49 |
MOZ_RELEASE_ASSERT(map2.lookup(3)->value() == 30); |
| 50 |
MOZ_RELEASE_ASSERT(map2.lookup(4)->value() == 40); |
| 51 |
} |
| 52 |
|
| 53 |
void TestSwap() { |
| 54 |
using namespace mozilla; |
| 55 |
|
| 56 |
HashMap<int, int> map1; |
| 57 |
MOZ_RELEASE_ASSERT(map1.putNew(1, 10)); |
| 58 |
MOZ_RELEASE_ASSERT(map1.putNew(2, 20)); |
| 59 |
CheckSwapMap1(map1); |
| 60 |
|
| 61 |
HashMap<int, int> map2; |
| 62 |
MOZ_RELEASE_ASSERT(map2.putNew(3, 30)); |
| 63 |
MOZ_RELEASE_ASSERT(map2.putNew(4, 40)); |
| 64 |
CheckSwapMap2(map2); |
| 65 |
|
| 66 |
map1.swap(map2); |
| 67 |
CheckSwapMap2(map1); |
| 68 |
CheckSwapMap1(map2); |
| 69 |
} |
| 70 |
|
| 71 |
enum SimpleEnum { SIMPLE_1, SIMPLE_2 }; |
| 72 |
|
| 73 |
enum class ClassEnum : int { |
| 74 |
CLASS_ENUM_1, |
| 75 |
CLASS_ENUM_2, |
| 76 |
}; |
| 77 |
|
| 78 |
void TestEnumHash() { |
| 79 |
using namespace mozilla; |
| 80 |
|
| 81 |
HashMap<SimpleEnum, int> map; |
| 82 |
MOZ_RELEASE_ASSERT(map.put(SIMPLE_1, 1)); |
| 83 |
MOZ_RELEASE_ASSERT(map.put(SIMPLE_2, 2)); |
| 84 |
|
| 85 |
MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_1)->value() == 1); |
| 86 |
MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_2)->value() == 2); |
| 87 |
|
| 88 |
HashMap<ClassEnum, int> map2; |
| 89 |
MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_1, 1)); |
| 90 |
MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_2, 2)); |
| 91 |
|
| 92 |
MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_1)->value() == 1); |
| 93 |
MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_2)->value() == 2); |
| 94 |
} |
| 95 |
|
| 96 |
void TestHashPair() { |
| 97 |
using namespace mozilla; |
| 98 |
|
| 99 |
// Test with std::pair |
| 100 |
{ |
| 101 |
HashMap<std::pair<int, bool>, int, PairHasher<int, bool>> map; |
| 102 |
std::pair<int, bool> key1 = std::make_pair(1, true); |
| 103 |
MOZ_RELEASE_ASSERT(map.putNew(key1, 1)); |
| 104 |
MOZ_RELEASE_ASSERT(map.has(key1)); |
| 105 |
std::pair<int, bool> key2 = std::make_pair(1, false); |
| 106 |
MOZ_RELEASE_ASSERT(map.putNew(key2, 1)); |
| 107 |
std::pair<int, bool> key3 = std::make_pair(2, false); |
| 108 |
MOZ_RELEASE_ASSERT(map.putNew(key3, 2)); |
| 109 |
MOZ_RELEASE_ASSERT(map.has(key3)); |
| 110 |
|
| 111 |
MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1); |
| 112 |
MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1); |
| 113 |
MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2); |
| 114 |
} |
| 115 |
// Test wtih compact pair |
| 116 |
{ |
| 117 |
HashMap<mozilla::CompactPair<int, bool>, int, CompactPairHasher<int, bool>> |
| 118 |
map; |
| 119 |
mozilla::CompactPair<int, bool> key1 = mozilla::MakeCompactPair(1, true); |
| 120 |
MOZ_RELEASE_ASSERT(map.putNew(key1, 1)); |
| 121 |
MOZ_RELEASE_ASSERT(map.has(key1)); |
| 122 |
mozilla::CompactPair<int, bool> key2 = mozilla::MakeCompactPair(1, false); |
| 123 |
MOZ_RELEASE_ASSERT(map.putNew(key2, 1)); |
| 124 |
mozilla::CompactPair<int, bool> key3 = mozilla::MakeCompactPair(2, false); |
| 125 |
MOZ_RELEASE_ASSERT(map.putNew(key3, 2)); |
| 126 |
MOZ_RELEASE_ASSERT(map.has(key3)); |
| 127 |
|
| 128 |
MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1); |
| 129 |
MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1); |
| 130 |
MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
int main() { |
| 135 |
TestMoveConstructor(); |
| 136 |
TestEnumHash(); |
| 137 |
TestHashPair(); |
| 138 |
return 0; |
| 139 |
} |