From 12b67923bc45fe89ff84a43aabf2dc287412e88a Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Wed, 11 Dec 2019 16:20:08 +0100 Subject: [PATCH] CGS tests added --- src/include/units/physical/cgs/area.h | 8 ++ src/include/units/physical/cgs/length.h | 4 +- src/include/units/physical/cgs/mass.h | 4 +- src/include/units/physical/si/mass.h | 12 +-- test/unit_test/static/CMakeLists.txt | 1 + test/unit_test/static/cgs_test.cpp | 125 ++++++++++++------------ test/unit_test/static/si_cgs_test.cpp | 111 +++++++++++++++++++++ test/unit_test/static/si_test.cpp | 7 +- 8 files changed, 198 insertions(+), 74 deletions(-) create mode 100644 test/unit_test/static/si_cgs_test.cpp diff --git a/src/include/units/physical/cgs/area.h b/src/include/units/physical/cgs/area.h index 4eac9f6a..c9618a53 100644 --- a/src/include/units/physical/cgs/area.h +++ b/src/include/units/physical/cgs/area.h @@ -36,4 +36,12 @@ struct dim_area : physical::dim_area {} template using area = quantity; +inline namespace literals { + +// sq_cm +constexpr auto operator"" sq_cm(unsigned long long l) { return area(l); } +constexpr auto operator"" sq_cm(long double l) { return area(l); } + +} + } // namespace units::cgs diff --git a/src/include/units/physical/cgs/length.h b/src/include/units/physical/cgs/length.h index 3b672ecd..a49a4cb8 100644 --- a/src/include/units/physical/cgs/length.h +++ b/src/include/units/physical/cgs/length.h @@ -37,7 +37,9 @@ using length = quantity; inline namespace literals { -using si::literals::operator"" cm; +// cm +constexpr auto operator"" cm(unsigned long long l) { return length(l); } +constexpr auto operator"" cm(long double l) { return length(l); } } diff --git a/src/include/units/physical/cgs/mass.h b/src/include/units/physical/cgs/mass.h index c58d1ca1..d809eb32 100644 --- a/src/include/units/physical/cgs/mass.h +++ b/src/include/units/physical/cgs/mass.h @@ -37,7 +37,9 @@ using mass = quantity; inline namespace literals { -using si::literals::operator"" g; +// g +constexpr auto operator""g(unsigned long long l) { return mass(l); } +constexpr auto operator""g(long double l) { return mass(l); } } diff --git a/src/include/units/physical/si/mass.h b/src/include/units/physical/si/mass.h index c6c59d15..7af3df31 100644 --- a/src/include/units/physical/si/mass.h +++ b/src/include/units/physical/si/mass.h @@ -38,13 +38,13 @@ using mass = quantity; inline namespace literals { - // g - constexpr auto operator""g(unsigned long long l) { return mass(l); } - constexpr auto operator""g(long double l) { return mass(l); } +// g +constexpr auto operator""g(unsigned long long l) { return mass(l); } +constexpr auto operator""g(long double l) { return mass(l); } - // kg - constexpr auto operator""kg(unsigned long long l) { return mass(l); } - constexpr auto operator""kg(long double l) { return mass(l); } +// kg +constexpr auto operator""kg(unsigned long long l) { return mass(l); } +constexpr auto operator""kg(long double l) { return mass(l); } } // namespace literals diff --git a/test/unit_test/static/CMakeLists.txt b/test/unit_test/static/CMakeLists.txt index 4b39dc76..790ee380 100644 --- a/test/unit_test/static/CMakeLists.txt +++ b/test/unit_test/static/CMakeLists.txt @@ -30,6 +30,7 @@ add_library(unit_tests_static quantity_test.cpp ratio_test.cpp si_test.cpp + si_cgs_test.cpp type_list_test.cpp unit_test.cpp ) diff --git a/test/unit_test/static/cgs_test.cpp b/test/unit_test/static/cgs_test.cpp index 07348f4e..e58bb4d4 100644 --- a/test/unit_test/static/cgs_test.cpp +++ b/test/unit_test/static/cgs_test.cpp @@ -30,77 +30,74 @@ #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include namespace { using namespace units; +using namespace units::cgs; -static_assert(cgs::length(100) == si::length(1)); -static_assert(cgs::mass(1'000) == si::mass(1)); -static_assert(cgs::time(1) == si::time(1)); -static_assert(cgs::velocity(100) == si::velocity(1)); -static_assert(cgs::acceleration(100) == si::acceleration(1)); -static_assert(cgs::force(100'000) == si::force(1)); -static_assert(cgs::energy(10'000'000) == si::energy(1)); -static_assert(cgs::power(10'000'000) == si::power(1)); -static_assert(cgs::pressure(10) == si::pressure(1)); +/* ************** BASE DIMENSIONS **************** */ -namespace si_test { +// length -using namespace units::si::literals; +static_assert(centimetre::symbol == "cm"); -static_assert(cgs::length(100) == 1m); -static_assert(cgs::mass(1'000) == 1kg); -static_assert(cgs::time(1) == 1s); -static_assert(cgs::velocity(100) == 1mps); -static_assert(cgs::acceleration(100) == 1mps_sq); -static_assert(cgs::force(100'000) == 1N); -static_assert(cgs::energy(10'000'000) == 1_J); -static_assert(cgs::power(10'000'000) == 1W); -static_assert(cgs::pressure(10) == 1Pa); - -} - -namespace cgs_test { - -using namespace units::cgs::literals; - -static_assert(100cm == si::length(1)); -static_assert(1'000g == si::mass(1)); -static_assert(1s == si::time(1)); -static_assert(100cmps == si::velocity(1)); -static_assert(100Gal == si::acceleration(1)); -static_assert(100'000dyn == si::force(1)); -static_assert(10'000'000_erg == si::energy(1)); -static_assert(10'000'000_ergps == si::power(1)); -static_assert(10Ba == si::pressure(1)); - -} - -namespace both_test { - -using namespace units::si::literals; -using namespace units::cgs::literals; - -static_assert(100cm == 1m); -static_assert(1'000g == 1kg); -static_assert(1s == 1s); -static_assert(100cmps == 1mps); -static_assert(100Gal == 1mps_sq); -static_assert(100'000dyn == 1N); -static_assert(10'000'000_erg == 1_J); -static_assert(10'000'000_ergps == 1W); -static_assert(10Ba == quantity_cast(1Pa)); - -} +// mass + +// time + +/* ************** DERIVED DIMENSIONS IN TERMS OF BASE UNITS **************** */ + +// velocity + +static_assert(10cm / 5s == 2cmps); +static_assert(10cm / 2cmps == 5s); +static_assert(10cm == 2cmps * 5s); + +static_assert(detail::unit_text() == "cm/s"); + +// area +static_assert(std::is_same_v::ratio>, ratio<1>>); + +static_assert(1cm * 1cm == 1sq_cm); +static_assert(100sq_cm / 10cm == 10cm); + +static_assert(detail::unit_text() == "cm²"); + +/* ************** DERIVED DIMENSIONS WITH NAMED UNITS **************** */ + +// acceleration + +static_assert(10cmps / 10s == 1Gal); +static_assert(10cmps / 1Gal == 10s); +static_assert(1Gal * 10s == 10cmps); + +// force + +static_assert(10g * 10Gal == 100dyn); +static_assert(100dyn / 10g == 10Gal); +static_assert(100dyn / 10Gal == 10g); + +// pressure + +static_assert(10dyn / 10sq_cm == 1Ba); +static_assert(10dyn / 1Ba == 10sq_cm); +static_assert(1Ba * 10sq_cm == 10dyn); + +// energy + +static_assert(10dyn * 10cm == 100_erg); +static_assert(100_erg / 10cm == 10dyn); +static_assert(100_erg / 10dyn == 10cm); + +/* ************** DERIVED DIMENSIONS IN TERMS OF OTHER UNITS **************** */ + +// power + +static_assert(10_erg / 10s == 1_ergps); +static_assert(1_ergps * 10s == 10_erg); +static_assert(10_erg / 1_ergps == 10s); + +static_assert(detail::unit_text() == "erg/s"); } diff --git a/test/unit_test/static/si_cgs_test.cpp b/test/unit_test/static/si_cgs_test.cpp new file mode 100644 index 00000000..151e920f --- /dev/null +++ b/test/unit_test/static/si_cgs_test.cpp @@ -0,0 +1,111 @@ +// 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace { + +using namespace units; + +static_assert(cgs::length(100) == si::length(1)); +static_assert(cgs::mass(1'000) == si::mass(1)); +static_assert(cgs::time(1) == si::time(1)); +static_assert(cgs::velocity(100) == si::velocity(1)); +static_assert(cgs::area(10000) == si::area(1)); +static_assert(cgs::acceleration(100) == si::acceleration(1)); +static_assert(cgs::force(100'000) == si::force(1)); +static_assert(cgs::energy(10'000'000) == si::energy(1)); +static_assert(cgs::power(10'000'000) == si::power(1)); +static_assert(cgs::pressure(10) == si::pressure(1)); + +namespace si_test { + +using namespace units::si::literals; + +static_assert(cgs::length(100) == 1m); +static_assert(cgs::mass(1'000) == 1kg); +static_assert(cgs::time(1) == 1s); +static_assert(cgs::velocity(100) == 1mps); +static_assert(cgs::acceleration(100) == 1mps_sq); +static_assert(cgs::force(100'000) == 1N); +static_assert(cgs::energy(10'000'000) == 1_J); +static_assert(cgs::power(10'000'000) == 1W); +static_assert(cgs::pressure(10) == 1Pa); + +} + +namespace cgs_test { + +using namespace units::cgs::literals; + +static_assert(100cm == si::length(1)); +static_assert(1'000g == si::mass(1)); +static_assert(1s == si::time(1)); +static_assert(100cmps == si::velocity(1)); +static_assert(100Gal == si::acceleration(1)); +static_assert(100'000dyn == si::force(1)); +static_assert(10'000'000_erg == si::energy(1)); +static_assert(10'000'000_ergps == si::power(1)); +static_assert(10Ba == si::pressure(1)); + +} + +namespace both_test { + +using namespace units::si::literals; +using namespace units::cgs::literals; + +// static_assert(100cm == 1m); // ambiguous +// static_assert(1'000g == 1kg); // ambiguous +static_assert(1s == 1s); +static_assert(100cmps == 1mps); +static_assert(100Gal == 1mps_sq); +static_assert(100'000dyn == 1N); +static_assert(10'000'000_erg == 1_J); +static_assert(10'000'000_ergps == 1W); +static_assert(10Ba == quantity_cast(1Pa)); + +} + +} + +// TODO add tests for arithmetic operations and ensure that they require quantity_cast diff --git a/test/unit_test/static/si_test.cpp b/test/unit_test/static/si_test.cpp index 6179f669..1fec088e 100644 --- a/test/unit_test/static/si_test.cpp +++ b/test/unit_test/static/si_test.cpp @@ -123,11 +123,13 @@ static_assert(2 / 1Hz == 2s); // force static_assert(10kg * 10mps_sq == 100N); static_assert(100N / 1mps_sq == 100kg); -static_assert(100.N / 1kg == 100mps_sq); +static_assert(100N / 1kg == 100mps_sq); // pressure static_assert(10N / 10sq_m == 1Pa); +static_assert(10N / 1Pa == 10sq_m); +static_assert(1Pa * 10sq_m == 10N); // energy @@ -223,7 +225,8 @@ static_assert(detail::unit_text() == "m/s // area -static_assert(1m * 1m == 1sq_m); +static_assert(10m * 10m == 100sq_m); +static_assert(100sq_m / 10m == 10m); static_assert(10km * 10km == 100sq_km); static_assert(1sq_m == 10'000sq_cm);