Skip to content

Commit

Permalink
xo-unit: + distance, time compile-time units
Browse files Browse the repository at this point in the history
  • Loading branch information
Rconybea committed Apr 29, 2024
1 parent ab689b5 commit d7fa715
Show file tree
Hide file tree
Showing 2 changed files with 247 additions and 3 deletions.
34 changes: 34 additions & 0 deletions include/xo/unit/quantity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ namespace xo {
};

namespace qty {
// ----- mass -----

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); }
Expand All @@ -55,6 +57,38 @@ namespace xo {
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); }

// ----- distance -----

inline constexpr auto picometers(double x) { return quantity<double, std::int64_t, nu::picometer>(x); }
inline constexpr auto nanometers(double x) { return quantity<double, std::int64_t, nu::nanometer>(x); }
inline constexpr auto micrometers(double x) { return quantity<double, std::int64_t, nu::micrometer>(x); }
inline constexpr auto millimeters(double x) { return quantity<double, std::int64_t, nu::millimeter>(x); }
inline constexpr auto meters(double x) { return quantity<double, std::int64_t, nu::meter>(x); }
inline constexpr auto kilometers(double x) { return quantity<double, std::int64_t, nu::kilometer>(x); }
inline constexpr auto megameters(double x) { return quantity<double, std::int64_t, nu::megameter>(x); }
inline constexpr auto gigameters(double x) { return quantity<double, std::int64_t, nu::gigameter>(x); }

inline constexpr auto lightseconds(double x) { return quantity<double, std::int64_t, nu::lightsecond>(x); }
inline constexpr auto astronomicalunits(double x) { return quantity<double, std::int64_t, nu::astronomicalunit>(x); }

// ----- time -----

