File tree Expand file tree Collapse file tree 4 files changed +44
-0
lines changed Expand file tree Collapse file tree 4 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ option(XGRAMMAR_BUILD_CXX_TESTS "Build C++ tests" OFF)
19
19
option (XGRAMMAR_ENABLE_CPPTRACE
20
20
"Enable C++ trace (Now only support Linux, and RelWithDebugInfo or Debug build)" OFF
21
21
)
22
+ option (XGRAMMAR_ENABLE_COVERAGE "Enable code coverage with gcov" OFF )
22
23
23
24
set (XGRAMMAR_CUDA_ARCHITECTURES
24
25
native
@@ -93,3 +94,16 @@ if(XGRAMMAR_BUILD_CXX_TESTS)
93
94
include (GoogleTest)
94
95
gtest_discover_tests(xgrammar_test)
95
96
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 ()
Original file line number Diff line number Diff line change 1
1
set (CMAKE_BUILD_TYPE RelWithDebInfo)
2
2
set (XGRAMMAR_BUILD_PYTHON_BINDINGS ON )
3
+ set (XGRAMMAR_ENABLE_COVERAGE OFF )
3
4
set (XGRAMMAR_BUILD_CXX_TESTS OFF )
4
5
set (XGRAMMAR_ENABLE_CPPTRACE OFF )
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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"
You can’t perform that action at this time.
0 commit comments