From 32f62c353416a155be747632abc887ad8a1ff7ef Mon Sep 17 00:00:00 2001 From: Jianning Wang Date: Thu, 18 Jun 2026 17:04:58 +0800 Subject: [PATCH] test(index): add tests for index param builders and fix builder setters --- .../core/interface/index_param_builders.h | 5 +-- tests/core/interface/index_interface_test.cc | 45 +++++++++++++++++++ 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/include/zvec/core/interface/index_param_builders.h b/src/include/zvec/core/interface/index_param_builders.h index 8b009c135..328e60b11 100644 --- a/src/include/zvec/core/interface/index_param_builders.h +++ b/src/include/zvec/core/interface/index_param_builders.h @@ -37,7 +37,7 @@ class BaseIndexParamBuilder { // : public virtual ~BaseIndexParamBuilder() = default; ActualIndexParamBuilderType &WithVersion(int version) { - param.version = version; + param->version = version; return static_cast(*this); } ActualIndexParamBuilderType &WithIndexType(IndexType index_type) { @@ -54,8 +54,7 @@ class BaseIndexParamBuilder { // : public } ActualIndexParamBuilderType &WithPreprocessParam( const PreprocessorParam &preprocess_param) { - param->preprocess_param = - std::make_shared(preprocess_param); + param->preprocess_param = preprocess_param; return static_cast(*this); } ActualIndexParamBuilderType &WithQuantizerParam( diff --git a/tests/core/interface/index_interface_test.cc b/tests/core/interface/index_interface_test.cc index 5fcfe37e4..c11950358 100644 --- a/tests/core/interface/index_interface_test.cc +++ b/tests/core/interface/index_interface_test.cc @@ -2380,6 +2380,51 @@ TEST(IndexInterface, IsDirtyBufferPool) { zvec::test_util::RemoveTestFiles(index_name); } +TEST(IndexInterface, BuilderSetsAllBaseFields) { + auto param = + FlatIndexParamBuilder() + .WithVersion(42) + .WithIndexType(IndexType::kFlat) + .WithMetricType(MetricType::kInnerProduct) + .WithDimension(128) + .WithDataType(DataType::DT_FP32) + .WithIsSparse(true) + .WithUseIDMap(false) + .WithUseExternalVector(true) + .WithPreprocessParam(PreprocessorParam(PreprocessorType::kPCA)) + .WithQuantizerParam(QuantizerParam(QuantizerType::kFP16)) + .Build(); + + ASSERT_NE(nullptr, param); + EXPECT_EQ(42, param->version); + EXPECT_EQ(IndexType::kFlat, param->index_type); + EXPECT_EQ(MetricType::kInnerProduct, param->metric_type); + EXPECT_EQ(128, param->dimension); + EXPECT_EQ(DataType::DT_FP32, param->data_type); + EXPECT_TRUE(param->is_sparse); + EXPECT_FALSE(param->use_id_map); + EXPECT_TRUE(param->use_external_vector); + EXPECT_EQ(PreprocessorType::kPCA, param->preprocess_param.type); + EXPECT_EQ(QuantizerType::kFP16, param->quantizer_param.type); +} + +TEST(IndexInterface, BuilderChainingReturnsCorrectType) { + HNSWIndexParamBuilder builder; + auto &ref = builder.WithVersion(1) + .WithMetricType(MetricType::kL2sq) + .WithDimension(64) + .WithM(16) + .WithEFConstruction(200); + auto param = ref.Build(); + + ASSERT_NE(nullptr, param); + EXPECT_EQ(1, param->version); + EXPECT_EQ(MetricType::kL2sq, param->metric_type); + EXPECT_EQ(64, param->dimension); + EXPECT_EQ(16, param->m); + EXPECT_EQ(200, param->ef_construction); +} + #if defined(__GNUC__) || defined(__GNUG__) #pragma GCC diagnostic pop #endif