11<a id =" top " ></a >
22
3- # Compile time testing
3+ # Compile- time testing
44
55Catch2 provides a way to test code during compilation. This is
66useful for ensuring the code does not lead to undefined behavior.
77
88> [ !IMPORTANT]
99> This feature is only available with ** C++17**
1010
11+ ## Why should I test code at compile-time ?
12+
13+ Testing code during compilation is the only way for ensuring the code does
14+ not lead to [ undefined behavior] ( https://en.cppreference.com/w/cpp/language/ub.html ) .
15+ This is critical to make your program safe, prevent unexpected crashes and worse
16+ consequences of undefined behavior.
17+
18+ ## How ?
19+
20+ Catch2 made it easy to test code at compile-time.
21+
1122The usage is pretty similar to testing at runtime:
1223
1324| Runtime macro | Compile time macro |
1425| -----------------| ---------------------------|
1526| ` SECTION ` | ` CONSTEXPR_SECTION ` |
1627| ` REQUIRE ` | ` CONSTEXPR_REQUIRE ` |
1728| ` REQUIRE_FALSE ` | ` CONSTEXPR_REQUIRE_FALSE ` |
29+ | ` CHECK ` | - |
30+ | ` CHECK_FALSE ` | - |
1831
1932``` c++
2033TEST_CASE ("My compile time test")
2134{
22- CONSTEXPR_SECTION("Use it the same way as SECTION ! ")
35+ CONSTEXPR_SECTION("std::array subscript and size ")
2336 {
2437 std::array v = {1, 2, 3, 4, 5};
2538 CONSTEXPR_REQUIRE( v.size() == 5 );
@@ -31,27 +44,61 @@ TEST_CASE("My compile time test")
3144
3245All code inside the `CONSTEXPR_SECTION` will be evaluated during compilation.
3346If any of the `CONSTEXPR_REQUIRE` or `CONSTEXPR_REQUIRE_FALSE` fails,
34- it will cause a compilation error.
47+ it will cause a **compilation error**.
48+
49+ > [!WARNING]
50+ > You cannot use `REQUIRE` or `CHECK` inside a `CONSTEXPR_SECTION`.
51+ > Be careful not to mistake `CONSTEXPR_REQUIRE` with `STATIC_REQUIRE`.
52+ > They both concern compilation-time testing, but they do not have the same
53+ > purpose.
54+
55+ > [!WARNING]
56+ > You cannot nest `CONSTEXPR_SECTION`s or put a `SECTION` inside a
57+ > `CONSTEXPR_SECTION`
3558
3659> [!NOTE]
37- > The code inside the `CONSTEXPR_SECTION` is never evaluated at runtime.
60+ > The code inside the `CONSTEXPR_SECTION` is also evaluated at
61+ > runtime. This way, it remains debuggable, and it contributes to the code
62+ > coverage analysis.
63+
64+ ## What can I test ?
65+
66+ You can test anything that can be evaluated in a `constexpr` function.
67+ This will depend on your compiler and the C++ standard you are using.
68+ C++20, C++23 and C++26 improved a lot the support for `constexpr`.
69+
70+ Take a look at the C++ compiler support on [cppreference](https://en.cppreference.com/w/cpp/compiler_support.html)
71+ to see what you can use.
3872
39- ## Debuging compile time tests
73+ ## Debug failing compile- time tests
4074
41- ### Investigate
75+ ### Investigate from the compiler output
4276
4377You can check the output of your compiler to find the failing assertion.
44- The failing line should be highlighted somewhere.
78+ The failing line should be highlighted somewhere. If you cannot see any
79+ line:
80+
81+ - Ensure the `CONSTEXPR_SECTION` runs only code that can be evaluated
82+ at compile-time. This will depend on your compiler and the C++ standard you
83+ are using.
84+ - Ensure you did not use unsupported Catch2 macros inside the `CONSTEXPR_SECTION`.
85+ The only supported macros are `CONSTEXPR_REQUIRE` and `CONSTEXPR_REQUIRE_FALSE`.
86+ - Ensure the code you wrote doesn't produce undefined behavior (UB cannot
87+ compile). Reading a range outside its bounds, dereferencing an invalid pointer,
88+ reading a variable after it has been destroyed, are widespread undefined
89+ behaviors. They will be all caught by these tests.
4590
4691### Investigate at runtime
4792
48- If you want to debug the code in a `CONSTEXPR_SECTION`, you can simply replace
49- the `CONSTEXPR_SECTION` with a `SECTION` and the code will be evaluated at runtime
50- instead.
93+ > [!TIP]
94+ > If you want to debug the code in a `CONSTEXPR_SECTION`, you can simply replace
95+ > the `CONSTEXPR_SECTION` with a `SECTION` and the code will be evaluated at runtime
96+ > only instead. Remember to come back to the `CONSTEXPR_SECTION` once you are
97+ > done !
5198
5299## Compile time test report
53100
54- At runtime, a `CONSTEXPR_SECTION` will add a section in the test report. These sections run nothing, but they
101+ At runtime, a `CONSTEXPR_SECTION` will add a section in the test report. These sections
55102provide a way to see what was tested during compilation in the test report.
56103
57104For instance, given the following test case:
0 commit comments