inline constexpr auto picoseconds(double x) { return quantity<double, std::int64_t, nu::picosecond>(x); }
inline constexpr auto nanoseconds(double x) { return quantity<double, std::int64_t, nu::nanosecond>(x); }
inline constexpr auto microseconds(double x) { return quantity<double, std::int64_t, nu::microsecond>(x); }
inline constexpr auto milliseconds(double x) { return quantity<double, std::int64_t, nu::millisecond>(x); }
inline constexpr auto seconds(double x) { return quantity<double, std::int64_t, nu::second>(x); }
inline constexpr auto minutes(double x) { return quantity<double, std::int64_t, nu::minute>(x); }
inline constexpr auto hours(double x) { return quantity<double, std::int64_t, nu::hour>(x); }
inline constexpr auto days(double x) { return quantity<double, std::int64_t, nu::day>(x); }
inline constexpr auto weeks(double x) { return quantity<double, std::int64_t, nu::week>(x); }
inline constexpr auto months(double x) { return quantity<double, std::int64_t, nu::month>(x); }
inline constexpr auto years(double x) { return quantity<double, std::int64_t, nu::year>(x); }
inline constexpr auto year250s(double x) { return quantity<double, std::int64_t, nu::year250>(x); }
inline constexpr auto year360s(double x) { return quantity<double, std::int64_t, nu::year360>(x); }
inline constexpr auto year365s(double x) { return quantity<double, std::int64_t, nu::year365>(x); }
//inline constexpr auto year366s(double x) { return quantity<double, std::int64_t, nu::year366>(x); }
}
} /*namespace qty*/
} /*namespace xo*/
Expand Down
216 changes: 213 additions & 3 deletions utest/quantity.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace xo {
namespace qty {
TEST_CASE("quantity", "[quantity]") {
TEST_CASE("quantity.mass", "[quantity]") {
constexpr bool c_debug_flag = true;

// can get bits from /dev/random by uncommenting the 2nd line below
Expand All @@ -17,7 +17,7 @@ namespace xo {

//auto rng = xo::rng::xoshiro256ss(seed);

scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.quantity"));
scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.quantity.mass"));
//log && log("(A)", xtag("foo", foo));

constexpr auto pg = qty::picograms(1.0);
Expand Down Expand Up @@ -103,7 +103,217 @@ namespace xo {
REQUIRE(tostr(kt) == "1kt");
REQUIRE(tostr(mt) == "1Mt");
REQUIRE(tostr(gt) == "1Gt");
} /*TEST_CASE(quantity)*/
} /*TEST_CASE(quantity.mass)*/

TEST_CASE("quantity.distance", "[quantity]") {
constexpr bool c_debug_flag = true;

scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.quantity.distance"));

constexpr auto pm = qty::picometers(1.0);
static_assert(quantity2_concept<decltype(pm)>);
static_assert(sizeof(pm) == sizeof(double));
static_assert(pm.scale() == 1.0);
static_assert(pm.abbrev() == flatstring("pm"));

constexpr auto nm = qty::nanometers(1.0);
static_assert(quantity2_concept<decltype(nm)>);
static_assert(sizeof(nm) == sizeof(double));
static_assert(nm.scale() == 1.0);
static_assert(nm.abbrev() == flatstring("nm"));

constexpr auto um = qty::micrometers(1.0);
static_assert(quantity2_concept<decltype(um)>);
static_assert(sizeof(um) == sizeof(double));
static_assert(um.scale() == 1.0);
static_assert(um.abbrev() == flatstring("um"));

constexpr auto mm = qty::millimeters(1.0);
static_assert(quantity2_concept<decltype(mm)>);
static_assert(sizeof(mm) == sizeof(double));
static_assert(mm.scale() == 1.0);
static_assert(mm.abbrev() == flatstring("mm"));

constexpr auto m = qty::meters(1.0);
static_assert(quantity2_concept<decltype(m)>);
static_assert(sizeof(m) == sizeof(double));
static_assert(m.scale() == 1.0);
static_assert(m.abbrev() == flatstring("m"));

constexpr auto km = qty::kilometers(1.0);
static_assert(quantity2_concept<decltype(km)>);
static_assert(sizeof(km) == sizeof(double));
static_assert(km.scale() == 1.0);
static_assert(km.abbrev() == flatstring("km"));

constexpr auto Mm = qty::megameters(1.0);
static_assert(quantity2_concept<decltype(Mm)>);
static_assert(sizeof(Mm) == sizeof(double));
static_assert(Mm.scale() == 1.0);
static_assert(Mm.abbrev() == flatstring("Mm"));

constexpr auto Gm = qty::gigameters(1.0);
static_assert(quantity2_concept<decltype(Gm)>);
static_assert(sizeof(Gm) == sizeof(double));
static_assert(Gm.scale() == 1.0);
static_assert(Gm.abbrev() == flatstring("Gm"));

constexpr auto lsec = qty::lightseconds(1.0);
static_assert(quantity2_concept<decltype(lsec)>);
static_assert(sizeof(lsec) == sizeof(double));
static_assert(lsec.scale() == 1.0);
static_assert(lsec.abbrev() == flatstring("lsec"));

constexpr auto AU = qty::astronomicalunits(1.0);
static_assert(quantity2_concept<decltype(AU)>);
static_assert(sizeof(AU) == sizeof(double));
static_assert(AU.scale() == 1.0);
static_assert(AU.abbrev() == flatstring("AU"));

log && log(xtag("pm.abbrev", pm.abbrev()));
log && log(xtag("nm.abbrev", nm.abbrev()));
log && log(xtag("um.abbrev", um.abbrev()));
log && log(xtag("mm.abbrev", mm.abbrev()));
log && log(xtag("m.abbrev", m.abbrev()));
log && log(xtag("km.abbrev", km.abbrev()));
log && log(xtag("Mm.abbrev", Mm.abbrev()));
log && log(xtag("Gm.abbrev", Gm.abbrev()));
log && log(xtag("lsec.abbrev", lsec.abbrev()));
log && log(xtag("AU.abbrev", AU.abbrev()));

REQUIRE(tostr(pm) == "1pm");
REQUIRE(tostr(nm) == "1nm");
REQUIRE(tostr(um) == "1um");
REQUIRE(tostr(mm) == "1mm");
REQUIRE(tostr(m) == "1m");
REQUIRE(tostr(km) == "1km");
REQUIRE(tostr(Mm) == "1Mm");
REQUIRE(tostr(Gm) == "1Gm");
REQUIRE(tostr(lsec) == "1lsec");
REQUIRE(tostr(AU) == "1AU");

} /*TEST_CASE(quantity.distance)*/

TEST_CASE("quantity.time", "[quantity]") {
constexpr bool c_debug_flag = true;

scope log(XO_DEBUG2(c_debug_flag, "TEST_CASE.quantity.time"));

constexpr auto ps = qty::picoseconds(1.0);
static_assert(quantity2_concept<decltype(ps)>);
static_assert(sizeof(ps) == sizeof(double));
static_assert(ps.scale() == 1.0);
static_assert(ps.abbrev() == flatstring("ps"));

constexpr auto ns = qty::nanoseconds(1.0);
static_assert(quantity2_concept<decltype(ns)>);
static_assert(sizeof(ns) == sizeof(double));
static_assert(ns.scale() == 1.0);
static_assert(ns.abbrev() == flatstring("ns"));

constexpr auto us = qty::microseconds(1.0);
static_assert(quantity2_concept<decltype(us)>);
static_assert(sizeof(us) == sizeof(double));
static_assert(us.scale() == 1.0);
static_assert(us.abbrev() == flatstring("us"));

constexpr auto ms = qty::milliseconds(1.0);
static_assert(quantity2_concept<decltype(ms)>);
static_assert(sizeof(ms) == sizeof(double));
static_assert(ms.scale() == 1.0);
static_assert(ms.abbrev() == flatstring("ms"));

constexpr auto s = qty::seconds(1.0);
static_assert(quantity2_concept<decltype(s)>);
static_assert(sizeof(s) == sizeof(double));
static_assert(s.scale() == 1.0);
static_assert(s.abbrev() == flatstring("s"));

constexpr auto min = qty::minutes(1.0);
static_assert(quantity2_concept<decltype(min)>);
static_assert(sizeof(min) == sizeof(double));
static_assert(min.scale() == 1.0);
static_assert(min.abbrev() == flatstring("min"));

constexpr auto hr = qty::hours(1.0);
static_assert(quantity2_concept<decltype(hr)>);
static_assert(sizeof(hr) == sizeof(double));
static_assert(hr.scale() == 1.0);
static_assert(hr.abbrev() == flatstring("hr"));

constexpr auto dy = qty::days(1.0);
static_assert(quantity2_concept<decltype(dy)>);
static_assert(sizeof(dy) == sizeof(double));
static_assert(dy.scale() == 1.0);
static_assert(dy.abbrev() == flatstring("dy"));

constexpr auto wk = qty::weeks(1.0);
static_assert(quantity2_concept<decltype(wk)>);
static_assert(sizeof(wk) == sizeof(double));
static_assert(wk.scale() == 1.0);
static_assert(wk.abbrev() == flatstring("wk"));

constexpr auto mo = qty::months(1.0);
static_assert(quantity2_concept<decltype(mo)>);
static_assert(sizeof(mo) == sizeof(double));
static_assert(mo.scale() == 1.0);
static_assert(mo.abbrev() == flatstring("mo"));

constexpr auto yr = qty::years(1.0);
static_assert(quantity2_concept<decltype(yr)>);
static_assert(sizeof(yr) == sizeof(double));
static_assert(yr.scale() == 1.0);
static_assert(yr.abbrev() == flatstring("yr"));

constexpr auto yr250 = qty::year250s(1.0);
static_assert(quantity2_concept<decltype(yr250)>);
static_assert(sizeof(yr250) == sizeof(double));
static_assert(yr250.scale() == 1.0);
static_assert(yr250.abbrev() == flatstring("yr250"));

constexpr auto yr360 = qty::year360s(1.0);
static_assert(quantity2_concept<decltype(yr360)>);
static_assert(sizeof(yr360) == sizeof(double));
static_assert(yr360.scale() == 1.0);
static_assert(yr360.abbrev() == flatstring("yr360"));

constexpr auto yr365 = qty::year365s(1.0);
static_assert(quantity2_concept<decltype(yr365)>);
static_assert(sizeof(yr365) == sizeof(double));
static_assert(yr365.scale() == 1.0);
static_assert(yr365.abbrev() == flatstring("yr365"));

log && log(xtag("ps.abbrev", ps.abbrev()));
log && log(xtag("ns.abbrev", ns.abbrev()));
log && log(xtag("us.abbrev", us.abbrev()));
log && log(xtag("ms.abbrev", ms.abbrev()));
log && log(xtag("s.abbrev", s.abbrev()));
log && log(xtag("min.abbrev", min.abbrev()));
log && log(xtag("hr.abbrev", hr.abbrev()));
log && log(xtag("dy.abbrev", dy.abbrev()));
log && log(xtag("wk.abbrev", wk.abbrev()));
log && log(xtag("mo.abbrev", mo.abbrev()));
log && log(xtag("yr.abbrev", yr.abbrev()));
log && log(xtag("yr250.abbrev", yr250.abbrev()));
log && log(xtag("yr360.abbrev", yr360.abbrev()));
log && log(xtag("yr365.abbrev", yr365.abbrev()));

REQUIRE(tostr(ps) == "1ps");
REQUIRE(tostr(ns) == "1ns");
REQUIRE(tostr(us) == "1us");
REQUIRE(tostr(ms) == "1ms");
REQUIRE(tostr(s) == "1s");
REQUIRE(tostr(min) == "1min");
REQUIRE(tostr(hr) == "1hr");
REQUIRE(tostr(dy) == "1dy");
REQUIRE(tostr(wk) == "1wk");
REQUIRE(tostr(mo) == "1mo");
REQUIRE(tostr(yr) == "1yr");
REQUIRE(tostr(yr250) == "1yr250");
REQUIRE(tostr(yr360) == "1yr360");
REQUIRE(tostr(yr365) == "1yr365");
} /*TEST_CASE(quantity.time)*/

} /*namespace qty*/
} /*namespace xo*/

Expand Down

0 comments on commit d7fa715

Please sign in to comment.