File: TestHashTable.cpp

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (175 lines) | stat: -rw-r--r-- 5,360 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/CompactPair.h"
#include "mozilla/HashTable.h"
#include "mozilla/PairHash.h"

#include <utility>

void TestMoveConstructor() {
  using namespace mozilla;

  HashMap<int, int> map;
  MOZ_RELEASE_ASSERT(map.putNew(3, 32));
  MOZ_RELEASE_ASSERT(map.putNew(4, 42));
  MOZ_RELEASE_ASSERT(map.count() == 2);
  MOZ_RELEASE_ASSERT(!map.empty());
  MOZ_RELEASE_ASSERT(!map.lookup(2));
  MOZ_RELEASE_ASSERT(map.lookup(3)->value() == 32);
  MOZ_RELEASE_ASSERT(map.lookup(4)->value() == 42);

  HashMap<int, int> moved = std::move(map);
  MOZ_RELEASE_ASSERT(moved.count() == 2);
  MOZ_RELEASE_ASSERT(!moved.empty());
  MOZ_RELEASE_ASSERT(!moved.lookup(2));
  MOZ_RELEASE_ASSERT(moved.lookup(3)->value() == 32);
  MOZ_RELEASE_ASSERT(moved.lookup(4)->value() == 42);

  MOZ_RELEASE_ASSERT(map.empty());
  MOZ_RELEASE_ASSERT(!map.count());
}

void CheckSwapMap1(const mozilla::HashMap<int, int>& map1) {
  MOZ_RELEASE_ASSERT(map1.count() == 2);
  MOZ_RELEASE_ASSERT(!map1.empty());
  MOZ_RELEASE_ASSERT(!map1.lookup(3));
  MOZ_RELEASE_ASSERT(!map1.lookup(4));
  MOZ_RELEASE_ASSERT(map1.lookup(1)->value() == 10);
  MOZ_RELEASE_ASSERT(map1.lookup(2)->value() == 20);
}

void CheckSwapMap2(const mozilla::HashMap<int, int>& map2) {
  MOZ_RELEASE_ASSERT(map2.count() == 2);
  MOZ_RELEASE_ASSERT(!map2.empty());
  MOZ_RELEASE_ASSERT(!map2.lookup(1));
  MOZ_RELEASE_ASSERT(!map2.lookup(2));
  MOZ_RELEASE_ASSERT(map2.lookup(3)->value() == 30);
  MOZ_RELEASE_ASSERT(map2.lookup(4)->value() == 40);
}

void TestSwap() {
  using namespace mozilla;

  HashMap<int, int> map1;
  MOZ_RELEASE_ASSERT(map1.putNew(1, 10));
  MOZ_RELEASE_ASSERT(map1.putNew(2, 20));
  CheckSwapMap1(map1);

  HashMap<int, int> map2;
  MOZ_RELEASE_ASSERT(map2.putNew(3, 30));
  MOZ_RELEASE_ASSERT(map2.putNew(4, 40));
  CheckSwapMap2(map2);

  map1.swap(map2);
  CheckSwapMap2(map1);
  CheckSwapMap1(map2);
}

enum SimpleEnum { SIMPLE_1, SIMPLE_2 };

enum class ClassEnum : int {
  CLASS_ENUM_1,
  CLASS_ENUM_2,
};

void TestEnumHash() {
  using namespace mozilla;

  HashMap<SimpleEnum, int> map;
  MOZ_RELEASE_ASSERT(map.put(SIMPLE_1, 1));
  MOZ_RELEASE_ASSERT(map.put(SIMPLE_2, 2));

  MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_1)->value() == 1);
  MOZ_RELEASE_ASSERT(map.lookup(SIMPLE_2)->value() == 2);

  HashMap<ClassEnum, int> map2;
  MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_1, 1));
  MOZ_RELEASE_ASSERT(map2.put(ClassEnum::CLASS_ENUM_2, 2));

  MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_1)->value() == 1);
  MOZ_RELEASE_ASSERT(map2.lookup(ClassEnum::CLASS_ENUM_2)->value() == 2);
}

void TestHashPair() {
  using namespace mozilla;

  // Test with std::pair
  {
    HashMap<std::pair<int, bool>, int, PairHasher<int, bool>> map;
    std::pair<int, bool> key1 = std::make_pair(1, true);
    MOZ_RELEASE_ASSERT(map.putNew(key1, 1));
    MOZ_RELEASE_ASSERT(map.has(key1));
    std::pair<int, bool> key2 = std::make_pair(1, false);
    MOZ_RELEASE_ASSERT(map.putNew(key2, 1));
    std::pair<int, bool> key3 = std::make_pair(2, false);
    MOZ_RELEASE_ASSERT(map.putNew(key3, 2));
    MOZ_RELEASE_ASSERT(map.has(key3));

    MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2);
  }
  // Test wtih compact pair
  {
    HashMap<mozilla::CompactPair<int, bool>, int, CompactPairHasher<int, bool>>
        map;
    mozilla::CompactPair<int, bool> key1 = mozilla::MakeCompactPair(1, true);
    MOZ_RELEASE_ASSERT(map.putNew(key1, 1));
    MOZ_RELEASE_ASSERT(map.has(key1));
    mozilla::CompactPair<int, bool> key2 = mozilla::MakeCompactPair(1, false);
    MOZ_RELEASE_ASSERT(map.putNew(key2, 1));
    mozilla::CompactPair<int, bool> key3 = mozilla::MakeCompactPair(2, false);
    MOZ_RELEASE_ASSERT(map.putNew(key3, 2));
    MOZ_RELEASE_ASSERT(map.has(key3));

    MOZ_RELEASE_ASSERT(map.lookup(key1)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key2)->value() == 1);
    MOZ_RELEASE_ASSERT(map.lookup(key3)->value() == 2);
  }
}

void TestCapacityAfterRemove() {
  mozilla::HashMap<int, int> map;
  MOZ_RELEASE_ASSERT(map.count() == 0);
  MOZ_RELEASE_ASSERT(map.capacity() == 0);

  MOZ_RELEASE_ASSERT(map.putNew(1, 1));
  MOZ_RELEASE_ASSERT(map.count() == 1);
  MOZ_RELEASE_ASSERT(map.capacity() != 0);

  map.remove(1);
  MOZ_RELEASE_ASSERT(map.count() == 0);
  MOZ_RELEASE_ASSERT(map.capacity() != 0);

  map.compact();
  MOZ_RELEASE_ASSERT(map.count() == 0);
  MOZ_RELEASE_ASSERT(map.capacity() == 0);

  MOZ_RELEASE_ASSERT(map.putNew(1, 1));
  MOZ_RELEASE_ASSERT(map.count() == 1);
  MOZ_RELEASE_ASSERT(map.capacity() != 0);

  {
    auto iter = map.modIter();
    MOZ_RELEASE_ASSERT(!iter.done());
    iter.remove();
  }
  MOZ_RELEASE_ASSERT(map.count() == 0);
  MOZ_RELEASE_ASSERT(map.capacity() != 0);

  map.compact();
  MOZ_RELEASE_ASSERT(map.count() == 0);
  MOZ_RELEASE_ASSERT(map.capacity() == 0);
}

int main() {
  TestMoveConstructor();
  TestEnumHash();
  TestHashPair();
  TestCapacityAfterRemove();
  return 0;
}