diff --git a/include/xo/unit/natural_unit.hpp b/include/xo/unit/natural_unit.hpp index 445c3eb1..414266ac 100644 --- a/include/xo/unit/natural_unit.hpp +++ b/include/xo/unit/natural_unit.hpp @@ -11,6 +11,8 @@ namespace xo { namespace qty { + using nu_abbrev_type = flatstring<32>; + /** @class natural_unit * @brief an array representing the cartesian product of distinct basis-power-units * @@ -32,6 +34,18 @@ namespace xo { constexpr std::size_t n_bpu() const { return n_bpu_; } constexpr bpu * bpu_v() const { return bpu_v_; } + constexpr nu_abbrev_type abbrev() const { + nu_abbrev_type retval; + + for (std::size_t i = 0; i < n_bpu_; ++i) { + if (i > 0) + retval.append("."); + retval.append(bpu_v_[i].abbrev(), 0, -1); + } + + return retval; + } + constexpr void push_back(const bpu & bpu) { if (n_bpu_ < n_dim) bpu_v_[n_bpu_++] = bpu; diff --git a/utest/unit.test.cpp b/utest/unit.test.cpp index b364b22a..7d0d6856 100644 --- a/utest/unit.test.cpp +++ b/utest/unit.test.cpp @@ -751,6 +751,87 @@ 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 + = (bpu_array_maker::make_bpu_array + (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 + = (bpu_array_maker::make_bpu_array + (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 + = (bpu_array_maker::make_bpu_array + (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("scaled_unit0", "[scaled_unit0]") { constexpr bool c_debug_flag = true;