예제 #1
0
    def test_equal_to_dict(self):
        d = EqualToDict({a: "a", b: "b"})
        # Ensure that we call `EqualTo`.
        self.assertFalse(Item.equal_to_called)
        self.assertEqual(d[a], "a")
        self.assertTrue(Item.equal_to_called)

        self.assertEqual(d[b], "b")
        self.assertTrue(a in d)

        # Ensure hash collision does not occur.
        self.assertEqual(hash(a.value), hash(a))
        self.assertFalse(a.value in d)

        # Obtaining the original representation (e.g. for `pybind11`):
        # - Constructing using `dict` will not be what is desired; the keys at
        # present are not directly convertible, thus would create an error.
        # N.B. At present, this behavior may not be overridable via Python, as
        # copying is done via `dict.update`, which has a special case for
        # `dict`-inheriting types which does not have any hooks for key
        # transformations.
        raw_attempt = dict(d)
        keys = list(raw_attempt.keys())
        self.assertFalse(isinstance(keys[0], Item))
        # - Calling `raw()` should provide the desired behavior.
        raw = d.raw()
        keys = list(raw.keys())
        self.assertTrue(isinstance(keys[0], Item))
예제 #2
0
 def test_constructor_map(self):
     powers_in = {x: 2, y: 3, z: 4}
     m = sym.Monomial(powers_in)
     powers_out = EqualToDict(m.get_powers())
     self.assertEqual(powers_out[x], 2)
     self.assertEqual(powers_out[y], 3)
     self.assertEqual(powers_out[z], 4)
예제 #3
0
 def test_get_powers(self):
     m = sym.Monomial(x, 2) * sym.Monomial(y)  # m = x²y
     powers = EqualToDict(m.get_powers())
     self.assertEqual(powers[x], 2)
     self.assertEqual(powers[y], 1)