This guide provides instructions on how to build and run the test suite for the cTensor library. Our tests are designed to ensure the correctness and reliability of the tensor operations and neural network functionalities.
Before you begin, make sure you have the following installed on your system:
- CMake: Version 3.10 or higher. CMake is used to configure and generate the build system.
- C Compiler: A C11-compliant compiler such as GCC, Clang, or MSVC (for Windows).
The tests are built as part of the main cTensor project using CMake. Follow these steps:
-
Navigate to the Root Directory: Open your terminal and change to the root directory of the cTensor project.
cd /path/to/cTensor -
Configure CMake: Create a build directory (e.g.,
build) and run CMake to configure the project. This step prepares the build files.cmake -B build -S .- The
-B buildoption specifies that the build files should be generated in a subdirectory namedbuild. - The
-S .option indicates that the source directory is the current directory (the project root).
- The
-
Build the Test Executable: Compile the project, specifically targeting the test executable
cten_tests.cmake --build build --target cten_tests
This command will compile the necessary source files and link them to create the
cten_testsexecutable. On Windows, this will becten_tests.exe.
Once the tests are built, you can run them in a couple of ways:
CTest is CMake's testing tool and is the recommended way to run tests as it integrates well with the CMake build process.
-
Navigate to the Build Directory:
cd build -
Run CTest:
ctest -C Debug --output-on-failure
-C Debugspecifies the build configuration (can beReleaseor other configurations if set up). Adjust if you used a different configuration.--output-on-failurewill display the output from tests only if they fail, keeping the console clean for successful runs.
You can also run the compiled test executable directly.
-
The executable is typically located in a subdirectory within your
buildfolder. The exact path might vary slightly based on your system and CMake generator:- On Linux/macOS:
build/bin/cten_testsorbuild/cten_tests - On Windows:
build\bin\Debug\cten_tests.exeorbuild\Debug\cten_tests.exe
- On Linux/macOS:
-
Execute the test program: For example, on Linux/macOS:
./build/bin/cten_tests
Or on Windows (from the project root):
.\build\bin\Debug\cten_tests.exe
After running the tests, a CSV file named cten_test_report.csv is generated in the build directory. This report contains a summary of the test results in a multi-column format that includes:
- Operator: The name of the operator being tested (e.g., "add")
- TestPoint: The specific test case name (e.g., "add_scalar")
- 1, 2, 3, ...: Numbered columns for sub-tests within each test case
Passing tests are marked with a / symbol, while failing tests include detailed error information.
The cTensor test suite uses a buffered CSV reporter system that stores test results in memory until all tests complete. The API includes:
int csv_reporter_init(const char *filename);
void csv_reporter_record_result(const char *operator_name, const char *test_point_name,
int sub_test_index, const char *result_detail);
void csv_reporter_close();Follow these steps to add a new operator test:
- Create a new file
test_<operator>.cin thetests/Operator/directory - Add a function declaration
void test_<operator>_operator();intests/cten_tests.c - Call this function from
main()intests/cten_tests.c
Each test file should follow this pattern:
#include "../test_utils.h"
void test_<operator>_operator() {
const char* op_name = "<operator>";
PoolId pool_id = 0;
cten_begin_malloc(pool_id);
// Test Case 1
{
const char* tc_name = "<operator>_<test_case>";
/* Create test tensors, perform operations */
// Use explicit sub-test indexing
compare_tensors(&actual_res, &expected_res, op_name, tc_name, 1, TEST_FLOAT_TOLERANCE);
}
// Multiple sub-tests for the same test case use the same tc_name but different sub_test_index values
{
const char* tc_name = "<operator>_<test_case>";
/* Different test parameters */
compare_tensors(&actual_res, &expected_res, op_name, tc_name, 2, TEST_FLOAT_TOLERANCE);
}
cten_end_malloc(pool_id);
}- Always specify explicit sub-test indices (1, 2, 3, etc.) when calling
compare_tensors() - Use the same
tc_namefor different sub-tests of the same test case - The
compare_tensors()function handles CSV reporting internally; don't make separate calls tocsv_reporter_record_result()
Happy testing! If you encounter any issues or have suggestions, please feel free to open an issue in the project repository.