CGS tests added

This commit is contained in:
Mateusz Pusz
2019-12-11 16:20:08 +01:00
parent 71f38222ea
commit 12b67923bc
8 changed files with 198 additions and 74 deletions

View File

@@ -36,4 +36,12 @@ struct dim_area : physical::dim_area<dim_area, square_centimetre, dim_length> {}
template<Unit U, Scalar Rep = double>
using area = quantity<dim_area, U, Rep>;
inline namespace literals {
// sq_cm
constexpr auto operator"" sq_cm(unsigned long long l) { return area<square_centimetre, std::int64_t>(l); }
constexpr auto operator"" sq_cm(long double l) { return area<square_centimetre, long double>(l); }
}
} // namespace units::cgs

View File

@@ -37,7 +37,9 @@ using length = quantity<dim_length, U, Rep>;
inline namespace literals {
using si::literals::operator"" cm;
// cm
constexpr auto operator"" cm(unsigned long long l) { return length<centimetre, std::int64_t>(l); }
constexpr auto operator"" cm(long double l) { return length<centimetre, long double>(l); }
}

View File

@@ -37,7 +37,9 @@ using mass = quantity<dim_mass, U, Rep>;
inline namespace literals {
using si::literals::operator"" g;
// g
constexpr auto operator""g(unsigned long long l) { return mass<gram, std::int64_t>(l); }
constexpr auto operator""g(long double l) { return mass<gram, long double>(l); }
}

View File

@@ -38,13 +38,13 @@ using mass = quantity<dim_mass, U, Rep>;
inline namespace literals {
// g
constexpr auto operator""g(unsigned long long l) { return mass<gram, std::int64_t>(l); }
constexpr auto operator""g(long double l) { return mass<gram, long double>(l); }
// g
constexpr auto operator""g(unsigned long long l) { return mass<gram, std::int64_t>(l); }
constexpr auto operator""g(long double l) { return mass<gram, long double>(l); }
// kg
constexpr auto operator""kg(unsigned long long l) { return mass<kilogram, std::int64_t>(l); }
constexpr auto operator""kg(long double l) { return mass<kilogram, long double>(l); }
// kg
constexpr auto operator""kg(unsigned long long l) { return mass<kilogram, std::int64_t>(l); }
constexpr auto operator""kg(long double l) { return mass<kilogram, long double>(l); }
} // namespace literals

View File

@@ -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
)

View File

