Catch2 support added (resolves #8)
This commit is contained in:
1 parent
44067dfd19
commit
3475f5193e
15 files changed
+230
-36
No files matched your search
@@ -0,0 +1,35 @@
|
||||
# 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.
|
||||
|
||||
add_library(unit_tests_static
|
||||
custom_unit_test.cpp
|
||||
dimension_test.cpp
|
||||
math_test.cpp
|
||||
quantity_test.cpp
|
||||
ratio_test.cpp
|
||||
type_list_test.cpp
|
||||
unit_test.cpp
|
||||
)
|
||||
target_link_libraries(unit_tests_static
|
||||
PRIVATE
|
||||
mp::units
|
||||
)
|
||||
@@ -0,0 +1,81 @@
|
||||
// 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/dimensions/voltage.h>
|
||||
#include <units/dimensions/frequency.h>
|
||||
#include <units/math.h>
|
||||
|
||||
/* ************** DERIVED DIMENSIONS THAT INCLUDE UNITS WITH SPECIAL NAMES **************** */
|
||||
|
||||
namespace {
|
||||
|
||||
struct base_dim_digital_information { static constexpr const char* value = "digital information"; };
|
||||
|
||||
struct digital_information : units::derived_dimension<digital_information, units::exp<base_dim_digital_information, 1>> {};
|
||||
|
||||
template<typename T>
|
||||
concept DigitalInformation = units::QuantityOf<T, digital_information>;
|
||||
|
||||
struct bit : units::derived_unit<bit, digital_information> {};
|
||||
struct byte : units::derived_unit<byte, digital_information, units::ratio<8>> {};
|
||||
|
||||
inline namespace literals {
|
||||
|
||||
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); }
|
||||
constexpr auto operator""_b(long double l) { return units::quantity<bit, long double>(l); }
|
||||
|
||||
constexpr auto operator""_B(unsigned long long l) { return units::quantity<byte, std::int64_t>(l); }
|
||||
constexpr auto operator""_B(long double l) { return units::quantity<byte, long double>(l); }
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
static_assert(1_B == 8_b);
|
||||
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
// power spectral density
|
||||
struct power_spectral_density : derived_dimension<power_spectral_density, units::exp<voltage, 2>, units::exp<frequency, -1>> {};
|
||||
struct sq_volt_per_hertz : derived_unit<sq_volt_per_hertz, power_spectral_density> {};
|
||||
|
||||
// amplitude spectral density
|
||||
struct amplitude_spectral_density : derived_dimension<amplitude_spectral_density, units::exp<voltage, 1>, units::exp<frequency, -1, 2>> {};
|
||||
// todo: add support for derived_unit
|
||||
//struct volt_per_sq_hertz : derived_unit<volt_per_sq_hertz, amplitude_spectral_density, volt, hertz> {};
|
||||
struct volt_per_sqrt_hertz : derived_unit<volt_per_sqrt_hertz, amplitude_spectral_density> {};
|
||||
}
|
||||
|
||||
namespace {
|
||||
|
||||
static_assert(std::is_same_v<dimension_sqrt<power_spectral_density>, amplitude_spectral_density>);
|
||||
static_assert(std::is_same_v<dimension_pow<amplitude_spectral_density, 2>, power_spectral_density>);
|
||||
|
||||
static_assert(std::is_same_v<decltype(pow<2>(quantity<volt_per_sqrt_hertz>(4))), decltype(quantity<sq_volt_per_hertz>(16))>);
|
||||
static_assert(std::is_same_v<decltype(sqrt(quantity<sq_volt_per_hertz>(16))), decltype(quantity<volt_per_sqrt_hertz>(4))>);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
// 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 {
|
||||
|
||||
struct d0 { static constexpr const char* value = "d0"; };
|
||||
struct d1 { static constexpr const char* value = "d1"; };
|
||||
struct d2 { static constexpr const char* value = "d2"; };
|
||||
struct d3 { static constexpr const char* value = "d3"; };
|
||||
|
||||
// exp_invert
|
||||
|
||||
static_assert(std::is_same_v<exp_invert<exp<d0, 1>>, exp<d0, -1>>);
|
||||
static_assert(std::is_same_v<exp_invert<exp<d1, -1>>, exp<d1, 1>>);
|
||||
|
||||
// extract
|
||||
|
||||
template<typename T>
|
||||
struct typeinfo;
|
||||
|
||||
template<typename... Ts>
|
||||
using extract = detail::extract<Ts...>::type;
|
||||
|
||||
static_assert(std::is_same_v<extract<>, dimension<>>);
|
||||
static_assert(std::is_same_v<extract<exp<d0, 1>>, dimension<exp<d0, 1>>>);
|
||||
static_assert(std::is_same_v<extract<exp<d0, 1>, exp<d1, 2>>, dimension<exp<d0, 1>, exp<d1, 2>>>);
|
||||
using dim0 = dimension<>;
|
||||
using dim1 = dimension<exp<d0, 1>>;
|
||||
using dim2 = dimension<exp<d0, 1>, exp<d1, 2>>;
|
||||
static_assert(std::is_same_v<extract<exp<dim0, 2>, exp<d0, 1>>, dimension<exp<d0, 1>>>);
|
||||
static_assert(std::is_same_v<extract<exp<dim1, 2>, exp<d0, 1>>, dimension<exp<d0, 2>, exp<d0, 1>>>);
|
||||
static_assert(std::is_same_v<extract<exp<dim2, -2>, exp<d0, 1>, exp<d1, 2>>, dimension<exp<d0, -2>, exp<d1, -4>, exp<d0, 1>, exp<d1, 2>>>);
|
||||
|
||||
// make_dimension
|
||||
|
||||
template<typename... Ts>
|
||||
using make_dimension = detail::make_dimension<Ts...>::type;
|
||||
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>>, dimension<exp<d0, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>>, dimension<exp<d0, 1>, exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d0, 1>>, dimension<exp<d0, 1>, exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, 1>>, dimension<exp<d1, 2>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, -1>>, dimension<>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d1, 1>, exp<d1, 1, 2>>, dimension<exp<d1, 3, 2>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d1, 1, 2>, exp<d1, 1, 2>>, dimension<exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d1, 2>, exp<d1, 1, 2>>, dimension<exp<d1, 5, 2>>>);
|
||||
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, 1>, exp<d1, 1>>, dimension<exp<d0, 2>, exp<d1, 2>>>);
|
||||
static_assert(
|
||||
std::is_same_v<make_dimension<exp<d0, -1>, exp<d1, -1>, exp<d0, -1>, exp<d1, -1>>, dimension<exp<d0, -2>, exp<d1, -2>>>);
|
||||
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d1, -1>>, dimension<exp<d0, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d0, -1>, exp<d1, 1>>, dimension<exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, -1>>, dimension<exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<make_dimension<exp<d0, 1>, exp<d1, 1>, exp<d0, -1>, exp<d1, -1>>, dimension<>>);
|
||||
|
||||
// dimension_multiply
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<dimension_multiply<dimension<exp<d0, 1>>, dimension<exp<d1, 1>>>, dimension<exp<d0, 1>, exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply<dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, dimension<exp<d3, 1>>>,
|
||||
dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>, exp<d3, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply<dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, dimension<exp<d1, 1>>>,
|
||||
dimension<exp<d0, 1>, exp<d1, 2>, exp<d2, 1>>>);
|
||||
static_assert(std::is_same_v<dimension_multiply<dimension<exp<d0, 1>, exp<d1, 1>, exp<d2, 1>>, dimension<exp<d1, -1>>>,
|
||||
dimension<exp<d0, 1>, exp<d2, 1>>>);
|
||||
|
||||
// dimension_divide
|
||||
|
||||
static_assert(
|
||||
std::is_same_v<dimension_divide<dimension<exp<d0, 1>>, dimension<exp<d1, 1>>>, dimension<exp<d0, 1>, exp<d1, -1>>>);
|
||||
static_assert(std::is_same_v<dimension_divide<dimension<exp<d0, 1>>, dimension<exp<d0, 1>>>, dimension<>>);
|
||||
|
||||
} // namespace
|
||||
@@ -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.
|
||||
|
||||
#include "units/dimensions/area.h"
|
||||
#include "units/math.h"
|
||||
|
||||
using namespace units;
|
||||
|
||||
namespace {
|
||||
|
||||
static_assert(std::is_same_v<decltype(pow<1>(2m)), decltype(2m)>);
|
||||
static_assert(std::is_same_v<decltype(pow<2>(2m)), decltype(4sq_m)>);
|
||||
static_assert(std::is_same_v<decltype(sqrt(4sq_m)), decltype(2m)>);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,298 @@
|
||||
// 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/dimensions/velocity.h"
|
||||
#include "units/dimensions/frequency.h"
|
||||
#include "units/dimensions/area.h"
|
||||
#include "units/math.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>, my_value<U>> : std::type_identity<my_value<common_type_t<T, U>>> {
|
||||
};
|
||||
|
||||
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<length, second, int> q; // should a static_assert
|
||||
// constexpr quantity<length, metre, quantity<metre, int>> error(0m); // should trigger a static_assert
|
||||
// constexpr quantity<int, int, double> error(0); // should trigger a static_assert
|
||||
// constexpr quantity<length, unit<length, std::ratio<-1, 1>>, int> error(0); // should trigger a static_assert
|
||||
|
||||
// member types
|
||||
|
||||
static_assert(std::is_same_v<quantity<metre, int>::rep, int>);
|
||||
static_assert(std::is_same_v<quantity<metre, double>::rep, double>);
|
||||
static_assert(std::is_same_v<quantity<metre, int>::unit, metre>);
|
||||
static_assert(std::is_same_v<quantity<kilometre, int>::unit, kilometre>);
|
||||
|
||||
// constructors
|
||||
|
||||
static_assert(quantity<metre, int>().count() == 0);
|
||||
constexpr quantity<metre, int> km{1000};
|
||||
static_assert(km.count() == 1000);
|
||||
static_assert(quantity<metre, int>(km).count() == km.count());
|
||||
|
||||
static_assert(quantity<metre, int>(1).count() == 1);
|
||||
static_assert(quantity<metre, int>(my_value(1)).count() == 1);
|
||||
static_assert(quantity<metre, my_value<int>>(1).count() == 1);
|
||||
// static_assert(quantity<metre, int>(1.0).count() == 1); // should not compile
|
||||
// static_assert(quantity<metre, int>(my_value(1.0)).count() == 1); // should not compile
|
||||
// static_assert(quantity<metre, my_value>(1.0).count() == 1); // should not compile
|
||||
static_assert(quantity<metre, double>(1.0).count() == 1.0);
|
||||
static_assert(quantity<metre, double>(my_value(1.0)).count() == 1.0);
|
||||
static_assert(quantity<metre, double>(1).count() == 1.0);
|
||||
static_assert(quantity<metre, double>(my_value(1)).count() == 1.0);
|
||||
static_assert(quantity<metre, double>(3.14).count() == 3.14);
|
||||
static_assert(quantity<metre, my_value<double>>(1.0).count() == 1.0);
|
||||
static_assert(quantity<metre, my_value<double>>(1).count() == 1.0);
|
||||
static_assert(quantity<metre, my_value<double>>(3.14).count() == 3.14);
|
||||
|
||||
static_assert(quantity<metre, int>(km).count() == 1000);
|
||||
// static_assert(quantity<metre, int>(quantity<metre, double>(3.14)).count() == 3); // should not compile
|
||||
static_assert(quantity<metre, int>(quantity_cast<quantity<metre, my_value<int>>>(3.14m)).count() == 3);
|
||||
// static_assert(quantity<metre, int>(quantity<metre, my_value<double>>(1000.0)).count() == 1000); // should not compile
|
||||
// static_assert(quantity<metre, my_value>(1000.0m).count() == 1000); // should not compile
|
||||
static_assert(quantity<metre, double>(1000.0m).count() == 1000.0);
|
||||
static_assert(quantity<metre, double>(quantity<metre, my_value<double>>(1000.0)).count() == 1000.0);
|
||||
static_assert(quantity<metre, my_value<double>>(1000.0m).count() == 1000.0);
|
||||
static_assert(quantity<metre, double>(km).count() == 1000.0);
|
||||
static_assert(quantity<metre, my_value<double>>(km).count() == 1000.0);
|
||||
static_assert(quantity<metre, int>(1km).count() == 1000);
|
||||
// static_assert(quantity<metre, int>(1_s).count() == 1); // should not compile
|
||||
// static_assert(quantity<kilometre, int>(1010m).count() == 1); // should not compile
|
||||
static_assert(quantity<kilometre, int>(quantity_cast<quantity<kilometre, my_value<int>>>(1010m)).count() == 1);
|
||||
|
||||
// assignment operator
|
||||
|
||||
static_assert([]() {
|
||||
quantity<metre, int> l1(1), l2(2);
|
||||
return l2 = l1;
|
||||
}().count() == 1);
|
||||
|
||||
// static member functions
|
||||
|
||||
static_assert(quantity<metre, int>::zero().count() == 0);
|
||||
static_assert(quantity<metre, int>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(quantity<metre, int>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(quantity<metre, double>::zero().count() == 0.0);
|
||||
static_assert(quantity<metre, double>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(quantity<metre, double>::max().count() == std::numeric_limits<double>::max());
|
||||
static_assert(quantity<metre, my_value<int>>::zero().count() == 0);
|
||||
static_assert(quantity<metre, my_value<int>>::min().count() == std::numeric_limits<int>::lowest());
|
||||
static_assert(quantity<metre, my_value<int>>::max().count() == std::numeric_limits<int>::max());
|
||||
static_assert(quantity<metre, my_value<double>>::zero().count() == 0.0);
|
||||
static_assert(quantity<metre, my_value<double>>::min().count() == std::numeric_limits<double>::lowest());
|
||||
static_assert(quantity<metre, 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(quantity<metre, int>(1001), quantity<metre, int>(1000)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = ++v;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(quantity<metre, int>(1001), quantity<metre, int>(1001)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = v--;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(quantity<metre, int>(999), quantity<metre, int>(1000)));
|
||||
static_assert([](auto v) {
|
||||
auto vv = --v;
|
||||
return std::make_pair(v, vv);
|
||||
}(km) == std::make_pair(quantity<metre, int>(999), quantity<metre, int>(999)));
|
||||
|
||||
// compound assignment
|
||||
|
||||
static_assert((1m += 1m).count() == 2);
|
||||
static_assert((2m -= 1m).count() == 1);
|
||||
static_assert((1m *= 2).count() == 2);
|
||||
static_assert((2m /= 2).count() == 1);
|
||||
static_assert((7m %= 2).count() == 1);
|
||||
static_assert((7m %= 2m).count() == 1);
|
||||
// static_assert((7.m %= 2.).count() == 1); // should not compile
|
||||
// static_assert((7.m %= 2).count() == 1); // should not compile
|
||||
// static_assert((7m %= 2.).count() == 1); // should not compile
|
||||
static_assert((7m %= 2m).count() == 1);
|
||||
// static_assert((7.m %= 2.m).count() == 1); // should not compile
|
||||
// static_assert((7.m %= 2m).count() == 1); // should not compile
|
||||
// static_assert((7m %= 2.m).count() == 1); // should not compile
|
||||
|
||||
// non-member arithmetic operators
|
||||
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() + quantity<metre, double>()), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() + quantity<metre, double>()), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<kilometre, int>() + quantity<metre, double>()), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, double>() - quantity<metre, int>()), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<kilometre, double>() - quantity<metre, int>()), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() * 1.0), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(1.0 * quantity<metre, int>()), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre_per_second, int>() * quantity<second, int>()), quantity<metre, int>>);
|
||||
static_assert(std::is_same_v<decltype(1 / quantity<second, int>()), quantity<hertz, int>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() / 1.0), quantity<metre, double>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() / quantity<metre, double>()), double>);
|
||||
static_assert(std::is_same_v<decltype(quantity<kilometre, int>() / quantity<metre, double>()), double>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() / quantity<second, int>()), quantity<metre_per_second, int>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() % short(1)), quantity<metre, int>>);
|
||||
static_assert(std::is_same_v<decltype(quantity<metre, int>() % quantity<metre, short>(1)), quantity<metre, int>>);
|
||||
|
||||
static_assert((1m + km).count() == 1001);
|
||||
static_assert((1m + 1km).count() == 1001);
|
||||
static_assert((km - 1m).count() == 999);
|
||||
static_assert((1km - 1m).count() == 999);
|
||||
static_assert((2m * 2).count() == 4);
|
||||
static_assert((3 * 3m).count() == 9);
|
||||
static_assert((4m / 2).count() == 2);
|
||||
static_assert(4m / 2m == 2);
|
||||
static_assert(4km / 2000m == 2);
|
||||
static_assert((7m % 2).count() == 1);
|
||||
static_assert((7m % 2m).count() == 1);
|
||||
static_assert((7km % 2000m).count() == 1000);
|
||||
|
||||
// comparators
|
||||
|
||||
static_assert(2m + 1m == 3m);
|
||||
static_assert(!(2m + 2m == 3m));
|
||||
static_assert(2m + 2m != 3m);
|
||||
static_assert(!(2m + 2m != 4m));
|
||||
static_assert(2m > 1m);
|
||||
static_assert(!(1m > 1m));
|
||||
static_assert(1m < 2m);
|
||||
static_assert(!(2m < 2m));
|
||||
static_assert(2m >= 1m);
|
||||
static_assert(2m >= 2m);
|
||||
static_assert(!(2m >= 3m));
|
||||
static_assert(1m <= 2m);
|
||||
static_assert(2m <= 2m);
|
||||
static_assert(!(3m <= 2m));
|
||||
|
||||
static_assert(3m == 3.0m);
|
||||
static_assert(3m != 3.14m);
|
||||
static_assert(2m > 1.0m);
|
||||
static_assert(1.0m < 2m);
|
||||
static_assert(2.0m >= 1m);
|
||||
static_assert(1m <= 2.0m);
|
||||
|
||||
static_assert(1000m == 1km);
|
||||
static_assert(1001m != 1km);
|
||||
static_assert(1001m > 1km);
|
||||
static_assert(999m < 1km);
|
||||
static_assert(1000m >= 1km);
|
||||
static_assert(1000m <= 1km);
|
||||
|
||||
// is_quantity
|
||||
|
||||
static_assert(Quantity<quantity<millimetre, int>>);
|
||||
|
||||
// common_quantity
|
||||
|
||||
static_assert(std::is_same_v<common_quantity<quantity<metre, int>, quantity<kilometre, int>>, quantity<metre, int>>);
|
||||
static_assert(std::is_same_v<common_quantity<quantity<kilometre, long long>, quantity<metre, int>>, quantity<metre, long long>>);
|
||||
static_assert(std::is_same_v<common_quantity<quantity<kilometre, long long>, quantity<millimetre, double>>, quantity<millimetre, double>>);
|
||||
|
||||
// quantity_cast
|
||||
|
||||
// static_assert(quantity_cast<int>(2km).count() == 2000); // should not compile
|
||||
static_assert(quantity_cast<quantity<metre, int>>(2km).count() == 2000);
|
||||
static_assert(quantity_cast<quantity<kilometre, int>>(2000m).count() == 2);
|
||||
|
||||
// time
|
||||
|
||||
// static_assert(1s == 1m); // should not compile
|
||||
static_assert(1h == 3600s);
|
||||
|
||||
// length
|
||||
|
||||
static_assert(1km == 1000m);
|
||||
static_assert(1km + 1m == 1001m);
|
||||
static_assert(10km / 5km == 2);
|
||||
static_assert(10km / 2 == 5km);
|
||||
|
||||
// velocity
|
||||
|
||||
static_assert(10m / 5s == 2mps);
|
||||
static_assert(10 / 5s * 1m == 2mps);
|
||||
static_assert(1km / 1s == 1000mps);
|
||||
static_assert(2kmph * 2h == 4km);
|
||||
static_assert(2km / 2kmph == 1h);
|
||||
|
||||
static_assert(std::is_same_v<decltype(pow<2>(2m)), decltype(4sq_m)>);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,66 @@
|
||||
// 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/ratio.h"
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
template<Ratio R1, Ratio R2>
|
||||
inline constexpr bool same = R1::num == R2::num && R1::den == R2::den;
|
||||
|
||||
static_assert(same<ratio<2, 4>, ratio<1, 2>>);
|
||||
|
||||
static_assert(std::is_same_v<ratio_multiply<ratio<4>, ratio<1, 8>>, ratio<1, 2>>);
|
||||
static_assert(std::is_same_v<ratio_multiply<ratio<4>, ratio<1, 2>>, ratio<2>>);
|
||||
static_assert(std::is_same_v<ratio_multiply<ratio<1, 8>, ratio<2>>, ratio<1, 4>>);
|
||||
static_assert(std::is_same_v<ratio_multiply<ratio<1, 2>, ratio<8>>, ratio<4>>);
|
||||
|
||||
static_assert(std::is_same_v<ratio_divide<ratio<4>, ratio<2>>, ratio<2>>);
|
||||
static_assert(std::is_same_v<ratio_divide<ratio<2>, ratio<8>>, ratio<1, 4>>);
|
||||
static_assert(std::is_same_v<ratio_divide<ratio<1, 8>, ratio<2>>, ratio<1, 16>>);
|
||||
static_assert(std::is_same_v<ratio_divide<ratio<6>, ratio<3>>, ratio<2>>);
|
||||
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 0>, ratio<1>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 1>, ratio<2>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 2>, ratio<4>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<2>, 3>, ratio<8>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 0>, ratio<1>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 1>, ratio<1, 2>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 2>, ratio<1, 4>>);
|
||||
static_assert(std::is_same_v<ratio_pow<ratio<1, 2>, 3>, ratio<1, 8>>);
|
||||
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<9>>, ratio<3>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<4>>, ratio<2>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<1>>, ratio<1>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<0>>, ratio<0>>);
|
||||
static_assert(std::is_same_v<ratio_sqrt<ratio<1, 4>>, ratio<1, 2>>);
|
||||
|
||||
// common_ratio
|
||||
|
||||
static_assert(std::is_same_v<common_ratio<ratio<1>, ratio<1000>>, ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio<ratio<1000>, ratio<1>>, ratio<1>>);
|
||||
static_assert(std::is_same_v<common_ratio<ratio<1>, ratio<1, 1000>>, ratio<1, 1000>>);
|
||||
static_assert(std::is_same_v<common_ratio<ratio<1, 1000>, ratio<1>>, ratio<1, 1000>>);
|
||||
|
||||
} // 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/bits/type_list.h"
|
||||
#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<type_list<>, int>, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_push_front<type_list<>, int, long, double>, type_list<int, long, double>>);
|
||||
static_assert(std::is_same_v<type_list_push_front<type_list<double>, int, long>, type_list<int, long, double>>);
|
||||
|
||||
// type_list_push_back
|
||||
|
||||
static_assert(std::is_same_v<type_list_push_back<type_list<>, int>, type_list<int>>);
|
||||
static_assert(std::is_same_v<type_list_push_back<type_list<>, int, long, double>, type_list<int, long, double>>);
|
||||
static_assert(std::is_same_v<type_list_push_back<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
|
||||
struct d0 { static constexpr const char* value = "d0"; };
|
||||
struct d1 { static constexpr const char* value = "d1"; };
|
||||
|
||||
static_assert(std::is_same_v<type_list_merge_sorted<type_list<exp<d0, 1>>, type_list<exp<d1, 1>>, exp_less>,
|
||||
type_list<exp<d0, 1>, exp<d1, 1>>>);
|
||||
static_assert(std::is_same_v<type_list_merge_sorted<type_list<exp<d1, 1>>, type_list<exp<d0, 1>>, exp_less>,
|
||||
type_list<exp<d0, 1>, exp<d1, 1>>>);
|
||||
|
||||
// type_list_sort
|
||||
|
||||
template<TypeList List>
|
||||
using exp_sort = type_list_sort<List, exp_less>;
|
||||
|
||||
static_assert(std::is_same_v<exp_sort<dimension<exp<d0, 1>>>, dimension<exp<d0, 1>>>);
|
||||
static_assert(std::is_same_v<exp_sort<dimension<exp<d0, 1>, exp<d1, -1>>>, dimension<exp<d0, 1>, exp<d1, -1>>>);
|
||||
static_assert(std::is_same_v<exp_sort<dimension<exp<d1, 1>, exp<d0, -1>>>, dimension<exp<d0, -1>, exp<d1, 1>>>);
|
||||
|
||||
} // namespace
|
||||
@@ -0,0 +1,154 @@
|
||||
// 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/dimensions/acceleration.h>
|
||||
#include <units/dimensions/area.h>
|
||||
#include <units/dimensions/capacitance.h>
|
||||
#include <units/dimensions/current.h>
|
||||
#include <units/dimensions/electric_charge.h>
|
||||
#include <units/dimensions/energy.h>
|
||||
#include <units/dimensions/force.h>
|
||||
#include <units/dimensions/frequency.h>
|
||||
#include <units/dimensions/length.h>
|
||||
#include <units/dimensions/luminous_intensity.h>
|
||||
#include <units/dimensions/mass.h>
|
||||
#include <units/dimensions/power.h>
|
||||
#include <units/dimensions/pressure.h>
|
||||
#include <units/dimensions/substance.h>
|
||||
#include <units/dimensions/temperature.h>
|
||||
#include <units/dimensions/time.h>
|
||||
#include <units/dimensions/velocity.h>
|
||||
#include <units/dimensions/voltage.h>
|
||||
#include <units/dimensions/volume.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace units;
|
||||
|
||||
/* ************** BASE DIMENSIONS **************** */
|
||||
|
||||
// time
|
||||
|
||||
static_assert(1h == 3600s);
|
||||
|
||||
// length
|
||||
|
||||
static_assert(1km == 1000m);
|
||||
static_assert(1m == 100cm);
|
||||
static_assert(1m == 1000mm);
|
||||
static_assert(1km + 1m == 1001m);
|
||||
static_assert(10km / 5km == 2);
|
||||
static_assert(100mm / 5cm == 2);
|
||||
static_assert(10km / 2 == 5km);
|
||||
|
||||
static_assert(1yd == 0.9144m);
|
||||
static_assert(1yd == 3ft);
|
||||
static_assert(1ft == 12in);
|
||||
static_assert(1mi == 1760yd);
|
||||
|
||||
static_assert(5in + 8cm == 207mm);
|
||||
|
||||
/* ************** DERIVED DIMENSIONS WITH NAMED UNITS **************** */
|
||||
|
||||
// frequency
|
||||
|
||||
static_assert(2 / 1s == 2Hz);
|
||||
static_assert(120 / 1min == 2Hz);
|
||||
static_assert(1000 / 1s == 1kHz);
|
||||
static_assert(1 / 1ms == 1kHz);
|
||||
static_assert(3.2GHz == 3'200'000'000Hz);
|
||||
static_assert(10Hz * 1min == 600);
|
||||
|
||||
// force
|
||||
|
||||
static_assert(10kg * 10mps_sq == 100N);
|
||||
|
||||
// pressure
|
||||
|
||||
static_assert(10N / 10sq_m == 1Pa);
|
||||
|
||||
// energy
|
||||
|
||||
static_assert(10N * 10m == 100_J);
|
||||
static_assert(10Pa * 10cub_m == 100_J);
|
||||
|
||||
// power
|
||||
|
||||
static_assert(10_J / 10s == 1W);
|
||||
|
||||
// electric charge
|
||||
|
||||
static_assert(10A * 10s == 100C);
|
||||
|
||||
// voltage
|
||||
|
||||
static_assert(10W / 10A == 1V);
|
||||
static_assert(10_J / 10C == 1V);
|
||||
|
||||
// capacitance
|
||||
|
||||
static_assert(10C / 10V == 1F);
|
||||
|
||||
/* ************** DERIVED DIMENSIONS IN TERMS OF BASE UNITS **************** */
|
||||
|
||||
// velocity
|
||||
|
||||
static_assert(std::is_same_v<decltype(1km / 1s), quantity<unit<velocity, ratio<1000, 1>>, std::int64_t>>);
|
||||
|
||||
static_assert(10m / 5s == 2mps);
|
||||
static_assert(10 / 5s * 1m == 2mps);
|
||||
static_assert(1km / 1s == 1000mps);
|
||||
// static_assert(1km / 1h == 1kmph); // should not compile
|
||||
static_assert(1.0km / 1h == 1kmph);
|
||||
static_assert(1000.0m / 3600.0s == 1kmph);
|
||||
|
||||
static_assert(10.0mi / 2h == 5mph);
|
||||
|
||||
static_assert(2kmph * 2h == 4km);
|
||||
// static_assert(2kmph * 15min == 500m); // should not compile
|
||||
static_assert(2kmph * 15.0min == 500m);
|
||||
static_assert(2.0kmph * 15min == 500m);
|
||||
|
||||
static_assert(2km / 2kmph == 1h);
|
||||
// static_assert(2000m / 2kmph == 1h); // should not compile
|
||||
static_assert(quantity_cast<quantity<kilometre, int>>(2000m) / 2kmph == 1h);
|
||||
|
||||
// acceleration
|
||||
|
||||
static_assert(10mps / 10s == 1mps_sq);
|
||||
|
||||
// area
|
||||
|
||||
static_assert(1m * 1m == 1sq_m);
|
||||
static_assert(10km * 10km == 100sq_km);
|
||||
static_assert(1sq_m == 10'000sq_cm);
|
||||
|
||||
// volume
|
||||
|
||||
static_assert(1m * 1m * 1m == 1cub_m);
|
||||
static_assert(10sq_m * 10m == 100cub_m);
|
||||
static_assert(10km * 10km * 10km == 1000cub_km);
|
||||
static_assert(1cub_m == 1'000'000cub_cm);
|
||||
|
||||
} // namespace
|
||||
Reference in new issue
Block a user