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
/
fingerprint_store_test.cc
131 lines (111 loc) · 5.12 KB
/
fingerprint_store_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
// 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: fingerprint_store_test.cc
// -----------------------------------------------------------------------------
#include "fingerprint_store.h"
#include <string>
#include "absl/types/span.h"
#include "cuckoo_utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
namespace ci {
constexpr uint32_t kMurmurHashConstant = 0x5bd1e995;
constexpr size_t kNumFingerprints = 1e3;
// **** Helper methods ****
// Creates `n` random fingerprints with different `lengths` such that all
// `slots_per_bucket` fingerprints in a bucket share the same length.
std::vector<Fingerprint> CreateRandomFingerprints(const size_t n,
const size_t slots_per_bucket,
std::vector<size_t> lengths) {
assert(lengths.size() > 0);
std::sort(lengths.begin(), lengths.end());
// Insert `length[i]` lengths.size()-i times such that shorter lengths are
// more likely to be chosen.
std::vector<size_t> lengths_to_draw_from;
for (size_t i = 0; i < lengths.size(); ++i) {
for (size_t j = 0; j < lengths.size() - i; ++j)
lengths_to_draw_from.push_back(lengths[i]);
}
std::vector<Fingerprint> fingerprints;
fingerprints.reserve(n);
for (size_t i = 0; i < n; i += slots_per_bucket) {
const size_t hash_bucket = i * kMurmurHashConstant;
const size_t num_bits =
lengths_to_draw_from[hash_bucket % lengths_to_draw_from.size()];
// Fill all slots.
for (size_t j = 0; j < slots_per_bucket; ++j) {
const size_t hash_slot = (i + j) * kMurmurHashConstant;
Fingerprint fp{/*active=*/(i + j) % 10 == 0 ? false : true, num_bits,
/*fingerprint=*/hash_slot % (1 << num_bits)};
fingerprints.push_back(fp);
}
}
return fingerprints;
}
// **** Test cases ****
// Creates fingerprints with different `lengths`, stores them in a
// FingerprintStore, and calls GetFingerprint(..) on each of them.
void CreateStoreAndGetFingerprints(const std::vector<size_t>& lengths,
const size_t slots_per_bucket,
const bool use_rle_to_encode_block_bitmaps) {
const std::vector<Fingerprint> fingerprints =
CreateRandomFingerprints(kNumFingerprints, slots_per_bucket, lengths);
const FingerprintStore store(fingerprints, slots_per_bucket,
use_rle_to_encode_block_bitmaps);
for (size_t i = 0; i < fingerprints.size(); ++i) {
const Fingerprint fp = store.GetFingerprint(i);
ASSERT_EQ(fp.active, fingerprints[i].active);
if (fp.active) {
ASSERT_EQ(fp.num_bits, fingerprints[i].num_bits);
ASSERT_EQ(fp.fingerprint, fingerprints[i].fingerprint);
}
}
}
TEST(FingerprintStore, GetFingerprintReturnsCorrectFingerprintSingleBlock) {
CreateStoreAndGetFingerprints(/*lengths=*/{8}, /*slots_per_bucket=*/1,
/*use_rle_to_encode_block_bitmaps=*/false);
}
TEST(FingerprintStore, GetFingerprintReturnsCorrectFingerprintSingleBlockRLE) {
CreateStoreAndGetFingerprints(/*lengths=*/{8}, /*slots_per_bucket=*/1,
/*use_rle_to_encode_block_bitmaps=*/true);
}
TEST(FingerprintStore, GetFingerprintReturnsCorrectFingerprintFiveBlocks) {
CreateStoreAndGetFingerprints(/*lengths=*/{1, 2, 4, 8, 16},
/*slots_per_bucket=*/1,
/*use_rle_to_encode_block_bitmaps=*/false);
}
TEST(FingerprintStore, GetFingerprintReturnsCorrectFingerprintFiveBlocksRLE) {
CreateStoreAndGetFingerprints(/*lengths=*/{1, 2, 4, 8, 16},
/*slots_per_bucket=*/1,
/*use_rle_to_encode_block_bitmaps=*/true);
}
TEST(FingerprintStore, GetFingerprintReturnsCorrectFingerprintZeroBits) {
CreateStoreAndGetFingerprints(/*lengths=*/{0},
/*slots_per_bucket=*/1,
/*use_rle_to_encode_block_bitmaps=*/false);
}
TEST(FingerprintStore, GetFingerprintReturnsCorrectFingerprintZeroAndOneBits) {
CreateStoreAndGetFingerprints(/*lengths=*/{0, 1},
/*slots_per_bucket=*/1,
/*use_rle_to_encode_block_bitmaps=*/false);
}
TEST(FingerprintStore,
GetFingerprintReturnsCorrectFingerprintTwoSlotsPerBucket) {
CreateStoreAndGetFingerprints(/*lengths=*/{1, 2, 4, 8, 16},
/*slots_per_bucket=*/2,
/*use_rle_to_encode_block_bitmaps=*/false);
}
} // namespace ci