Skip to content

Commit 4124681

Browse files
s-gobrielpre-commit-ci-lite[bot]github-actions[bot]
authored
add test cases for collection manager (#304)
* add test cases for collection manager * [pre-commit.ci lite] apply automatic fixes * Automated coverage update --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent a31dd69 commit 4124681

File tree

4 files changed

+167
-12
lines changed

4 files changed

+167
-12
lines changed

.github/coverage/cpp.develop.coverage_report.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ src/vcl/DescriptorSet.cc 179 126 70% 65,69-70,93-94
3838
src/vcl/DescriptorSetData.cc 55 47 85% 48,58,64,67,114,116-118
3939
src/vcl/Exception.cc 7 6 85% 38
4040
src/vcl/FaissDescriptorSet.cc 212 183 86% 83,115-116,132,167,187-188,204-205,224-225,238-239,245,258-259,261,272-273,279,305-306,308-309,311,374-375,381,405
41-
src/vcl/Filter.cc 132 90 68% 77,82,92-93,108-109,121-126,140-141,147-148,169-173,175,181-183,185,190-191,196-197,204-205,218-219,225-226,233,242,278,285,289-290
41+
src/vcl/Filter.cc 132 102 77% 77,82,92-93,108-109,121-126,140-141,147-148,190-191,196-197,204-205,218-219,225-226,233,242,278,285
4242
src/vcl/FlinngDescriptorSet.cc 149 109 73% 60-66,89,109-111,113-114,118-121,124,126,128,130,132,134-137,140-141,143-144,170-171,176-177,182,206,208,228,248,279
4343
src/vcl/Image.cc 924 692 74% 62,73-74,76-78,81-84,86,92,101,122-123,125,132-133,135,147,165,170,193,196-199,223,246,249-252,264,273,276-279,291,323,326-329,341,347,349-352,360-362,369,393-396,415,417,425,427,432,436,441,445,459,462,467-468,471-472,474,490,500,513,531,553-556,566,597,603-605,614-615,617,625,629,634,637-648,676-678,730,775-776,827,856-860,862-868,870,872-873,914,917-918,957-958,962-963,984,1003-1004,1006,1046-1048,1050-1054,1056-1060,1062-1066,1068-1072,1074-1078,1080-1083,1106,1127,1146-1154,1165-1166,1185-1204,1216-1217,1225,1236,1238-1240,1242-1244,1246,1264-1265,1267,1272-1273,1275,1296,1300,1303,1310,1325,1331,1340,1354,1379,1397,1480,1499
4444
src/vcl/KeyFrame.cc 303 244 80% 58,62,86,90,95,97,102,105-107,109-111,113,119,139,148,154,172,186,190,216,220,224,235,239,249,255,274,284,288,307,315,341,345,347,359,367,369,394,396,405,430,442,449,465,469,478,483,495,500,507,514,518,525,541,547,557,563
@@ -63,5 +63,5 @@ utils/src/kubernetes/KubeHelper.cc 183 37 20% 23-30,32-35,37
6363
utils/src/stats/SystemStats.cc 250 249 99% 455
6464
utils/src/timers/TimerMap.cc 82 74 90% 126,151,153,155-158,162
6565
------------------------------------------------------------------------------
66-
TOTAL 11565 8050 69%
66+
TOTAL 11565 8062 69%
6767
------------------------------------------------------------------------------
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
69.6066
1+
69.7103

include/vcl/Filter.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -302,19 +302,19 @@ class Filter {
302302
uint32_t get_num_keys() const { return num_keys_; }
303303
uint32_t get_ef() const { return extra_flag_; }
304304

305-
/**
306-
* @brief Lists the names of all currently managed filters.
307-
* @return A vector of strings, where each string is the name of an active
308-
* filter.
309-
* * Returns an empty vector if no filters are currently defined.
310-
* */
311-
std::vector<std::string> filter_list_all_names();
312-
313305
// --- create instances of derived classes based on FilterParameters::engine
314306
// ---
315307
static UniqueFilterPtr create_filter_instance(const FilterParameters *params);
316308
};
317309

310+
/**
311+
* @brief Lists the names of all currently managed filters.
312+
* @return A vector of strings, where each string is the name of an active
313+
* filter.
314+
* * Returns an empty vector if no filters are currently defined.
315+
* */
316+
std::vector<std::string> filter_list_all_names();
317+
318318
// =============================================================
319319
// Internal Filter Collection Management Class
320320
// This class is responsible for owning and managing the lifecycle

tests/unit_tests/Filter_test.cc

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,22 @@ class FilterTest : public ::testing::Test {
3939
VCL::FilterParameters params_ht_max;
4040
VCL::FilterParameters params_cache_max;
4141

42+
43+
static int filter_name_counter;
44+
std::vector<std::string> filter_names;
45+
46+
4247
void SetUp() override {
48+
49+
// Clear any filters from previous tests to ensure clean state
50+
// since filter_create adds to a global manager
51+
std::vector<std::string> existing_filter_names = VCL::filter_list_all_names();
52+
for (const std::string& name : existing_filter_names) {
53+
VCL::filter_free(VCL::filter_find_existing(name.c_str()));
54+
}
55+
56+
filter_names.clear();
57+
4358
// Common parameters for small filters
4459
params_ht_small.num_keys = SMALL_NUM_KEYS;
4560
params_ht_small.key_len = TEST_KEY_LEN;
@@ -78,17 +93,157 @@ class FilterTest : public ::testing::Test {
7893
}
7994

8095
void TearDown() override {
81-
// No explicit teardown needed for unique_ptr or stack objects
96+
// Free filters after test to prevent duplicate names
97+
std::vector<std::string> existing_filter_names = VCL::filter_list_all_names();
98+
for (const std::string& name : existing_filter_names) {
99+
VCL::filter_free(VCL::filter_find_existing(name.c_str()));
100+
}
101+
102+
filter_names.clear();
82103
}
83104
};
84105

106+
int FilterTest::filter_name_counter = 0;
85107

86108
/*
87109
********************************
88110
* I. Filter Creation Tests *
89111
********************************
90112
*/
91113

114+
// Test cases for filter listing and finding
115+
TEST_F(FilterTest, Filter_list_all_names_Empty) {
116+
std::vector<std::string> names = VCL::filter_list_all_names();
117+
EXPECT_TRUE(names.empty()) << "Expected no filters initially, but found " << names.size();
118+
}
119+
120+
TEST_F(FilterTest, Filter_list_all_names_MultipleFilters) {
121+
filter_name_counter = 0;
122+
123+
filter_names.emplace_back("TestCacheFilterMedium_" + std::to_string(filter_name_counter++));
124+
filter_names.emplace_back("TestCacheFilterMedium_" + std::to_string(filter_name_counter++));
125+
filter_names.emplace_back("TestCacheFilterMedium_" + std::to_string(filter_name_counter++));
126+
127+
VCL::FilterParameters p0 = params_cache_medium;
128+
p0.name = filter_names[0].c_str();
129+
VCL::Filter* filter0 = VCL::filter_create(&p0);
130+
131+
VCL::FilterParameters p1 = params_cache_medium;
132+
p1.name = filter_names[1].c_str();
133+
VCL::Filter* filter1 = VCL::filter_create(&p1);
134+
135+
VCL::FilterParameters p2 = params_cache_medium;
136+
p2.name = filter_names[2].c_str();
137+
VCL::Filter* filter2 = VCL::filter_create(&p2);
138+
139+
ASSERT_NE(filter0, nullptr);
140+
ASSERT_NE(filter1, nullptr);
141+
ASSERT_NE(filter2, nullptr);
142+
143+
std::vector<std::string> names = VCL::filter_list_all_names();
144+
EXPECT_EQ(names.size(), 3) << "Expected 3 filters, but found " << names.size();
145+
146+
std::vector<std::string> expected_names;
147+
expected_names.push_back(p0.name);
148+
expected_names.push_back(p1.name);
149+
expected_names.push_back(p2.name);
150+
151+
std::sort(names.begin(), names.end());
152+
std::sort(expected_names.begin(), expected_names.end());
153+
154+
// Compare the sorted lists
155+
for (size_t i = 0; i < expected_names.size(); ++i) {
156+
EXPECT_EQ(names[i], expected_names[i])
157+
<< "Mismatch at index " << i << ". Expected: " << expected_names[i] << ", Actual: " << names[i];
158+
}
159+
}
160+
161+
TEST_F(FilterTest, Filter_list_all_names_AfterFree) {
162+
VCL::FilterParameters p1 = params_ht_small;
163+
filter_names.emplace_back("TestHTFilterSmall_" + std::to_string(filter_name_counter++));
164+
p1.name = filter_names.back().c_str();
165+
VCL::Filter* filter1 = VCL::filter_create(&p1);
166+
167+
VCL::FilterParameters p2 = params_cache_medium;
168+
filter_names.emplace_back("TestCacheFilterMedium_" + std::to_string(filter_name_counter++));
169+
p2.name = filter_names.back().c_str();
170+
VCL::Filter* filter2 = VCL::filter_create(&p2);
171+
172+
ASSERT_NE(filter1, nullptr);
173+
ASSERT_NE(filter2, nullptr);
174+
175+
std::vector<std::string> names_before_free = VCL::filter_list_all_names();
176+
EXPECT_EQ(names_before_free.size(), 2);
177+
178+
VCL::filter_free(filter1); // Free filter1
179+
180+
std::vector<std::string> names_after_free_1 = VCL::filter_list_all_names();
181+
EXPECT_EQ(names_after_free_1.size(), 1);
182+
EXPECT_EQ(names_after_free_1[0], p2.name); // Only p2.name should remain
183+
184+
VCL::filter_free(filter2); // Free filter2
185+
186+
std::vector<std::string> names_after_free_2 = VCL::filter_list_all_names();
187+
EXPECT_TRUE(names_after_free_2.empty());
188+
189+
}
190+
191+
TEST_F(FilterTest, Filter_find_existing_Success) {
192+
VCL::FilterParameters p1 = params_ht_small;
193+
filter_names.emplace_back("TestHTFilterSmall_" + std::to_string(filter_name_counter++));
194+
p1.name = filter_names.back().c_str();
195+
VCL::Filter* filter1 = VCL::filter_create(&p1);
196+
ASSERT_NE(filter1, nullptr);
197+
198+
VCL::Filter* found_filter = VCL::filter_find_existing(p1.name);
199+
ASSERT_NE(found_filter, nullptr);
200+
EXPECT_EQ(found_filter, filter1);
201+
EXPECT_STREQ(found_filter->get_name(), p1.name);
202+
203+
// Test a different filter type
204+
VCL::FilterParameters p2 = params_cache_medium;
205+
filter_names.emplace_back("TestCacheFilterMedium_" + std::to_string(filter_name_counter++));
206+
p2.name = filter_names.back().c_str();
207+
VCL::Filter* filter2 = VCL::filter_create(&p2);
208+
ASSERT_NE(filter2, nullptr);
209+
VCL::Filter* found_filter2 = VCL::filter_find_existing(p2.name);
210+
ASSERT_NE(found_filter2, nullptr);
211+
EXPECT_EQ(found_filter2, filter2);
212+
}
213+
214+
TEST_F(FilterTest, Filter_find_existing_NotFound) {
215+
// No filters created yet (or existing ones cleared by SetUp)
216+
std::string non_existent_name = "NonExistentFilter_" + std::to_string(filter_name_counter++);
217+
VCL::Filter* found_filter = VCL::filter_find_existing(non_existent_name.c_str());
218+
EXPECT_EQ(found_filter, nullptr);
219+
220+
// Create one filter, then search for a different non-existent one
221+
VCL::FilterParameters p_temp = params_ht_small;
222+
filter_names.emplace_back("TempFilter_" + std::to_string(filter_name_counter++));
223+
p_temp.name = filter_names.back().c_str();
224+
VCL::filter_create(&p_temp);
225+
226+
std::string another_non_existent_name = "AnotherNonExistentFilter_" + std::to_string(filter_name_counter++);
227+
found_filter = VCL::filter_find_existing(another_non_existent_name.c_str());
228+
EXPECT_EQ(found_filter, nullptr);
229+
}
230+
231+
TEST_F(FilterTest, Filter_find_existing_NullName) {
232+
VCL::Filter* found_filter = VCL::filter_find_existing(nullptr);
233+
EXPECT_EQ(found_filter, nullptr);
234+
}
235+
236+
237+
TEST_F(FilterTest, Filter_GetName) {
238+
VCL::Filter* ht_filter = VCL::filter_create(&params_ht_small);
239+
ASSERT_NE(ht_filter, nullptr);
240+
ASSERT_TRUE(ht_filter->is_valid());
241+
EXPECT_STREQ(ht_filter->get_name(), params_ht_small.name);
242+
VCL::filter_free(ht_filter); // Clean up explicitly
243+
244+
}
245+
246+
92247
TEST_F(FilterTest, CuckooHTFilter_Creation_Success) {
93248
std::unique_ptr<VCL::CuckooHTFilter> filter;
94249
ASSERT_NO_THROW(filter = std::make_unique<VCL::CuckooHTFilter>(params_ht_small));

0 commit comments

Comments
 (0)