def test_formula_parse_with_wide_count(self): f = Formula.parse('C6H10O2') self.assertEqual(f, Formula({ Atom('C'): 6, Atom('H'): 10, Atom('O'): 2 }))
def test_formula_parse_with_implicit_digit(self): f = Formula.parse('C2H5NO2') self.assertEqual( f, Formula({ Atom('C'): 2, Atom('H'): 5, Atom('N'): 1, Atom('O'): 2 }))
def test_formula_balance_subgroups_cancel_out(self): f1, f2 = Formula.balance(Formula.parse('H2(CH2)n'), Formula.parse('CH3O(CH2)n')) self.assertEqual(f1, Formula({ Atom('C'): 1, Atom('H'): 1, Atom('O'): 1 })) self.assertEqual(f2, Formula())
def test_formula_parse_with_numbered_radical(self): f = Formula.parse('C2H4NO2(R1)') self.assertEqual( f, Formula({ Atom('C'): 2, Atom('H'): 4, Atom('N'): 1, Atom('O'): 2, Radical('R1'): 1 }))
def test_formula_parse_with_implicitly_counted_subgroup(self): f = Formula.parse('C2H6O2(CH)') self.assertEqual( f, Formula({ Atom('C'): 2, Atom('H'): 6, Atom('O'): 2, Formula({ Atom('C'): 1, Atom('H'): 1 }): 1 }))
def test_formula_parse_with_two_identical_counted_subgroups(self): f = Formula.parse('C2H6O2(CH)2(CH)2') self.assertEqual( f, Formula({ Atom('C'): 2, Atom('H'): 6, Atom('O'): 2, Formula({ Atom('C'): 1, Atom('H'): 1 }): 4 }))
def test_formula_parse_with_wide_counted_subgroup(self): f = Formula.parse('C2(CH)10NO2') self.assertEqual( f, Formula({ Atom('C'): 2, Atom('N'): 1, Atom('O'): 2, Formula({ Atom('C'): 1, Atom('H'): 1 }): 10 }))
def calc_formula(f, charge, neutral=False): formula = {} for e, value in iteritems(f): if e != Atom('H') or charge is None or not neutral: formula[e] = value else: formula[e] = value - charge # No. of H in neutral stat return formula
def test_formula_items(self): f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6}) self.assertEqual(dict(f.items()), { Atom('C'): 6, Atom('H'): 12, Atom('O'): 6 })
def test_formula_merge_same_formulas_with_same_atoms(self): f = Formula({ Atom('H'): 2, Atom('O'): 1 }) | Formula({ Atom('N'): 1, Atom('O'): 2 }) self.assertEqual(f, Formula({ Atom('H'): 2, Atom('N'): 1, Atom('O'): 3 }))
def test_formula_to_string_with_group(self): f = Formula({ Atom('C'): 1, Atom('H'): 3, Formula({ Atom('C'): 1, Atom('H'): 2 }): 14, Formula({ Atom('C'): 1, Atom('O'): 1, Formula({ Atom('H'): 1, Atom('O'): 1 }): 1 }): 1 })
def test_atom_equals(self): a1 = Atom('H') a2 = Atom('Zn') self.assertEqual(a1, a1) self.assertNotEqual(a1, a2)
def test_formula_not_equals_other_with_different_number(self): f = Formula({Atom('Au'): 1}) self.assertNotEqual(f, Formula({Atom('Au'): 2}))
def test_atom_to_string(self): a = Atom('C') self.assertEqual(str(a), 'C')
def test_atom_symbol_wide(self): a = Atom('Zn') self.assertEqual(a.symbol, 'Zn')
def test_formula_parse_with_implicit_final_digit(self): f = Formula.parse('H2O') self.assertEqual(f, Formula({Atom('H'): 2, Atom('O'): 1}))
def test_formula_contains(self): f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6}) self.assertIn(Atom('C'), f) self.assertNotIn(Atom('Ag'), f)
def test_formula_balance_missing_on_both_sides(self): f1, f2 = Formula.balance(Formula.parse('C3H6OH'), Formula.parse('CH6O2')) self.assertEqual(f1, Formula({Atom('O'): 1})) self.assertEqual(f2, Formula({Atom('C'): 2, Atom('H'): 1}))
def test_formula_balance_missing_on_one_side(self): f1, f2 = Formula.balance(Formula.parse('H2O'), Formula.parse('OH')) self.assertEqual(f1, Formula()) self.assertEqual(f2, Formula({Atom('H'): 1}))
def test_formula_flattened(self): f = Formula({ Atom('C'): 1, Atom('H'): 3, Formula({ Atom('C'): 1, Atom('H'): 2 }): 14, Formula({ Atom('C'): 1, Atom('O'): 1, Formula({ Atom('H'): 1, Atom('O'): 1 }): 1 }): 1 }) self.assertEqual(f.flattened(), Formula({ Atom('C'): 16, Atom('H'): 32, Atom('O'): 2 }))
def test_formula_merge_formulas_that_cancel_out(self): f = Formula({Atom('H'): 3}) | Formula({Atom('H'): -3}) self.assertEqual(f, Formula())
def test_formula_parse_with_wide_element(self): f = Formula.parse('ZnO') self.assertEqual(f, Formula({Atom('Zn'): 1, Atom('O'): 1}))
def test_atom_symbol(self): a = Atom('H') self.assertEqual(a.symbol, 'H')
def test_atom_equals(self): a1 = Atom('H') self.assertEqual(a1, Atom.H) self.assertNotEqual(a1, Atom.Zn)
def test_atom_symbol_non_standard(self): a = Atom('X') self.assertEqual(a.symbol, 'X')
def test_formula_not_equals_other_with_distinct_elements(self): f = Formula({Atom('Au'): 1}) self.assertNotEqual(f, Formula({Atom('Ag'): 1}))
def test_atom_repr(self): a = Atom('Si') self.assertEqual(repr(a), "Atom('Si')")
def test_formula_to_string(self): f = Formula({Atom('H'): 12, Atom('C'): 6, Atom('O'): 6}) self.assertEqual(str(f), 'C6H12O6')
def test_atom_ordered(self): a1 = Atom('H') a2 = Atom('C') a3 = Atom('Zn') self.assertGreater(a1, a2) self.assertLess(a1, a3)
def test_formula_equals_other_formula(self): f = Formula({Atom('H'): 2, Atom('O'): 1}) self.assertEqual(f, Formula({Atom('O'): 1, Atom('H'): 2}))