Skip to content

Commit d01f485

Browse files
committed
fix(test): resolve ReadOnlyGraph.Search test failure
The test had two issues: 1. The graph was not properly connected - each vertex only connected to vertices {0,1,2,3}, making vertices 4-7 unreachable from entry vertex 0. Fixed by connecting each vertex to its adjacent neighbors (i-2, i-1, i+1, i+2) to ensure full connectivity. 2. ResultSet::top() returns the worst (highest distance) result in a max-heap, not the best. Changed the test to use std::min_element to find the result with minimum distance.
1 parent c86deed commit d01f485

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

cpp/deglib/include/builder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22

33
#include <iostream>
4+
#include <iomanip>
45
#include <random>
56
#include <chrono>
67
#include <thread>

cpp/test/src/unit/graph/test_readonly_graph.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,15 +229,27 @@ TEST(ReadOnlyGraph, Search) {
229229
mutable_graph.addVertex(i, make_float_bytes(v).get());
230230
}
231231

232-
// fully connected graph (neighbors must be sorted ascending)
232+
// connect each vertex to its neighbors so the graph is connected
233+
// (neighbors must be sorted ascending)
233234
for (int i = 0; i < 8; ++i) {
234235
std::vector<uint32_t> neighbors;
235236
std::vector<float> weights;
236-
for (int j = 0; j < 8; j++) {
237-
if (j < 4) { // only first 4 neighbors fit
238-
neighbors.push_back(static_cast<uint32_t>(j));
239-
weights.push_back(static_cast<float>(std::abs(i - j)));
240-
}
237+
// connect to previous and next vertices to ensure connectivity
238+
if (i > 0) {
239+
neighbors.push_back(static_cast<uint32_t>(i - 1));
240+
weights.push_back(1.0f);
241+
}
242+
if (i > 1) {
243+
neighbors.push_back(static_cast<uint32_t>(i - 2));
244+
weights.push_back(1.0f);
245+
}
246+
if (i < 7) {
247+
neighbors.push_back(static_cast<uint32_t>(i + 1));
248+
weights.push_back(1.0f);
249+
}
250+
if (i < 6) {
251+
neighbors.push_back(static_cast<uint32_t>(i + 2));
252+
weights.push_back(1.0f);
241253
}
242254
// fill remaining with self
243255
while (neighbors.size() < 4) {
@@ -263,8 +275,11 @@ TEST(ReadOnlyGraph, Search) {
263275
auto results = graph.search({0}, make_float_bytes(query).get(), 0.1f, 3);
264276

265277
EXPECT_GT(results.size(), 0u);
266-
auto best = results.top();
267-
EXPECT_EQ(best.getInternalIndex(), graph.getInternalIndex(4));
278+
// ResultSet is a max-heap (std::less), so top() returns the worst of the top-k results.
279+
// Find the best (minimum distance) result by iterating through all results.
280+
auto best = std::min_element(results.begin(), results.end(),
281+
[](const auto& a, const auto& b) { return a.getDistance() < b.getDistance(); });
282+
EXPECT_EQ(best->getInternalIndex(), graph.getInternalIndex(4));
268283
}
269284

270285
TEST(ReadOnlyGraph, HasPath) {

0 commit comments

Comments
 (0)