Initial commit

This commit is contained in:
yichuan520030910320
2025-06-30 09:05:05 +00:00
commit 46f6cc100b
1231 changed files with 278432 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT license.
set(CMAKE_COMPILE_WARNING_AS_ERROR ON)
find_package(Boost COMPONENTS unit_test_framework)
# For Windows, fall back to nuget version if find_package didn't find it.
if (MSVC AND NOT Boost_FOUND)
set(DISKANN_BOOST_INCLUDE "${DISKANN_MSVC_PACKAGES}/boost/lib/native/include")
# Multi-threaded static library.
set(UNIT_TEST_FRAMEWORK_LIB_PATTERN "${DISKANN_MSVC_PACKAGES}/boost_unit_test_framework-vc${MSVC_TOOLSET_VERSION}/lib/native/libboost_unit_test_framework-vc${MSVC_TOOLSET_VERSION}-mt-x64-*.lib")
file(GLOB DISKANN_BOOST_UNIT_TEST_FRAMEWORK_LIB ${UNIT_TEST_FRAMEWORK_LIB_PATTERN})
set(UNIT_TEST_FRAMEWORK_DLIB_PATTERN "${DISKANN_MSVC_PACKAGES}/boost_unit_test_framework-vc${MSVC_TOOLSET_VERSION}/lib/native/libboost_unit_test_framework-vc${MSVC_TOOLSET_VERSION}-mt-gd-x64-*.lib")
file(GLOB DISKANN_BOOST_UNIT_TEST_FRAMEWORK_DLIB ${UNIT_TEST_FRAMEWORK_DLIB_PATTERN})
if (EXISTS ${DISKANN_BOOST_INCLUDE} AND EXISTS ${DISKANN_BOOST_UNIT_TEST_FRAMEWORK_LIB} AND EXISTS ${DISKANN_BOOST_UNIT_TEST_FRAMEWORK_DLIB})
set(Boost_FOUND ON)
set(Boost_INCLUDE_DIR ${DISKANN_BOOST_INCLUDE})
add_library(Boost::unit_test_framework STATIC IMPORTED)
set_target_properties(Boost::unit_test_framework PROPERTIES IMPORTED_LOCATION_RELEASE "${DISKANN_BOOST_UNIT_TEST_FRAMEWORK_LIB}")
set_target_properties(Boost::unit_test_framework PROPERTIES IMPORTED_LOCATION_DEBUG "${DISKANN_BOOST_UNIT_TEST_FRAMEWORK_DLIB}")
message(STATUS "Falling back to using Boost from the nuget package")
else()
message(WARNING "Couldn't find Boost. Was looking for ${DISKANN_BOOST_INCLUDE} and ${UNIT_TEST_FRAMEWORK_LIB_PATTERN}")
endif()
endif()
if (NOT Boost_FOUND)
message(FATAL_ERROR "Couldn't find Boost dependency")
endif()
set(DISKANN_UNIT_TEST_SOURCES main.cpp index_write_parameters_builder_tests.cpp)
add_executable(${PROJECT_NAME}_unit_tests ${DISKANN_SOURCES} ${DISKANN_UNIT_TEST_SOURCES})
target_link_libraries(${PROJECT_NAME}_unit_tests ${PROJECT_NAME} ${DISKANN_TOOLS_TCMALLOC_LINK_OPTIONS} Boost::unit_test_framework)
add_test(NAME ${PROJECT_NAME}_unit_tests COMMAND ${PROJECT_NAME}_unit_tests)

View File

@@ -0,0 +1,11 @@
# Unit Test project
This unit test project is based on the [boost unit test framework](https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/index.html). Below are the simple steps to add new unit test, you could find more usage from the [boost unit test document](https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/index.html).
## How to add unit test
- Create new [BOOST_AUTO_TEST_SUITE](https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/boost_test/utf_reference/test_org_reference/test_org_boost_auto_test_suite.html) for each class in an individual cpp file
- Add [BOOST_AUTO_TEST_CASE](https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/boost_test/utf_reference/test_org_reference/test_org_boost_auto_test_case.html) for each test case in the [BOOST_AUTO_TEST_SUITE](https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/boost_test/utf_reference/test_org_reference/test_org_boost_auto_test_suite.html)
- Update the [CMakeLists.txt](CMakeLists.txt) file to add the new cpp file to the test project

View File

@@ -0,0 +1,58 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include <boost/test/unit_test.hpp>
#include "parameters.h"
BOOST_AUTO_TEST_SUITE(IndexWriteParametersBuilder_tests)
BOOST_AUTO_TEST_CASE(test_build)
{
uint32_t search_list_size = rand();
uint32_t max_degree = rand();
float alpha = (float)rand();
uint32_t filter_list_size = rand();
uint32_t max_occlusion_size = rand();
bool saturate_graph = true;
diskann::IndexWriteParametersBuilder builder(search_list_size, max_degree);
builder.with_alpha(alpha)
.with_filter_list_size(filter_list_size)
.with_max_occlusion_size(max_occlusion_size)
.with_num_threads(0)
.with_saturate_graph(saturate_graph);
{
auto parameters = builder.build();
BOOST_TEST(search_list_size == parameters.search_list_size);
BOOST_TEST(max_degree == parameters.max_degree);
BOOST_TEST(alpha == parameters.alpha);
BOOST_TEST(filter_list_size == parameters.filter_list_size);
BOOST_TEST(max_occlusion_size == parameters.max_occlusion_size);
BOOST_TEST(saturate_graph == parameters.saturate_graph);
BOOST_TEST(parameters.num_threads > (uint32_t)0);
}
{
uint32_t num_threads = rand() + 1;
saturate_graph = false;
builder.with_num_threads(num_threads).with_saturate_graph(saturate_graph);
auto parameters = builder.build();
BOOST_TEST(search_list_size == parameters.search_list_size);
BOOST_TEST(max_degree == parameters.max_degree);
BOOST_TEST(alpha == parameters.alpha);
BOOST_TEST(filter_list_size == parameters.filter_list_size);
BOOST_TEST(max_occlusion_size == parameters.max_occlusion_size);
BOOST_TEST(saturate_graph == parameters.saturate_graph);
BOOST_TEST(num_threads == parameters.num_threads);
}
}
BOOST_AUTO_TEST_SUITE_END()

View File

@@ -0,0 +1,6 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#define BOOST_TEST_MODULE diskann_unit_tests
#include <boost/test/unit_test.hpp>