Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unit output cleanup #265

Merged
merged 15 commits into from
Dec 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions test/test_defined_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ TEST(unit_name_definitions, si_duplicates)
continue;
}
auto res = testMap.emplace(ustring.first, ustring.second);
EXPECT_TRUE(res.second) << "duplicate si unit string " << ustring.second<< " matching with "<<res.first->second;
EXPECT_TRUE(res.second) << "duplicate si unit string " << ustring.second
<< " matching with " << res.first->second;
}
}

Expand All @@ -150,7 +151,8 @@ TEST(unit_name_definitions, customary_duplicates)
continue;
}
auto res = testMap.emplace(ustring.first, ustring.second);
EXPECT_TRUE(res.second) << "duplicate unit string " << ustring.second<< " matching with "<<res.first->second;
EXPECT_TRUE(res.second) << "duplicate unit string " << ustring.second
<< " matching with " << res.first->second;
}
}

Expand All @@ -162,7 +164,8 @@ TEST(unit_name_definitions, combined_duplicates)
continue;
}
auto res = testMap.emplace(ustring.first, ustring.second);
EXPECT_TRUE(res.second) << "duplicate si unit string " << ustring.second;
EXPECT_TRUE(res.second)
<< "duplicate si unit string " << ustring.second;
}

for (std::size_t ii = 0; ii < units::defined_unit_names_customary.size();
Expand All @@ -177,4 +180,4 @@ TEST(unit_name_definitions, combined_duplicates)
<< "duplicate unit string " << ii << " "
<< units::defined_unit_names_customary[ii].second;
}
}
}
12 changes: 5 additions & 7 deletions test/test_siunits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,19 +73,17 @@ TEST(SI, keyUnits)
// completely equivalent
continue;
}
if (longConv.has_e_flag())
{
if (longConv.has_e_flag()) {
if (unit_cast(longConv) != unit_cast(altConv)) {
longConv.clear_flags();
}
}
EXPECT_EQ(unit_cast(longConv), unit_cast(altConv))
<< alt << " does not generate the same unit as " << longForm;
//if (unit_cast(longConv) != unit_cast(altConv)) {
// longConv = units::unit_from_string(alt);
//}
if (shortConv.has_e_flag())
{
// if (unit_cast(longConv) != unit_cast(altConv)) {
// longConv = units::unit_from_string(alt);
// }
if (shortConv.has_e_flag()) {
if (unit_cast(shortConv) != unit_cast(altConv)) {
shortConv.clear_flags();
}
Expand Down
1 change: 0 additions & 1 deletion test/test_unit_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,6 @@ TEST(unitStrings, electronVolt)
EXPECT_EQ(str, "m/eV");
}


TEST(unitStrings, Hertz)
{
EXPECT_EQ(to_string(precise::Hz), "Hz");
Expand Down
12 changes: 6 additions & 6 deletions units/unit_definitions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ namespace precise {
becquerel(detail::unit_data(0, 0, -1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0));
constexpr precise_unit
gray(detail::unit_data(2, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
//seivert includes relative biological factor so it marked by the eflag to discriminate from gray
// seivert includes relative biological factor so it marked by the eflag to
// discriminate from gray
constexpr precise_unit
sievert(detail::unit_data(2, 0, -2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0));
constexpr precise_unit
Expand Down Expand Up @@ -774,12 +775,11 @@ namespace precise {
constexpr precise_unit psig = psi * eflag;
constexpr precise_unit inHg{3376.849669, Pa}; // at 60 degF
constexpr precise_unit mmHg{133.322387415, Pa};
constexpr precise_unit torr{
1.0 / 760.0,
atm* iflag}; // this is really close to mmHg
constexpr precise_unit torr{1.0 / 760.0, atm* iflag}; // this is really
// close to mmHg
constexpr precise_unit inH2O{248.843004, Pa}; // at 60 degF
constexpr precise_unit mmH2O{1.0 / 25.4, inH2O}; // at 60 degF

constexpr precise_unit att = gm::at; //!< technical atmosphere same as
//!< gravitational metric system
} // namespace pressure
Expand Down Expand Up @@ -844,7 +844,7 @@ namespace precise {
constexpr precise_unit btu_60{1054.68, J};
constexpr precise_unit btu_mean{1055.87, J};
constexpr precise_unit btu_it{1055.05585262, J}; // international table
// btu
// btu
constexpr precise_unit btu_iso{1055.06, J}; // rounded btu_it
constexpr precise_unit quad(1e15, btu_it);
constexpr precise_unit tonc(12000.0, btu_th / h);
Expand Down
9 changes: 4 additions & 5 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -577,8 +577,8 @@ static void addUnitFlagStrings(const precise_unit& un, std::string& unitString)
}
}

/** add the unit power to the string if it is positive and return 0, return 1 if negative and skip
the return value is if any power remains
/** add the unit power to the string if it is positive and return 0, return 1 if
negative and skip the return value is if any power remains
*/
static inline int addPosUnits(
std::string& str,
Expand Down Expand Up @@ -623,9 +623,8 @@ static std::string
cnt += addPosUnits(val, "rad", bu.radian(), flags);
addUnitFlagStrings(un, val);
if (cnt == 1) {
if (bu.second() == -1 && val.empty())
{
//deal with 1/s which is usually Hz
if (bu.second() == -1 && val.empty()) {
// deal with 1/s which is usually Hz
addPosUnits(val, "Hz", 1, flags);
return val;
}
Expand Down
129 changes: 64 additions & 65 deletions units/units_conversion_maps.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 110>
// remove Bq since it is often confusing in other contexts 1/s
// {Bq, "Bq"},
{unit(2.58e-4, C / kg), "R"},

{unit_cast(precise::cgs::curie), "Ci"},
{(mega * m).pow(3),
"ZL"}, // another one of those units that can be confused
Expand All @@ -69,7 +69,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 110>
{deg, "deg"},
{rad, "rad"},
{degC, u8"\u00B0C"},

{cm, "cm"},
{km, "km"},
{km * km, "km^2"},
Expand All @@ -91,7 +91,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 110>
{Gy, "Gy"},
{Sv, "Sv"},
{Hz, "Hz"},
{kilo*Hz, "kHz"},
{kilo * Hz, "kHz"},
{rpm, "rpm"},
{kat, "kat"},
{sr, "sr"},
Expand All @@ -116,7 +116,7 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 110>
{unit_cast(precise::pressure::atm), "atm"},
{unit_cast(precise::pressure::mmHg), "mmHg"},
{unit_cast(precise::pressure::mmH2O), "mmH2O"},

{unit_cast(precise::laboratory::IU), "[IU]"},
{kWh, "kWh"},
{MWh, "MWh"},
Expand All @@ -141,66 +141,65 @@ UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 110>
{unit_cast(precise::other::ppm), "ppm"},
{unit_cast(precise::other::ppb), "ppb"}}};

UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 56>
defined_unit_names_customary{
{
{in, "in"},
{unit_cast(precise::in.pow(2)), "in^2"},
{unit_cast(precise::in.pow(3)), "in^3"},
{ft, "ft"},
{unit_cast(precise::imp::foot), "ft_br"},
{unit_cast(precise::imp::inch), "in_br"},
{unit_cast(precise::imp::yard), "yd_br"},
{unit_cast(precise::imp::rod), "rd_br"},
{unit_cast(precise::imp::mile), "mi_br"},
{unit_cast(precise::imp::pace), "pc_br"},
{unit_cast(precise::imp::link), "lk_br"},
{unit_cast(precise::imp::chain), "ch_br"},
{unit_cast(precise::imp::nautical_mile), "nmi_br"},
{unit_cast(precise::imp::knot), "kn_br"},
{unit_cast(precise::nautical::knot), "knot"},
{unit_cast(precise::ft.pow(2)), "ft^2"},
{unit_cast(precise::ft.pow(3)), "ft^3"},
{degF, u8"\u00B0F"},
{yd, "yd"},
{unit_cast(precise::us::rod), "rd"},
{unit_cast(precise::yd.pow(2)), "yd^2"},
{unit_cast(precise::yd.pow(3)), "yd^3"},
{unit_cast(precise::time::syr), "syr"},
{unit_cast(precise::time::ag), "a_g"},
{unit_cast(precise::time::at), "a_t"},
{unit_cast(precise::time::aj), "a_j"},
{unit_cast(precise::angle::grad), "grad"},
{mile, "mi"},
{unit_cast(precise::mile.pow(2)), "mi^2"},
{acre, "acre"},
{unit_cast(precise::energy::therm_ec), "therm"},
{unit_cast(precise::energy::tonc), "tonc"},
{hp, "hp"},
{mph, "mph"},
{kcal, "kcal"},
{btu, "btu"},
{unit_cast(precise::other::CFM), "CFM"},
{unit_cast(precise::pressure::psi), "psi"},
{unit_cast(precise::pressure::psig), "psig"},
{unit_cast(precise::pressure::inHg), "inHg"},
{unit_cast(precise::pressure::inH2O), "inH2O"},
{unit_cast(precise::pressure::torr), "torr"},
{unit_cast(precise::energy::EER), "EER"},
{unit_cast(precise::energy::quad), "quad"},
{gal, "gal"},
{unit_cast(precise::us::barrel), "bbl"},
{lb, "lb"},
{ton, "ton"},
{unit_cast(precise::us::dry::bushel), "bu"},
{unit_cast(precise::us::floz), "floz"},
{oz, "oz"},
{unit_cast(precise::us::cup), "cup"},
{unit_cast(precise::us::tsp), "tsp"},
{unit_cast(precise::us::tbsp), "tbsp"},
{unit_cast(precise::us::quart), "qt"},
{unit_cast(precise::lbf),"lbf"},
}};
UNITS_CPP14_CONSTEXPR_OBJECT std::array<std::pair<unit, const char*>, 56>
defined_unit_names_customary{{
{in, "in"},
{unit_cast(precise::in.pow(2)), "in^2"},
{unit_cast(precise::in.pow(3)), "in^3"},
{ft, "ft"},
{unit_cast(precise::imp::foot), "ft_br"},
{unit_cast(precise::imp::inch), "in_br"},
{unit_cast(precise::imp::yard), "yd_br"},
{unit_cast(precise::imp::rod), "rd_br"},
{unit_cast(precise::imp::mile), "mi_br"},
{unit_cast(precise::imp::pace), "pc_br"},
{unit_cast(precise::imp::link), "lk_br"},
{unit_cast(precise::imp::chain), "ch_br"},
{unit_cast(precise::imp::nautical_mile), "nmi_br"},
{unit_cast(precise::imp::knot), "kn_br"},
{unit_cast(precise::nautical::knot), "knot"},
{unit_cast(precise::ft.pow(2)), "ft^2"},
{unit_cast(precise::ft.pow(3)), "ft^3"},
{degF, u8"\u00B0F"},
{yd, "yd"},
{unit_cast(precise::us::rod), "rd"},
{unit_cast(precise::yd.pow(2)), "yd^2"},
{unit_cast(precise::yd.pow(3)), "yd^3"},
{unit_cast(precise::time::syr), "syr"},
{unit_cast(precise::time::ag), "a_g"},
{unit_cast(precise::time::at), "a_t"},
{unit_cast(precise::time::aj), "a_j"},
{unit_cast(precise::angle::grad), "grad"},
{mile, "mi"},
{unit_cast(precise::mile.pow(2)), "mi^2"},
{acre, "acre"},
{unit_cast(precise::energy::therm_ec), "therm"},
{unit_cast(precise::energy::tonc), "tonc"},
{hp, "hp"},
{mph, "mph"},
{kcal, "kcal"},
{btu, "btu"},
{unit_cast(precise::other::CFM), "CFM"},
{unit_cast(precise::pressure::psi), "psi"},
{unit_cast(precise::pressure::psig), "psig"},
{unit_cast(precise::pressure::inHg), "inHg"},
{unit_cast(precise::pressure::inH2O), "inH2O"},
{unit_cast(precise::pressure::torr), "torr"},
{unit_cast(precise::energy::EER), "EER"},
{unit_cast(precise::energy::quad), "quad"},
{gal, "gal"},
{unit_cast(precise::us::barrel), "bbl"},
{lb, "lb"},
{ton, "ton"},
{unit_cast(precise::us::dry::bushel), "bu"},
{unit_cast(precise::us::floz), "floz"},
{oz, "oz"},
{unit_cast(precise::us::cup), "cup"},
{unit_cast(precise::us::tsp), "tsp"},
{unit_cast(precise::us::tbsp), "tbsp"},
{unit_cast(precise::us::quart), "qt"},
{unit_cast(precise::lbf), "lbf"},
}};

/// definitions for the default units for specific types of measurmeents
UNITS_CPP14_CONSTEXPR_OBJECT std::array<
Expand Down Expand Up @@ -2710,7 +2709,7 @@ std::array<std::pair<const char*, precise_unit>, 237> defined_measurement_types{
{"nfr", precise::one},
{"num", precise::one},
{"age", precise::time::ag},
{"strain",precise::pu*precise::m},
{"strain", precise::pu* precise::m},
{"impulse", precise::N* precise::s},
{"imp", precise::N* precise::s},
{"absorbeddoserate", precise::Gy / precise::s},
Expand Down