coherent_derived_unit and custom prefixes support added

This commit is contained in:
Mateusz Pusz
2019-10-17 10:16:44 +02:00
parent 9a9abca81d
commit 3e30c779a6
29 changed files with 261 additions and 115 deletions
+1
View File
@@ -22,6 +22,7 @@
add_executable(unit_tests_runtime
catch_main.cpp
digital_information_test.cpp
math_test.cpp
text_test.cpp
)
@@ -0,0 +1,77 @@
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include <units/quantity.h>
#include <catch2/catch.hpp>
#include <sstream>
namespace data {
struct base_dim_digital_information : units::base_dimension<"digital information", "b"> {};
struct digital_information : units::derived_dimension<digital_information, units::exp<base_dim_digital_information, 1>> {};
template<typename T>
concept DigitalInformation = units::QuantityOf<T, digital_information>;
using namespace units::hacks;
struct data_prefix;
struct bit : units::coherent_derived_unit<bit, decltype("b"_fs), digital_information, data_prefix> {};
struct kilobit : units::derived_unit<bit, decltype("Kib"_fs), digital_information, units::ratio<1'024>> {};
struct byte : units::derived_unit<byte, decltype("B"_fs), digital_information, units::ratio<8>> {};
inline namespace literals {
constexpr auto operator""_b(unsigned long long l) { return units::quantity<bit, std::int64_t>(l); }
constexpr auto operator""_Kib(unsigned long long l) { return units::quantity<kilobit, std::int64_t>(l); }
constexpr auto operator""_B(unsigned long long l) { return units::quantity<byte, std::int64_t>(l); }
}
}
template<> inline constexpr std::string_view units::prefix_symbol<data::data_prefix, units::ratio< 1'024>> = "Ki";
template<> inline constexpr std::string_view units::prefix_symbol<data::data_prefix, units::ratio<1'048'576>> = "Mi";
using namespace data;
TEST_CASE("operator<< on a custom quantity", "[text][ostream]")
{
std::stringstream stream;
SECTION("quantity with a predefined unit and prefix")
{
SECTION("named unit")
{
stream << 64_B;
REQUIRE(stream.str() == "64 B");
}
SECTION("other unit matching prefix")
{
stream << 8_Kib * 8_Kib / 2_b;
REQUIRE(stream.str() == "32 Mib");
}
}
}
+11 -5
View File
@@ -39,25 +39,31 @@ TEST_CASE("operator<< on a quantity", "[text][ostream]")
{
SECTION("integral representation")
{
stream << 16sq_m;
REQUIRE(stream.str() == "16 m^2");
stream << 60W;
REQUIRE(stream.str() == "60 W");
}
SECTION("floating-point representation")
{
stream << 72.5kmph;
REQUIRE(stream.str() == "72.5 km/h");
stream << 72.5kJ;
REQUIRE(stream.str() == "72.5 kJ");
}
}
SECTION("quantity with a predefined dimension but unknown unit")
{
SECTION("unit::ratio as an SI prefix")
SECTION("unit::ratio as an SI prefix for a dimension with a special symbol")
{
stream << 4.N * 2cm;
REQUIRE(stream.str() == "8 cJ");
}
SECTION("unit::ratio for a dimension without a special symbol")
{
stream << 2.cm * 2m * 2m;
REQUIRE(stream.str() == "8 [1/100]m^3");
}
SECTION("unit::ratio::num != 1 && unit::ratio::den == 1")
{
stream << 4 * 2min / (2s * 2s);