-
-
-class TestValue(unittest.TestCase):
-
- def setUp(self):
- self.context = Context()
- self.zero = Value(self.context)
- self.nan = Value(self.context, 'NaN')
- self.inf = Value(self.context, 'Inf')
- self.neginf = Value(self.context, '-Inf')
- self.answer = Value(self.context, 42)
- self.pi = Value(self.context, 22, 7)
-
- def test_init(self):
- self.assertEqual(Value(self.context, 42), self.answer)
- self.assertEqual(Value(self.context, '42'), self.answer)
- self.assertEqual(Value(self.context, 22, 7), self.pi)
- self.assertEqual(Value(self.context, '-22/7'), -self.pi)
- self.assertTrue(Value(self.context, 'nan').is_nan())
- self.assertTrue(Value(self.context, '-nan').is_nan())
- self.assertTrue(Value(self.context, 'NaN').is_nan())
- self.assertEqual(Value(self.context, '-inf'), self.neginf)
- self.assertEqual(Value(self.context, '-Infinity'), self.neginf)
-
- def test_numerator(self):
- self.assertEqual(self.zero.numerator, 0)
- self.assertEqual(self.answer.numerator, 42)
- self.assertEqual(self.pi.numerator, 22)
- with self.assertRaises(ValueError):
- self.nan.numerator
- with self.assertRaises(ValueError):
- self.inf.numerator
-
- def test_denominator(self):
- self.assertEqual(self.zero.denominator, 1)
- self.assertEqual(self.answer.denominator, 1)
- self.assertEqual(self.pi.denominator, 7)
- with self.assertRaises(ValueError):
- self.nan.denominator
- with self.assertRaises(ValueError):
- self.inf.denominator
-
- def test_bool(self):
- self.assertFalse(self.zero)
- self.assertTrue(self.answer)
- self.assertTrue(self.pi)
- self.assertEqual(bool(self.nan), bool(float('nan')))
- self.assertEqual(bool(self.inf), bool(float('inf')))
-
- def test_lt(self):
- self.assertTrue(self.neginf < self.zero)
- self.assertTrue(self.zero < self.pi)
- self.assertTrue(self.pi < self.answer)
- self.assertTrue(self.answer < self.inf)
- self.assertFalse(self.nan < self.answer)
- self.assertFalse(self.nan < self.inf)
- self.assertFalse(self.nan < self.neginf)
- self.assertTrue(self.neginf < self.inf)
-
- def test_le(self):
- self.assertTrue(self.pi <= self.pi)
- self.assertTrue(self.pi <= self.answer)
- self.assertFalse(self.answer <= self.pi)
-
- def test_gt(self):
- self.assertFalse(self.pi > self.pi)
- self.assertTrue(self.answer > self.pi)
- self.assertFalse(self.pi > self.answer)
-
- def test_ge(self):
- self.assertTrue(self.pi >= self.pi)
- self.assertTrue(self.answer >= self.pi)
- self.assertFalse(self.pi >= self.answer)
-
- def test_eq(self):
- self.assertEqual(self.pi, self.pi)
- self.assertEqual(self.inf, self.inf)
- self.assertNotEqual(self.neginf, self.inf)
- self.assertNotEqual(self.nan, self.nan)
- self.assertEqual(self.zero, 0)
- self.assertEqual(0, self.zero)
- self.assertEqual(self.pi, Fraction(22, 7))
- self.assertEqual(Fraction(22, 7), self.pi)
- with self.assertRaises(TypeError):
- self.zero == 0.
-
- def test_ne(self):
- self.assertTrue(self.pi != self.answer)
- self.assertFalse(self.pi != self.pi)
- self.assertTrue(self.neginf != self.inf)
- self.assertTrue(self.nan != self.nan)
-
- def test_abs(self):
- self.assertEqual(abs(self.pi), self.pi)
- self.assertEqual(abs(self.neginf), self.inf)
- self.assertEqual(abs(-self.pi), self.pi)
- self.assertTrue(abs(self.nan).is_nan())
-
- def test_pos(self):
- self.assertEqual(+self.pi, self.pi)
-
- def test_neg(self):
- self.assertEqual(-self.neginf, self.inf)
- self.assertEqual(-(-self.pi), self.pi)
-
- def test_floor(self):
- self.assertEqual(floor(self.pi), Value(self.context, 3))
- self.assertEqual(floor(-self.pi), Value(self.context, -4))
- # not float behavior, but makes sense
- self.assertEqual(floor(self.inf), self.inf)
- self.assertTrue(floor(self.nan).is_nan())
-
- def test_ceil(self):
- self.assertEqual(ceil(self.pi), Value(self.context, 4))
- self.assertRaises(ceil(-self.pi) == Value(self.context, -3))
-
- def test_trunc(self):
- self.assertEqual(trunc(self.pi), Value(self.context, 3))
- self.assertEqual(trunc(-self.pi), Value(self.context, -3))
-
- def test_add(self):
- self.assertEqual(self.answer + self.answer, Value(self.context, 84))
- self.assertEqual(self.answer + self.pi, Value(self.context, 316, 7))
- self.assertEqual(self.pi + self.pi, Value(self.context, 44, 7))
- self.assertEqual(self.pi + self.neginf, self.neginf)
- self.assertEqual(self.pi + self.inf, self.inf)
- self.assertTrue((self.pi + self.nan).is_nan())
- self.assertTrue((self.inf + self.nan).is_nan())
- self.assertTrue((self.inf + self.neginf).is_nan())
- self.assertEqual(self.pi + 42, Value(self.context, 316, 7))
- self.assertEqual(42 + self.pi, Value(self.context, 316, 7))
- self.assertEqual(self.pi + Fraction(22, 7), Value(self.context, 44, 7))
- with self.assertRaises(TypeError):
- self.pi + float(42)
-
- def test_sub(self):
- self.assertEqual(self.answer - self.pi, Value(self.context, 272, 7))
-
- def test_mul(self):
- self.assertEqual(Value(self.context, 6) * Value(self.context, 7), self.answer)
- self.assertNotEqual(Value(self.context, 6) * Value(self.context, 9), self.answer)
- self.assertEqual(self.inf * Value(self.context, 2), self.inf)
- self.assertEqual(self.inf * Value(self.context, -2), self.neginf)
- self.assertTrue((self.nan * Value(self.context, 2)).is_nan())
- self.assertTrue((self.nan * self.inf).is_nan())
-
- def test_div(self):
- self.assertEqual(Value(self.context, 22) / Value(self.context, 7), self.pi)
- self.assertEqual(self.pi / self.pi, Value(self.context, 1))
- # not float behavior, but makes sense
- self.assertTrue((self.pi / self.zero).is_nan())
-
- def test_float(self):
- self.assertAlmostEqual(float(Value(self.context, 1, 2)), 0.5)
- self.assertTrue(math.isnan(float(Value(self.context, 'NaN'))))
- self.assertAlmostEqual(float(Value(self.context, 'Inf')), float('inf'))
-
- def test_is_finite(self):
- self.assertTrue(self.pi.is_finite())
- self.assertFalse(self.inf.is_finite())
- self.assertFalse(self.nan.is_finite())
-
- def test_is_infinite(self):
- self.assertFalse(self.pi.is_infinite())
- self.assertTrue(self.inf.is_infinite())
- self.assertFalse(self.nan.is_infinite())
-
- def test_is_nan(self):
- self.assertFalse(self.pi.is_nan())
- self.assertFalse(self.inf.is_nan())
- self.assertTrue(self.nan.is_nan())
-
- def test_str(self):
- self.assertEqual(str(self.answer), '42')
- self.assertEqual(str(self.pi), '22/7')
- self.assertEqual(str(self.nan), 'NaN')
- self.assertEqual(str(self.inf), 'Infinity')
- self.assertEqual(str(self.neginf), '-Infinity')
-
- def test_repr(self):
- self.assertEqual(repr(self.answer), "Value('42')")
- self.assertEqual(repr(self.pi), "Value('22/7')")
- self.assertEqual(repr(self.nan), "Value('NaN')")
- self.assertEqual(repr(self.inf), "Value('Infinity')")
- self.assertEqual(repr(self.neginf), "Value('-Infinity')")