From bf3abc59f2c6026c8e66df6a62c578c4420d5a28 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 28 Apr 2024 16:02:00 -0400 Subject: [PATCH] xo-unit: test: move natural_unit tests out of unit.test.cpp + tidy --- utest/natural_unit.test.cpp | 172 +++++++++++++++++++++++++++++++--- utest/unit.test.cpp | 180 ------------------------------------ 2 files changed, 160 insertions(+), 192 deletions(-) diff --git a/utest/natural_unit.test.cpp b/utest/natural_unit.test.cpp index d3c0f756..854d8fe8 100644 --- a/utest/natural_unit.test.cpp +++ b/utest/natural_unit.test.cpp @@ -1,9 +1,17 @@ /* @file natural_unit.test.cpp */ +#include "xo/unit/scaled_unit.hpp" +#include "xo/unit/scaled_unit_iostream.hpp" #include "xo/unit/natural_unit.hpp" +#include "xo/indentlog/scope.hpp" +#include "xo/indentlog/print/tag.hpp" #include namespace xo { + using xo::qty::detail::su_ratio; + using xo::qty::detail::nu_ratio_inplace; + using xo::qty::detail::nu_maker; + namespace qty { using nu64_type = natural_unit; @@ -15,21 +23,161 @@ namespace xo { constexpr nu_abbrev_type nu_mpl_abbrev = nu.abbrev(); TEST_CASE("natural_unit", "[natural_unit]") { - //constexpr bool c_debug_flag = false; - - // can get bits from /dev/random by uncommenting the 2nd line below - //uint64_t seed = xxx; - //rng::Seed seed; - - //auto rng = xo::rng::xoshiro256ss(seed); - - //scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.bpu")); - //log && log("(A)", xtag("foo", foo)); - static_assert(nu_mpl_abbrev == nu::gram.abbrev()); REQUIRE(nu_mpl_abbrev == nu::gram.abbrev()); - } /*TEST_CASE(natural_unit)*/ + + TEST_CASE("natural_unit0", "[natural_unit]") { + constexpr bool c_debug_flag = false; + + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit0")); + + { + constexpr natural_unit v + = (nu_maker::make_nu + (bpu(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)), + bpu(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1)))); + + static_assert(v.n_bpu() == 2); + + log && log(xtag("v.abbrev", v.abbrev())); + + static_assert(v.abbrev().size() > 0); + static_assert(v.abbrev() == flatstring("mm^2.mg^-1")); + } + } /*TEST_CASE(natural_unit0)*/ + + TEST_CASE("natural_unit1", "[natural_unit]") { + constexpr bool c_debug_flag = false; + + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit1")); + + { + constexpr natural_unit v + = (nu_maker::make_nu + (bpu(dim::distance, scalefactor_ratio_type(1000, 1), power_ratio_type(2, 1)))); + + static_assert(v.n_bpu() == 1); + + log && log(xtag("v.abbrev", v.abbrev())); + + static_assert(v.abbrev().size() > 0); + static_assert(v.abbrev() == flatstring("km^2")); + } + } /*TEST_CASE(natural_unit1)*/ + + TEST_CASE("natural_unit2", "[natural_unit]") { + constexpr bool c_debug_flag = false; + + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit2")); + + { + constexpr natural_unit v + = (nu_maker::make_nu + (bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)), + bpu(dim::distance, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)), + bpu(dim::time, scalefactor_ratio_type(1, 1), power_ratio_type(-2, 1)))); + + static_assert(v.n_bpu() == 3); + + log && log(xtag("v.abbrev", v.abbrev())); + + static_assert(v.abbrev().size() > 0); + static_assert(v.abbrev() == flatstring("kg.m.s^-2")); + } + } /*TEST_CASE(natural_unit2)*/ + + TEST_CASE("natural_unit3", "[natural_unit]") { + constexpr bool c_debug_flag = false; + + scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit3")); + + { + constexpr natural_unit v + = (nu_maker::make_nu + (bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)), + bpu(dim::distance, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)))); + + static_assert(v.n_bpu() == 2); + + log && log(xtag("v.abbrev", v.abbrev())); + + static_assert(v.abbrev().size() > 0); + static_assert(v.abbrev() == flatstring("kg.m")); + + { + natural_unit w = v; + + nu_ratio_inplace(&w, + bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1))); + + log && log(xtag("w.abbrev", w.abbrev())); + + REQUIRE(w.n_bpu() == 1); + REQUIRE(w[0].native_dim() == dim::distance); + REQUIRE(w.abbrev() == flatstring("m")); + } + + { + constexpr natural_unit w + = (nu_maker::make_nu + (bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)))); + + static_assert(w.n_bpu() == 1); + + log && log(xtag("w.abbrev", w.abbrev())); + + constexpr auto rr = su_ratio(v, w); + + log && log(xtag("rr", rr)); + + REQUIRE(rr.natural_unit_.n_bpu() == 1); + REQUIRE(rr.natural_unit_[0].native_dim() == dim::distance); + REQUIRE(rr.natural_unit_.abbrev() == flatstring("m")); + } + + { + constexpr natural_unit w + = (nu_maker::make_nu + (bpu(dim::time, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)))); + + static_assert(w.n_bpu() == 1); + + log && log(xtag("w.abbrev", w.abbrev())); + + constexpr auto rr = su_ratio(v, w); + + log && log(xtag("rr", rr)); + + REQUIRE(rr.natural_unit_.n_bpu() == 3); + REQUIRE(rr.natural_unit_[0].native_dim() == dim::mass); + REQUIRE(rr.natural_unit_[1].native_dim() == dim::distance); + REQUIRE(rr.natural_unit_[2].native_dim() == dim::time); + REQUIRE(rr.natural_unit_.abbrev() == flatstring("kg.m.s^-1")); + } + + { + natural_unit w = v; + + REQUIRE(w.n_bpu() == 2); + REQUIRE(w[0].native_dim() == dim::mass); + + nu_ratio_inplace(&w, + bpu(dim::time, scalefactor_ratio_type(1, 1), power_ratio_type(2, 1))); + + REQUIRE(w.n_bpu() == 3); + REQUIRE(w[0].native_dim() == dim::mass); + REQUIRE(w[1].native_dim() == dim::distance); + REQUIRE(w[2].native_dim() == dim::time); + + log && log(xtag("w.abbrev", w.abbrev())); + + REQUIRE(w.n_bpu() == 3); + REQUIRE(w.abbrev() == flatstring("kg.m.s^-2")); + } + } + } /*TEST_CASE(natural_unit3)*/ + } /*namespace qty*/ } /*namespace xo*/ diff --git a/utest/unit.test.cpp b/utest/unit.test.cpp index 7e08294d..020c2e0d 100644 --- a/utest/unit.test.cpp +++ b/utest/unit.test.cpp @@ -44,7 +44,6 @@ namespace xo { using xo::qty::detail::nu_maker; using xo::qty::detail::su_product; //using xo::qty::detail::su_bpu_product; - using xo::qty::detail::nu_ratio_inplace; using xo::qty::detail::su_ratio; using xo::qty::unit_qty; @@ -621,185 +620,6 @@ namespace xo { } } /*TEST_CASE(bpu_array_product3)*/ - TEST_CASE("natural_unit0", "[natural_unit]") { - constexpr bool c_debug_flag = true; - - // can get bits from /dev/random by uncommenting the 2nd line below - //uint64_t seed = xxx; - //rng::Seed seed; - - //auto rng = xo::rng::xoshiro256ss(seed); - - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit0")); - //log && log("(A)", xtag("foo", foo)); - - { - constexpr natural_unit v - = (nu_maker::make_nu - (bpu(dim::distance, scalefactor_ratio_type(1, 1000), power_ratio_type(2, 1)), - bpu(dim::mass, scalefactor_ratio_type(1, 1000), power_ratio_type(-1, 1)))); - - static_assert(v.n_bpu() == 2); - - log && log(xtag("v.abbrev", v.abbrev())); - - static_assert(v.abbrev().size() > 0); - static_assert(v.abbrev() == flatstring("mm^2.mg^-1")); - } - } /*TEST_CASE(natural_unit0)*/ - - TEST_CASE("natural_unit1", "[natural_unit]") { - constexpr bool c_debug_flag = true; - - // can get bits from /dev/random by uncommenting the 2nd line below - //uint64_t seed = xxx; - //rng::Seed seed; - - //auto rng = xo::rng::xoshiro256ss(seed); - - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit1")); - //log && log("(A)", xtag("foo", foo)); - - { - constexpr natural_unit v - = (nu_maker::make_nu - (bpu(dim::distance, scalefactor_ratio_type(1000, 1), power_ratio_type(2, 1)))); - - static_assert(v.n_bpu() == 1); - - log && log(xtag("v.abbrev", v.abbrev())); - - static_assert(v.abbrev().size() > 0); - static_assert(v.abbrev() == flatstring("km^2")); - } - } /*TEST_CASE(natural_unit1)*/ - - TEST_CASE("natural_unit2", "[natural_unit]") { - constexpr bool c_debug_flag = true; - - // can get bits from /dev/random by uncommenting the 2nd line below - //uint64_t seed = xxx; - //rng::Seed seed; - - //auto rng = xo::rng::xoshiro256ss(seed); - - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit2")); - //log && log("(A)", xtag("foo", foo)); - - { - constexpr natural_unit v - = (nu_maker::make_nu - (bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)), - bpu(dim::distance, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)), - bpu(dim::time, scalefactor_ratio_type(1, 1), power_ratio_type(-2, 1)))); - - static_assert(v.n_bpu() == 3); - - log && log(xtag("v.abbrev", v.abbrev())); - - static_assert(v.abbrev().size() > 0); - static_assert(v.abbrev() == flatstring("kg.m.s^-2")); - } - } /*TEST_CASE(natural_unit2)*/ - - TEST_CASE("natural_unit3", "[natural_unit]") { - constexpr bool c_debug_flag = true; - - // can get bits from /dev/random by uncommenting the 2nd line below - //uint64_t seed = xxx; - //rng::Seed seed; - - //auto rng = xo::rng::xoshiro256ss(seed); - - scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.natural_unit3")); - //log && log("(A)", xtag("foo", foo)); - - { - constexpr natural_unit v - = (nu_maker::make_nu - (bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)), - bpu(dim::distance, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)))); - - static_assert(v.n_bpu() == 2); - - log && log(xtag("v.abbrev", v.abbrev())); - - static_assert(v.abbrev().size() > 0); - static_assert(v.abbrev() == flatstring("kg.m")); - - { - natural_unit w = v; - - nu_ratio_inplace(&w, - bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1))); - - log && log(xtag("w.abbrev", w.abbrev())); - - REQUIRE(w.n_bpu() == 1); - REQUIRE(w[0].native_dim() == dim::distance); - REQUIRE(w.abbrev() == flatstring("m")); - } - - { - constexpr natural_unit w - = (nu_maker::make_nu - (bpu(dim::mass, scalefactor_ratio_type(1000, 1), power_ratio_type(1, 1)))); - - static_assert(w.n_bpu() == 1); - - log && log(xtag("w.abbrev", w.abbrev())); - - constexpr auto rr = su_ratio(v, w); - - log && log(xtag("rr", rr)); - - REQUIRE(rr.natural_unit_.n_bpu() == 1); - REQUIRE(rr.natural_unit_[0].native_dim() == dim::distance); - REQUIRE(rr.natural_unit_.abbrev() == flatstring("m")); - } - - { - constexpr natural_unit w - = (nu_maker::make_nu - (bpu(dim::time, scalefactor_ratio_type(1, 1), power_ratio_type(1, 1)))); - - static_assert(w.n_bpu() == 1); - - log && log(xtag("w.abbrev", w.abbrev())); - - constexpr auto rr = su_ratio(v, w); - - log && log(xtag("rr", rr)); - - REQUIRE(rr.natural_unit_.n_bpu() == 3); - REQUIRE(rr.natural_unit_[0].native_dim() == dim::mass); - REQUIRE(rr.natural_unit_[1].native_dim() == dim::distance); - REQUIRE(rr.natural_unit_[2].native_dim() == dim::time); - REQUIRE(rr.natural_unit_.abbrev() == flatstring("kg.m.s^-1")); - } - - { - natural_unit w = v; - - REQUIRE(w.n_bpu() == 2); - REQUIRE(w[0].native_dim() == dim::mass); - - nu_ratio_inplace(&w, - bpu(dim::time, scalefactor_ratio_type(1, 1), power_ratio_type(2, 1))); - - REQUIRE(w.n_bpu() == 3); - REQUIRE(w[0].native_dim() == dim::mass); - REQUIRE(w[1].native_dim() == dim::distance); - REQUIRE(w[2].native_dim() == dim::time); - - log && log(xtag("w.abbrev", w.abbrev())); - - REQUIRE(w.n_bpu() == 3); - REQUIRE(w.abbrev() == flatstring("kg.m.s^-2")); - } - } - } /*TEST_CASE(natural_unit3)*/ - TEST_CASE("scaled_unit0", "[scaled_unit0]") { constexpr bool c_debug_flag = true;