Debian Codegraph @

3rdparty/catch2/examples/302-Gen-Table.cpp

raw symbols SCIP
1 // 302-Gen-Table.cpp
2 // Shows how to use table to run a test many times with different inputs. Lifted from examples on
3 // issue #850.
4
5 #include <catch2/catch_test_macros.hpp>
6 #include <catch2/generators/catch_generators.hpp>
7 #include <string>
8
9 struct TestSubject {
10 // this is the method we are going to test. It returns the length of the
11 // input string.
12 size_t GetLength( const std::string& input ) const { return input.size(); }
13 };
14
15
16 TEST_CASE("Table allows pre-computed test inputs and outputs", "[example][generator]") {
17 using std::make_tuple;
18 // do setup here as normal
19 TestSubject subj;
20
21 SECTION("This section is run for each row in the table") {
22 std::string test_input;
23 size_t expected_output;
24 std::tie( test_input, expected_output ) =
25 GENERATE( table<std::string, size_t>(
26 { /* In this case one of the parameters to our test case is the
27 * expected output, but this is not required. There could be
28 * multiple expected values in the table, which can have any
29 * (fixed) number of columns.
30 */
31 make_tuple( "one", 3 ),
32 make_tuple( "two", 3 ),
33 make_tuple( "three", 5 ),
34 make_tuple( "four", 4 ) } ) );
35
36 // run the test
37 auto result = subj.GetLength(test_input);
38 // capture the input data to go with the outputs.
39 CAPTURE(test_input);
40 // check it matches the pre-calculated data
41 REQUIRE(result == expected_output);
42 } // end section
43 }
44
45 /* Possible simplifications where less legacy toolchain support is needed:
46 *
47 * - With libstdc++6 or newer, the make_tuple() calls can be omitted
48 * (technically C++17 but does not require -std in GCC/Clang). See
49 * https://stackoverflow.com/questions/12436586/tuple-vector-and-initializer-list
50 *
51 * - In C++17 mode std::tie() and the preceding variable declarations can be
52 * replaced by structured bindings: auto [test_input, expected] = GENERATE(
53 * table<std::string, size_t>({ ...
54 */
55 // Compiling and running this file will result in 4 successful assertions