def test_frozen_zero_instance_immutable(self): """Test that FrozenPolynomial returns correct zero instance.""" fp = FrozenPolynomial(0) fpz = FrozenPolynomial.zero_instance() self._assert_polynomials_are_the_same(fp, fpz) self.assertRaises(AttributeError, fpz.__setitem__, 0, 1) self.assertRaises(AttributeError, fpz.__setattr__, "a", 1)
def test_frozen_polynomial_hash_equal(self): """Test that equal frozen polynomials have equal hash value.""" f1 = FrozenPolynomial(1, 2, 3) f2 = FrozenPolynomial(f1) h1 = hash(f1) h2 = hash(f2) self.assertEqual(h1, h2) self.assertEqual({h1}, {h1, h2})
def test_frozen_polynomial_raises_err(self): """Test that a FrozenPolynomial raises errors when setting values.""" f = FrozenPolynomial(1, 2, 3) self.assertRaises(AttributeError, f.__setattr__, "x", 5) self.assertRaises(AttributeError, f.__setitem__, 0, 5) self.assertRaises(AttributeError, f.__imul__, 5)
def test_zero_polynomial_hash_equal_to_empty_frozen_polynomal_hash(self): """Test that hash(ZeroPolynomial()) == hash(FrozenPolynomial()).""" z = ZeroPolynomial() f = FrozenPolynomial() hz = hash(z) hf = hash(f) self.assertEqual(hz, hf)
def test_frozen_vector_immutable(self): """Test that frozen vectors can't be modified.""" a = ZeroPolynomial() b = FrozenPolynomial(1, 2, 3) def set_item(obj, index, value): obj[index] = value possible_errs = (TypeError, AttributeError) self.assertRaises(possible_errs, set_item, a._vector, 0, 5) self.assertRaises(possible_errs, set_item, b._vector, 0, 5)
def test_casting_to_frozen_polynomial(self): """Casting polynomials is fine.""" a = Polynomial(1, 2, 3) b = FrozenPolynomial.from_polynomial(a) self.assertEqual(a, b)
def test_frozen(self): """Test that repr() output of a FrozenPolynomial is valid.""" expect = "FrozenPolynomial(1, 2, 3)" r = repr(FrozenPolynomial(1, 2, 3)) self.assertEqual(expect, r)