| 1 | // SPDX-FileCopyrightText: 2018 Kitsune Ral <kitsune-ral@users.sf.net> |
| 2 | // SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | |
| 4 | #include "function_traits.h" |
| 5 | |
| 6 | // Tests for function_traits<> |
| 7 | |
| 8 | using namespace Quotient; |
| 9 | |
| 10 | template <typename FnT> |
| 11 | using fn_return_t = typename function_traits<FnT>::return_type; |
| 12 | |
| 13 | int f_(); |
| 14 | static_assert(std::is_same_v<fn_return_t<decltype(f_)>, int>, |
| 15 | "Test fn_return_t<>" ); |
| 16 | |
| 17 | void f1_(int, float); |
| 18 | static_assert(std::is_same_v<fn_arg_t<decltype(f1_), 1>, float>, |
| 19 | "Test fn_arg_t<>" ); |
| 20 | |
| 21 | struct Fo { |
| 22 | int operator()(); |
| 23 | static constexpr auto l = [] { return 0.0f; }; |
| 24 | bool memFn(); |
| 25 | void constMemFn() const&; |
| 26 | double field; |
| 27 | const double field2; |
| 28 | }; |
| 29 | static_assert(std::is_same_v<fn_return_t<Fo>, int>, |
| 30 | "Test return type of function object" ); |
| 31 | static_assert(std::is_same_v<fn_return_t<decltype(Fo::l)>, float>, |
| 32 | "Test return type of lambda" ); |
| 33 | static_assert(std::is_same_v<fn_arg_t<decltype(&Fo::memFn)>, Fo>, |
| 34 | "Test first argument type of member function" ); |
| 35 | static_assert(std::is_same_v<fn_return_t<decltype(&Fo::memFn)>, bool>, |
| 36 | "Test return type of member function" ); |
| 37 | static_assert(std::is_same_v<fn_arg_t<decltype(&Fo::constMemFn)>, const Fo&>, |
| 38 | "Test first argument type of const member function" ); |
| 39 | static_assert(std::is_void_v<fn_return_t<decltype(&Fo::constMemFn)>>, |
| 40 | "Test return type of const member function" ); |
| 41 | static_assert(std::is_same_v<fn_return_t<decltype(&Fo::field)>, double&>, |
| 42 | "Test return type of a class member" ); |
| 43 | static_assert(std::is_same_v<fn_return_t<decltype(&Fo::field2)>, const double&>, |
| 44 | "Test return type of a const class member" ); |
| 45 | |
| 46 | struct Fo1 { |
| 47 | void operator()(int); |
| 48 | }; |
| 49 | static_assert(std::is_same_v<fn_arg_t<Fo1>, int>, |
| 50 | "Test fn_arg_t defaulting to first argument" ); |
| 51 | |
| 52 | template <typename T> |
| 53 | [[maybe_unused]] static void ft(const std::vector<T>&); |
| 54 | static_assert( |
| 55 | std::is_same<fn_arg_t<decltype(ft<double>)>, const std::vector<double>&>(), |
| 56 | "Test function templates" ); |
| 57 | |