This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
cuckoo_utils_test.cc
275 lines (218 loc) · 9.03 KB
/
cuckoo_utils_test.cc
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// -----------------------------------------------------------------------------
// File: cuckoo_utils_test.cc
// -----------------------------------------------------------------------------
#include "cuckoo_utils.h"
#include <string>
#include "evaluation_utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace ci {
TEST(CuckooUtilsTest, GetMinNumBuckets2SlotsPerBucket) {
// kMaxLoadFactor2SlotsPerBucket: 0.84
// (1 / 0.84) * 10 values / 2 slots per bucket = 5.95
ASSERT_EQ(GetMinNumBuckets(/*num_values=*/10, /*slots_per_bucket=*/2), 6);
}
TEST(CuckooUtilsTest, GetMinNumBuckets4SlotsPerBucket) {
// kMaxLoadFactor4SlotsPerBucket: 0.95
// (1 / 0.95) * 10 values / 4 slots per bucket = 2.63
ASSERT_EQ(GetMinNumBuckets(/*num_values=*/10, /*slots_per_bucket=*/4), 3);
}
TEST(CuckooUtilsTest, GetMinNumBuckets8SlotsPerBucket) {
// kMaxLoadFactor8SlotsPerBucket: 0.98
// (1 / 0.98) * 10 values / 2 slots per bucket = 1.28
ASSERT_EQ(GetMinNumBuckets(/*num_values=*/10, /*slots_per_bucket=*/8), 2);
}
TEST(CuckooUtilsTest, GetMinNumBucketsCustomLoadFactor) {
// (1 / 0.5) * 10 values / 1 slots per bucket = 20
ASSERT_EQ(GetMinNumBuckets(/*num_values=*/10, /*slots_per_bucket=*/1,
/*max_load_factor=*/0.5),
20);
}
TEST(CuckooUtilsTest, GetMinNumBucketsAtLeastOneBucket) {
// (1 / 0.99) * 1 values / 8 slots per bucket = 0.13
ASSERT_EQ(GetMinNumBuckets(/*num_values=*/1, /*slots_per_bucket=*/8,
/*max_load_factor=*/0.99),
1);
}
TEST(CuckooUtilsTest, GetMinNumBucketsZeroValues) {
// (1 / 0.5) * 0 values / 1 slots per bucket = 0
ASSERT_EQ(GetMinNumBuckets(/*num_values=*/0, /*slots_per_bucket=*/1,
/*max_load_factor=*/0.5),
0);
}
TEST(CuckooUtilsTest, GetFingerprintPrefix) {
EXPECT_EQ(GetFingerprintPrefix(0b1111ULL << 60, /*num_bits=*/0), 0b0);
EXPECT_EQ(GetFingerprintPrefix(0b1111ULL << 60, /*num_bits=*/1), 0b1);
EXPECT_EQ(GetFingerprintPrefix(0b1111ULL << 60, /*num_bits=*/2), 0b11);
EXPECT_EQ(GetFingerprintPrefix(0b1111ULL << 60, /*num_bits=*/3), 0b111);
EXPECT_EQ(GetFingerprintPrefix(0b1011ULL << 60, /*num_bits=*/0), 0b0);
EXPECT_EQ(GetFingerprintPrefix(0b1011ULL << 60, /*num_bits=*/1), 0b1);
EXPECT_EQ(GetFingerprintPrefix(0b1011ULL << 60, /*num_bits=*/2), 0b10);
EXPECT_EQ(GetFingerprintPrefix(0b1011ULL << 60, /*num_bits=*/3), 0b101);
}
TEST(CuckooUtilsTest, GetMinCollisionFreeFingerprintLength) {
const std::vector<uint64_t> fingerprints = {0b1, 0b11, 0b111};
EXPECT_EQ(GetMinCollisionFreeFingerprintLength(fingerprints,
/*use_prefix_bits=*/false),
3);
EXPECT_EQ(GetMinCollisionFreeFingerprintLength(fingerprints,
/*use_prefix_bits=*/true),
63);
// Check whether convenience method finds the same result.
bool use_prefix_bits;
EXPECT_EQ(GetMinCollisionFreeFingerprintPrefixOrSuffix(fingerprints,
&use_prefix_bits),
3);
EXPECT_EQ(use_prefix_bits, false);
}
TEST(CuckooUtilsTest, CheckWhetherAllBucketsOnlyContainSameSizeFingerprints) {
const std::vector<Fingerprint> fingerprints = {
{/*active=*/false, /*num_bits=*/1, /*fingerprint=*/0b0},
{/*active=*/true, /*num_bits=*/1, /*fingerprint=*/0b0},
{/*active=*/true, /*num_bits=*/2, /*fingerprint=*/0b0},
{/*active=*/true, /*num_bits=*/2, /*fingerprint=*/0b0}};
EXPECT_TRUE(CheckWhetherAllBucketsOnlyContainSameSizeFingerprints(
fingerprints, /*slots_per_bucket=*/1));
EXPECT_TRUE(CheckWhetherAllBucketsOnlyContainSameSizeFingerprints(
fingerprints, /*slots_per_bucket=*/2));
EXPECT_FALSE(CheckWhetherAllBucketsOnlyContainSameSizeFingerprints(
fingerprints, /*slots_per_bucket=*/4));
}
TEST(BucketTest, BucketInsertValue) {
Bucket bucket(/*num_slots=*/1);
// Insert should succeed, since `bucket` has capacity for another slot.
EXPECT_TRUE(bucket.InsertValue({/*value=*/42, /*num_buckets=*/1}));
// Insert should fail, since `bucket` is full.
EXPECT_FALSE(bucket.InsertValue({/*value=*/17, /*num_buckets=*/1}));
}
TEST(BucketTest, BucketContainsValue) {
CuckooValue value(/*value=*/42, /*num_buckets=*/1);
Bucket bucket(/*num_slots=*/1);
EXPECT_FALSE(bucket.ContainsValue(value));
bucket.InsertValue(value);
EXPECT_TRUE(bucket.ContainsValue(value));
EXPECT_FALSE(bucket.ContainsValue({/*value=*/17, /*num_buckets=*/1}));
}
TEST(BitmapRank, GetRankSingleBit) {
ASSERT_EQ(GetRank(CreateBitmap({0}), /*idx=*/0), 0);
ASSERT_EQ(GetRank(CreateBitmap({1}), /*idx=*/0), 0);
}
TEST(BitmapRank, GetRankAllZerosBitmap) {
const Bitmap64 bitmap = CreateBitmap({0, 0, 0});
ASSERT_EQ(GetRank(bitmap, /*idx=*/0), 0);
ASSERT_EQ(GetRank(bitmap, /*idx=*/1), 0);
ASSERT_EQ(GetRank(bitmap, /*idx=*/2), 0);
}
TEST(BitmapRank, GetRankAllOnesBitmap) {
const Bitmap64 bitmap = CreateBitmap({1, 1, 1});
ASSERT_EQ(GetRank(bitmap, /*idx=*/0), 0);
ASSERT_EQ(GetRank(bitmap, /*idx=*/1), 1);
ASSERT_EQ(GetRank(bitmap, /*idx=*/2), 2);
}
TEST(BitmapRank, GetRankRandomBitmap) {
const Bitmap64 bitmap = CreateBitmap({1, 0, 1});
ASSERT_EQ(GetRank(bitmap, /*idx=*/0), 0);
ASSERT_EQ(GetRank(bitmap, /*idx=*/1), 1);
ASSERT_EQ(GetRank(bitmap, /*idx=*/2), 1);
}
TEST(BitmapRank, GetRankWithRankLookupTable) {
const size_t num_bits = kRankBlockSize * 2 + kRankBlockSize / 10;
std::vector<int> bits;
bits.resize(num_bits);
for (size_t i = 0; i < num_bits; ++i) {
bits[i] = (i % 2 == 0) ? 1 : 0;
}
const Bitmap64 bitmap = CreateBitmap(bits);
Bitmap64 bitmap_with_rank = CreateBitmap(bits);
bitmap_with_rank.InitRankLookupTable();
for (size_t i = 0; i < num_bits; ++i) {
ASSERT_EQ(GetRank(bitmap, i), GetRank(bitmap_with_rank, i));
}
}
TEST(BitmapSelect, SelectSingleBit) {
size_t pos;
ASSERT_FALSE(SelectOne(CreateBitmap({0}), /*ith=*/0, &pos));
ASSERT_TRUE(SelectZero(CreateBitmap({0}), /*ith=*/0, &pos));
ASSERT_EQ(pos, 0);
ASSERT_TRUE(SelectOne(CreateBitmap({1}), /*ith=*/0, &pos));
ASSERT_EQ(pos, 0);
ASSERT_FALSE(SelectZero(CreateBitmap({1}), /*ith=*/0, &pos));
}
TEST(BitmapSelect, SelectAllZerosBitmap) {
const Bitmap64 bitmap = CreateBitmap({0, 0, 0});
size_t pos;
ASSERT_FALSE(SelectOne(bitmap, /*ith=*/0, &pos));
ASSERT_TRUE(SelectZero(bitmap, /*ith=*/0, &pos));
ASSERT_EQ(pos, 0);
ASSERT_TRUE(SelectZero(bitmap, /*ith=*/1, &pos));
ASSERT_EQ(pos, 1);
ASSERT_TRUE(SelectZero(bitmap, /*ith=*/2, &pos));
ASSERT_EQ(pos, 2);
}
TEST(BitmapSelect, SelectAllOnesBitmap) {
const Bitmap64 bitmap = CreateBitmap({1, 1, 1});
size_t pos;
ASSERT_TRUE(SelectOne(bitmap, /*ith=*/0, &pos));
ASSERT_EQ(pos, 0);
ASSERT_TRUE(SelectOne(bitmap, /*ith=*/1, &pos));
ASSERT_EQ(pos, 1);
ASSERT_TRUE(SelectOne(bitmap, /*ith=*/2, &pos));
ASSERT_EQ(pos, 2);
ASSERT_FALSE(SelectZero(bitmap, /*ith=*/0, &pos));
}
TEST(BitmapSelect, SelectRandomBitmap) {
const Bitmap64 bitmap = CreateBitmap({1, 0, 1});
size_t pos;
ASSERT_TRUE(SelectOne(bitmap, /*ith=*/0, &pos));
ASSERT_EQ(pos, 0);
ASSERT_TRUE(SelectOne(bitmap, /*ith=*/1, &pos));
ASSERT_EQ(pos, 2);
ASSERT_FALSE(SelectOne(bitmap, /*ith=*/2, &pos));
ASSERT_TRUE(SelectZero(bitmap, /*ith=*/0, &pos));
ASSERT_EQ(pos, 1);
}
TEST(EmptyBucketsBitmap, GetEmptyBucketsBitmap) {
const Bitmap64 empty_slots_bitmap = CreateBitmap({0, 1, 1, 1});
EXPECT_EQ(GetEmptyBucketsBitmap(empty_slots_bitmap, /*slots_per_bucket=*/1)
->ToString(),
CreateBitmap({0, 1, 1, 1}).ToString());
EXPECT_EQ(GetEmptyBucketsBitmap(empty_slots_bitmap, /*slots_per_bucket=*/2)
->ToString(),
CreateBitmap({0, 1}).ToString());
EXPECT_EQ(GetEmptyBucketsBitmap(empty_slots_bitmap, /*slots_per_bucket=*/4)
->ToString(),
CreateBitmap({0}).ToString());
}
TEST(BitmapSerialization, EncodeAndDecodeBitmap) {
const size_t num_bits = kRankBlockSize * 2 + kRankBlockSize / 10;
std::vector<int> bits;
bits.resize(num_bits);
for (size_t i = 0; i < num_bits; ++i) {
bits[i] = (i % 2 == 0) ? 1 : 0;
}
Bitmap64 bitmap = CreateBitmap(bits);
bitmap.InitRankLookupTable();
std::string encoded;
Bitmap64::DenseEncode(bitmap, &encoded);
const Bitmap64 decoded = Bitmap64::DenseDecode(encoded);
ASSERT_EQ(bitmap.bits(), decoded.bits());
for (size_t i = 0; i < num_bits; ++i) {
ASSERT_EQ(bitmap.Get(i), decoded.Get(i));
ASSERT_EQ(GetRank(bitmap, i), GetRank(decoded, i));
}
}
} // namespace ci