Skip to content

Commit

Permalink
Merge pull request #112 from SimonHeybrock/strings-eV
Browse files Browse the repository at this point in the history
Improve to_string for eV and charge units
  • Loading branch information
phlptp authored Feb 5, 2021
2 parents 2829d2c + 4d65b6b commit 4c6debf
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
41 changes: 41 additions & 0 deletions test/test_unit_strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,35 @@ TEST(unitStrings, crazyunits)
EXPECT_EQ(to_string(precise_unit(10, precise::pu)), "10*pu");
}

TEST(unitStrings, charge)
{
// A * s = C
EXPECT_EQ(to_string(precise::A * precise::s), "C");
// A * h = 3600 C, better use A * h
EXPECT_EQ(to_string(precise::A * precise::hr), "Ahr");
EXPECT_EQ(to_string(precise::femto * precise::A * precise::hr), "fAhr");
EXPECT_EQ(to_string(precise::pico * precise::A * precise::hr), "pAhr");
EXPECT_EQ(to_string(precise::nano * precise::A * precise::hr), "nAhr");
EXPECT_EQ(to_string(precise::micro * precise::A * precise::hr), "uAhr");
EXPECT_EQ(to_string(precise::milli * precise::A * precise::hr), "mAhr");
EXPECT_EQ(to_string(precise::kilo * precise::A * precise::hr), "kAhr");
EXPECT_EQ(to_string(precise::mega * precise::A * precise::hr), "MAhr");
EXPECT_EQ(to_string(precise::giga * precise::A * precise::hr), "GAhr");
EXPECT_EQ(to_string(precise::tera * precise::A * precise::hr), "TAhr");
}

TEST(unitStrings, electronVolt)
{
EXPECT_EQ(to_string(precise::energy::eV), "eV");
EXPECT_EQ(to_string(precise::nano * precise::energy::eV), "neV");
EXPECT_EQ(to_string(precise::micro * precise::energy::eV), "ueV");
EXPECT_EQ(to_string(precise::milli * precise::energy::eV), "meV");
EXPECT_EQ(to_string(precise::kilo * precise::energy::eV), "keV");
EXPECT_EQ(to_string(precise::mega * precise::energy::eV), "MeV");
EXPECT_EQ(to_string(precise::giga * precise::energy::eV), "GeV");
EXPECT_EQ(to_string(precise::tera * precise::energy::eV), "TeV");
}

TEST(unitStrings, customUnits)
{
EXPECT_EQ(to_string(precise::generate_custom_unit(762)), "CXUN[762]");
Expand Down Expand Up @@ -523,6 +552,18 @@ TEST(stringToUnits, equivalents3)
EXPECT_EQ(u3.multiplier(), std::pow(2.0, 345.0));
}

TEST(stringToUnits, electronVolt)
{
EXPECT_EQ(unit_from_string("eV"), precise::energy::eV);
EXPECT_EQ(unit_from_string("neV"), precise::nano * precise::energy::eV);
EXPECT_EQ(unit_from_string("ueV"), precise::micro * precise::energy::eV);
EXPECT_EQ(unit_from_string("meV"), precise::milli * precise::energy::eV);
EXPECT_EQ(unit_from_string("keV"), precise::kilo * precise::energy::eV);
EXPECT_EQ(unit_from_string("MeV"), precise::mega * precise::energy::eV);
EXPECT_EQ(unit_from_string("GeV"), precise::giga * precise::energy::eV);
EXPECT_EQ(unit_from_string("TeV"), precise::tera * precise::energy::eV);
}

class roundTripString : public ::testing::TestWithParam<std::string> {
};

Expand Down
17 changes: 17 additions & 0 deletions units/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@ static const umap base_unit_names{
{kg, "kg"},
{mol, "mol"},
{A, "A"},
{A * hr, "Ahr"},
{femto * A * hr, "fAhr"},
{pico * A * hr, "pAhr"},
{nano * A * hr, "nAhr"},
{micro * A * hr, "uAhr"},
{milli * A * hr, "mAhr"},
{kilo * A * hr, "kAhr"},
{mega * A * hr, "MAhr"},
{giga * A * hr, "GAhr"},
{tera * A * hr, "TAhr"},
{V, "V"},
{s, "s"},
// this is so Gs doesn't get used which can cause issues
Expand Down Expand Up @@ -267,6 +277,13 @@ static const umap base_unit_names{
{hp, "hp"},
{mph, "mph"},
{unit_cast(precise::energy::eV), "eV"},
{unit_cast(precise::nano * precise::energy::eV), "neV"},
{unit_cast(precise::micro * precise::energy::eV), "ueV"},
{unit_cast(precise::milli * precise::energy::eV), "meV"},
{unit_cast(precise::kilo * precise::energy::eV), "keV"},
{unit_cast(precise::mega * precise::energy::eV), "MeV"},
{unit_cast(precise::giga * precise::energy::eV), "GeV"},
{unit_cast(precise::tera * precise::energy::eV), "TeV"},
{kcal, "kcal"},
{btu, "btu"},
{unit_cast(precise::other::CFM), "CFM"},
Expand Down

0 comments on commit 4c6debf

Please sign in to comment.