Skip to content

Commit

Permalink
[Unittest] Add tests for UnitSystem.defaults
Browse files Browse the repository at this point in the history
Also: remove redundant tests for Units objects.
  • Loading branch information
ischoegl committed Aug 19, 2021
1 parent c176637 commit d10d1f4
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 10 deletions.
57 changes: 57 additions & 0 deletions interfaces/cython/cantera/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,63 @@

from cantera._cantera import _py_to_any_to_py


class TestUnitSystem(utilities.CanteraTest):

def test_default(self):
defaults = ct.UnitSystem().defaults
checks = {
"activation-energy": "J/kmol",
"current": "A",
"energy": "J",
"length": "m",
"mass": "kg",
"pressure": "Pa",
"quantity": "kmol",
"temperature": "K",
"time": "s",
}
for dim, unit in defaults.items():
self.assertIn(dim, checks)
self.assertEqual(checks[dim], unit)

def test_cgs(self):
system = ct.UnitSystem({
"length": "cm", "mass": "g", "time": "s",
"quantity": "mol", "pressure": "dyn/cm^2", "energy": "erg",
"activation-energy": "cal/mol"})
defaults = system.defaults
checks = {
"activation-energy": "cal/mol",
"current": "A",
"energy": "erg",
"length": "cm",
"mass": "g",
"pressure": "dyn/cm^2",
"quantity": "mol",
"temperature": "K",
"time": "s",
}
for dim, unit in defaults.items():
self.assertIn(dim, checks)
self.assertEqual(checks[dim], unit)

def test_activation_energy(self):
system = ct.UnitSystem({"activation-energy": "eV"})
defaults = system.defaults
self.assertEqual(defaults["activation-energy"], "eV")

system = ct.UnitSystem({"activation-energy": "K"})
defaults = system.defaults
self.assertEqual(defaults["activation-energy"], "K")

def test_raises(self):
with self.assertRaisesRegex(ct.CanteraError, "non-unity conversion factor"):
ct.UnitSystem({"temperature": "2 K"})
with self.assertRaisesRegex(ct.CanteraError, "non-unity conversion factor"):
ct.UnitSystem({"current": "2 A"})


class TestPyToAnyValue(utilities.CanteraTest):

def check_conversion(self, value, check_type=None):
Expand Down
10 changes: 0 additions & 10 deletions test/general/test_units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ TEST(Units, from_string) {
EXPECT_EQ(Units("0.001 m^3").str(), "0.001 m^3");
}

TEST(Units, string_output) {
EXPECT_EQ(Units(1.).str(), "1.0");
EXPECT_EQ(Units(1., 1.).str(), "1.0 kg");
EXPECT_EQ(Units(1., 2.).str(), "1.0 kg^2");
EXPECT_EQ(Units(1., .5).str(), "1.0 kg^0.5");
EXPECT_EQ(Units(1., -1.).str(), "1.0 kg^-1");
EXPECT_EQ(Units(1., 1., -3.).str(), "1.0 kg * m^-3");
EXPECT_EQ(Units(.001, 0., 3.).str(), "0.001 m^3");
}

TEST(Units, copy_construct) {
EXPECT_EQ(Units(Units(1.)).str(), "1.0");
EXPECT_EQ(Units(Units(1., 1.)).str(), "1.0 kg");
Expand Down

0 comments on commit d10d1f4

Please sign in to comment.