Skip to content

Commit

Permalink
try to get coverage to 100% again (#318)
Browse files Browse the repository at this point in the history
* try to get coverage to 100% again

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
phlptp and pre-commit-ci[bot] authored Oct 27, 2023
1 parent 429d6ca commit 0141ccd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 45 deletions.
8 changes: 8 additions & 0 deletions test/test_unit_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,9 @@ TEST(stringToUnits, specialUnits)

EXPECT_EQ(precise::pu * precise::kg, unit_from_string("perunitkilogram"));
EXPECT_EQ(precise::pu * precise::kg, unit_from_string("perunitkg"));
EXPECT_EQ(precise::pu * precise::kg, unit_from_string("pukilogram"));
EXPECT_EQ(precise::pu * precise::kg, unit_from_string("pukg"));
EXPECT_EQ(precise::pu * precise::kg, unit_from_string("pumass"));
}

TEST(stringToUnits, mult)
Expand Down Expand Up @@ -1218,6 +1221,11 @@ TEST(stringToUnit, handlingOfSquared)
EXPECT_EQ(u7, g * (deci * m).pow(2));
}

TEST(stringToUnits, bracketExpansion)
{
EXPECT_EQ(unit_from_string("B(SPL)"), unit_from_string("B[SPL]"));
}

TEST(stringToUnits, modifiedStrings)
{
auto u1 = unit_from_string("linear yard");
Expand Down
47 changes: 21 additions & 26 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2654,6 +2654,7 @@ static const std::unordered_map<std::string, std::string> modifiers{
ckpair{"angle", "ang"},
ckpair{"unitofangle", "ang"},
ckpair{"unit of angle", "ang"},
ckpair{"planeangle", "ang"},
ckpair{"H2O", "H2O"},
ckpair{"water", "H2O"},
ckpair{"Hg", "Hg"},
Expand All @@ -2668,11 +2669,18 @@ static const std::unordered_map<std::string, std::string> modifiers{
ckpair{"cooling", "cooling"},
ckpair{"cloth", "cloth"},
ckpair{"clothing", "cloth"},
ckpair{"SPL", "SPL"},
ckpair{"10.nV", "tnv"},
ckpair{"10nV", "tnv"},
ckpair{"10*nV", "tnv"},
ckpair{"10*NV", "tnv"},
ckpair{"15degC", "[15]"},
ckpair{"20degC", "[20]"},
ckpair{"59degF", "[59]"},
ckpair{"60degF", "[60]"},
ckpair{"39degF", "[39]"},
ckpair{"20degC", "[20]"},
ckpair{"20C", "[20]"},
ckpair{"23degC", "[23]"},
ckpair{"23 degC", "[23]"},
ckpair{"0degC", "[00]"},
Expand Down Expand Up @@ -4387,7 +4395,7 @@ static bool cleanUnitString(std::string& unit_string, std::uint64_t match_flags)
ckpair{"deg ", "deg"},
}};

static UNITS_CPP14_CONSTEXPR_OBJECT std::array<ckpair, 31>
static UNITS_CPP14_CONSTEXPR_OBJECT std::array<ckpair, 33>
allCodeReplacements{{
ckpair{"sq.", "square"},
ckpair{"cu.", "cubic"},
Expand Down Expand Up @@ -4417,7 +4425,9 @@ static bool cleanUnitString(std::string& unit_string, std::uint64_t match_flags)
// \\ is always considered a segment terminator so it won't be
// misinterpreted as a known escape sequence
ckpair{"perunit", "pu"},
ckpair{"percent", "%"},
ckpair{"per-unit", "pu"},
ckpair{"per unit ", "pu"},
ckpair{"/square*", "/square"},
ckpair{"/cubic*", "/cubic"},
ckpair{"Hz^0.5", "rootHertz"},
Expand Down Expand Up @@ -4852,28 +4862,6 @@ static inline std::uint64_t getMinPartitionSize(std::uint64_t match_flags)
static precise_unit
checkPerModifications(std::string unit_string, std::uint64_t match_flags)
{
if (unit_string.compare(0, 7, "percent") == 0) {
auto bunit = default_unit(unit_string.substr(7));
if (is_valid(bunit)) {
return precise::percent * precise::pu * bunit;
}
bunit = unit_from_string_internal(
unit_string.substr(7), match_flags | minimum_partition_size3);
if (is_valid(bunit)) {
return precise::percent * precise::pu * bunit;
}
}
if (unit_string.compare(0, 7, "perunit") == 0) {
auto bunit = default_unit(unit_string.substr(7));
if (is_valid(bunit)) {
return precise::pu * bunit;
}
bunit = unit_from_string_internal(
unit_string.substr(7), match_flags | minimum_partition_size3);
if (is_valid(bunit)) {
return precise::pu * bunit;
}
}
// try changing out any "per" words for division sign
if ((match_flags & no_per_operators) == 0) {
auto fnd = findWordOperatorSep(unit_string, "per");
Expand Down Expand Up @@ -4926,13 +4914,13 @@ static precise_unit
return precise::percent * precise::pu * bunit;
}
}
if (unit_string.compare(0, 7, "perunit") == 0) {
auto bunit = default_unit(unit_string.substr(7));
if (unit_string.compare(0, 2, "pu") == 0) {
auto bunit = default_unit(unit_string.substr(2));
if (is_valid(bunit)) {
return precise::pu * bunit;
}
bunit = unit_from_string_internal(
unit_string.substr(7), match_flags | minimum_partition_size3);
unit_string.substr(2), match_flags | minimum_partition_size3);
if (is_valid(bunit)) {
return precise::pu * bunit;
}
Expand Down Expand Up @@ -5530,6 +5518,13 @@ static precise_unit unit_from_string_internal(
b_unit = unit_from_string_internal(
unit_string.substr(sep + 1), match_flags - recursion_modifier);
if (!is_valid(b_unit)) {
if ((unit_string[sep] == '*') &&
(a_unit == precise::pu || a_unit == precise::percent)) {
b_unit = default_unit(unit_string.substr(sep + 1));
if (is_valid(b_unit)) {
return a_unit * b_unit;
}
}
return precise::invalid;
}
}
Expand Down
30 changes: 11 additions & 19 deletions units/units_conversion_maps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 56>
/// definitions for the default units for specific types of measurmeents
UNITS_CPP14_CONSTEXPR_OBJECT std::array<
std::pair<const char*, precise_unit>,
1219>
1211>
defined_unit_strings_si{{
{"", precise::defunit},
{"[]", precise::defunit},
Expand Down Expand Up @@ -330,8 +330,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"ronna", precise::ronna},
{"quetta", precise::quetta},
{"%", precise::percent},
{"percent", precise::percent},
{"percentage", precise::percent},
{"%age", precise::percent},
{"permille", precise::milli},
{u8"\u2030", precise::milli}, // per mille symbol
{"bp", precise_unit(0.1, precise::milli)},
Expand Down Expand Up @@ -403,7 +402,6 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"TAN", precise::generate_custom_unit(213)},
{"g%", precise_unit(10.0, precise::g / precise::L)},
{"gram%", precise_unit(10.0, precise::g / precise::L)},
{"grampercent", precise_unit(10.0, precise::g / precise::L)},
{"G%", precise_unit(10.0, precise::g / precise::L)},
{"U", precise::laboratory::enzyme_unit},
// this may not be best but it doesn't actually conflict with anything
Expand Down Expand Up @@ -692,8 +690,6 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"workweek", precise_unit(40.0, precise::hr)},
{"workday", precise_unit(8.0, precise::hr)},
{"arcdeg", precise::deg},
{"deg-planeangle", precise::deg},
{"deg(planeangle)", precise::deg},
{"angulardeg", precise::deg},
{"deg", precise::deg},
{"DEG", precise::deg},
Expand Down Expand Up @@ -1303,7 +1299,6 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"kp", precise::kilo* precise::gm::pond},
// this is probably more common than kilopoise
{"tonforce_m", precise::mass::tonne* constants::g0.as_unit()},
{"tonforce(metric)", precise::mass::tonne* constants::g0.as_unit()},
{"tf", precise::mass::tonne* constants::g0.as_unit()},
{"hyl", precise::gm::hyl},
{"GF", precise::g* constants::g0.as_unit()}, // gram-force vs GF
Expand Down Expand Up @@ -1380,7 +1375,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"%slope", precise::clinical::prism_diopter},
{"%[slope]", precise::clinical::prism_diopter},
{"%[SLOPE]", precise::clinical::prism_diopter},
{"percentofslope", precise::clinical::prism_diopter},
{"%ofslope", precise::clinical::prism_diopter},
{"drp", precise::clinical::drop},
{"[drp]", precise::clinical::drop},
{"[DRP]", precise::clinical::drop},
Expand Down Expand Up @@ -1431,12 +1426,13 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"phon", precise::generate_custom_unit(41)},
{"BSPL", precise::log::B_SPL},
{"B[SPL]", precise::log::B_SPL},
{"B(SPL)", precise::log::B_SPL},
{"B_SPL", precise::log::B_SPL},
{"b_SPL", precise::log::B_SPL},
{"belSPL", precise::log::B_SPL},
{"belsoundpressure", precise::log::B_SPL},
{"dB[SPL]", precise::log::dB_SPL},
{"decibel(SPL)", precise::log::dB_SPL},
{"DB[SPL]", precise::log::dB_SPL},
{"dB_SPL", precise::log::dB_SPL},
{"decibel_SPL", precise::log::dB_SPL},
{"DB_SPL", precise::log::dB_SPL},
{"DBSPL", precise::log::dB_SPL},
{"B[V]", precise::log::B_V},
{"B(V)", precise::log::B_V},
Expand Down Expand Up @@ -1465,15 +1461,11 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<
{"BuV", precise::log::B_uV},
{"belmicrovolt", precise::log::B_uV},
{"DB[UV]", precise::log::dB_uV},
{"B[10.nV]", precise::log::B_10nV},
{"B(10nV)", precise::log::B_10nV},
{"B_tnv", precise::log::B_10nV},
{"b_tnv", precise::log::B_10nV},
{"bel10nanovolt", precise::log::B_10nV},
{"dB[10.nV]", precise::log::dB_10nV},
{"dB[10*nV]", precise::log::dB_10nV},
{"dB_tnv", precise::log::dB_10nV},
{"decibel10nanovolt", precise::log::B_10nV},
{"B[10*NV]", precise::log::B_10nV},
{"B[10*nV]", precise::log::B_10nV},
{"DB[10*NV]", precise::log::dB_10nV},
{"B[W]", precise::log::B_W},
{"B(W)", precise::log::B_W},
{"belwatt", precise::log::B_W},
Expand Down

0 comments on commit 0141ccd

Please sign in to comment.