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 17
/
Copy pathcuckoo_kicker_test.cc
136 lines (113 loc) · 4.74 KB
/
cuckoo_kicker_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
// 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_kicker_test.cc
// -----------------------------------------------------------------------------
#include "cuckoo_kicker.h"
#include <string>
#include "absl/types/span.h"
#include "cuckoo_utils.h"
#include "gtest/gtest.h"
namespace ci {
constexpr size_t kNumValues = 1e5;
constexpr size_t kSlotsPerBucket = 2;
constexpr size_t kMaxNumRetries = 10;
// **** Helper methods ****
std::vector<int> CreateValues(const size_t num_values) {
std::vector<int> values(num_values);
for (size_t i = 0; i < num_values; ++i) values[i] = i;
return values;
}
// Returns true if all `values` could be found in `buckets`. Sets
// `in_primary_ratio` according to the ratio of items residing in primary
// buckets.
bool LookupValuesInBuckets(const std::vector<Bucket>& buckets,
const std::vector<int>& values,
double* in_primary_ratio) {
size_t num_in_primary = 0;
for (const int value : values) {
CuckooValue cuckoo_value(value, buckets.size());
bool in_primary_flag;
if (!LookupValueInBuckets(buckets, cuckoo_value, &in_primary_flag))
return false;
num_in_primary += in_primary_flag;
}
*in_primary_ratio = static_cast<double>(num_in_primary) / values.size();
return true;
}
// **** Test cases ****
// Starts with the minimum number of buckets required for `kSlotsPerBucket`
// slots and `values.size()`. If construction fails, increases the number of
// buckets and retries (one additional bucket at a time).
std::vector<Bucket> DistributeValuesByKicking(const std::vector<int>& values,
const bool skew_kicking) {
size_t num_buckets = GetMinNumBuckets(kNumValues, kSlotsPerBucket);
for (size_t i = 0; i < kMaxNumRetries; ++i) {
std::vector<Bucket> buckets(num_buckets, Bucket(kSlotsPerBucket));
std::vector<CuckooValue> cuckoo_values;
cuckoo_values.reserve(values.size());
for (const int value : values)
cuckoo_values.push_back(CuckooValue(value, num_buckets));
CuckooKicker kicker(kSlotsPerBucket, absl::MakeSpan(buckets), skew_kicking);
if (kicker.InsertValues(cuckoo_values)) return buckets;
++num_buckets;
}
std::cerr << "Exceeded kMaxNumRetries: " << kMaxNumRetries << std::endl;
exit(EXIT_FAILURE);
}
TEST(CuckooKickerTest, InsertValues) {
const std::vector<int> values = CreateValues(kNumValues);
// Distribute values by kicking.
const std::vector<Bucket> buckets =
DistributeValuesByKicking(values, /*skew_kicking=*/false);
// Lookup values.
double in_primary_ratio;
ASSERT_TRUE(LookupValuesInBuckets(buckets, values, &in_primary_ratio));
ASSERT_GT(in_primary_ratio, 0.0);
}
TEST(CuckooKickerTest, InsertValuesWithSkewedKicking) {
const std::vector<int> values = CreateValues(kNumValues);
// Distribute values by kicking.
const std::vector<Bucket> buckets =
DistributeValuesByKicking(values, /*skew_kicking=*/true);
// Lookup values.
double in_primary_ratio;
ASSERT_TRUE(LookupValuesInBuckets(buckets, values, &in_primary_ratio));
ASSERT_GT(in_primary_ratio, 0.6);
}
TEST(CuckooKickerTest, CheckForDeterministicBehavior) {
const std::vector<int> values = CreateValues(kNumValues);
// Distribute values twice using skewed kicker.
const std::vector<Bucket> buckets =
DistributeValuesByKicking(values, /*skew_kicking=*/true);
const std::vector<Bucket> buckets2 =
DistributeValuesByKicking(values, /*skew_kicking=*/true);
// Check that both bucket vectors contain the same CuckooValues in `slots_`
// and `kicked_`.
ASSERT_EQ(buckets.size(), buckets2.size());
for (size_t i = 0; i < buckets.size(); ++i) {
ASSERT_EQ(buckets[i].slots_.size(), buckets2[i].slots_.size());
ASSERT_EQ(buckets[i].kicked_.size(), buckets2[i].kicked_.size());
for (size_t j = 0; j < buckets[i].slots_.size(); j++) {
EXPECT_EQ(buckets[i].slots_[j].ToString(),
buckets2[i].slots_[j].ToString());
}
for (size_t j = 0; j < buckets[i].kicked_.size(); j++) {
EXPECT_EQ(buckets[i].kicked_[j].ToString(),
buckets2[i].kicked_[j].ToString());
}
}
}
} // namespace ci