General work flow of writing a test case for the Galerkin BEM project
With the help of my college classmate, we’ve setup Catch2
based unit test platform for the Galerkin BEM project. This is crucial for guaranteeing the correctness of a scientific software library which is gradually growing and becoming complicated. After several months of getting familiar with it, the work flow of writing and using a C++ test case becomes solidified, which is introduced in this article.
Assume the test case name is foo
, which is created inside the folder tests/foo
.
-
Call
${CMAKE_CURRENT_SOURCE_DIR}/gen_data.m
Octave script from C++ to produce the input data of the test case in the file${CMAKE_CURRENT_SOURCE_DIR}/foo.input
. This is optional.N.B. The value of
${CMAKE_CURRENT_SOURCE_DIR}
can be accessed from C++ via the constantSOURCE_DIR
. This is setup in theCMakeLists.txt
file of the test casefoo
as a definition associated with the target. Then C++ can locate the Octave scriptgen_data.m
and execute it.set(_target test-foo) target_compile_definitions(${_target} PUBLIC SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
- Test case generate results into the file
${CMAKE_CURRENT_SOURCE_DIR}/foo.output
in the format which can be loaded by Octave. - Call
${CMAKE_CURRENT_SOURCE_DIR}/process.m
Octave script from C++. Inprocess.m
,- Load the result file
${CMAKE_CURRENT_SOURCE_DIR}/foo.output
. - Run the Octave implementation of the C++ algorithm.
- Compare the C++ and Octave results. Usually, relative error is computed.
- Load the result file
- C++ check the relative error computed by
process.m
viaCatch2
.
After each compilation of the test case, ${CMAKE_CURRENT_SOURCE_DIR}/process.m
and ${CMAKE_CURRENT_SOURCE_DIR}/draw.m
will be automatically copied into the folder of the test case binary file by CMake.
add_custom_command(
TARGET ${_target}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/process.m
${CMAKE_CURRENT_BINARY_DIR}/process.m)
add_custom_command(
TARGET ${_target}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/draw.m
${CMAKE_CURRENT_BINARY_DIR}/draw.m)
Then, the test case can be manually run in this folder and the Octave scripts process.m
and draw.m
can be executed for validation and visualization.