-
Notifications
You must be signed in to change notification settings - Fork 0
/
design-hashset-test.cpp
150 lines (114 loc) · 3.2 KB
/
design-hashset-test.cpp
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
/*
* Copyright (c) 2018 Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
#include <gtest/gtest.h>
#include "design-hashset.cpp"
/*
static void graphHistogram( const vector<size_t> & histogram ) {
enum { MIN, MAX };
size_t lim[2]{ SIZE_MAX, 0 };
for( auto & h: histogram ) {
if ( h < lim[ MIN ] ) {
lim[ MIN ] = h;
}
if ( h > lim[ MAX ] ) {
lim[ MAX ] = h;
}
}
size_t M = lim[ MAX ]; // number of rows
size_t N = histogram.size(); // number of columns
vector<vector<bool>> bmp( M, vector<bool>( N, false ) );
for( size_t col = 0; col < N; col++ ) {
for( size_t row = 0; row < histogram[ col ]; row++ ) {
bmp[ lim[ MAX ] - row - 1 ][ col ] = true;
}
}
for( size_t row = 0; row < M; row++ ) {
for( size_t col = 0; col < N; col++ ) {
if ( bmp[ row ][ col ] ) {
cerr << 'x';
} else {
cerr << ' ';
}
}
cerr << endl;
}
for( size_t col = 0; col < N; col++ ) cerr << '=';
cerr << endl;
}
*/
size_t histogramNumSamples(const vector<size_t> &histogram) {
size_t n_samples = 0;
for (auto &h : histogram) {
n_samples += h;
}
return n_samples;
}
TEST(DesignHashset, Test_Example) {
MyHashSet hashSet;
hashSet.add(1);
hashSet.add(2);
EXPECT_TRUE(hashSet.contains(1));
EXPECT_FALSE(hashSet.contains(3));
hashSet.add(2);
EXPECT_TRUE(hashSet.contains(2));
hashSet.remove(2);
EXPECT_FALSE(hashSet.contains(2));
}
TEST(DesignHashset, Test_remove_nonexistent) {
MyHashSet hashSet;
vector<size_t> histogram;
hashSet.add(1);
EXPECT_TRUE(hashSet.contains(1));
EXPECT_FALSE(hashSet.contains(2));
hashSet.remove(2);
EXPECT_FALSE(hashSet.contains(2));
hashSet.remove(1);
EXPECT_FALSE(hashSet.contains(1));
}
TEST(DesignHashset, Test_80Random) {
MyHashSet hashSet;
vector<size_t> histogram;
for (size_t i = 0; i < 80; i++) {
int x;
for (x = random(); hashSet.contains(x); x = random())
;
ASSERT_FALSE(hashSet.contains(x));
hashSet.add(x);
ASSERT_TRUE(hashSet.contains(x));
histogram = hashSet.histogram();
ASSERT_EQ(histogramNumSamples(histogram), i + 1);
// graphHistogram( histogram );
}
}
TEST(DesignHashset, Test_1000Random) {
MyHashSet hashSet;
vector<size_t> histogram;
for (size_t i = 0; i < 1000; i++) {
int x;
for (x = random(); hashSet.contains(x); x = random())
;
ASSERT_FALSE(hashSet.contains(x));
hashSet.add(x);
ASSERT_TRUE(hashSet.contains(x));
histogram = hashSet.histogram();
ASSERT_EQ(histogramNumSamples(histogram), i + 1);
// graphHistogram( histogram );
}
}
TEST(DesignHashset, Test_1000000Random) {
MyHashSet hashSet;
for (size_t i = 0; i < 1000000; i++) {
int x;
bool add = !!random();
x = random();
if (add) {
hashSet.add(x);
ASSERT_TRUE(hashSet.contains(x));
} else {
hashSet.remove(x);
}
}
}