forked from mpusz/mp-units
test: type_list
unit tests added
This commit is contained in:
13
src/core/include/units/bits/external/type_list.h
vendored
13
src/core/include/units/bits/external/type_list.h
vendored
@@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
#include <units/bits/external/hacks.h> // IWYU pragma: keep
|
#include <units/bits/external/hacks.h> // IWYU pragma: keep
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <utility>
|
||||||
|
|
||||||
UNITS_DIAGNOSTIC_PUSH
|
UNITS_DIAGNOSTIC_PUSH
|
||||||
UNITS_DIAGNOSTIC_IGNORE_EXPR_ALWAYS_TF
|
UNITS_DIAGNOSTIC_IGNORE_EXPR_ALWAYS_TF
|
||||||
@@ -47,16 +48,16 @@ concept TypeList = detail::is_type_list<T>;
|
|||||||
|
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
template<template<typename...> typename List, typename... Ts>
|
template<typename List>
|
||||||
consteval std::size_t type_list_size_impl(List<Ts...>)
|
struct type_list_size_impl;
|
||||||
{
|
|
||||||
return sizeof...(Ts);
|
template<template<typename...> typename List, typename... Types>
|
||||||
}
|
struct type_list_size_impl<List<Types...>> : std::integral_constant<std::size_t, sizeof...(Types)> {};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
|
||||||
template<TypeList List>
|
template<TypeList List>
|
||||||
inline constexpr std::size_t type_list_size = detail::type_list_size_impl(List{});
|
inline constexpr std::size_t type_list_size = detail::type_list_size_impl<List>::value;
|
||||||
|
|
||||||
// front
|
// front
|
||||||
|
|
||||||
|
@@ -53,14 +53,14 @@ add_library(
|
|||||||
# math_test.cpp
|
# math_test.cpp
|
||||||
# point_origin_test.cpp
|
# point_origin_test.cpp
|
||||||
# prime_test.cpp
|
# prime_test.cpp
|
||||||
# ratio_test.cpp
|
ratio_test.cpp
|
||||||
# references_test.cpp
|
# references_test.cpp
|
||||||
# si_test.cpp
|
# si_test.cpp
|
||||||
# si_cgs_test.cpp
|
# si_cgs_test.cpp
|
||||||
# si_fps_test.cpp
|
# si_fps_test.cpp
|
||||||
# si_hep_test.cpp
|
# si_hep_test.cpp
|
||||||
# symbol_text_test.cpp
|
# symbol_text_test.cpp
|
||||||
# type_list_test.cpp
|
type_list_test.cpp
|
||||||
unit_test.cpp
|
unit_test.cpp
|
||||||
|
|
||||||
# us_test.cpp
|
# us_test.cpp
|
||||||
|
@@ -20,10 +20,8 @@
|
|||||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
// SOFTWARE.
|
// SOFTWARE.
|
||||||
|
|
||||||
#include <units/base_dimension.h>
|
|
||||||
#include <units/bits/external/type_list.h>
|
#include <units/bits/external/type_list.h>
|
||||||
#include <units/exponent.h>
|
#include <units/bits/external/type_traits.h>
|
||||||
#include <units/unit.h>
|
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
@@ -32,6 +30,29 @@ using namespace units;
|
|||||||
template<typename... Types>
|
template<typename... Types>
|
||||||
struct type_list;
|
struct type_list;
|
||||||
|
|
||||||
|
// TypeList
|
||||||
|
|
||||||
|
static_assert(TypeList<type_list<>>);
|
||||||
|
static_assert(TypeList<type_list<int>>);
|
||||||
|
static_assert(TypeList<type_list<int, float>>);
|
||||||
|
|
||||||
|
// type_list_size
|
||||||
|
|
||||||
|
static_assert(type_list_size<type_list<>> == 0);
|
||||||
|
static_assert(type_list_size<type_list<int>> == 1);
|
||||||
|
static_assert(type_list_size<type_list<int, int>> == 2);
|
||||||
|
static_assert(type_list_size<type_list<int, long, int>> == 3);
|
||||||
|
|
||||||
|
// type_list_front
|
||||||
|
|
||||||
|
template<template<typename...> typename List>
|
||||||
|
concept type_list_front_invalid_for_empty_list = !requires { typename type_list_front<List<>>; };
|
||||||
|
|
||||||
|
static_assert(type_list_front_invalid_for_empty_list<type_list>);
|
||||||
|
static_assert(is_same_v<type_list_front<type_list<int>>, int>);
|
||||||
|
static_assert(is_same_v<type_list_front<type_list<long, int>>, long>);
|
||||||
|
static_assert(is_same_v<type_list_front<type_list<float, long, int>>, float>);
|
||||||
|
|
||||||
// type_list_push_front
|
// type_list_push_front
|
||||||
|
|
||||||
static_assert(is_same_v<type_list_push_front<type_list<>, int>, type_list<int>>);
|
static_assert(is_same_v<type_list_push_front<type_list<>, int>, type_list<int>>);
|
||||||
@@ -55,6 +76,15 @@ static_assert(
|
|||||||
static_assert(is_same_v<type_list_join<type_list<int, short>, type_list<float, double>, type_list<bool>>,
|
static_assert(is_same_v<type_list_join<type_list<int, short>, type_list<float, double>, type_list<bool>>,
|
||||||
type_list<int, short, float, double, bool>>);
|
type_list<int, short, float, double, bool>>);
|
||||||
|
|
||||||
|
// type_list_join
|
||||||
|
static_assert(is_same_v<type_list_join<type_list<>, type_list<>>, type_list<>>);
|
||||||
|
static_assert(is_same_v<type_list_join<type_list<int>, type_list<>>, type_list<int>>);
|
||||||
|
static_assert(is_same_v<type_list_join<type_list<>, type_list<int>>, type_list<int>>);
|
||||||
|
static_assert(is_same_v<type_list_join<type_list<int>, type_list<int>>, type_list<int, int>>);
|
||||||
|
static_assert(is_same_v<type_list_join<type_list<int>, type_list<long>>, type_list<int, long>>);
|
||||||
|
static_assert(
|
||||||
|
is_same_v<type_list_join<type_list<int, long>, type_list<float, double>>, type_list<int, long, float, double>>);
|
||||||
|
|
||||||
// type_list_split
|
// type_list_split
|
||||||
|
|
||||||
static_assert(is_same_v<type_list_split<type_list<int>, 0>::first_list, type_list<>>);
|
static_assert(is_same_v<type_list_split<type_list<int>, 0>::first_list, type_list<>>);
|
||||||
@@ -93,28 +123,46 @@ static_assert(is_same_v<type_list_split_half<type_list<int, long, double, float>
|
|||||||
static_assert(
|
static_assert(
|
||||||
is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>);
|
is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>);
|
||||||
|
|
||||||
// type_list_merge_sorted
|
template<auto V>
|
||||||
struct u0 : named_unit<u0, "u0"> {};
|
struct constant {
|
||||||
struct d0 : base_dimension<"d0", u0> {};
|
static constexpr auto value = V;
|
||||||
struct u1 : named_unit<u1, "u1"> {};
|
};
|
||||||
struct d1 : base_dimension<"d1", u1> {};
|
|
||||||
|
|
||||||
|
struct v1 : constant<1> {};
|
||||||
|
struct v2 : constant<2> {};
|
||||||
|
struct v3 : constant<3> {};
|
||||||
|
struct v4 : constant<4> {};
|
||||||
|
|
||||||
|
template<typename T1, typename T2>
|
||||||
|
struct constant_less : std::bool_constant<(T1::value < T2::value)> {};
|
||||||
|
|
||||||
|
// type_list_merge_sorted
|
||||||
|
|
||||||
|
static_assert(is_same_v<type_list_merge_sorted<type_list<>, type_list<>, constant_less>, type_list<>>);
|
||||||
|
static_assert(is_same_v<type_list_merge_sorted<type_list<v1>, type_list<>, constant_less>, type_list<v1>>);
|
||||||
|
static_assert(is_same_v<type_list_merge_sorted<type_list<>, type_list<v1>, constant_less>, type_list<v1>>);
|
||||||
|
static_assert(is_same_v<type_list_merge_sorted<type_list<v1>, type_list<v2>, constant_less>, type_list<v1, v2>>);
|
||||||
static_assert(
|
static_assert(
|
||||||
is_same_v<type_list_merge_sorted<type_list<units::exponent<d0, 1>>, type_list<units::exponent<d1, 1>>, exponent_less>,
|
is_same_v<type_list_merge_sorted<type_list<v1, v3>, type_list<v2, v4>, constant_less>, type_list<v1, v2, v3, v4>>);
|
||||||
type_list<units::exponent<d0, 1>, units::exponent<d1, 1>>>);
|
static_assert(is_same_v<type_list_merge_sorted<type_list<v1, v2, v3>, type_list<v1, v2, v4>, constant_less>,
|
||||||
static_assert(
|
type_list<v1, v1, v2, v2, v3, v4>>);
|
||||||
is_same_v<type_list_merge_sorted<type_list<units::exponent<d1, 1>>, type_list<units::exponent<d0, 1>>, exponent_less>,
|
|
||||||
type_list<units::exponent<d0, 1>, units::exponent<d1, 1>>>);
|
|
||||||
|
|
||||||
// type_list_sort
|
// type_list_sort
|
||||||
|
|
||||||
template<TypeList List>
|
static_assert(is_same_v<type_list_sort<type_list<>, constant_less>, type_list<>>);
|
||||||
using exp_sort = type_list_sort<List, exponent_less>;
|
static_assert(is_same_v<type_list_sort<type_list<v1>, constant_less>, type_list<v1>>);
|
||||||
|
static_assert(is_same_v<type_list_sort<type_list<v1, v2>, constant_less>, type_list<v1, v2>>);
|
||||||
|
static_assert(is_same_v<type_list_sort<type_list<v2, v1>, constant_less>, type_list<v1, v2>>);
|
||||||
|
static_assert(is_same_v<type_list_sort<type_list<v2, v1, v3>, constant_less>, type_list<v1, v2, v3>>);
|
||||||
|
static_assert(is_same_v<type_list_sort<type_list<v4, v3, v2, v1>, constant_less>, type_list<v1, v2, v3, v4>>);
|
||||||
|
|
||||||
static_assert(is_same_v<exp_sort<exponent_list<units::exponent<d0, 1>>>, exponent_list<units::exponent<d0, 1>>>);
|
// type_list_map
|
||||||
static_assert(is_same_v<exp_sort<exponent_list<units::exponent<d0, 1>, units::exponent<d1, -1>>>,
|
|
||||||
exponent_list<units::exponent<d0, 1>, units::exponent<d1, -1>>>);
|
template<typename... Types>
|
||||||
static_assert(is_same_v<exp_sort<exponent_list<units::exponent<d1, 1>, units::exponent<d0, -1>>>,
|
struct other_list;
|
||||||
exponent_list<units::exponent<d0, -1>, units::exponent<d1, 1>>>);
|
|
||||||
|
static_assert(is_same_v<type_list_map<type_list<>, other_list>, other_list<>>);
|
||||||
|
static_assert(is_same_v<type_list_map<type_list<v1>, other_list>, other_list<v1>>);
|
||||||
|
static_assert(is_same_v<type_list_map<type_list<v1, v2>, other_list>, other_list<v1, v2>>);
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
Reference in New Issue
Block a user