Metabench support and tests added
This commit is contained in:
1 parent
a63b5ab111
commit
7bebf6e1e0
14 files changed
+1334
-12
No files matched your search
@@ -0,0 +1,34 @@
|
||||
# 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.
|
||||
|
||||
# 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
|
||||
)
|
||||
@@ -0,0 +1,72 @@
|
||||
// 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/dimension.h"
|
||||
#include <utility>
|
||||
|
||||
using namespace units;
|
||||
|
||||
namespace {
|
||||
|
||||
template<int Id, int Value>
|
||||
using e = exp<dim_id<Id>, Value>;
|
||||
|
||||
// exp_invert
|
||||
|
||||
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>>);
|
||||
|
||||
// make_dimension
|
||||
|
||||
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<>>);
|
||||
|
||||
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<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_multiply
|
||||
|
||||
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<1, 2>, e<0, 1>, 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
|
||||
@@ -0,0 +1,284 @@
|
||||
// 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/velocity.h"
|
||||
#include "units/frequency.h"
|
||||
#include <utility>
|
||||
#include <chrono>
|
||||
|
||||
using namespace units;
|
||||
|
||||
namespace {
|
||||
|
||||
template<typename T>
|
||||
class my_value {
|
||||
T value_{};
|
||||
public:
|
||||
my_value() = default;
|
||||
constexpr my_value(T v) : value_{std::move(v)} {}
|
||||
constexpr my_value& operator+=(const my_value& other) { value_ += other.value_; return *this; }
|
||||
constexpr my_value& operator-=(const my_value& other) { value_ -= other.value_; return *this; }
|
||||
constexpr my_value& operator*=(const my_value& other) { value_ *= other.value_; return *this; }
|
||||
constexpr my_value& operator/=(const my_value& other) { value_ /= other.value_; return *this; }
|
||||
constexpr operator const T&() const & { return value_; }
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace units {
|
||||
|
||||
template<typename T>
|
||||
inline constexpr bool treat_as_floating_point<my_value<T>> = std::is_floating_point_v<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;
|
||||
|
||||
// class invariants
|
||||
|
||||
// constexpr quantity<dimension_length, second, int> q; // should a static_assert
|
||||
// constexpr quantity<dimension_length, meter, length<meter, int>> error(0_m); // should trigger a static_assert
|
||||
// constexpr quantity<int, int, double> error(0); // should trigger a static_assert
|
||||
// constexpr quantity<dimension_length, unit<dimension_length, std::ratio<-1, 1>>, int> error(0); // should trigger a static_assert
|
||||
|
||||
// member types
|
||||
|
||||
static_assert(std::is_same_v<length<meter, int>::rep, int>);
|
||||
static_assert(std::is_same_v<length<meter, double>::rep, double>);
|
||||
static_assert(std::is_same_v<length<meter, int>::unit, meter>);
|
||||
static_assert(std::is_same_v<length<kilometer, int>::unit, kilometer>);
|
||||
|
||||
// constructors
|
||||
|
||||
static_assert(length<meter, int>().count() == 0);
|
||||
constexpr length<meter, int> km{1000};
|
||||
static_assert(km.count() == 1000);
|
||||
static_assert(length<meter, int>(km).count() == km.count());
|
||||
|
||||
static_assert(length<meter, int>(1).count() == 1);
|
||||
static_assert(length<meter, int>(my_value(1)).count() == 1);
|
||||
static_assert(length<meter, my_value<int>>(1).count() == 1);
|
||||
// static_assert(length<meter, int>(1.0).count() == 1); // should not compile
|
||||
// static_assert(length<meter, int>(my_value(1.0)).count() == 1); // should not compile
|
||||
// static_assert(length<meter, my_value>(1.0).count() == 1); // should not compile
|
||||
static_assert(length<meter, double>(1.0).count() == 1.0);
|
||||
static_assert(length<meter, double>(my_value(1.0)).count() == 1.0);
|
||||
static_assert(length<meter, double>(1).count() == 1.0);
|
||||
static_assert(length<meter, double>(my_value(1)).count() == 1.0);
|
||||
static_assert(length<meter, double>(3.14).count() == 3.14);
|
||||
static_assert(length<meter, my_value<double>>(1.0).count() == 1.0);
|
||||
static_assert(length<meter, my_value<double>>(1).count() == 1.0);
|
||||
static_assert(length<meter, my_value<double>>(3.14).count() == 3.14);
|
||||
|
||||
static_assert(length<meter, int>(km).count() == 1000);
|
||||
// static_assert(length<meter, int>(length<meter, double>(3.14)).count() == 3); // should not compile
|
||||
static_assert(length<meter, int>(quantity_cast<length<meter, my_value<int>>>(3.14_m)).count() == 3);
|
||||
// static_assert(length<meter, int>(length<meter, my_value<double>>(1000.0)).count() == 1000); // should not compile
|
||||
// static_assert(length<meter, my_value>(1000.0_m).count() == 1000); // should not compile
|
||||
static_assert(length<meter, double>(1000.0_m).count() == 1000.0);
|
||||
static_assert(length<meter, double>(length<meter, my_value<double>>(1000.0)).count() == 1000.0);
|
||||
static_assert(length<meter, my_value<double>>(1000.0_m).count() == 1000.0);
|
||||
static_assert(length<meter, double>(km).count() == 1000.0);
|
||||
static_assert(length<meter, my_value<double>>(km).count() == 1000.0);
|
||||
static_assert(length<meter, int>(1_km).count() == 1000);
|
||||
// static_assert(length<meter, int>(1_s).count() == 1); // should not compile
|
||||
// static_assert(length<kilometer, int>(1010_m).count() == 1); // should not compile
|
||||
static_assert(length<kilometer, int>(quantity_cast<length<kilometer, my_value<int>>>(1010_m)).count() == 1);
|
||||
|
||||
// assignment operator
|
||||
|
||||
static_assert([]() {
|
||||
length<meter, int> l1(1), l2(2);
|
||||
return l2 = l1;
|
||||
}()
|
||||
.count() == 1);
|
||||
|
||||
// static member functions
|
||||
|
||||
static_assert(length<meter, int>::zero().count() == 0);
|
||||
static_assert(length<meter, int>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(length<meter, int>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(length<meter, double>::zero().count() == 0.0);
|
||||
static_assert(length<meter, double>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(length<meter, double>::max().count() == std::numeric_limits<double>::max());
|
||||
static_assert(length<meter, my_value<int>>::zero().count() == 0);
|
||||
static_assert(length<meter, my_value<int>>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(length<meter, my_value<int>>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(length<meter, my_value<double>>::zero().count() == 0.0);
|
||||
static_assert(length<meter, my_value<double>>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(length<meter, my_value<double>>::max().count() == std::numeric_limits<double>::max());
|
||||
|
||||
// unary member operators
|
||||
|
||||
static_assert((+km).count() == 1000);
|
||||
static_assert((-km).count() == -1000);
|
||||
static_assert((+(-km)).count() == -1000);
|
||||
static_assert((-(-km)).count() == 1000);
|
||||
|
||||
// binary member operators
|
||||
|
||||
static_assert([](auto v) {
|
||||
auto vv = v++;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(length<meter, int>(1001), length<meter, int>(1000)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = ++v;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(length<meter, int>(1001), length<meter, int>(1001)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = v--;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(length<meter, int>(999), length<meter, int>(1000)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = --v;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(length<meter, int>(999), length<meter, 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(length<meter, int>() + length<meter, double>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() + length<meter, double>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<kilometer, int>() + length<meter, double>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, double>() - length<meter, int>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<kilometer, double>() - length<meter, int>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() * 1.0), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(1.0 * length<meter, int>()), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(velocity<meter_per_second, int>() * units::time<second, int>()), quantity<dimension_length, units::meter, int>>);
|
||||
static_assert(std::is_same_v<decltype(1 / units::time<second, int>()), quantity<dimension_frequency, hertz, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / 1.0), quantity<dimension_length, meter, double>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / length<meter, double>()), double>);
|
||||
static_assert(std::is_same_v<decltype(length<kilometer, int>() / length<meter, double>()), double>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() / units::time<second, int>()), quantity<dimension_velocity, meter_per_second, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() % short(1)), quantity<dimension_length, meter, int>>);
|
||||
static_assert(std::is_same_v<decltype(length<meter, int>() % length<meter, short>(1)), quantity<dimension_length, meter, int>>);
|
||||
|
||||
static_assert((1_m + km).count() == 1001);
|
||||
static_assert((1_m + 1_km).count() == 1001);
|
||||
static_assert((km - 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(Quantity<length<millimeter, int>>);
|
||||
|
||||
// common_quantity
|
||||
|
||||
static_assert(std::is_same_v<common_quantity_t<length<meter, int>, length<kilometer, int>>, length<meter, int>>);
|
||||
static_assert(std::is_same_v<common_quantity_t<length<kilometer, long long>, length<meter, int>>, length<meter, long long>>);
|
||||
static_assert(std::is_same_v<common_quantity_t<length<kilometer, long long>, length<millimeter, double>>, length<millimeter, double>>);
|
||||
|
||||
// quantity_cast
|
||||
|
||||
// static_assert(quantity_cast<int>(2_km).count() == 2000); // should not compile
|
||||
static_assert(quantity_cast<length<meter, int>>(2_km).count() == 2000);
|
||||
static_assert(quantity_cast<length<kilometer, int>>(2000_m).count() == 2);
|
||||
|
||||
// time
|
||||
|
||||
// static_assert(1_s == 1_m); // should not compile
|
||||
static_assert(1_h == 3600_s);
|
||||
|
||||
// length
|
||||
|
||||
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
|
||||
|
||||
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);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,48 @@
|
||||
// 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/bits/ratio_tools.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
// static_sign
|
||||
|
||||
static_assert(static_sign<2>::value == 1);
|
||||
static_assert(static_sign<-3>::value == -1);
|
||||
static_assert(static_sign<0>::value == 1);
|
||||
|
||||
// static_abs
|
||||
|
||||
static_assert(static_abs<2>::value == 2);
|
||||
static_assert(static_abs<-3>::value == 3);
|
||||
static_assert(static_abs<0>::value == 0);
|
||||
|
||||
// common_ratio
|
||||
|
||||
static_assert(std::is_same_v<common_ratio_t<std::ratio<1>, std::kilo>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio_t<std::kilo, std::ratio<1>>, std::ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio_t<std::ratio<1>, std::milli>, std::milli>);
|
||||
static_assert(std::is_same_v<common_ratio_t<std::milli, std::ratio<1>>, std::milli>);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,122 @@
|
||||
// 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/dimension.h"
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
template<typename... Types>
|
||||
struct type_list;
|
||||
|
||||
// type_list_push_front
|
||||
|
||||
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_back
|
||||
|
||||
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>>);
|
||||
|
||||
// type_list_split
|
||||
|
||||
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>>);
|
||||
|
||||
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_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_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>>);
|
||||
|
||||
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_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<TypeList List>
|
||||
using dim_sort_t = type_list_sort_t<List, 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_dim_id_less
|
||||
|
||||
template<int Id, int Value>
|
||||
using e = exp<dim_id<Id>, Value>;
|
||||
|
||||
template<TypeList List>
|
||||
using exp_sort_t = type_list_sort_t<List, exp_dim_id_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
|
||||
@@ -0,0 +1,103 @@
|
||||
// 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/time.h>
|
||||
#include <units/length.h>
|
||||
#include <units/mass.h>
|
||||
#include <units/current.h>
|
||||
#include <units/current.h>
|
||||
#include <units/temperature.h>
|
||||
#include <units/substance.h>
|
||||
#include <units/luminous_intensity.h>
|
||||
|
||||
#include <units/frequency.h>
|
||||
#include <units/velocity.h>
|
||||
#include <units/area.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
/* ************** BASE DIMENSIONS **************** */
|
||||
|
||||
// time
|
||||
|
||||
static_assert(1_h == 3600_s);
|
||||
|
||||
// length
|
||||
|
||||
static_assert(1_km == 1000_m);
|
||||
static_assert(1_m == 100_cm);
|
||||
static_assert(1_m == 1000_mm);
|
||||
static_assert(1_km + 1_m == 1001_m);
|
||||
static_assert(10_km / 5_km == 2);
|
||||
static_assert(10_km / 2 == 5_km);
|
||||
|
||||
static_assert(1_yd == 0.9144_m);
|
||||
static_assert(1_yd == 3_ft);
|
||||
static_assert(1_ft == 12_in);
|
||||
static_assert(1_mi == 1760_yd);
|
||||
|
||||
// static_assert(5_in + 8_cm == 207_mm);
|
||||
|
||||
|
||||
|
||||
/* ************** DERIVED DIMENSIONS **************** */
|
||||
|
||||
// frequency
|
||||
|
||||
static_assert(2 / 1_s == 2_Hz);
|
||||
static_assert(1000 / 1_s == 1_kHz);
|
||||
static_assert(1 / 1_ms == 1_kHz);
|
||||
static_assert(3.2_GHz == 3'200'000'000_Hz);
|
||||
|
||||
// velocity
|
||||
|
||||
static_assert(std::is_same_v<decltype(1_km / 1_s), velocity<unit<dimension_velocity, std::ratio<1000, 1>>, std::int64_t>>);
|
||||
|
||||
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(10.0_mi / 2_h == 5_mph);
|
||||
|
||||
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<length<kilometer, int>>(2000_m) / 2_kmph == 1_h);
|
||||
|
||||
// area
|
||||
|
||||
static_assert(1_m * 1_m == 1_sq_m);
|
||||
static_assert(10_km * 10_km == 100_sq_km);
|
||||
static_assert(1_sq_m == 10'000_sq_cm);
|
||||
|
||||
} // namespace
|
||||
Reference in new issue
Block a user