xo-unit: + quantity printing + tests for mass units
This commit is contained in:
parent
a3c6ef6d33
commit
ab689b51df
4 changed files with 122 additions and 2 deletions
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "Quantity.hpp"
|
||||
#include "natural_unit_iostream.hpp"
|
||||
#include <iostream>
|
||||
//#include <iostream>
|
||||
|
||||
namespace xo {
|
||||
namespace qty {
|
||||
|
|
|
|||
|
|
@ -45,7 +45,16 @@ namespace xo {
|
|||
};
|
||||
|
||||
namespace qty {
|
||||
inline constexpr auto picograms(double x) { return quantity<double, std::int64_t, nu::picogram>(x); }
|
||||
inline constexpr auto nanograms(double x) { return quantity<double, std::int64_t, nu::nanogram>(x); }
|
||||
inline constexpr auto micrograms(double x) { return quantity<double, std::int64_t, nu::microgram>(x); }
|
||||
inline constexpr auto milligrams(double x) { return quantity<double, std::int64_t, nu::milligram>(x); }
|
||||
inline constexpr auto grams(double x) { return quantity<double, std::int64_t, nu::gram>(x); }
|
||||
inline constexpr auto kilograms(double x) { return quantity<double, std::int64_t, nu::kilogram>(x); }
|
||||
inline constexpr auto tonnes(double x) { return quantity<double, std::int64_t, nu::tonne>(x); }
|
||||
inline constexpr auto kilotonnes(double x) { return quantity<double, std::int64_t, nu::kilotonne>(x); }
|
||||
inline constexpr auto megatonnes(double x) { return quantity<double, std::int64_t, nu::megatonne>(x); }
|
||||
inline constexpr auto gigatonnes(double x) { return quantity<double, std::int64_t, nu::gigatonne>(x); }
|
||||
}
|
||||
} /*namespace qty*/
|
||||
} /*namespace xo*/
|
||||
|
|
|
|||
31
include/xo/unit/quantity_iostream.hpp
Normal file
31
include/xo/unit/quantity_iostream.hpp
Normal file
|
|
@ -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<Int> NaturalUnit = natural_unit<Int>(),
|
||||
typename Int2x = detail::width2x<Int>
|
||||
>
|
||||
inline std::ostream &
|
||||
operator<< (std::ostream & os,
|
||||
const quantity<Repr, Int, NaturalUnit, Int2x> & x)
|
||||
{
|
||||
os << x.scale() << x.abbrev();
|
||||
return os;
|
||||
}
|
||||
|
||||
} /*namespace qty*/
|
||||
|
||||
} /*namespace xo*/
|
||||
|
||||
/** end quantity_iostream.hpp **/
|
||||
|
|
@ -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 <catch2/catch.hpp>
|
||||
|
||||
|
|
@ -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<decltype(pg)>);
|
||||
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<decltype(ng)>);
|
||||
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<decltype(ug)>);
|
||||
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<decltype(mg)>);
|
||||
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<decltype(g)>);
|
||||
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<decltype(kg)>);
|
||||
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<decltype(t)>);
|
||||
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<decltype(kt)>);
|
||||
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<decltype(mt)>);
|
||||
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<decltype(gt)>);
|
||||
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*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue