ut icon indicating copy to clipboard operation
ut copied to clipboard

Support parameterized test on types without instantiating the types

Open mrizaln opened this issue 10 months ago • 0 comments

Problem:

Parameterized test on types requires instantiating the types itself inside a std::tuple. This has been a major pain point for me, and looking at the issues, someone also think the same: #539.

Solution:

Instead of using std::tuple, we can use a custom type that wraps a list of types that has no state (type_list). We also create a single wrapper type that wraps the type to be passed to ut::events::test as arg (wrapped_type). The purpose of the type itself is just to carry the type information it wraps. By doing this, any kind of types can be copied around without instantiating it.

namespace detail {
template <typename T>
struct wrapped_type {
  using type = T;
};
}

template <typename... Ts>
struct type_list {
  using types = std::tuple<Ts...>;
  static constexpr auto size = sizeof...(Ts);

  template <std::size_t I>
    requires(I < size)
  using get = detail::wrapped_type<std::tuple_element_t<I, types>>;
};

type_list then participate with other types on the operator| overloads so that it can be used.

template <typename F, typename... Ts>
[[nodiscard]] constexpr auto operator|(const F& f, type_list<Ts...>);

The usage should be like this:

"parameterized test"_test = []<typename T>() {
   // do something...
} | type_list<std::unique_ptr<int>, std::packaged_task<void()>>;

Issue: #539

Reviewers: @

mrizaln avatar Mar 17 '25 10:03 mrizaln