xo-unit: + separate basis-unit test
This commit is contained in:
parent
376393b292
commit
f535e393e8
6 changed files with 228 additions and 164 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "xo/unit/unit2.hpp"
|
||||
#include "xo/unit/Quantity_iostream.hpp"
|
||||
#include "xo/unit/scaled_unit_iostream.hpp"
|
||||
#include "xo/unit/Quantity.hpp"
|
||||
#include "xo/unit/scaled_unit_iostream.hpp"
|
||||
#include "xo/unit/natural_unit.hpp"
|
||||
|
|
@ -13,6 +14,7 @@
|
|||
#include "xo/unit/native_unit.hpp"
|
||||
#include "xo/indentlog/scope.hpp"
|
||||
#include "xo/indentlog/print/tag.hpp"
|
||||
#include "xo/flatstring/int128_iostream.hpp"
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
namespace xo {
|
||||
|
|
@ -25,12 +27,11 @@ namespace xo {
|
|||
using xo::qty::Quantity;
|
||||
using xo::qty::dim;
|
||||
using xo::qty::basis_unit2_abbrev_type;
|
||||
using xo::qty::native_unit2_v;
|
||||
using xo::qty::scalefactor_ratio_type;
|
||||
using xo::qty::units::scaled_native_unit2_abbrev;
|
||||
//using xo::qty::units::scaled_native_unit2_abbrev_v;
|
||||
using xo::qty::basis_unit;
|
||||
using xo::qty::abbrev::basis_unit2_abbrev;;
|
||||
using xo::qty::abbrev::basis_unit2_abbrev;
|
||||
using xo::qty::bpu_abbrev_type;
|
||||
using xo::qty::abbrev::bpu_abbrev;
|
||||
using xo::qty::basis_unit2_store;
|
||||
|
|
@ -38,155 +39,15 @@ namespace xo {
|
|||
using xo::qty::abbrev::flatstring_from_exponent;
|
||||
using xo::qty::bpu;
|
||||
using xo::qty::detail::bpu2_rescale;
|
||||
using xo::qty::detail::bpu2_product;
|
||||
//using xo::qty::detail::bpu2_product;
|
||||
using xo::qty::natural_unit;
|
||||
using xo::qty::detail::nu_maker;
|
||||
using xo::qty::detail::su_product;
|
||||
using xo::qty::detail::su_bpu_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;
|
||||
|
||||
TEST_CASE("basis_unit", "[basis_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<xoshio256ss> seed;
|
||||
|
||||
//auto rng = xo::rng::xoshiro256ss(seed);
|
||||
|
||||
scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.basis_unit"));
|
||||
//log && log("(A)", xtag("foo", foo));
|
||||
|
||||
static_assert(native_unit2_v[static_cast<int>(dim::mass)].native_dim() == dim::mass);
|
||||
static_assert(native_unit2_v[static_cast<int>(dim::distance)].native_dim() == dim::distance);
|
||||
static_assert(native_unit2_v[static_cast<int>(dim::time)].native_dim() == dim::time);
|
||||
static_assert(native_unit2_v[static_cast<int>(dim::time)].native_dim() == dim::time);
|
||||
static_assert(native_unit2_v[static_cast<int>(dim::currency)].native_dim() == dim::currency);
|
||||
static_assert(native_unit2_v[static_cast<int>(dim::price)].native_dim() == dim::price);
|
||||
|
||||
log && log(xtag("mass*10^3", basis_unit2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1))));
|
||||
|
||||
static_assert(basis_unit2_abbrev(dim::mass, scalefactor_ratio_type(1000, 1))
|
||||
== basis_unit2_abbrev_type::from_chars("kg"));
|
||||
|
||||
log && log("---------------------");
|
||||
|
||||
/* note: using CHECK to make test show up in coverage */
|
||||
|
||||
# define REQUIRE_x2(x) static_assert(x); REQUIRE(x)
|
||||
|
||||
REQUIRE_x2(bu::picogram.abbrev() == basis_unit2_abbrev_type::from_chars("pg"));
|
||||
REQUIRE_x2(bu::nanogram.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("ng"));
|
||||
REQUIRE_x2(bu::microgram.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("ug"));
|
||||
REQUIRE_x2(bu::milligram.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("mg"));
|
||||
REQUIRE_x2(bu::gram.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("g"));
|
||||
REQUIRE_x2(bu::kilogram.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("kg"));
|
||||
REQUIRE_x2(bu::tonne.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("t"));
|
||||
REQUIRE_x2(bu::kilotonne.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("kt"));
|
||||
REQUIRE_x2(bu::megatonne.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("Mt"));
|
||||
|
||||
log && log(xtag("distance", basis_unit2_abbrev(dim::distance, scalefactor_ratio_type(1, 1))));
|
||||
|
||||
REQUIRE_x2(bu::picometre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("pm"));
|
||||
REQUIRE_x2(bu::nanometre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("nm"));
|
||||
REQUIRE_x2(bu::micrometre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("um"));
|
||||
REQUIRE_x2(bu::millimetre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("mm"));
|
||||
REQUIRE_x2(bu::metre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("m"));
|
||||
REQUIRE_x2(bu::kilometre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("km"));
|
||||
REQUIRE_x2(bu::megametre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("Mm"));
|
||||
REQUIRE_x2(bu::gigametre.abbrev()
|
||||
== basis_unit2_abbrev_type::from_chars("Gm"));
|
||||
|
||||
REQUIRE_x2(bu::lightsecond.abbrev() == basis_unit2_abbrev_type::from_chars("lsec"));
|
||||
REQUIRE_x2(bu::astronomicalunit.abbrev() == basis_unit2_abbrev_type::from_chars("AU"));
|
||||
|
||||
log && log(xtag("time", basis_unit2_abbrev(dim::time, scalefactor_ratio_type(1, 1))));
|
||||
|
||||
REQUIRE_x2(bu::second.abbrev() == basis_unit2_abbrev_type::from_chars("s"));
|
||||
REQUIRE_x2(bu::picosecond.abbrev() == basis_unit2_abbrev_type::from_chars("ps"));
|
||||
REQUIRE_x2(bu::nanosecond.abbrev() == basis_unit2_abbrev_type::from_chars("ns"));
|
||||
REQUIRE_x2(bu::microsecond.abbrev() == basis_unit2_abbrev_type::from_chars("us"));
|
||||
REQUIRE_x2(bu::millisecond.abbrev() == basis_unit2_abbrev_type::from_chars("ms"));
|
||||
REQUIRE_x2(bu::second.abbrev() == basis_unit2_abbrev_type::from_chars("s"));
|
||||
REQUIRE_x2(bu::minute.abbrev() == basis_unit2_abbrev_type::from_chars("min"));
|
||||
REQUIRE_x2(bu::hour.abbrev() == basis_unit2_abbrev_type::from_chars("hr"));
|
||||
REQUIRE_x2(bu::day.abbrev() == basis_unit2_abbrev_type::from_chars("dy"));
|
||||
REQUIRE_x2(bu::week.abbrev() == basis_unit2_abbrev_type::from_chars("wk"));
|
||||
REQUIRE_x2(bu::month.abbrev() == basis_unit2_abbrev_type::from_chars("mo"));
|
||||
|
||||
REQUIRE_x2(bu::year.abbrev() == basis_unit2_abbrev_type::from_chars("yr"));
|
||||
REQUIRE_x2(bu::year250.abbrev() == basis_unit2_abbrev_type::from_chars("yr250"));
|
||||
REQUIRE_x2(bu::year360.abbrev() == basis_unit2_abbrev_type::from_chars("yr360"));
|
||||
REQUIRE_x2(bu::year365.abbrev() == basis_unit2_abbrev_type::from_chars("yr365"));
|
||||
|
||||
log && log(xtag("currency", basis_unit2_abbrev(dim::currency, scalefactor_ratio_type(1, 1))));
|
||||
|
||||
REQUIRE_x2(bu::currency.abbrev() == flatstring("ccy"));
|
||||
|
||||
log && log(xtag("price", basis_unit2_abbrev(dim::price, scalefactor_ratio_type(1, 1))));
|
||||
|
||||
REQUIRE_x2(bu::price.abbrev() == flatstring("px"));
|
||||
|
||||
# undef REQUIRE_x2
|
||||
|
||||
#ifdef OBSOLETE
|
||||
log && log("---------------------");
|
||||
|
||||
log && log(xtag("mass*10^-9", scaled_native_unit2_abbrev_v<dim::mass, 1, 1000000000>));
|
||||
log && log(xtag("mass*10^-6", scaled_native_unit2_abbrev_v<dim::mass, 1, 1000000>));
|
||||
log && log(xtag("mass*10^-3", scaled_native_unit2_abbrev_v<dim::mass, 1, 1000>));
|
||||
log && log(xtag("mass", scaled_native_unit2_abbrev_v<dim::mass>));
|
||||
log && log(xtag("mass*10^3", scaled_native_unit2_abbrev_v<dim::mass, 1000, 1>));
|
||||
log && log(xtag("mass*10^6", scaled_native_unit2_abbrev_v<dim::mass, 1000000, 1>));
|
||||
log && log(xtag("mass*10^9", scaled_native_unit2_abbrev_v<dim::mass, 1000000000, 1>));
|
||||
|
||||
log && log(xtag("distance*10^-9", scaled_native_unit2_abbrev_v<dim::distance, 1, 1000000000>));
|
||||
log && log(xtag("distance*10^-6", scaled_native_unit2_abbrev_v<dim::distance, 1, 1000000>));
|
||||
log && log(xtag("distance*10^-3", scaled_native_unit2_abbrev_v<dim::distance, 1, 1000>));
|
||||
log && log(xtag("distance", scaled_native_unit2_abbrev_v<dim::distance>));
|
||||
log && log(xtag("distance*10^3", scaled_native_unit2_abbrev_v<dim::distance, 1000, 1>));
|
||||
|
||||
log && log(xtag("time*10^-9", scaled_native_unit2_abbrev_v<dim::time, 1, 1000000000>));
|
||||
log && log(xtag("time*10^-6", scaled_native_unit2_abbrev_v<dim::time, 1, 1000000>));
|
||||
log && log(xtag("time*10^-3", scaled_native_unit2_abbrev_v<dim::time, 1, 1000>));
|
||||
log && log(xtag("time", scaled_native_unit2_abbrev_v<dim::time>));
|
||||
log && log(xtag("time*60", scaled_native_unit2_abbrev_v<dim::time, 60, 1>));
|
||||
log && log(xtag("time*3600", scaled_native_unit2_abbrev_v<dim::time, 3600, 1>));
|
||||
log && log(xtag("time*24*3600", scaled_native_unit2_abbrev_v<dim::time, 24*3600, 1>));
|
||||
log && log(xtag("time*250*24*3600", scaled_native_unit2_abbrev_v<dim::time, 250*24*3600, 1>));
|
||||
log && log(xtag("time*360*24*3600", scaled_native_unit2_abbrev_v<dim::time, 360*24*3600, 1>));
|
||||
log && log(xtag("time*365*24*3600", scaled_native_unit2_abbrev_v<dim::time, 365*24*3600, 1>));
|
||||
|
||||
log && log(xtag("currency", scaled_native_unit2_abbrev_v<dim::currency>));
|
||||
|
||||
log && log(xtag("price", scaled_native_unit2_abbrev_v<dim::price>));
|
||||
|
||||
REQUIRE(xo::qty::units::scaled_native_unit2_abbrev<dim::mass>::value == xo::flatstring("g"));
|
||||
|
||||
/* proof that scaled_native_unit2_abbrev::value is constexpr */
|
||||
static_assert(scaled_native_unit2_abbrev_v<dim::mass>
|
||||
== basis_unit2_abbrev_type::from_flatstring(xo::flatstring("g")));
|
||||
#endif
|
||||
|
||||
} /*TEST_CASE(basis_unit)*/
|
||||
|
||||
TEST_CASE("basis_unit2_store", "[basis_unit2_store]") {
|
||||
constexpr bool c_debug_flag = false;
|
||||
|
||||
|
|
@ -362,20 +223,20 @@ namespace xo {
|
|||
constexpr auto p_floor = orig_bpu.power().floor();
|
||||
static_assert(p_floor == 1);
|
||||
|
||||
constexpr auto p_frac = orig_bpu.power().frac().template to<double>();
|
||||
constexpr auto p_frac = orig_bpu.power().frac().template convert_to<double>();
|
||||
static_assert(p_frac == 0.0);
|
||||
|
||||
constexpr auto outer_sf_exact = mult.power(p_floor);
|
||||
static_assert(outer_sf_exact.num() == 1);
|
||||
static_assert(outer_sf_exact.den() == 1000);
|
||||
|
||||
constexpr auto mult_inexact = mult.template to<double>();
|
||||
constexpr auto mult_inexact = mult.template convert_to<double>();
|
||||
static_assert(mult_inexact == 0.001);
|
||||
|
||||
constexpr auto rr = bpu2_rescale<int64_t>(orig_bpu, scalefactor_ratio_type(1000000, 1));
|
||||
|
||||
static_assert(rr.bpu_rescaled_.power() == power_ratio_type(1,1));
|
||||
static_assert(rr.outer_scale_exact_ == outer_sf_exact);
|
||||
static_assert(rr.outer_scale_factor_ == outer_sf_exact);
|
||||
static_assert(rr.outer_scale_sq_ == 1.0);
|
||||
}
|
||||
|
||||
|
|
@ -398,23 +259,23 @@ namespace xo {
|
|||
constexpr auto p_floor = orig_bpu.power().floor();
|
||||
static_assert(p_floor == 0);
|
||||
|
||||
constexpr auto p_frac = orig_bpu.power().frac().template to<double>();
|
||||
constexpr auto p_frac = orig_bpu.power().frac().template convert_to<double>();
|
||||
static_assert(p_frac == -0.5);
|
||||
|
||||
constexpr auto outer_sf_exact = mult.power(p_floor);
|
||||
static_assert(outer_sf_exact.num() == 1);
|
||||
static_assert(outer_sf_exact.den() == 1);
|
||||
|
||||
constexpr auto mult_inexact = mult.template to<double>();
|
||||
constexpr auto mult_inexact = mult.template convert_to<double>();
|
||||
static_assert(mult_inexact == 12.0);
|
||||
|
||||
constexpr auto rr = bpu2_rescale<int64_t>(orig_bpu, scalefactor_ratio_type(30*24*3600, 1));
|
||||
|
||||
log && log(xtag("rr.outer_scale_exact", rr.outer_scale_exact_),
|
||||
log && log(xtag("rr.outer_scale_exact", rr.outer_scale_factor_),
|
||||
xtag("rr.outer_scale_sq", rr.outer_scale_sq_));
|
||||
|
||||
static_assert(rr.bpu_rescaled_.power() == power_ratio_type(-1,2));
|
||||
static_assert(rr.outer_scale_exact_ == outer_sf_exact);
|
||||
static_assert(rr.outer_scale_factor_ == outer_sf_exact);
|
||||
static_assert(rr.outer_scale_sq_ == 12.0);
|
||||
}
|
||||
|
||||
|
|
@ -437,23 +298,23 @@ namespace xo {
|
|||
constexpr auto p_floor = orig_bpu.power().floor();
|
||||
static_assert(p_floor == -1);
|
||||
|
||||
constexpr auto p_frac = orig_bpu.power().frac().template to<double>();
|
||||
constexpr auto p_frac = orig_bpu.power().frac().template convert_to<double>();
|
||||
static_assert(p_frac == -0.5);
|
||||
|
||||
constexpr auto outer_sf_exact = mult.power(p_floor);
|
||||
static_assert(outer_sf_exact.num() == 1);
|
||||
static_assert(outer_sf_exact.den() == 12);
|
||||
|
||||
constexpr auto mult_inexact = mult.template to<double>();
|
||||
constexpr auto mult_inexact = mult.template convert_to<double>();
|
||||
static_assert(mult_inexact == 12.0);
|
||||
|
||||
constexpr auto rr = bpu2_rescale<int64_t>(orig_bpu, scalefactor_ratio_type(30*24*3600, 1));
|
||||
|
||||
log && log(xtag("rr.outer_scale_exact", rr.outer_scale_exact_),
|
||||
log && log(xtag("rr.outer_scale_exact", rr.outer_scale_factor_),
|
||||
xtag("rr.outer_scale_sq", rr.outer_scale_sq_));
|
||||
|
||||
static_assert(rr.bpu_rescaled_.power() == power_ratio_type(-3,2));
|
||||
static_assert(rr.outer_scale_exact_ == outer_sf_exact);
|
||||
static_assert(rr.outer_scale_factor_ == outer_sf_exact);
|
||||
static_assert(rr.outer_scale_sq_ == 12.0);
|
||||
}
|
||||
} /*TEST_CASE(bpu_rescale)*/
|
||||
|
|
@ -481,6 +342,7 @@ namespace xo {
|
|||
power_ratio_type(1,2));
|
||||
static_assert(bpu_y.native_dim() == dim::time);
|
||||
|
||||
#ifdef NOT_USING
|
||||
constexpr auto bpu_prod = bpu2_product<int64_t>(bpu_x, bpu_y);
|
||||
|
||||
log && log(xtag("bpu_prod.bpu_rescaled", bpu_prod.bpu_rescaled_));
|
||||
|
|
@ -492,6 +354,7 @@ namespace xo {
|
|||
static_assert(bpu_prod.bpu_rescaled_.power() == power_ratio_type(-1, 1));
|
||||
static_assert(bpu_prod.outer_scale_exact_ == scalefactor_ratio_type(1,1));
|
||||
static_assert(bpu_prod.outer_scale_sq_ == 1.0);
|
||||
#endif
|
||||
}
|
||||
} /*TEST_CASE(bpu_product)*/
|
||||
|
||||
|
|
@ -518,6 +381,7 @@ namespace xo {
|
|||
power_ratio_type(1,2));
|
||||
static_assert(bpu_y.native_dim() == dim::time);
|
||||
|
||||
#ifdef NOT_USING
|
||||
constexpr auto bpu_prod = bpu2_product<int64_t>(bpu_x, bpu_y);
|
||||
|
||||
log && log(xtag("bpu_prod.bpu_rescaled", bpu_prod.bpu_rescaled_));
|
||||
|
|
@ -529,6 +393,7 @@ namespace xo {
|
|||
static_assert(bpu_prod.bpu_rescaled_.power() == power_ratio_type(-1, 1));
|
||||
static_assert(bpu_prod.outer_scale_exact_ == scalefactor_ratio_type(1,1));
|
||||
static_assert(bpu_prod.outer_scale_sq_ == 1.0/12.0);
|
||||
#endif
|
||||
}
|
||||
} /*TEST_CASE(bpu_product2)*/
|
||||
|
||||
|
|
@ -594,6 +459,7 @@ namespace xo {
|
|||
|
||||
static_assert(bpu.power() == power_ratio_type(-1, 2));
|
||||
|
||||
#ifdef NOT_USING
|
||||
constexpr auto prod_rr = su_bpu_product(v, bpu);
|
||||
|
||||
log && log(xtag("prod_rr.bpu_array", prod_rr.natural_unit_));
|
||||
|
|
@ -612,6 +478,7 @@ namespace xo {
|
|||
static_assert(prod_rr.natural_unit_[2].power() == power_ratio_type(-1, 2));
|
||||
static_assert(prod_rr.outer_scale_factor_ == scalefactor_ratio_type(1, 1));
|
||||
static_assert(prod_rr.outer_scale_sq_ == 1.0);
|
||||
#endif
|
||||
}
|
||||
} /*TEST_CASE(bpu_array_product0)*/
|
||||
|
||||
|
|
@ -641,6 +508,7 @@ namespace xo {
|
|||
|
||||
static_assert(bpu.power() == power_ratio_type(-1, 2));
|
||||
|
||||
#ifdef NOT_USING
|
||||
constexpr auto prod_rr = su_bpu_product(v, bpu);
|
||||
|
||||
log && log(xtag("prod_rr.bpu_array", prod_rr.natural_unit_));
|
||||
|
|
@ -656,6 +524,7 @@ namespace xo {
|
|||
static_assert(prod_rr.natural_unit_[1].power() == power_ratio_type(-1, 1));
|
||||
static_assert(prod_rr.outer_scale_factor_ == scalefactor_ratio_type(1, 1));
|
||||
static_assert(prod_rr.outer_scale_sq_ == 12.0);
|
||||
#endif
|
||||
}
|
||||
} /*TEST_CASE(bpu_array_product1)*/
|
||||
|
||||
|
|
@ -685,6 +554,7 @@ namespace xo {
|
|||
|
||||
static_assert(bpu.power() == power_ratio_type(-1, 1));
|
||||
|
||||
#ifdef NOT_USING
|
||||
constexpr auto prod_rr = su_bpu_product(v, bpu);
|
||||
|
||||
log && log(xtag("prod_rr.bpu_array", prod_rr.natural_unit_));
|
||||
|
|
@ -700,6 +570,7 @@ namespace xo {
|
|||
static_assert(prod_rr.natural_unit_[1].power() == power_ratio_type(-1, 1));
|
||||
static_assert(prod_rr.outer_scale_factor_ == scalefactor_ratio_type(1, 1));
|
||||
static_assert(prod_rr.outer_scale_sq_ == 1.0);
|
||||
#endif
|
||||
}
|
||||
} /*TEST_CASE(bpu_array_product2)*/
|
||||
|
||||
|
|
@ -732,7 +603,7 @@ namespace xo {
|
|||
constexpr auto prod_rr = su_product<int64_t, __int128_t>(v, w);
|
||||
|
||||
log && log(xtag("prod_rr.bpu_array", prod_rr.natural_unit_));
|
||||
log && log(xtag("prod_rr.outer_scale_exact", prod_rr.outer_scale_factor_));
|
||||
log && log(xtag("prod_rr.outer_scale_exact", prod_rr.outer_scale_factor_.convert_to<int64_t>()));
|
||||
log && log(xtag("prod_rr.outer_scale_sq", prod_rr.outer_scale_sq_));
|
||||
|
||||
static_assert(prod_rr.natural_unit_.n_bpu() == 3);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue