mirror of
https://github.com/mpusz/mp-units.git
synced 2025-07-30 02:17:16 +02:00
Units library moved to concepts world
This commit is contained in:
@ -43,4 +43,4 @@ enable_testing()
|
||||
add_subdirectory(test)
|
||||
|
||||
# add usage example
|
||||
add_subdirectory(example)
|
||||
#add_subdirectory(example)
|
||||
|
@ -6,13 +6,8 @@ class UnitConan(ConanFile):
|
||||
author = "Mateusz Pusz"
|
||||
license = "https://github.com/mpusz/units/blob/master/LICENSE"
|
||||
url = "https://github.com/mpusz/units"
|
||||
description = "Physical Units library implementation for C++"
|
||||
description = "Physical Units library for C++"
|
||||
settings = "os", "compiler", "build_type", "arch"
|
||||
build_requires = (
|
||||
"gtest/1.8.0@bincrafters/stable"
|
||||
)
|
||||
default_options = "gtest:shared=False"
|
||||
generators = "cmake_paths"
|
||||
|
||||
def build(self):
|
||||
cmake = CMake(self)
|
||||
|
@ -47,7 +47,7 @@ add_library(units INTERFACE)
|
||||
# include/units/bits/quantity.h
|
||||
# include/units/bits/tools.h
|
||||
#)
|
||||
target_compile_features(units INTERFACE cxx_std_17)
|
||||
target_compile_features(units INTERFACE cxx_std_20)
|
||||
target_include_directories(units
|
||||
INTERFACE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
|
44
src/include/units/bits/stdconcepts.h
Normal file
44
src/include/units/bits/stdconcepts.h
Normal file
@ -0,0 +1,44 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <ratio>
|
||||
#include <type_traits>
|
||||
|
||||
namespace mp {
|
||||
|
||||
namespace detail {
|
||||
template<class T, class U>
|
||||
bool concept SameHelper = std::is_same_v<T, U>;
|
||||
}
|
||||
|
||||
template<class T, class U>
|
||||
bool concept Same = detail::SameHelper<T, U> && detail::SameHelper<U, T>;
|
||||
|
||||
template <class From, class To>
|
||||
bool concept ConvertibleTo = std::is_convertible_v<From, To> &&
|
||||
requires(From (&f)()) {
|
||||
static_cast<To>(f());
|
||||
};
|
||||
|
||||
} // namespace mp
|
@ -22,16 +22,12 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "stdconcepts.h"
|
||||
#include <ratio>
|
||||
#include <type_traits>
|
||||
|
||||
namespace mp {
|
||||
|
||||
// Requires
|
||||
|
||||
template<bool B>
|
||||
using Requires = std::enable_if_t<B, bool>;
|
||||
|
||||
// static_sign
|
||||
|
||||
template<std::intmax_t Pn>
|
||||
@ -60,6 +56,8 @@ namespace mp {
|
||||
|
||||
// is_ratio
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
struct is_ratio : std::false_type {
|
||||
};
|
||||
@ -68,17 +66,22 @@ namespace mp {
|
||||
struct is_ratio<std::ratio<Num, Den>> : std::true_type {
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool concept Ratio = detail::is_ratio<T>::value;
|
||||
|
||||
// common_ratio
|
||||
|
||||
// todo: simplified
|
||||
template<typename Ratio1, typename Ratio2>
|
||||
template<Ratio Ratio1, Ratio Ratio2>
|
||||
struct common_ratio {
|
||||
using gcd_num = static_gcd<Ratio1::num, Ratio2::num>;
|
||||
using gcd_den = static_gcd<Ratio1::den, Ratio2::den>;
|
||||
using type = std::ratio<gcd_num::value, (Ratio1::den / gcd_den::value) * Ratio2::den>;
|
||||
};
|
||||
|
||||
template<typename Ratio1, typename Ratio2>
|
||||
template<Ratio Ratio1, Ratio Ratio2>
|
||||
using common_ratio_t = typename common_ratio<Ratio1, Ratio2>::type;
|
||||
|
||||
} // namespace mp
|
||||
|
@ -32,9 +32,22 @@ namespace mp {
|
||||
template<typename... Types>
|
||||
struct type_list;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
struct is_type_list : std::false_type {};
|
||||
|
||||
template<typename... Types>
|
||||
struct is_type_list<type_list<Types...>> : std::true_type {};
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool concept TypeList = detail::is_type_list<T>::value;
|
||||
|
||||
// push_front
|
||||
|
||||
template<typename TypeList, typename... Types>
|
||||
template<TypeList List, typename... Types>
|
||||
struct type_list_push_front;
|
||||
|
||||
template<typename... OldTypes, typename... NewTypes>
|
||||
@ -42,12 +55,12 @@ namespace mp {
|
||||
using type = type_list<NewTypes..., OldTypes...>;
|
||||
};
|
||||
|
||||
template<typename TypeList, typename... Types>
|
||||
using type_list_push_front_t = typename type_list_push_front<TypeList, Types...>::type;
|
||||
template<TypeList List, typename... Types>
|
||||
using type_list_push_front_t = typename type_list_push_front<List, Types...>::type;
|
||||
|
||||
// push_back
|
||||
|
||||
template<typename TypeList, typename... Types>
|
||||
template<TypeList List, typename... Types>
|
||||
struct type_list_push_back;
|
||||
|
||||
template<typename... OldTypes, typename... NewTypes>
|
||||
@ -55,8 +68,8 @@ namespace mp {
|
||||
using type = type_list<OldTypes..., NewTypes...>;
|
||||
};
|
||||
|
||||
template<typename TypeList, typename... Types>
|
||||
using type_list_push_back_t = typename type_list_push_back<TypeList, Types...>::type;
|
||||
template<TypeList List, typename... Types>
|
||||
using type_list_push_back_t = typename type_list_push_back<List, Types...>::type;
|
||||
|
||||
// split
|
||||
|
||||
@ -76,17 +89,18 @@ namespace mp {
|
||||
using base = split_impl<Idx + 1, N, Rest...>;
|
||||
using base_first = typename base::first_list;
|
||||
using base_second = typename base::second_list;
|
||||
using first_list = std::conditional_t < Idx<N, type_list_push_front_t<base_first, T>, base_first>;
|
||||
using second_list = std::conditional_t < Idx<N, base_second, type_list_push_front_t<base_second, T>>;
|
||||
using first_list = std::conditional_t<Idx<N, type_list_push_front_t<base_first, T>, base_first>;
|
||||
using second_list = std::conditional_t<Idx<N, base_second, type_list_push_front_t<base_second, T>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
template<TypeList List, std::size_t N>
|
||||
struct type_list_split;
|
||||
|
||||
template<std::size_t N, typename... Types>
|
||||
struct type_list_split<type_list<Types...>, N> {
|
||||
static_assert(N <= sizeof...(Types), "Invalid index provided");
|
||||
using split = detail::split_impl<0, N, Types...>;
|
||||
using first_list = typename split::first_list;
|
||||
using second_list = typename split::second_list;
|
||||
@ -94,7 +108,7 @@ namespace mp {
|
||||
|
||||
// split_half
|
||||
|
||||
template<typename T>
|
||||
template<TypeList List>
|
||||
struct type_list_split_half;
|
||||
|
||||
template<typename... Types>
|
||||
@ -103,11 +117,11 @@ namespace mp {
|
||||
|
||||
// merge_sorted
|
||||
|
||||
template<typename T1, typename T2, template<typename, typename> typename Pred>
|
||||
template<TypeList SortedList1, TypeList SortedList2, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted;
|
||||
|
||||
template<typename T1, typename T2, template<typename, typename> typename Pred>
|
||||
using type_list_merge_sorted_t = typename type_list_merge_sorted<T1, T2, Pred>::type;
|
||||
template<TypeList SortedList1, TypeList SortedList2, template<typename, typename> typename Pred>
|
||||
using type_list_merge_sorted_t = typename type_list_merge_sorted<SortedList1, SortedList2, Pred>::type;
|
||||
|
||||
template<typename... Lhs, template<typename, typename> typename Pred>
|
||||
struct type_list_merge_sorted<type_list<Lhs...>, type_list<>, Pred> {
|
||||
@ -132,7 +146,7 @@ namespace mp {
|
||||
|
||||
// sort
|
||||
|
||||
template<typename T, template<typename, typename> typename Pred>
|
||||
template<TypeList List, template<typename, typename> typename Pred>
|
||||
struct type_list_sort;
|
||||
|
||||
template<template<typename, typename> typename Pred>
|
||||
@ -154,7 +168,7 @@ namespace mp {
|
||||
using type = type_list_merge_sorted_t<left, right, Pred>;
|
||||
};
|
||||
|
||||
template<typename T, template<typename, typename> typename Pred>
|
||||
using type_list_sort_t = typename type_list_sort<T, Pred>::type;
|
||||
template<TypeList TypeList, template<typename, typename> typename Pred>
|
||||
using type_list_sort_t = typename type_list_sort<TypeList, Pred>::type;
|
||||
|
||||
} // namespace mp
|
@ -35,7 +35,7 @@ namespace units {
|
||||
// dim_id_less
|
||||
|
||||
template<typename D1, typename D2>
|
||||
struct dim_id_less : std::bool_constant<D1::value<D2::value> {
|
||||
struct dim_id_less : std::bool_constant<D1::value < D2::value> {
|
||||
};
|
||||
|
||||
// exp
|
||||
@ -46,15 +46,30 @@ namespace units {
|
||||
static constexpr int value = Value;
|
||||
};
|
||||
|
||||
// is_exp
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct is_exp : std::false_type {
|
||||
};
|
||||
|
||||
template<typename BaseDim, int Value>
|
||||
struct is_exp<exp<BaseDim, Value>> : std::true_type {
|
||||
};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool concept Exp = detail::is_exp<T>::value;
|
||||
|
||||
|
||||
// exp_less
|
||||
|
||||
template<typename E1, typename E2>
|
||||
template<Exp E1, Exp E2>
|
||||
struct exp_less : dim_id_less<typename E1::dimension, typename E2::dimension> {
|
||||
};
|
||||
|
||||
// exp_invert
|
||||
|
||||
template<typename Exponent>
|
||||
template<Exp Exponent>
|
||||
struct exp_invert;
|
||||
|
||||
template<typename BaseDimension, int Value>
|
||||
@ -62,88 +77,96 @@ namespace units {
|
||||
using type = exp<BaseDimension, -Value>;
|
||||
};
|
||||
|
||||
template<typename Exponent>
|
||||
template<Exp Exponent>
|
||||
using exp_invert_t = typename exp_invert<Exponent>::type;
|
||||
|
||||
// dimension
|
||||
|
||||
template<typename... Exponents>
|
||||
template<Exp... Exponents>
|
||||
using dimension = mp::type_list<Exponents...>;
|
||||
|
||||
// is_dimension
|
||||
namespace detail {
|
||||
template<typename T>
|
||||
struct is_dimension : std::false_type {};
|
||||
|
||||
template<Exp... Exponents>
|
||||
struct is_dimension<dimension<Exponents...>> : std::bool_constant<(is_exp<Exponents>::value && ...)> {};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool concept Dimension = detail::is_dimension<T>::value;
|
||||
|
||||
|
||||
// make_dimension
|
||||
|
||||
namespace detail {
|
||||
|
||||
struct scalar;
|
||||
|
||||
template<typename D>
|
||||
template<Dimension D>
|
||||
struct dim_consolidate;
|
||||
|
||||
template<typename D>
|
||||
template<Dimension D>
|
||||
using dim_consolidate_t = typename dim_consolidate<D>::type;
|
||||
|
||||
template<>
|
||||
struct dim_consolidate<dimension<>> {
|
||||
using type = dimension<scalar>;
|
||||
using type = dimension<>;
|
||||
};
|
||||
|
||||
template<typename E1>
|
||||
struct dim_consolidate<dimension<E1>> {
|
||||
using type = dimension<E1>;
|
||||
template<Exp E>
|
||||
struct dim_consolidate<dimension<E>> {
|
||||
using type = dimension<E>;
|
||||
};
|
||||
|
||||
template<typename... ERest>
|
||||
struct dim_consolidate<dimension<scalar, ERest...>> {
|
||||
using type = dim_consolidate_t<dimension<ERest...>>;
|
||||
template<Exp E1, Exp... ERest>
|
||||
struct dim_consolidate<dimension<E1, ERest...>> {
|
||||
using rest = dim_consolidate_t<dimension<ERest...>>;
|
||||
using type = std::conditional_t<std::is_same_v<rest, dimension<>>, dimension<E1>,
|
||||
mp::type_list_push_front_t<rest, E1>>;
|
||||
};
|
||||
|
||||
template<typename D, int V1, int V2, typename... ERest>
|
||||
template<typename D, int V1, int V2, Exp... ERest>
|
||||
struct dim_consolidate<dimension<exp<D, V1>, exp<D, V2>, ERest...>> {
|
||||
using type = std::conditional_t<V1 + V2 == 0, dim_consolidate_t<dimension<ERest...>>,
|
||||
dim_consolidate_t<dimension<exp<D, V1 + V2>, ERest...>>>;
|
||||
};
|
||||
|
||||
template<typename E1, typename... ERest>
|
||||
struct dim_consolidate<dimension<E1, ERest...>> {
|
||||
using rest = dim_consolidate_t<dimension<ERest...>>;
|
||||
using type = std::conditional_t<std::is_same_v<rest, dimension<scalar>>, dimension<E1>,
|
||||
mp::type_list_push_front_t<rest, E1>>;
|
||||
};
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename... Exponents>
|
||||
template<Exp... Exponents>
|
||||
struct make_dimension {
|
||||
using type = detail::dim_consolidate_t<mp::type_list_sort_t<mp::type_list<Exponents...>, exp_less>>;
|
||||
using type = detail::dim_consolidate_t<mp::type_list_sort_t<dimension<Exponents...>, exp_less>>;
|
||||
};
|
||||
|
||||
template<typename... Exponents>
|
||||
template<Exp... Exponents>
|
||||
using make_dimension_t = typename make_dimension<Exponents...>::type;
|
||||
|
||||
|
||||
|
||||
// dimension_multiply
|
||||
|
||||
template<typename D1, typename D2>
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_multiply;
|
||||
|
||||
template<typename... Exp1, typename... Exp2>
|
||||
template<Exp... Exp1, Exp... Exp2>
|
||||
struct dimension_multiply<dimension<Exp1...>, dimension<Exp2...>> {
|
||||
using type = make_dimension_t<Exp1..., Exp2...>;
|
||||
};
|
||||
|
||||
template<typename D1, typename D2>
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_multiply_t = typename dimension_multiply<D1, D2>::type;
|
||||
|
||||
// dimension_divide
|
||||
|
||||
template<typename D1, typename D2>
|
||||
template<Dimension D1, Dimension D2>
|
||||
struct dimension_divide;
|
||||
|
||||
template<typename... Exp1, typename... Exp2>
|
||||
template<Exp... Exp1, Exp... Exp2>
|
||||
struct dimension_divide<dimension<Exp1...>, dimension<Exp2...>>
|
||||
: dimension_multiply<dimension<Exp1...>, dimension<exp_invert_t<Exp2>...>> {
|
||||
};
|
||||
|
||||
template<typename D1, typename D2>
|
||||
template<Dimension D1, Dimension D2>
|
||||
using dimension_divide_t = typename dimension_divide<D1, D2>::type;
|
||||
|
||||
} // namespace units
|
||||
|
@ -28,23 +28,26 @@
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio>
|
||||
class quantity;
|
||||
|
||||
// is_quantity
|
||||
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
class quantity;
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename T>
|
||||
struct is_quantity : std::false_type {
|
||||
};
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio>
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
struct is_quantity<quantity<Dimension, Rep, Ratio>> : std::true_type {
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
bool concept Quantity = detail::is_quantity<T>::value;
|
||||
|
||||
// treat_as_floating_point
|
||||
|
||||
template<class Rep>
|
||||
@ -57,9 +60,9 @@ namespace units {
|
||||
|
||||
namespace detail {
|
||||
|
||||
template<typename To, typename CRatio, typename CRep, bool NumIsOne = false, bool DenIsOne = false>
|
||||
template<Quantity To, mp::Ratio CRatio, typename CRep, bool NumIsOne = false, bool DenIsOne = false>
|
||||
struct quantity_cast_impl {
|
||||
template<typename Dimension, typename Rep, typename Ratio>
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num) /
|
||||
@ -67,27 +70,27 @@ namespace units {
|
||||
}
|
||||
};
|
||||
|
||||
template<typename To, typename CRatio, typename CRep>
|
||||
template<Quantity To, mp::Ratio CRatio, typename CRep>
|
||||
struct quantity_cast_impl<To, CRatio, CRep, true, true> {
|
||||
template<typename Dimension, typename Rep, typename Ratio>
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(q.count()));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename To, typename CRatio, typename CRep>
|
||||
template<Quantity To, mp::Ratio CRatio, typename CRep>
|
||||
struct quantity_cast_impl<To, CRatio, CRep, true, false> {
|
||||
template<typename Dimension, typename Rep, typename Ratio>
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) / static_cast<CRep>(CRatio::den)));
|
||||
}
|
||||
};
|
||||
|
||||
template<typename To, typename CRatio, typename CRep>
|
||||
template<Quantity To, mp::Ratio CRatio, typename CRep>
|
||||
struct quantity_cast_impl<To, CRatio, CRep, false, true> {
|
||||
template<typename Dimension, typename Rep, typename Ratio>
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
static constexpr To cast(const quantity<Dimension, Rep, Ratio>& q)
|
||||
{
|
||||
return To(static_cast<typename To::rep>(static_cast<CRep>(q.count()) * static_cast<CRep>(CRatio::num)));
|
||||
@ -96,8 +99,8 @@ namespace units {
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template<typename To, typename Dimension, typename Rep, typename Ratio,
|
||||
mp::Requires<detail::is_quantity<To>::value && std::is_same_v<typename To::dimension, Dimension>> = true>
|
||||
template<Quantity To, Dimension Dimension, typename Rep, mp::Ratio Ratio>
|
||||
requires mp::Same<typename To::dimension, Dimension>
|
||||
constexpr To quantity_cast(const quantity<Dimension, Rep, Ratio>& q)
|
||||
{
|
||||
using c_ratio = std::ratio_divide<Ratio, typename To::ratio>;
|
||||
@ -117,7 +120,7 @@ namespace units {
|
||||
|
||||
// quantity
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
|
||||
template<Dimension Dimension, typename Rep, mp::Ratio Ratio = std::ratio<1>>
|
||||
class quantity {
|
||||
Rep value_;
|
||||
public:
|
||||
@ -126,21 +129,20 @@ namespace units {
|
||||
using ratio = Ratio;
|
||||
|
||||
static_assert(!detail::is_quantity<Rep>::value, "rep cannot be a quantity");
|
||||
static_assert(mp::is_ratio<ratio>::value, "ratio must be a specialization of std::ratio");
|
||||
static_assert(ratio::num > 0, "ratio must be positive");
|
||||
|
||||
quantity() = default;
|
||||
quantity(const quantity&) = default;
|
||||
|
||||
template<class Rep2, mp::Requires<std::is_convertible_v<Rep2, rep> &&
|
||||
(treat_as_floating_point_v<rep> || !treat_as_floating_point_v<Rep2>)> = true>
|
||||
template<class Rep2>
|
||||
requires mp::ConvertibleTo<Rep2, rep> && (treat_as_floating_point_v<rep> || !treat_as_floating_point_v<Rep2>)
|
||||
constexpr explicit quantity(const Rep2& r) : value_{static_cast<rep>(r)}
|
||||
{
|
||||
}
|
||||
|
||||
template<class Rep2, class Ratio2,
|
||||
mp::Requires<treat_as_floating_point_v<rep> ||
|
||||
(std::ratio_divide<Ratio2, ratio>::den == 1 && !treat_as_floating_point<Rep2>::value)> = true>
|
||||
template<class Rep2, mp::Ratio Ratio2>
|
||||
requires mp::ConvertibleTo<Rep2, rep> && (treat_as_floating_point_v<rep> ||
|
||||
(std::ratio_divide<Ratio2, ratio>::den == 1 && !treat_as_floating_point_v<Rep2>))
|
||||
constexpr quantity(const quantity<Dimension, Rep2, Ratio2>& q) : value_{quantity_cast<quantity>(q).count()}
|
||||
{
|
||||
}
|
||||
@ -208,7 +210,7 @@ namespace units {
|
||||
};
|
||||
|
||||
// clang-format off
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>
|
||||
constexpr operator+(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
@ -217,7 +219,7 @@ namespace units {
|
||||
return ret(ret(lhs).count() + ret(rhs).count());
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>
|
||||
constexpr operator-(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
@ -226,7 +228,7 @@ namespace units {
|
||||
return ret(ret(lhs).count() - ret(rhs).count());
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio, typename Rep2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio, typename Rep2>
|
||||
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
|
||||
constexpr operator*(const quantity<Dimension, Rep1, Ratio>& q,
|
||||
const Rep2& v)
|
||||
@ -235,7 +237,7 @@ namespace units {
|
||||
return ret(ret(q).count() * v);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, typename Rep2, class Ratio>
|
||||
template<typename Rep1, Dimension Dimension, typename Rep2, mp::Ratio Ratio>
|
||||
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
|
||||
constexpr operator*(const Rep1& v,
|
||||
const quantity<Dimension, Rep2, Ratio>& q)
|
||||
@ -243,8 +245,8 @@ namespace units {
|
||||
return q * v;
|
||||
}
|
||||
|
||||
template<typename Dimension1, typename Rep1, class Ratio1, typename Dimension2, typename Rep2, class Ratio2,
|
||||
mp::Requires<treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_multiply<Ratio1, Ratio2>::den == 1> = true>
|
||||
template<Dimension Dimension1, typename Rep1, mp::Ratio Ratio1, Dimension Dimension2, typename Rep2, mp::Ratio Ratio2>
|
||||
requires treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_multiply<Ratio1, Ratio2>::den == 1
|
||||
quantity<dimension_multiply_t<Dimension1, Dimension2>, std::common_type_t<Rep1, Rep2>, std::ratio_multiply<Ratio1, Ratio2>>
|
||||
constexpr operator*(const quantity<Dimension1, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension2, Rep2, Ratio2>& rhs)
|
||||
@ -253,7 +255,7 @@ namespace units {
|
||||
return ret(lhs.count() * rhs.count());
|
||||
}
|
||||
|
||||
template<typename Rep1, typename Exponent, class Ratio, typename Rep2>
|
||||
template<typename Rep1, Exp Exponent, mp::Ratio Ratio, typename Rep2>
|
||||
quantity<dimension<exp_invert_t<Exponent>>, std::common_type_t<Rep1, Rep2>, Ratio>
|
||||
constexpr operator/(const Rep1& v,
|
||||
const quantity<dimension<Exponent>, Rep2, Ratio>& q)
|
||||
@ -263,7 +265,7 @@ namespace units {
|
||||
return ret(v / den(q).count());
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio, typename Rep2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio, typename Rep2>
|
||||
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
|
||||
constexpr operator/(const quantity<Dimension, Rep1, Ratio>& q,
|
||||
const Rep2& v)
|
||||
@ -272,7 +274,7 @@ namespace units {
|
||||
return ret(ret(q).count() / v);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
std::common_type_t<Rep1, Rep2>
|
||||
constexpr operator/(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
@ -281,8 +283,8 @@ namespace units {
|
||||
return cq(lhs).count() / cq(rhs).count();
|
||||
}
|
||||
|
||||
template<typename Dimension1, typename Rep1, class Ratio1, typename Dimension2, typename Rep2, class Ratio2,
|
||||
mp::Requires<treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_divide<Ratio1, Ratio2>::den == 1> = true>
|
||||
template<Dimension Dimension1, typename Rep1, mp::Ratio Ratio1, Dimension Dimension2, typename Rep2, mp::Ratio Ratio2>
|
||||
requires treat_as_floating_point_v<std::common_type_t<Rep1, Rep2>> || std::ratio_divide<Ratio1, Ratio2>::den == 1
|
||||
quantity<dimension_divide_t<Dimension1, Dimension2>, std::common_type_t<Rep1, Rep2>, std::ratio_divide<Ratio1, Ratio2>>
|
||||
constexpr operator/(const quantity<Dimension1, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension2, Rep2, Ratio2>& rhs)
|
||||
@ -291,7 +293,7 @@ namespace units {
|
||||
return ret(lhs.count() / rhs.count());
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio, typename Rep2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio, typename Rep2>
|
||||
quantity<Dimension, std::common_type_t<Rep1, Rep2>, Ratio>
|
||||
constexpr operator%(const quantity<Dimension, Rep1, Ratio>& q,
|
||||
const Rep2& v)
|
||||
@ -300,7 +302,7 @@ namespace units {
|
||||
return ret(ret(q).count() % v);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
std::common_type_t<quantity<Dimension, Rep1, Ratio1>, quantity<Dimension, Rep2, Ratio2>>
|
||||
constexpr operator%(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
@ -309,7 +311,9 @@ namespace units {
|
||||
return ret(ret(lhs).count() % ret(rhs).count());
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
// clang-format on
|
||||
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
constexpr bool operator==(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
{
|
||||
@ -317,14 +321,14 @@ namespace units {
|
||||
return ct(lhs).count() == ct(rhs).count();
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
constexpr bool operator!=(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
{
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
constexpr bool operator<(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
{
|
||||
@ -332,34 +336,33 @@ namespace units {
|
||||
return ct(lhs).count() < ct(rhs).count();
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
constexpr bool operator<=(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
{
|
||||
return !(rhs < lhs);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
constexpr bool operator>(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
{
|
||||
return rhs < lhs;
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep1, class Ratio1, typename Rep2, class Ratio2>
|
||||
template<Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
constexpr bool operator>=(const quantity<Dimension, Rep1, Ratio1>& lhs,
|
||||
const quantity<Dimension, Rep2, Ratio2>& rhs)
|
||||
{
|
||||
return !(lhs < rhs);
|
||||
}
|
||||
// clang-format on
|
||||
|
||||
} // namespace units
|
||||
|
||||
namespace std {
|
||||
|
||||
// todo: simplified
|
||||
template<typename Dimension, typename Rep1, typename Ratio1, typename Rep2, typename Ratio2>
|
||||
template<units::Dimension Dimension, typename Rep1, mp::Ratio Ratio1, typename Rep2, mp::Ratio Ratio2>
|
||||
struct common_type<units::quantity<Dimension, Rep1, Ratio1>, units::quantity<Dimension, Rep2, Ratio2>> {
|
||||
using type =
|
||||
units::quantity<Dimension, std::common_type_t<Rep1, Rep2>, mp::common_ratio_t<Ratio1, Ratio2>>;
|
||||
|
@ -23,7 +23,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "../dimension.h"
|
||||
#include "../quantity.h"
|
||||
|
||||
namespace units {
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "base_dimensions.h"
|
||||
#include "../quantity.h"
|
||||
|
||||
namespace units {
|
||||
|
||||
|
@ -23,6 +23,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "base_dimensions.h"
|
||||
#include "../quantity.h"
|
||||
|
||||
namespace units {
|
||||
|
||||
|
@ -37,16 +37,16 @@ if(NOT TARGET mp::units)
|
||||
endif()
|
||||
|
||||
# unit tests
|
||||
add_executable(unit_tests
|
||||
add_library(unit_tests
|
||||
test_dimension.cpp
|
||||
test_quantity.cpp
|
||||
test_tools.cpp
|
||||
test_type_list.cpp
|
||||
test_units.cpp
|
||||
)
|
||||
target_link_libraries(unit_tests
|
||||
PRIVATE
|
||||
mp::units
|
||||
GTest::Main
|
||||
)
|
||||
add_test(NAME units.unit_tests
|
||||
COMMAND
|
||||
|
@ -21,7 +21,6 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include "units/dimension.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include <utility>
|
||||
|
||||
using namespace units;
|
||||
@ -30,36 +29,44 @@ namespace {
|
||||
|
||||
template<int Id, int Value>
|
||||
using e = exp<dim_id<Id>, Value>;
|
||||
}
|
||||
|
||||
// make_dimension
|
||||
// exp_invert
|
||||
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>>, dimension<e<0, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>>, dimension<e<0, 1>, e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<0, 1>>, dimension<e<0, 1>, e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<1, 1>>, dimension<e<1, 2>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<1, -1>>, dimension<detail::scalar>>);
|
||||
static_assert(std::is_same_v<exp_invert_t<e<0, 1>>, e<0, -1>>);
|
||||
static_assert(std::is_same_v<exp_invert_t<e<1, -1>>, e<1, 1>>);
|
||||
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, 1>, e<1, 1>>, dimension<e<0, 2>, e<1, 2>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, -1>, e<1, -1>, e<0, -1>, e<1, -1>>, dimension<e<0, -2>, e<1, -2>>>);
|
||||
// make_dimension
|
||||
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<1, -1>>, dimension<e<0, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<0, -1>, e<1, 1>>, dimension<e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, -1>>, dimension<e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, -1>, e<1, -1>>, dimension<detail::scalar>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>>, dimension<e<0, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>>, dimension<e<0, 1>, e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<0, 1>>, dimension<e<0, 1>, e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<1, 1>>, dimension<e<1, 2>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<1, 1>, e<1, -1>>, dimension<>>);
|
||||
|
||||
// dimension_multiply
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, 1>, e<1, 1>>, dimension<e<0, 2>, e<1, 2>>>);
|
||||
static_assert(
|
||||
std::is_same_v<make_dimension_t<e<0, -1>, e<1, -1>, e<0, -1>, e<1, -1>>, dimension<e<0, -2>, e<1, -2>>>);
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<dimension_multiply_t<dimension<e<0, 1>>, dimension<e<1, 1>>>, dimension<e<0, 1>, e<1, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<3, 1>>>,
|
||||
dimension<e<0, 1>, e<1, 1>, e<2, 1>, e<3, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<1, 1>>>,
|
||||
dimension<e<0, 1>, e<1, 2>, e<2, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<1, -1>>>,
|
||||
dimension<e<0, 1>, e<2, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<1, -1>>, dimension<e<0, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<0, -1>, e<1, 1>>, dimension<e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, -1>>, dimension<e<1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension_t<e<0, 1>, e<1, 1>, e<0, -1>, e<1, -1>>, dimension<>>);
|
||||
|
||||
// dimension_divide
|
||||
// dimension_multiply
|
||||
|
||||
static_assert(std::is_same_v<dimension_divide_t<dimension<e<0, 1>>, dimension<e<1, 1>>>, dimension<e<0, 1>, e<1, -1>>>);
|
||||
static_assert(std::is_same_v<dimension_divide_t<dimension<e<0, 1>>, dimension<e<0, 1>>>, dimension<detail::scalar>>);
|
||||
static_assert(
|
||||
std::is_same_v<dimension_multiply_t<dimension<e<0, 1>>, dimension<e<1, 1>>>, dimension<e<0, 1>, e<1, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<3, 1>>>,
|
||||
dimension<e<0, 1>, e<1, 1>, e<2, 1>, e<3, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<1, 1>>>,
|
||||
dimension<e<0, 1>, e<1, 2>, e<2, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply_t<dimension<e<0, 1>, e<1, 1>, e<2, 1>>, dimension<e<1, -1>>>,
|
||||
dimension<e<0, 1>, e<2, 1>>>);
|
||||
|
||||
// dimension_divide
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<dimension_divide_t<dimension<e<0, 1>>, dimension<e<1, 1>>>, dimension<e<0, 1>, e<1, -1>>>);
|
||||
static_assert(std::is_same_v<dimension_divide_t<dimension<e<0, 1>>, dimension<e<0, 1>>>, dimension<>>);
|
||||
|
||||
} // namespace
|
||||
|
@ -21,153 +21,254 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include "units/si/velocity.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include <utility>
|
||||
|
||||
using namespace units;
|
||||
using namespace units::literals;
|
||||
|
||||
namespace {
|
||||
|
||||
template<typename T>
|
||||
class my_value {
|
||||
T data_{};
|
||||
|
||||
public:
|
||||
my_value() = default;
|
||||
constexpr my_value(T v) : data_{v} {}
|
||||
constexpr operator T() const { return data_; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename T>
|
||||
struct treat_as_floating_point<my_value<T>> : std::is_floating_point<T> {
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct quantity_values<my_value<T>> {
|
||||
static constexpr my_value<T> zero() { return my_value<T>(0); }
|
||||
static constexpr my_value<T> max() { return std::numeric_limits<T>::max(); }
|
||||
static constexpr my_value<T> min() { return std::numeric_limits<T>::lowest(); }
|
||||
};
|
||||
|
||||
} // namespace units
|
||||
|
||||
namespace std {
|
||||
|
||||
template<typename T, typename U>
|
||||
struct common_type<my_value<T>, U> : common_type<T, U> {
|
||||
};
|
||||
|
||||
template<typename T, typename U>
|
||||
struct common_type<T, my_value<U>> : common_type<T, U> {
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
using namespace units::literals;
|
||||
|
||||
// class invariants
|
||||
|
||||
// constexpr quantity<meters<int>> error(0_m); // should trigger a static_assert
|
||||
// constexpr quantity<int, float> error(0); // should trigger a static_assert
|
||||
// constexpr quantity<int, std::ratio<-1, 1>> error(0); // should trigger a static_assert
|
||||
|
||||
// member types
|
||||
|
||||
static_assert(std::is_same_v<meters<int>::rep, int>);
|
||||
static_assert(std::is_same_v<meters<float>::rep, float>);
|
||||
static_assert(std::is_same_v<meters<int>::ratio, std::ratio<1, 1>>);
|
||||
static_assert(std::is_same_v<kilometers<int>::ratio, std::ratio<1000, 1>>);
|
||||
|
||||
// constructors
|
||||
|
||||
static_assert(meters<int>().count() == 0);
|
||||
constexpr meters<int> kilometer{1000};
|
||||
static_assert(kilometer.count() == 1000);
|
||||
static_assert(meters<int>(kilometer).count() == kilometer.count());
|
||||
|
||||
static_assert(meters<int>(1).count() == 1);
|
||||
static_assert(meters<int>(my_value<int>(1)).count() == 1);
|
||||
static_assert(meters<my_value<int>>(1).count() == 1);
|
||||
// static_assert(meters<int>(1.0).count() == 1); // should not compile
|
||||
// static_assert(meters<int>(my_value<float>(1.0)).count() == 1); // should not compile
|
||||
// static_assert(meters<my_value<int>>(1.0).count() == 1); // should not compile
|
||||
static_assert(meters<float>(1.0).count() == 1.0);
|
||||
static_assert(meters<float>(my_value<float>(1.0)).count() == 1.0);
|
||||
static_assert(meters<float>(1).count() == 1.0);
|
||||
static_assert(meters<float>(my_value<int>(1)).count() == 1.0);
|
||||
static_assert(meters<float>(3.14f).count() == 3.14f);
|
||||
static_assert(meters<my_value<float>>(1.0).count() == 1.0);
|
||||
static_assert(meters<my_value<float>>(1).count() == 1.0);
|
||||
static_assert(meters<my_value<float>>(3.14f).count() == 3.14f);
|
||||
|
||||
static_assert(meters<int>(kilometer).count() == 1000);
|
||||
// static_assert(meters<int>(meters<float>(3.14)).count() == 3); // should not compile
|
||||
static_assert(meters<int>(quantity_cast<meters<int>>(3.14_m)).count() == 3);
|
||||
// static_assert(meters<int>(meters<my_value<float>>(1000.0)).count() == 1000); // should not compile
|
||||
// static_assert(meters<my_value<int>>(1000.0_m).count() == 1000); // should not compile
|
||||
static_assert(meters<float>(1000.0_m).count() == 1000.0);
|
||||
static_assert(meters<float>(meters<my_value<float>>(1000.0)).count() == 1000.0);
|
||||
static_assert(meters<my_value<float>>(1000.0_m).count() == 1000.0);
|
||||
static_assert(meters<float>(kilometer).count() == 1000.0);
|
||||
static_assert(meters<my_value<float>>(kilometer).count() == 1000.0);
|
||||
static_assert(meters<int>(1_km).count() == 1000);
|
||||
// static_assert(meters<int>(1_s).count() == 1); // should not compile
|
||||
// static_assert(kilometers<int>(1010_m).count() == 1); // should not compile
|
||||
static_assert(kilometers<int>(quantity_cast<kilometers<int>>(1010_m)).count() == 1);
|
||||
|
||||
// assignment operator
|
||||
|
||||
static_assert([]() {
|
||||
meters<int> l1(1), l2(2);
|
||||
return l2 = l1;
|
||||
}()
|
||||
.count() == 1);
|
||||
|
||||
// static member functions
|
||||
|
||||
static_assert(meters<int>::zero().count() == 0);
|
||||
static_assert(meters<int>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(meters<int>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(meters<float>::zero().count() == 0.0);
|
||||
static_assert(meters<float>::min().count() == std::numeric_limits<float>::lowest());
|
||||
static_assert(meters<float>::max().count() == std::numeric_limits<float>::max());
|
||||
static_assert(meters<my_value<int>>::zero().count() == 0);
|
||||
static_assert(meters<my_value<int>>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(meters<my_value<int>>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(meters<my_value<float>>::zero().count() == 0.0);
|
||||
static_assert(meters<my_value<float>>::min().count() == std::numeric_limits<float>::lowest());
|
||||
static_assert(meters<my_value<float>>::max().count() == std::numeric_limits<float>::max());
|
||||
|
||||
// unary member operators
|
||||
|
||||
static_assert((+kilometer).count() == 1000);
|
||||
static_assert((-kilometer).count() == -1000);
|
||||
static_assert((+(-kilometer)).count() == -1000);
|
||||
static_assert((-(-kilometer)).count() == 1000);
|
||||
|
||||
// binary member operators
|
||||
|
||||
static_assert([](auto v) {
|
||||
auto vv = v++;
|
||||
return std::make_pair(v, vv);
|
||||
}(kilometer) == std::make_pair(meters<int>(1001), meters<int>(1000)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = ++v;
|
||||
return std::make_pair(v, vv);
|
||||
}(kilometer) == std::make_pair(meters<int>(1001), meters<int>(1001)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = v--;
|
||||
return std::make_pair(v, vv);
|
||||
}(kilometer) == std::make_pair(meters<int>(999), meters<int>(1000)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = --v;
|
||||
return std::make_pair(v, vv);
|
||||
}(kilometer) == std::make_pair(meters<int>(999), meters<int>(999)));
|
||||
|
||||
// compound assignment
|
||||
|
||||
static_assert((1_m += 1_m).count() == 2);
|
||||
static_assert((2_m -= 1_m).count() == 1);
|
||||
static_assert((1_m *= 2).count() == 2);
|
||||
static_assert((2_m /= 2).count() == 1);
|
||||
static_assert((7_m %= 2).count() == 1);
|
||||
static_assert((7_m %= 2_m).count() == 1);
|
||||
|
||||
// non-member arithmetic operators
|
||||
|
||||
static_assert(std::is_same_v<decltype(meters<int>() + meters<float>()), meters<float>>);
|
||||
static_assert(std::is_same_v<decltype(meters<float>() - meters<int>()), meters<float>>);
|
||||
static_assert(std::is_same_v<decltype(meters<int>() * 1.0f), meters<float>>);
|
||||
static_assert(std::is_same_v<decltype(1.0f * meters<int>()), meters<float>>);
|
||||
static_assert(std::is_same_v<decltype(meters<int>() / 1.0f), meters<float>>);
|
||||
static_assert(std::is_same_v<decltype(meters<int>() / meters<float>()), float>);
|
||||
static_assert(std::is_same_v<decltype(meters<int>() % short(1)), meters<int>>);
|
||||
static_assert(std::is_same_v<decltype(meters<int>() % meters<short>(1)), meters<int>>);
|
||||
|
||||
static_assert((1_m + kilometer).count() == 1001);
|
||||
static_assert((1_m + 1_km).count() == 1001);
|
||||
static_assert((kilometer - 1_m).count() == 999);
|
||||
static_assert((1_km - 1_m).count() == 999);
|
||||
static_assert((2_m * 2).count() == 4);
|
||||
static_assert((3 * 3_m).count() == 9);
|
||||
static_assert((4_m / 2).count() == 2);
|
||||
static_assert(4_m / 2_m == 2);
|
||||
static_assert(4_km / 2000_m == 2);
|
||||
static_assert((7_m % 2).count() == 1);
|
||||
static_assert((7_m % 2_m).count() == 1);
|
||||
static_assert((7_km % 2000_m).count() == 1000);
|
||||
|
||||
// comparators
|
||||
|
||||
static_assert(2_m + 1_m == 3_m);
|
||||
static_assert(!(2_m + 2_m == 3_m));
|
||||
static_assert(2_m + 2_m != 3_m);
|
||||
static_assert(!(2_m + 2_m != 4_m));
|
||||
static_assert(2_m > 1_m);
|
||||
static_assert(!(1_m > 1_m));
|
||||
static_assert(1_m < 2_m);
|
||||
static_assert(!(2_m < 2_m));
|
||||
static_assert(2_m >= 1_m);
|
||||
static_assert(2_m >= 2_m);
|
||||
static_assert(!(2_m >= 3_m));
|
||||
static_assert(1_m <= 2_m);
|
||||
static_assert(2_m <= 2_m);
|
||||
static_assert(!(3_m <= 2_m));
|
||||
|
||||
static_assert(3_m == 3.0_m);
|
||||
static_assert(3_m != 3.14_m);
|
||||
static_assert(2_m > 1.0_m);
|
||||
static_assert(1.0_m < 2_m);
|
||||
static_assert(2.0_m >= 1_m);
|
||||
static_assert(1_m <= 2.0_m);
|
||||
|
||||
static_assert(1000_m == 1_km);
|
||||
static_assert(1001_m != 1_km);
|
||||
static_assert(1001_m > 1_km);
|
||||
static_assert(999_m < 1_km);
|
||||
static_assert(1000_m >= 1_km);
|
||||
static_assert(1000_m <= 1_km);
|
||||
|
||||
// is_quantity
|
||||
|
||||
static_assert(units::detail::is_quantity<millimeters<int>>::value);
|
||||
static_assert(units::detail::is_quantity<millimeters<int>>::value);
|
||||
|
||||
// common_type
|
||||
|
||||
// common_type
|
||||
static_assert(std::is_same_v<std::common_type_t<meters<int>, kilometers<int>>, meters<int>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<kilometers<long long>, meters<int>>, meters<long long>>);
|
||||
static_assert(std::is_same_v<std::common_type_t<kilometers<long long>, millimeters<float>>, millimeters<float>>);
|
||||
|
||||
static_assert(std::is_same_v<std::common_type_t<meters<int>, kilometers<int>>, meters<int>>);
|
||||
// quantity_cast
|
||||
|
||||
// static_assert(quantity_cast<int>(2_km).count() == 2000); // should not compile
|
||||
static_assert(quantity_cast<meters<int>>(2_km).count() == 2000);
|
||||
static_assert(quantity_cast<kilometers<int>>(2000_m).count() == 2);
|
||||
|
||||
// constructors
|
||||
// time
|
||||
|
||||
constexpr quantity<dimension_length, int> kilometer{1000};
|
||||
static_assert(kilometer.count() == 1000);
|
||||
static_assert(length<int>(kilometer).count() == kilometer.count());
|
||||
static_assert(meters<int>(kilometer).count() == kilometer.count());
|
||||
static_assert(millimeters<int>(kilometer).count() == kilometer.count() * 1000);
|
||||
static_assert(quantity_cast<kilometers<int>>(kilometer).count() == kilometer.count() / 1000);
|
||||
// static_assert(1_s == 1_m); // should not compile
|
||||
static_assert(1_h == 3600_s);
|
||||
|
||||
//static_assert(meters<int>(1.0) == 1_m); // should not compile
|
||||
static_assert(meters<int>(1) == 1_m);
|
||||
static_assert(meters<float>(1.0) == 1_m);
|
||||
static_assert(meters<float>(1) == 1_m);
|
||||
// length
|
||||
|
||||
//static_assert(kilometers<int>(1000_m) == 1000_m); // should not compile
|
||||
static_assert(kilometers<float>(1000_m) == 1000_m);
|
||||
//static_assert(meters<int>(meters<float>(1)) == 1_m); // should not compile
|
||||
static_assert(meters<int>(quantity_cast<meters<int>>(meters<float>(1))) == 1_m);
|
||||
static_assert(1_km == 1000_m);
|
||||
static_assert(1_km + 1_m == 1001_m);
|
||||
static_assert(10_km / 5_km == 2);
|
||||
static_assert(10_km / 2 == 5_km);
|
||||
|
||||
// velocity
|
||||
|
||||
// zero(), min(), max()
|
||||
static_assert(10_m / 5_s == 2_mps);
|
||||
static_assert(10 / 5_s * 1_m == 2_mps);
|
||||
static_assert(1_km / 1_s == 1000_mps);
|
||||
static_assert(2_kmph * 2_h == 4_km);
|
||||
static_assert(2_km / 2_kmph == 1_h);
|
||||
|
||||
static_assert(length<int>::zero().count() == 0);
|
||||
static_assert(length<int>::min().count() == std::numeric_limits<int>::min());
|
||||
static_assert(length<int>::max().count() == std::numeric_limits<int>::max());
|
||||
|
||||
|
||||
// unary member operators
|
||||
|
||||
static_assert((+1_m).count() == 1);
|
||||
static_assert((-1_m).count() == -1);
|
||||
|
||||
|
||||
// binary member operators
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
|
||||
constexpr auto post_inc(quantity<Dimension, Rep, Ratio> v)
|
||||
{
|
||||
auto vv = v++;
|
||||
return std::make_pair(v, vv);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
|
||||
constexpr auto pre_inc(quantity<Dimension, Rep, Ratio> v)
|
||||
{
|
||||
auto vv = ++v;
|
||||
return std::make_pair(v, vv);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
|
||||
constexpr auto post_dec(quantity<Dimension, Rep, Ratio> v)
|
||||
{
|
||||
auto vv = v--;
|
||||
return std::make_pair(v, vv);
|
||||
}
|
||||
|
||||
template<typename Dimension, typename Rep, class Ratio = std::ratio<1>>
|
||||
constexpr auto pre_dec(quantity<Dimension, Rep, Ratio> v)
|
||||
{
|
||||
auto vv = --v;
|
||||
return std::make_pair(v, vv);
|
||||
}
|
||||
|
||||
constexpr auto r1 = post_inc(1_m);
|
||||
static_assert(r1.first.count() == 2);
|
||||
static_assert(r1.second.count() == 1);
|
||||
|
||||
constexpr auto r2 = pre_inc(1_m);
|
||||
static_assert(r2.first.count() == 2);
|
||||
static_assert(r2.second.count() == 2);
|
||||
|
||||
constexpr auto r3 = post_dec(1_m);
|
||||
static_assert(r3.first.count() == 0);
|
||||
static_assert(r3.second.count() == 1);
|
||||
|
||||
constexpr auto r4 = pre_dec(1_m);
|
||||
static_assert(r4.first.count() == 0);
|
||||
static_assert(r4.second.count() == 0);
|
||||
|
||||
|
||||
// compound assignment
|
||||
|
||||
static_assert((1_km += 1_km).count() == 2);
|
||||
static_assert((2_km -= 1_km).count() == 1);
|
||||
static_assert((1_km *= 2).count() == 2);
|
||||
static_assert((2_km /= 2).count() == 1);
|
||||
static_assert((7_km %= 2).count() == 1);
|
||||
static_assert((7_km %= 2_km).count() == 1);
|
||||
|
||||
|
||||
// non-member arithmetic operators
|
||||
|
||||
static_assert((1_km + 1_m).count() == 1001);
|
||||
static_assert((1_km - 1_m).count() == 999);
|
||||
static_assert((2_km * 2).count() == 4);
|
||||
static_assert((3 * 3_km).count() == 9);
|
||||
static_assert((2_kmph * 2_h).count() == 4);
|
||||
//static_assert(kilometers<float>(2_kmph * 15_min).count() == 500_m); // should not compile
|
||||
static_assert(kilometers<float>(2_kmph * 120.0_min).count() == 4);
|
||||
static_assert(kilometers<float>(2.0_kmph * 120_min).count() == 4);
|
||||
static_assert((4_km / 2).count() == 2);
|
||||
static_assert(4_km / 2_km == 2);
|
||||
static_assert((1_km / 1_s).count() == 1);
|
||||
//static_assert((1_km / 1_h).count() == 1); // should not compile
|
||||
static_assert((1.0_km / 1_h).count() == 1);
|
||||
static_assert((7_km % 2).count() == 1);
|
||||
static_assert((7_km % 2_km).count() == 1);
|
||||
|
||||
|
||||
// comparators
|
||||
|
||||
static_assert(2_km + 1_km == 3_km);
|
||||
static_assert(!(2_km + 2_km == 3_km));
|
||||
static_assert(2_km + 2_km != 3_km);
|
||||
static_assert(!(2_km + 2_km != 4_km));
|
||||
static_assert(2_km > 1_km);
|
||||
static_assert(!(1_km > 1_km));
|
||||
static_assert(1_km < 2_km);
|
||||
static_assert(!(2_km < 2_km));
|
||||
static_assert(2_km >= 1_km);
|
||||
static_assert(2_km >= 2_km);
|
||||
static_assert(!(2_km >= 3_km));
|
||||
static_assert(1_km <= 2_km);
|
||||
static_assert(2_km <= 2_km);
|
||||
static_assert(!(3_km <= 2_km));
|
||||
|
||||
|
||||
|
||||
// quantity_cast
|
||||
|
||||
static_assert(quantity_cast<kilometers<int>>(2000_m) == 2_km);
|
||||
// static_assert(quantity_cast<seconds<int>>(2_m) == 2_s); // should not compile
|
||||
} // namespace
|
||||
|
47
test/test_tools.cpp
Normal file
47
test/test_tools.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2018 Mateusz Pusz
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "units/si/velocity.h"
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
|
||||
// static_sign
|
||||
|
||||
static_assert(mp::static_sign<2>::value == 1);
|
||||
static_assert(mp::static_sign<-3>::value == -1);
|
||||
static_assert(mp::static_sign<0>::value == 1);
|
||||
|
||||
// static_abs
|
||||
|
||||
static_assert(mp::static_abs<2>::value == 2);
|
||||
static_assert(mp::static_abs<-3>::value == 3);
|
||||
static_assert(mp::static_abs<0>::value == 0);
|
||||
|
||||
// common_ratio
|
||||
|
||||
static_assert(std::is_same_v<mp::common_ratio_t<std::ratio<1>, std::kilo>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<mp::common_ratio_t<std::kilo, std::ratio<1>>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<mp::common_ratio_t<std::ratio<1>, std::milli>, std::milli>);
|
||||
static_assert(std::is_same_v<mp::common_ratio_t<std::milli, std::ratio<1>>, std::milli>);
|
||||
|
||||
} // namespace
|
@ -21,57 +21,100 @@
|
||||
// SOFTWARE.
|
||||
|
||||
#include "units/dimension.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include <utility>
|
||||
|
||||
using namespace mp;
|
||||
using namespace units;
|
||||
namespace {
|
||||
|
||||
// type_list_push_front
|
||||
using namespace mp;
|
||||
using namespace units;
|
||||
|
||||
static_assert(std::is_same_v<type_list_push_front_t<type_list<>, int>, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_push_front_t<type_list<>, int, long, double>, type_list<int, long, double>>);
|
||||
static_assert(std::is_same_v<type_list_push_front_t<type_list<double>, int, long>, type_list<int, long, double>>);
|
||||
// type_list_push_front
|
||||
|
||||
// type_list_split_half
|
||||
static_assert(std::is_same_v<type_list_push_front_t<type_list<>, int>, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_push_front_t<type_list<>, int, long, double>, type_list<int, long, double>>);
|
||||
static_assert(std::is_same_v<type_list_push_front_t<type_list<double>, int, long>, type_list<int, long, double>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int>>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int>>::second_list, type_list<>>);
|
||||
// type_list_push_back
|
||||
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long>>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long>>::second_list, type_list<long>>);
|
||||
static_assert(std::is_same_v<type_list_push_back_t<type_list<>, int>, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_push_back_t<type_list<>, int, long, double>, type_list<int, long, double>>);
|
||||
static_assert(std::is_same_v<type_list_push_back_t<type_list<double>, int, long>, type_list<double, int, long>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long, double>>::first_list, type_list<int, long>>);
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long, double>>::second_list, type_list<double>>);
|
||||
// type_list_split
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::first_list, type_list<int, long>>);
|
||||
static_assert(
|
||||
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int>, 0>::first_list, type_list<>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int>, 0>::second_list, type_list<int>>);
|
||||
|
||||
// type_list_merge_sorted
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int>, 1>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int>, 1>::second_list, type_list<>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<0>>, type_list<dim_id<1>>, dim_id_less>,
|
||||
type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<1>>, type_list<dim_id<0>>, dim_id_less>,
|
||||
type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long>, 0>::first_list, type_list<>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long>, 0>::second_list, type_list<int, long>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<27>, dim_id<38>>,
|
||||
type_list<dim_id<3>, dim_id<43>>, dim_id_less>,
|
||||
type_list<dim_id<3>, dim_id<27>, dim_id<38>, dim_id<43>>>);
|
||||
static_assert(
|
||||
std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<9>, dim_id<82>>, type_list<dim_id<10>>, dim_id_less>,
|
||||
type_list<dim_id<9>, dim_id<10>, dim_id<82>>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long>, 1>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long>, 1>::second_list, type_list<long>>);
|
||||
|
||||
// type_list_sort
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long>, 2>::first_list, type_list<int, long>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long>, 2>::second_list, type_list<>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_sort_t<type_list<dim_id<0>>, dim_id_less>, type_list<dim_id<0>>>);
|
||||
static_assert(
|
||||
std::is_same_v<type_list_sort_t<type_list<dim_id<0>, dim_id<1>>, dim_id_less>, type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(
|
||||
std::is_same_v<type_list_sort_t<type_list<dim_id<1>, dim_id<0>>, dim_id_less>, type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(
|
||||
std::is_same_v<
|
||||
type_list_sort_t<type_list<dim_id<38>, dim_id<27>, dim_id<43>, dim_id<3>, dim_id<9>, dim_id<82>, dim_id<10>>,
|
||||
dim_id_less>,
|
||||
type_list<dim_id<3>, dim_id<9>, dim_id<10>, dim_id<27>, dim_id<38>, dim_id<43>, dim_id<82>>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long, double>, 1>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long, double>, 1>::second_list, type_list<long, double>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long, double>, 2>::first_list, type_list<int, long>>);
|
||||
static_assert(std::is_same_v<type_list_split<type_list<int, long, double>, 2>::second_list, type_list<double>>);
|
||||
|
||||
// type_list_split_half
|
||||
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int>>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int>>::second_list, type_list<>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long>>::first_list, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long>>::second_list, type_list<long>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long, double>>::first_list, type_list<int, long>>);
|
||||
static_assert(std::is_same_v<type_list_split_half<type_list<int, long, double>>::second_list, type_list<double>>);
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::first_list, type_list<int, long>>);
|
||||
static_assert(
|
||||
std::is_same_v<type_list_split_half<type_list<int, long, double, float>>::second_list, type_list<double, float>>);
|
||||
|
||||
// type_list_merge_sorted
|
||||
|
||||
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<0>>, type_list<dim_id<1>>, dim_id_less>,
|
||||
type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<1>>, type_list<dim_id<0>>, dim_id_less>,
|
||||
type_list<dim_id<0>, dim_id<1>>>);
|
||||
|
||||
static_assert(std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<27>, dim_id<38>>,
|
||||
type_list<dim_id<3>, dim_id<43>>, dim_id_less>,
|
||||
type_list<dim_id<3>, dim_id<27>, dim_id<38>, dim_id<43>>>);
|
||||
static_assert(
|
||||
std::is_same_v<type_list_merge_sorted_t<type_list<dim_id<9>, dim_id<82>>, type_list<dim_id<10>>, dim_id_less>,
|
||||
type_list<dim_id<9>, dim_id<10>, dim_id<82>>>);
|
||||
|
||||
// type_list_sort
|
||||
|
||||
template<typename TypeList>
|
||||
using dim_sort_t = type_list_sort_t<TypeList, dim_id_less>;
|
||||
|
||||
static_assert(std::is_same_v<dim_sort_t<type_list<dim_id<0>>>, type_list<dim_id<0>>>);
|
||||
static_assert(std::is_same_v<dim_sort_t<type_list<dim_id<0>, dim_id<1>>>, type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(std::is_same_v<dim_sort_t<type_list<dim_id<1>, dim_id<0>>>, type_list<dim_id<0>, dim_id<1>>>);
|
||||
static_assert(std::is_same_v<
|
||||
dim_sort_t<type_list<dim_id<38>, dim_id<27>, dim_id<43>, dim_id<3>, dim_id<9>, dim_id<82>, dim_id<10>>>,
|
||||
type_list<dim_id<3>, dim_id<9>, dim_id<10>, dim_id<27>, dim_id<38>, dim_id<43>, dim_id<82>>>);
|
||||
|
||||
// exp_less
|
||||
|
||||
template<int Id, int Value>
|
||||
using e = exp<dim_id<Id>, Value>;
|
||||
|
||||
template<typename TypeList>
|
||||
using exp_sort_t = type_list_sort_t<TypeList, exp_less>;
|
||||
|
||||
static_assert(std::is_same_v<exp_sort_t<dimension<e<0, 1>>>, dimension<e<0, 1>>>);
|
||||
static_assert(std::is_same_v<exp_sort_t<dimension<e<0, 1>, e<1, -1>>>, dimension<e<0, 1>, e<1, -1>>>);
|
||||
static_assert(std::is_same_v<exp_sort_t<dimension<e<1, 1>, e<0, -1>>>, dimension<e<0, -1>, e<1, 1>>>);
|
||||
|
||||
} // namespace
|
||||
|
@ -20,51 +20,51 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
|
||||
#include "units/si/frequency.h"
|
||||
#include "units/si/length.h"
|
||||
#include "units/si/time.h"
|
||||
#include "units/si/frequency.h"
|
||||
#include "units/si/velocity.h"
|
||||
#include <gtest/gtest.h>
|
||||
#include <utility>
|
||||
|
||||
using namespace units;
|
||||
using namespace units::literals;
|
||||
namespace {
|
||||
using namespace units;
|
||||
using namespace units::literals;
|
||||
|
||||
// frequency
|
||||
|
||||
// frequency
|
||||
static_assert(2 / 1_s == 2_Hz);
|
||||
static_assert(1000 / 1_s == 1_kHz);
|
||||
static_assert(3.2_GHz == 3'200'000'000_Hz);
|
||||
|
||||
static_assert(2 / 1_s == 2_Hz);
|
||||
static_assert(1000 / 1_s == 1_kHz);
|
||||
static_assert(3.2_GHz == 3'200'000'000_Hz);
|
||||
// time
|
||||
|
||||
static_assert(1_h == 3600_s);
|
||||
|
||||
// time
|
||||
// length
|
||||
|
||||
static_assert(1_h == 3600_s);
|
||||
static_assert(1_km == 1000_m);
|
||||
static_assert(1_km + 1_m == 1001_m);
|
||||
static_assert(10_km / 5_km == 2);
|
||||
static_assert(10_km / 2 == 5_km);
|
||||
|
||||
// velocity
|
||||
|
||||
// length
|
||||
static_assert(std::is_same_v<decltype(1_km / 1_s), velocity<long long int, std::ratio<1000, 1>>>);
|
||||
|
||||
static_assert(1_km == 1000_m);
|
||||
static_assert(10_km / 5_km == 2);
|
||||
static_assert(10_km / 2 == 5_km);
|
||||
static_assert(10_m / 5_s == 2_mps);
|
||||
static_assert(10 / 5_s * 1_m == 2_mps);
|
||||
static_assert(1_km / 1_s == 1000_mps);
|
||||
// static_assert(1_km / 1_h == 1_kmph); // should not compile
|
||||
static_assert(1.0_km / 1_h == 1_kmph);
|
||||
static_assert(1000.0_m / 3600.0_s == 1_kmph);
|
||||
|
||||
static_assert(2_kmph * 2_h == 4_km);
|
||||
// static_assert(2_kmph * 15_min == 500_m); // should not compile
|
||||
static_assert(2_kmph * 15.0_min == 500_m);
|
||||
static_assert(2.0_kmph * 15_min == 500_m);
|
||||
|
||||
// velocity
|
||||
static_assert(2_km / 2_kmph == 1_h);
|
||||
// static_assert(2000_m / 2_kmph == 1_h); // should not compile
|
||||
static_assert(quantity_cast<kilometers<int>>(2000_m) / 2_kmph == 1_h);
|
||||
|
||||
static_assert(std::is_same_v<decltype(1_km / 1_s), velocity<long long int, std::ratio<1000, 1>>>);
|
||||
|
||||
static_assert(10_m / 5_s == 2_mps);
|
||||
static_assert(1_km / 1_s == 1000_mps);
|
||||
//static_assert(1_km / 1_h == 1_kmph); // should not compile
|
||||
static_assert(1.0_km / 1_h == 1_kmph);
|
||||
static_assert(1000.0_m / 3600.0_s == 1_kmph);
|
||||
|
||||
static_assert(2_kmph * 2_h == 4_km);
|
||||
//static_assert(2_kmph * 15_min == 500_m); // should not compile
|
||||
static_assert(2_kmph * 15.0_min == 500_m);
|
||||
static_assert(2.0_kmph * 15_min == 500_m);
|
||||
|
||||
static_assert(2_km / 2_kmph == 1_h);
|
||||
// static_assert(2000_m / 2_kmph == 1_h); // should not compile
|
||||
static_assert(quantity_cast<kilometers<int>>(2000_m) / 2_kmph == 1_h);
|
||||
} // namespace
|
||||
|
Reference in New Issue
Block a user