-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a units library [0] to allow compile-time dimensional analysis. This commit adds the README example as a unit test. [0] https://github.com/mpusz/mp-units Change-Id: I74eb111e2df7c65881e0ba893a3de58b2ebc8512
- Loading branch information
Showing
6 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include <boost/ut.hpp> | ||
|
||
#include <fmt/core.h> | ||
#include <mp-units/format.h> | ||
#include <mp-units/ostream.h> | ||
#include <mp-units/systems/international/international.h> | ||
#include <mp-units/systems/isq/space_and_time.h> | ||
#include <mp-units/systems/si/si.h> | ||
#include <sstream> | ||
|
||
using namespace mp_units; | ||
|
||
constexpr QuantityOf<isq::speed> auto | ||
avg_speed(QuantityOf<isq::length> auto d, QuantityOf<isq::time> auto t) | ||
{ | ||
return d / t; | ||
} | ||
|
||
constexpr auto to_string(const auto& value) | ||
{ | ||
auto ss = std::stringstream{}; | ||
ss << value; | ||
return ss.str(); | ||
} | ||
|
||
auto main() -> int | ||
{ | ||
using ::boost::ut::expect; | ||
using ::boost::ut::test; | ||
|
||
test("units example") = [] { | ||
using namespace mp_units::si::unit_symbols; | ||
using namespace mp_units::international::unit_symbols; | ||
|
||
constexpr auto v1 = 110 * (km / h); | ||
constexpr auto v2 = 70 * mph; | ||
constexpr auto v3 = avg_speed(220. * isq::distance[km], 2 * h); | ||
constexpr auto v4 = avg_speed(isq::distance(140. * mi), 2 * h); | ||
constexpr auto v5 = v3[m / s]; | ||
constexpr auto v6 = value_cast<m / s>(v4); | ||
constexpr auto v7 = value_cast<int>(v6); | ||
|
||
expect("110 km/h" == to_string(v1)); | ||
expect("70 mi/h" == to_string(v2)); | ||
expect("110 km/h" == fmt::format("{}", v3)); | ||
expect("***70 mi/h****" == fmt::format("{:*^14}", v4)); | ||
expect("30.5556 in m/s" == fmt::format("{:%Q in %q}", v5)); | ||
expect("31.2928 in m/s" == fmt::format("{0:%Q} in {0:%q}", v6)); | ||
expect("31" == fmt::format("{:%Q}", v7)); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
exports_files(["mp-units-clang-priv-ctor.patch"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
diff --git a/src/core/include/mp-units/quantity.h b/src/core/include/mp-units/quantity.h | ||
index 3d3642b3..5e16eff4 100644 | ||
--- a/src/core/include/mp-units/quantity.h | ||
+++ b/src/core/include/mp-units/quantity.h | ||
@@ -321,6 +321,7 @@ private: | ||
requires quantity<R2, std::remove_cvref_t<Rep2>>::_rep_safe_constructible_ | ||
friend constexpr quantity<R2, std::remove_cvref_t<Rep2>> make_quantity(Rep2&&); | ||
|
||
+public: | ||
template<typename Value> | ||
requires detail::RepSafeConstructibleFrom<rep, Value&&, unit> | ||
constexpr explicit quantity(Value&& v) : number_(std::forward<Value>(v)) |