-
Notifications
You must be signed in to change notification settings - Fork 39
Add C API tests for error handling, indexing, and search parameters #306
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: rfsaliev/c-api-index-num-threads
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -661,20 +661,32 @@ extern "C" size_t svs_index_dynamic_delete_points( | |||||
| svs_index_h index, const size_t* ids, size_t num_ids, svs_error_h out_err | ||||||
| ) { | ||||||
| using namespace svs::c_runtime; | ||||||
| return wrap_exceptions( | ||||||
| std::shared_ptr<DynamicIndex> dynamic_index_ptr; | ||||||
| auto result = wrap_exceptions( | ||||||
| [&]() { | ||||||
| EXPECT_ARG_NOT_NULL(index); | ||||||
| EXPECT_ARG_NOT_NULL(ids); | ||||||
| EXPECT_ARG_GT_THAN(num_ids, 0); | ||||||
| auto dynamic_index_ptr = std::dynamic_pointer_cast<DynamicIndex>(index->impl); | ||||||
| dynamic_index_ptr = std::dynamic_pointer_cast<DynamicIndex>(index->impl); | ||||||
| INVALID_ARGUMENT_IF( | ||||||
| dynamic_index_ptr == nullptr, "Index does not support dynamic updates" | ||||||
| ); | ||||||
| return dynamic_index_ptr->delete_points(std::span(ids, num_ids)); | ||||||
| return 0; // return 0 for success, actual deletion happens in the next | ||||||
| // wrap_exceptions call | ||||||
| }, | ||||||
| out_err, | ||||||
| static_cast<size_t>(-1) | ||||||
| ); | ||||||
| if (result != 0) { | ||||||
| return result; | ||||||
| } | ||||||
| // Call delete_points in a separate wrap_exceptions to return 0 if no entries are | ||||||
| // deleted. | ||||||
| return wrap_exceptions( | ||||||
| [&]() { return dynamic_index_ptr->delete_points(std::span(ids, num_ids)); }, | ||||||
| out_err, | ||||||
| 0 | ||||||
|
||||||
| 0 | |
| static_cast<size_t>(-1) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,105 @@ | ||||||
| # Copyright 2026 Intel Corporation | ||||||
| # | ||||||
| # 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 | ||||||
| # | ||||||
| # http://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. | ||||||
|
|
||||||
| set(TARGET_NAME svs_c_api_test) | ||||||
|
||||||
| set(TARGET_NAME svs_c_api_test) | |
| set(TARGET_NAME svs_c_api_tests) |
Copilot
AI
Mar 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This CMakeLists supports find_package(Catch2 ...), but the test sources use the prefixed Catch2 macros (CATCH_TEST_CASE, CATCH_SECTION, etc.). Those macros are only available when CATCH_CONFIG_PREFIX_ALL is enabled for the consumer compile, which isn’t guaranteed for a pre-installed Catch2 found via find_package. To avoid build failures when Catch2_FOUND, explicitly add the needed compile definition(s) to ${TARGET_NAME} (or avoid find_package and always FetchContent with the required configuration).
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,164 @@ | ||||||
| # C API Tests | ||||||
|
|
||||||
| This directory contains comprehensive tests for the SVS C API using the Catch2 testing framework. | ||||||
|
|
||||||
| ## Test Structure | ||||||
|
|
||||||
| The tests are organized into separate files by functionality: | ||||||
|
|
||||||
| - **c_api_error.cpp**: Tests for error handling functionality | ||||||
| - **c_api_algorithm.cpp**: Tests for algorithm creation and configuration (Vamana) | ||||||
| - **c_api_storage.cpp**: Tests for storage configurations (Simple, LeanVec, LVQ, SQ) | ||||||
| - **c_api_search_params.cpp**: Tests for search parameter creation and configuration | ||||||
| - **c_api_index_builder.cpp**: Tests for index builder creation and configuration | ||||||
| - **c_api_index.cpp**: Tests for index building, searching, and basic operations | ||||||
| - **c_api_dynamic_index.cpp**: Tests for dynamic index operations (add, delete, consolidate, compact) | ||||||
|
|
||||||
| Note: The main() function is provided by Catch2::Catch2WithMain automatically. | ||||||
|
|
||||||
| ## Building the Tests | ||||||
|
|
||||||
| The tests are built as part of the C API build process. To build them: | ||||||
|
|
||||||
| ```bash | ||||||
| # From the build directory | ||||||
| cmake -DSVS_BUILD_C_API_TESTS=ON .. | ||||||
| make svs_c_api_tests | ||||||
| ``` | ||||||
|
Comment on lines
+21
to
+27
|
||||||
|
|
||||||
| To disable building tests: | ||||||
|
|
||||||
| ```bash | ||||||
| cmake -DSVS_BUILD_C_API_TESTS=OFF .. | ||||||
| ``` | ||||||
|
|
||||||
| ## Running the Tests | ||||||
|
|
||||||
| ### Run all tests | ||||||
|
|
||||||
| ```bash | ||||||
| ./svs_c_api_tests | ||||||
| ``` | ||||||
|
|
||||||
| ### Run specific test cases | ||||||
|
|
||||||
| ```bash | ||||||
| # Run error handling tests only | ||||||
| ./svs_c_api_tests "[c_api][error]" | ||||||
|
|
||||||
| # Run algorithm tests only | ||||||
| ./svs_c_api_tests "[c_api][algorithm]" | ||||||
|
|
||||||
| # Run all index tests | ||||||
| ./svs_c_api_tests "[c_api][index]" | ||||||
|
|
||||||
| # Run dynamic index tests | ||||||
| ./svs_c_api_tests "[c_api][dynamic]" | ||||||
| ``` | ||||||
|
|
||||||
| ### Run with verbose output | ||||||
|
|
||||||
| ```bash | ||||||
| ./svs_c_api_tests -s | ||||||
| ``` | ||||||
|
|
||||||
| ### List all available tests | ||||||
|
|
||||||
| ```bash | ||||||
| ./svs_c_api_tests --list-tests | ||||||
| ``` | ||||||
|
|
||||||
| ### Run with CTest | ||||||
|
|
||||||
| ```bash | ||||||
| ctest -R svs_c_api_tests | ||||||
| ``` | ||||||
|
|
||||||
| ## Test Coverage | ||||||
|
|
||||||
| The tests cover the following aspects of the C API: | ||||||
|
|
||||||
| ### Error Handling | ||||||
|
|
||||||
| - Error handle creation and cleanup | ||||||
| - Error state checking | ||||||
| - Error codes and messages | ||||||
| - NULL error handle support | ||||||
|
|
||||||
| ### Algorithm Configuration | ||||||
|
|
||||||
| - Vamana algorithm creation | ||||||
| - Parameter getters and setters (graph_degree, build_window_size, alpha, search_history) | ||||||
| - Invalid parameter handling | ||||||
|
|
||||||
| ### Storage Configuration | ||||||
|
|
||||||
| - Simple storage (Float32, Float16, Int8, Uint8) | ||||||
| - LeanVec storage (various primary/secondary combinations) | ||||||
| - LVQ storage (with and without residual) | ||||||
| - Scalar Quantization storage | ||||||
|
|
||||||
| ### Search Parameters | ||||||
|
|
||||||
| - Vamana search parameter creation | ||||||
| - Various window sizes | ||||||
|
|
||||||
| ### Index Builder | ||||||
|
|
||||||
| - Index builder creation with different metrics (Euclidean, Cosine, Dot Product) | ||||||
| - Storage configuration | ||||||
| - Thread pool configuration (Native, OMP, Custom) | ||||||
|
|
||||||
| ### Index Operations | ||||||
|
|
||||||
| - Index building from data | ||||||
| - Searching with queries | ||||||
| - Different K values | ||||||
| - Distance calculation | ||||||
| - Vector reconstruction | ||||||
| - Thread count management | ||||||
|
|
||||||
| ### Dynamic Index Operations | ||||||
|
|
||||||
| - Dynamic index building with/without explicit IDs | ||||||
| - Adding points | ||||||
| - Deleting points | ||||||
| - ID existence checking | ||||||
| - Index consolidation | ||||||
| - Index compaction | ||||||
| - Search after modifications | ||||||
|
|
||||||
| ## Test Patterns | ||||||
|
|
||||||
| The tests follow the patterns established in the SVS project: | ||||||
|
|
||||||
| 1. Use `CATCH_TEST_CASE` for test case definitions | ||||||
| 2. Use `CATCH_SECTION` for test subsections | ||||||
| 3. Use `CATCH_REQUIRE` for assertions | ||||||
| 4. Clean up all resources (free handles) after each test | ||||||
| 5. Test both success and error paths | ||||||
| 6. Test with and without NULL error handles | ||||||
|
|
||||||
| ## Adding New Tests | ||||||
|
|
||||||
| When adding new tests: | ||||||
|
|
||||||
| 1. Create a new `.cpp` file or add to an existing one | ||||||
| 2. Follow the existing structure and naming conventions | ||||||
| 3. Include proper copyright header | ||||||
| 4. Use appropriate test tags: `[c_api][functionality]` | ||||||
| 5. Add the new test file to `CMakeLists.txt` if needed | ||||||
| 6. Clean up all allocated resources | ||||||
| 7. Test both success and error conditions | ||||||
|
|
||||||
| ## Dependencies | ||||||
|
|
||||||
| - Catch2 v3.x (automatically fetched if not found) | ||||||
| - SVS C API library | ||||||
| - C++17 or later compiler | ||||||
|
||||||
| - C++17 or later compiler | |
| - C++20 or later compiler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SVS_BUILD_C_API_TESTSdefaults to ON, which means building the C API will build the test executable (and potentially FetchContent-download Catch2) by default. This differs from the repo’s other test knobs (e.g.,SVS_BUILD_TESTS/SVS_BUILD_RUNTIME_TESTS, which are opt-in) and can be surprising for consumers embeddingbindings/c. Consider defaulting this option to OFF (or tying it to the parent project’s test option) so tests are only built when explicitly requested.