Skip to content

Commit b87ed7f

Browse files
[Script] Add a tool to test the code coverage. (#325)
This PR adds a script to test the code coverage. --------- Co-authored-by: Yixin Dong <[email protected]>
1 parent 563bbd1 commit b87ed7f

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

CMakeLists.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" OFF)
1919
option(XGRAMMAR_ENABLE_CPPTRACE
2020
"Enable C++ trace (Now only support Linux, and RelWithDebugInfo or Debug build)" OFF
2121
)
22+
option(XGRAMMAR_ENABLE_COVERAGE "Enable code coverage with gcov" OFF)
2223

2324
set(XGRAMMAR_CUDA_ARCHITECTURES
2425
native
@@ -93,3 +94,16 @@ if(XGRAMMAR_BUILD_CXX_TESTS)
9394
include(GoogleTest)
9495
gtest_discover_tests(xgrammar_test)
9596
endif()
97+
98+
if(XGRAMMAR_ENABLE_COVERAGE)
99+
target_link_libraries(xgrammar_bindings PRIVATE gcov)
100+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage")
101+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fprofile-arcs -ftest-coverage")
102+
103+
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
104+
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} --coverage")
105+
106+
if(XGRAMMAR_BUILD_PYTHON_BINDINGS)
107+
set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} --coverage")
108+
endif()
109+
endif()

cmake/config.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
set(CMAKE_BUILD_TYPE RelWithDebInfo)
22
set(XGRAMMAR_BUILD_PYTHON_BINDINGS ON)
3+
set(XGRAMMAR_ENABLE_COVERAGE OFF)
34
set(XGRAMMAR_BUILD_CXX_TESTS OFF)
45
set(XGRAMMAR_ENABLE_CPPTRACE OFF)

docs/how_to/code_coverage.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.. _how-to-test-code-coverage:
2+
3+
Code Coverage Test
4+
==================
5+
6+
The script ``run_coverage.sh`` offers a way to test the code coverage
7+
of the XGrammar library.
8+
9+
To run the coverage test, please follow these steps:
10+
11+
#. In ``config.cmake``, set the variable ``XGRAMMAR_ENABLE_COVERAGE`` to ``ON``.
12+
#. Compile the XGrammar library with the configured settings.
13+
#. Run the script ``run_coverage.sh`` in the root directory of the XGrammar library.
14+
15+
After running the script, you will find the coverage report in the
16+
``coverage_report`` directory.
17+
18+
You can modify the script to change the test cases or the output directory.

scripts/run_coverage.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
3+
# Usage: bash ./scripts/run_coverage.sh
4+
lcov --directory . --zerocounters
5+
ctest --test-dir build -V --timeout 60 --stop-on-failure
6+
pytest
7+
8+
lcov --gcov-tool /usr/bin/gcov-13 --directory . --capture --output-file coverage.info --ignore-errors mismatch,gcov
9+
genhtml coverage.info --output-directory coverage_report --ignore-errors version
10+
rm coverage.info
11+
echo "Coverage report generated at: $(pwd)/coverage_report/index.html"

0 commit comments

Comments
 (0)