diff --git a/README.md b/README.md index 46a093b..ac7e695 100644 --- a/README.md +++ b/README.md @@ -316,7 +316,7 @@ auto main() -> int
-Tests can be parameterized by type +Tests can be parameterized with a tuple-like ```cpp:example/type_parameterized.cpp #include "skytest/skytest.hpp" @@ -344,7 +344,7 @@ auto main() -> int ``` -or by value +or with a range ```cpp:example/value_parameterized.cpp #include "skytest/skytest.hpp" @@ -363,9 +363,26 @@ auto main() -> int ``` -If parameters are defined as a static constant, `param_ref` may enable -compile-time tests. +If parameters are defined as a static constant, `constexr_params` and +`param_ref` can be used to define compile-time tests. +```cpp:example/constexpr_params_parameterized.cpp +#include "skytest/skytest.hpp" + +auto main() -> int +{ + using namespace ::skytest::literals; + using ::skytest::constexpr_params; + using ::skytest::eq; + using ::skytest::expect; + + "types with param_ref"_ctest * constexpr_params<1, 2U> = // + [](auto param) { + return expect(eq(std::is_same_v ? 1 : 2, param)); + }; +} + +``` ```cpp:example/param_ref_parameterized.cpp #include "skytest/skytest.hpp" diff --git a/example/BUILD.bazel b/example/BUILD.bazel index d42bd49..e709a99 100644 --- a/example/BUILD.bazel +++ b/example/BUILD.bazel @@ -123,6 +123,12 @@ cc_test( deps = [":skytest_wrapper"], ) +cc_test( + name = "constexpr_params_parameterized", + srcs = ["constexpr_params_parameterized.cpp"], + deps = [":skytest_wrapper"], +) + cc_test( name = "param_parameterized", srcs = ["param_parameterized.cpp"], diff --git a/example/constexpr_params_parameterized.cpp b/example/constexpr_params_parameterized.cpp new file mode 100644 index 0000000..b5f0dd0 --- /dev/null +++ b/example/constexpr_params_parameterized.cpp @@ -0,0 +1,14 @@ +#include "skytest/skytest.hpp" + +auto main() -> int +{ + using namespace ::skytest::literals; + using ::skytest::constexpr_params; + using ::skytest::eq; + using ::skytest::expect; + + "types with param_ref"_ctest * constexpr_params<1, 2U> = // + [](auto param) { + return expect(eq(std::is_same_v ? 1 : 2, param)); + }; +} diff --git a/scripts/README.md.tmpl b/scripts/README.md.tmpl index 3da4259..85a34a4 100644 --- a/scripts/README.md.tmpl +++ b/scripts/README.md.tmpl @@ -109,19 +109,21 @@ C++20 enables a terser syntax. #### parameterized tests
-Tests can be parameterized by type +Tests can be parameterized with a tuple-like ```cpp:example/type_parameterized.cpp ``` -or by value +or with a range ```cpp:example/value_parameterized.cpp ``` -If parameters are defined as a static constant, `param_ref` may enable -compile-time tests. +If parameters are defined as a static constant, `constexr_params` and +`param_ref` can be used to define compile-time tests. +```cpp:example/constexpr_params_parameterized.cpp +``` ```cpp:example/param_ref_parameterized.cpp ``` @@ -143,4 +145,4 @@ Check that abort is called (e.g. in a function precondition).
[^1]: The default printer uses `std::cout` and `skytest::aborts` calls `fork`. -[^2]: https://en.cppreference.com/w/cpp/named_req/LiteralType \ No newline at end of file +[^2]: https://en.cppreference.com/w/cpp/named_req/LiteralType diff --git a/src/test_param.hpp b/src/test_param.hpp index 8b8ad0b..9b809d0 100644 --- a/src/test_param.hpp +++ b/src/test_param.hpp @@ -342,9 +342,32 @@ inline constexpr auto param = param_t{}; template inline constexpr auto types = param_ref>; +template +struct constexpr_params_t +{ + template + friend constexpr auto get(constexpr_params_t) -> auto + { + return std::get(std::tuple{Values...}); + } +}; + +template +inline constexpr auto constexpr_params_instance = + constexpr_params_t{}; + +template +inline constexpr auto constexpr_params = + param_ref>; + } // namespace skytest template struct std::tuple_size<::skytest::detail::type_list> : ::std::integral_constant {}; + +template +struct std::tuple_size> + : std::integral_constant +{}; diff --git a/test/BUILD.bazel b/test/BUILD.bazel index 62b2a30..bfb400f 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -295,7 +295,9 @@ skytest_test( stdout = [ "test1.*CONSTEXPR", "test2.*CONSTEXPR", - "all tests passed.*2 tests", + "test3.*CONSTEXPR", + "test3.*CONSTEXPR", + "all tests passed.*4 tests", ], ) diff --git a/test/requires_constexpr_test.cpp b/test/requires_constexpr_test.cpp index 8af9e23..0a9c638 100644 --- a/test/requires_constexpr_test.cpp +++ b/test/requires_constexpr_test.cpp @@ -1,8 +1,11 @@ #include "skytest/skytest.hpp" +#include + auto main() -> int { using namespace ::skytest::literals; + using ::skytest::constexpr_params; using ::skytest::eq; using ::skytest::expect; using ::skytest::types; @@ -13,4 +16,8 @@ auto main() -> int using T = typename decltype(param)::type; return expect(eq(0, T{})); }; + + "test3"_ctest * constexpr_params<1, 2U> = [](auto value) { + return expect(eq(std::is_same_v ? 1 : 2, value)); + }; }