Bug 1879774 [wpt PR 44524] - WebKit export: Implement field-sizing support for input...
[gecko.git] / mfbt / tests / TestHashTable.cpp
blobc648184040c1af175ddba3aeba83c5fc294b9aa0
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/. */
7 #include "mozilla/HashTable.h"
8 #include "mozilla/PairHash.h"
10 #include <utility>
12 void TestMoveConstructor() {
13 using namespace mozilla;
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);
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);
31 MOZ_RELEASE_ASSERT(map.empty());
32 MOZ_RELEASE_ASSERT(!map.count());
35 enum SimpleEnum { SIMPLE_1, SIMPLE_2 };
37 enum class ClassEnum : int {
38 CLASS_ENUM_1,
39 CLASS_ENUM_2,
42 void TestEnumHash() {
43 using namespace mozilla;
45 HashMap<SimpleEnum, int> map;
46 MOZ_RELEASE_ASSERT(map.put(SIMPLE_1, 1));
47 MOZ_RELEASE_ASSERT(map.put(SIMPLE_2, 2));
49 MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_1)->value() == 1);
50 MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_2)->value() == 2);
52 HashMap<ClassEnum, int> map2;
53 MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_1, 1));
54 MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_2, 2));
56 MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_1)->value() == 1);
57 MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_2)->value() == 2);
60 void TestHashPair() {
61 using namespace mozilla;
63 // Test with std::pair
65 HashMap<std::pair<int, bool>, int, PairHasher<int, bool>> map;
66 std::pair<int, bool> key1 = std::make_pair(1, true);
67 MOZ_RELEASE_ASSERT(map.putNew(key1, 1));
68 MOZ_RELEASE_ASSERT(map.has(key1));
69 std::pair<int, bool> key2 = std::make_pair(1, false);
70 MOZ_RELEASE_ASSERT(map.putNew(key2, 1));
71 std::pair<int, bool> key3 = std::make_pair(2, false);
72 MOZ_RELEASE_ASSERT(map.putNew(key3, 2));
73 MOZ_RELEASE_ASSERT(map.has(key3));
75 MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1);
76 MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1);
77 MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2);
79 // Test wtih compact pair
81 HashMap<mozilla::CompactPair<int, bool>, int, CompactPairHasher<int, bool>>
82 map;
83 mozilla::CompactPair<int, bool> key1 = mozilla::MakeCompactPair(1, true);
84 MOZ_RELEASE_ASSERT(map.putNew(key1, 1));
85 MOZ_RELEASE_ASSERT(map.has(key1));
86 mozilla::CompactPair<int, bool> key2 = mozilla::MakeCompactPair(1, false);
87 MOZ_RELEASE_ASSERT(map.putNew(key2, 1));
88 mozilla::CompactPair<int, bool> key3 = mozilla::MakeCompactPair(2, false);
89 MOZ_RELEASE_ASSERT(map.putNew(key3, 2));
90 MOZ_RELEASE_ASSERT(map.has(key3));
92 MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1);
93 MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1);
94 MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2);
98 int main() {
99 TestMoveConstructor();
100 TestEnumHash();
101 TestHashPair();
102 return 0;