@@ -30,77 +30,74 @@
#include <units/physical/cgs/pressure.h>
#include <units/physical/cgs/time.h>
#include <units/physical/cgs/velocity.h>
#include <units/physical/si/acceleration.h>
#include <units/physical/si/energy.h>
#include <units/physical/si/force.h>
#include <units/physical/si/length.h>
#include <units/physical/si/mass.h>
#include <units/physical/si/power.h>
#include <units/physical/si/pressure.h>
#include <units/physical/si/time.h>
#include <units/physical/si/velocity.h>
namespace {
using namespace units;
using namespace units::cgs;
static_assert(cgs::length<cgs::centimetre>(100) == si::length<si::metre>(1));
static_assert(cgs::mass<cgs::gram>(1'000) == si::mass<si::kilogram>(1));
static_assert(cgs::time<cgs::second>(1) == si::time<si::second>(1));
static_assert(cgs::velocity<cgs::centimetre_per_second>(100) == si::velocity<si::metre_per_second>(1));
static_assert(cgs::acceleration<cgs::gal>(100) == si::acceleration<si::metre_per_second_sq>(1));
static_assert(cgs::force<cgs::dyne>(100'000) == si::force<si::newton>(1));
static_assert(cgs::energy<cgs::erg>(10'000'000) == si::energy<si::joule>(1));
static_assert(cgs::power<cgs::erg_per_second>(10'000'000) == si::power<si::watt>(1));
static_assert(cgs::pressure<cgs::barye>(10) == si::pressure<si::pascal>(1));
/* ************** BASE DIMENSIONS **************** */
namespace si_test {
// length
using namespace units::si::literals;
static_assert(centimetre::symbol == "cm");
static_assert(cgs::length<cgs::centimetre>(100) == 1m);
static_assert(cgs::mass<cgs::gram>(1'000) == 1kg);
static_assert(cgs::time<cgs::second>(1) == 1s);
static_assert(cgs::velocity<cgs::centimetre_per_second>(100) == 1mps);
static_assert(cgs::acceleration<cgs::gal>(100) == 1mps_sq);
static_assert(cgs::force<cgs::dyne>(100'000) == 1N);
static_assert(cgs::energy<cgs::erg>(10'000'000) == 1_J);
static_assert(cgs::power<cgs::erg_per_second>(10'000'000) == 1W);
static_assert(cgs::pressure<cgs::barye>(10) == 1Pa);
}
namespace cgs_test {
using namespace units::cgs::literals;
static_assert(100cm == si::length<si::metre>(1));
static_assert(1'000g == si::mass<si::kilogram>(1));
static_assert(1s == si::time<si::second>(1));
static_assert(100cmps == si::velocity<si::metre_per_second>(1));
static_assert(100Gal == si::acceleration<si::metre_per_second_sq>(1));
static_assert(100'000dyn == si::force<si::newton>(1));
static_assert(10'000'000_erg == si::energy<si::joule>(1));
static_assert(10'000'000_ergps == si::power<si::watt>(1));
static_assert(10Ba == si::pressure<si::pascal>(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<double>(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<dim_velocity, centimetre_per_second>() == "cm/s");
// area
static_assert(std::is_same_v<ratio_divide<centimetre::ratio, dimension_unit<dim_length>::ratio>, ratio<1>>);
static_assert(1cm * 1cm == 1sq_cm);
static_assert(100sq_cm / 10cm == 10cm);
static_assert(detail::unit_text<dim_area, square_centimetre>() == "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<dim_power, erg_per_second>() == "erg/s");
}

View File

@@ -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 <units/physical/cgs/acceleration.h>
#include <units/physical/cgs/area.h>
#include <units/physical/cgs/energy.h>
#include <units/physical/cgs/force.h>
#include <units/physical/cgs/length.h>
#include <units/physical/cgs/mass.h>
#include <units/physical/cgs/power.h>
#include <units/physical/cgs/pressure.h>
#include <units/physical/cgs/time.h>
#include <units/physical/cgs/velocity.h>
#include <units/physical/si/acceleration.h>
#include <units/physical/si/area.h>
#include <units/physical/si/energy.h>
#include <units/physical/si/force.h>
#include <units/physical/si/length.h>
#include <units/physical/si/mass.h>
#include <units/physical/si/power.h>
#include <units/physical/si/pressure.h>
#include <units/physical/si/time.h>
#include <units/physical/si/velocity.h>
namespace {
using namespace units;
static_assert(cgs::length<cgs::centimetre>(100) == si::length<si::metre>(1));
static_assert(cgs::mass<cgs::gram>(1'000) == si::mass<si::kilogram>(1));
static_assert(cgs::time<cgs::second>(1) == si::time<si::second>(1));
static_assert(cgs::velocity<cgs::centimetre_per_second>(100) == si::velocity<si::metre_per_second>(1));
static_assert(cgs::area<cgs::square_centimetre>(10000) == si::area<si::square_metre>(1));
static_assert(cgs::acceleration<cgs::gal>(100) == si::acceleration<si::metre_per_second_sq>(1));
static_assert(cgs::force<cgs::dyne>(100'000) == si::force<si::newton>(1));
static_assert(cgs::energy<cgs::erg>(10'000'000) == si::energy<si::joule>(1));
static_assert(cgs::power<cgs::erg_per_second>(10'000'000) == si::power<si::watt>(1));
static_assert(cgs::pressure<cgs::barye>(10) == si::pressure<si::pascal>(1));
namespace si_test {
using namespace units::si::literals;
static_assert(cgs::length<cgs::centimetre>(100) == 1m);
static_assert(cgs::mass<cgs::gram>(1'000) == 1kg);
static_assert(cgs::time<cgs::second>(1) == 1s);
static_assert(cgs::velocity<cgs::centimetre_per_second>(100) == 1mps);
static_assert(cgs::acceleration<cgs::gal>(100) == 1mps_sq);
static_assert(cgs::force<cgs::dyne>(100'000) == 1N);
static_assert(cgs::energy<cgs::erg>(10'000'000) == 1_J);
static_assert(cgs::power<cgs::erg_per_second>(10'000'000) == 1W);
static_assert(cgs::pressure<cgs::barye>(10) == 1Pa);
}
namespace cgs_test {
using namespace units::cgs::literals;
static_assert(100cm == si::length<si::metre>(1));
static_assert(1'000g == si::mass<si::kilogram>(1));
static_assert(1s == si::time<si::second>(1));
static_assert(100cmps == si::velocity<si::metre_per_second>(1));
static_assert(100Gal == si::acceleration<si::metre_per_second_sq>(1));
static_assert(100'000dyn == si::force<si::newton>(1));
static_assert(10'000'000_erg == si::energy<si::joule>(1));
static_assert(10'000'000_ergps == si::power<si::watt>(1));
static_assert(10Ba == si::pressure<si::pascal>(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<double>(1Pa));
}
}
// TODO add tests for arithmetic operations and ensure that they require quantity_cast

View File

@@ -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<dim_acceleration, metre_per_second_sq>() == "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);