Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ auto main() -> int
<details><summary></summary>


Tests can be parameterized by type
Tests can be parameterized with a tuple-like

```cpp:example/type_parameterized.cpp
#include "skytest/skytest.hpp"
Expand Down Expand Up @@ -344,7 +344,7 @@ auto main() -> int

```

or by value
or with a range

```cpp:example/value_parameterized.cpp
#include "skytest/skytest.hpp"
Expand All @@ -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<int, decltype(param)> ? 1 : 2, param));
};
}

```
```cpp:example/param_ref_parameterized.cpp
#include "skytest/skytest.hpp"

Expand Down
6 changes: 6 additions & 0 deletions example/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand Down
14 changes: 14 additions & 0 deletions example/constexpr_params_parameterized.cpp
Original file line number Diff line number Diff line change
@@ -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<int, decltype(param)> ? 1 : 2, param));
};
}
12 changes: 7 additions & 5 deletions scripts/README.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,21 @@ C++20 enables a terser syntax.
#### parameterized tests
<details><summary></summary>

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
```

Expand All @@ -143,4 +145,4 @@ Check that abort is called (e.g. in a function precondition).
</details>

[^1]: The default printer uses `std::cout` and `skytest::aborts` calls `fork`.
[^2]: https://en.cppreference.com/w/cpp/named_req/LiteralType
[^2]: https://en.cppreference.com/w/cpp/named_req/LiteralType
23 changes: 23 additions & 0 deletions src/test_param.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,9 +342,32 @@ inline constexpr auto param = param_t<Params>{};
template <class... Ts>
inline constexpr auto types = param_ref<detail::type_params<Ts...>>;

template <auto... Values>
struct constexpr_params_t
{
template <std::size_t I>
friend constexpr auto get(constexpr_params_t) -> auto
{
return std::get<I>(std::tuple{Values...});
}
};

template <auto... Values>
inline constexpr auto constexpr_params_instance =
constexpr_params_t<Values...>{};

template <auto... Values>
inline constexpr auto constexpr_params =
param_ref<constexpr_params_instance<Values...>>;

} // namespace skytest

template <class... Args>
struct std::tuple_size<::skytest::detail::type_list<Args...>>
: ::std::integral_constant<std::size_t, sizeof...(Args)>
{};

template <auto... Values>
struct std::tuple_size<skytest::constexpr_params_t<Values...>>
: std::integral_constant<std::size_t, sizeof...(Values)>
{};
4 changes: 3 additions & 1 deletion test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
],
)

Expand Down
7 changes: 7 additions & 0 deletions test/requires_constexpr_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "skytest/skytest.hpp"

#include <type_traits>

auto main() -> int
{
using namespace ::skytest::literals;
using ::skytest::constexpr_params;
using ::skytest::eq;
using ::skytest::expect;
using ::skytest::types;
Expand All @@ -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<int, decltype(value)> ? 1 : 2, value));
};
}