From ab689b51df0cdaf0a8efaa8fdd8d0e7c0f41ddb4 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Sun, 28 Apr 2024 19:12:40 -0400 Subject: [PATCH] xo-unit: + quantity printing + tests for mass units --- include/xo/unit/Quantity_iostream.hpp | 2 +- include/xo/unit/quantity.hpp | 9 +++ include/xo/unit/quantity_iostream.hpp | 31 ++++++++++ utest/quantity.test.cpp | 82 ++++++++++++++++++++++++++- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 include/xo/unit/quantity_iostream.hpp diff --git a/include/xo/unit/Quantity_iostream.hpp b/include/xo/unit/Quantity_iostream.hpp index 27d5c458..402c77d8 100644 --- a/include/xo/unit/Quantity_iostream.hpp +++ b/include/xo/unit/Quantity_iostream.hpp @@ -7,7 +7,7 @@ #include "Quantity.hpp" #include "natural_unit_iostream.hpp" -#include +//#include namespace xo { namespace qty { diff --git a/include/xo/unit/quantity.hpp b/include/xo/unit/quantity.hpp index 1ab9c137..10658791 100644 --- a/include/xo/unit/quantity.hpp +++ b/include/xo/unit/quantity.hpp @@ -45,7 +45,16 @@ namespace xo { }; namespace qty { + inline constexpr auto picograms(double x) { return quantity(x); } + inline constexpr auto nanograms(double x) { return quantity(x); } + inline constexpr auto micrograms(double x) { return quantity(x); } + inline constexpr auto milligrams(double x) { return quantity(x); } inline constexpr auto grams(double x) { return quantity(x); } + inline constexpr auto kilograms(double x) { return quantity(x); } + inline constexpr auto tonnes(double x) { return quantity(x); } + inline constexpr auto kilotonnes(double x) { return quantity(x); } + inline constexpr auto megatonnes(double x) { return quantity(x); } + inline constexpr auto gigatonnes(double x) { return quantity(x); } } } /*namespace qty*/ } /*namespace xo*/ diff --git a/include/xo/unit/quantity_iostream.hpp b/include/xo/unit/quantity_iostream.hpp new file mode 100644 index 00000000..29302816 --- /dev/null +++ b/include/xo/unit/quantity_iostream.hpp @@ -0,0 +1,31 @@ +/** @file quantity_iostream.hpp + * + * Author: Roland Conybeare + **/ + +#pragma once + +#include "quantity.hpp" +#include "natural_unit_iostream.hpp" + +namespace xo { + namespace qty { + template < + typename Repr = double, + typename Int = std::int64_t, + natural_unit NaturalUnit = natural_unit(), + typename Int2x = detail::width2x + > + inline std::ostream & + operator<< (std::ostream & os, + const quantity & x) + { + os << x.scale() << x.abbrev(); + return os; + } + + } /*namespace qty*/ + +} /*namespace xo*/ + +/** end quantity_iostream.hpp **/ diff --git a/utest/quantity.test.cpp b/utest/quantity.test.cpp index 07bb736c..1f2d679a 100644 --- a/utest/quantity.test.cpp +++ b/utest/quantity.test.cpp @@ -1,6 +1,8 @@ /* @file quantity.test.cpp */ #include "xo/unit/quantity.hpp" +#include "xo/unit/quantity_iostream.hpp" +#include "xo/unit/quantity2_concept.hpp" #include "xo/indentlog/scope.hpp" #include @@ -18,11 +20,89 @@ namespace xo { scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.quantity")); //log && log("(A)", xtag("foo", foo)); + constexpr auto pg = qty::picograms(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(pg) == sizeof(double)); + static_assert(pg.scale() == 1.0); + static_assert(pg.abbrev() == flatstring("pg")); + + constexpr auto ng = qty::nanograms(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(ng) == sizeof(double)); + static_assert(ng.scale() == 1.0); + static_assert(ng.abbrev() == flatstring("ng")); + + constexpr auto ug = qty::micrograms(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(ug) == sizeof(double)); + static_assert(ug.scale() == 1.0); + static_assert(ug.abbrev() == flatstring("ug")); + + constexpr auto mg = qty::milligrams(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(mg) == sizeof(double)); + static_assert(mg.scale() == 1.0); + static_assert(mg.abbrev() == flatstring("mg")); + constexpr auto g = qty::grams(1.0); - + static_assert(quantity2_concept); + static_assert(sizeof(g) == sizeof(double)); static_assert(g.scale() == 1.0); + static_assert(g.abbrev() == flatstring("g")); + constexpr auto kg = qty::kilograms(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(kg) == sizeof(double)); + static_assert(kg.scale() == 1.0); + static_assert(kg.abbrev() == flatstring("kg")); + + constexpr auto t = qty::tonnes(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(t) == sizeof(double)); + static_assert(t.scale() == 1.0); + static_assert(t.abbrev() == flatstring("t")); + + constexpr auto kt = qty::kilotonnes(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(kt) == sizeof(double)); + static_assert(kt.scale() == 1.0); + static_assert(kt.abbrev() == flatstring("kt")); + + constexpr auto mt = qty::megatonnes(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(mt) == sizeof(double)); + static_assert(mt.scale() == 1.0); + static_assert(mt.abbrev() == flatstring("Mt")); + + constexpr auto gt = qty::gigatonnes(1.0); + static_assert(quantity2_concept); + static_assert(sizeof(gt) == sizeof(double)); + static_assert(gt.scale() == 1.0); + static_assert(gt.abbrev() == flatstring("Gt")); + + log && log(xtag("pg.abbrev", pg.abbrev())); + log && log(xtag("ng.abbrev", ng.abbrev())); + log && log(xtag("ug.abbrev", ug.abbrev())); + log && log(xtag("mg.abbrev", mg.abbrev())); log && log(xtag("g.abbrev", g.abbrev())); + log && log(xtag("kg.abbrev", kg.abbrev())); + log && log(xtag("t.abbrev", t.abbrev())); + log && log(xtag("kt.abbrev", kt.abbrev())); + log && log(xtag("mt.abbrev", mt.abbrev())); + log && log(xtag("gt.abbrev", gt.abbrev())); + + log && log(xtag("pg", pg)); + + REQUIRE(tostr(pg) == "1pg"); + REQUIRE(tostr(ng) == "1ng"); + REQUIRE(tostr(ug) == "1ug"); + REQUIRE(tostr(mg) == "1mg"); + REQUIRE(tostr(g) == "1g"); + REQUIRE(tostr(kg) == "1kg"); + REQUIRE(tostr(t) == "1t"); + REQUIRE(tostr(kt) == "1kt"); + REQUIRE(tostr(mt) == "1Mt"); + REQUIRE(tostr(gt) == "1Gt"); } /*TEST_CASE(quantity)*/ } /*namespace qty*/ } /*namespace xo*/