def test_sign(self): self.assertEqual(PyMPQScalar(-9).sign(), -1) self.assertEqual(PyMPQScalar(PyMPQ(-9)).sign(), -1) self.assertEqual(PyMPQScalar(9).sign(), 1) self.assertEqual(PyMPQScalar(PyMPQ(9)).sign(), 1) self.assertEqual(PyMPQScalar(0).sign(), 0) self.assertEqual(PyMPQScalar(PyMPQ(0)).sign(), 0) self.assertEqual(PyMPQScalar.init_infty(-9).sign(), -1) self.assertEqual(PyMPQScalar.init_infty(0).sign(), 0) self.assertEqual(PyMPQScalar.init_infty(9).sign(), 1)
def __init__(self, value_or_numerator=0, denumerator=1): if isinstance(value_or_numerator, (Scalar, PyMPQ)): super().__init__(discr=ScalarDiscr.AP_SCALAR_MPQ, value=value_or_numerator) elif isinstance(value_or_numerator, float): mpq = PyMPQ(value_or_numerator) super().__init__(discr=ScalarDiscr.AP_SCALAR_MPQ, value=mpq) else: assert isinstance(value_or_numerator, int) and isinstance(denumerator, int) mpq = PyMPQ(value_or_numerator, denumerator) super().__init__(discr=ScalarDiscr.AP_SCALAR_MPQ, value=mpq)
def __init__(self, value_or_inf_num=0, sup_num=0, inf_den=1, sup_den=1): if isinstance(value_or_inf_num, Interval): super().__init__(value_or_inf_num) elif isinstance(value_or_inf_num, PyMPQScalar) and isinstance( sup_num, PyMPQScalar): super().__init__(value_or_inf_num, sup_num) elif isinstance(value_or_inf_num, PyMPQ) and isinstance( sup_num, PyMPQ): super().__init__(value_or_inf_num, sup_num) else: super().__init__(PyMPQ(value_or_inf_num, inf_den), PyMPQ(sup_num, sup_den))
def test_is_top(self): self.assertFalse(PyMPQInterval().is_top()) self.assertFalse(PyMPQInterval(0, 0).is_top()) self.assertFalse(PyMPQInterval(0, 0, 1, 1).is_top()) self.assertFalse( PyMPQInterval(PyMPQScalar(0), PyMPQScalar(0)).is_top()) self.assertFalse( PyMPQInterval(PyMPQScalar(0, 1), PyMPQScalar(0, 1)).is_top()) self.assertFalse(PyMPQInterval(-1, 1, 2, 2).is_top()) self.assertFalse(PyMPQInterval(PyMPQ(-1, 2), PyMPQ(1, 2)).is_top()) self.assertFalse( PyMPQInterval(PyMPQScalar(-1, 2), PyMPQScalar(1, 2)).is_top()) self.assertTrue(PyMPQInterval.top().is_top()) self.assertFalse(PyMPQInterval.bottom().is_top())
def test_initialization(self): self.assertEqual(str(PyMPQScalar()), '0') self.assertEqual(str(PyMPQScalar(0)), '0') self.assertEqual(str(PyMPQScalar(0, 1)), '0') self.assertEqual(str(PyMPQScalar(PyMPQ(0))), '0') self.assertEqual(str(PyMPQScalar(PyMPQ(0, 1))), '0') self.assertEqual(str(PyMPQScalar(-9)), '-9') self.assertEqual(str(PyMPQScalar(PyMPQ(-9))), '-9') self.assertEqual(str(PyMPQScalar(9)), '9') self.assertEqual(str(PyMPQScalar(PyMPQ(9))), '9') self.assertEqual(str(PyMPQScalar(1, 2)), '1/2') self.assertEqual(str(PyMPQScalar(PyMPQ(1, 2))), '1/2') self.assertEqual(str(PyMPQScalar.init_infty(-9)), '-9/0') self.assertEqual(str(PyMPQScalar.init_infty(0)), '0') self.assertEqual(str(PyMPQScalar.init_infty(9)), '9/0')
def test_init(self): self.assertEqual(str(PyMPQInterval()), '[0,0]') self.assertEqual(str(PyMPQInterval(0, 0)), '[0,0]') self.assertEqual(str(PyMPQInterval(0, 0, 1, 1)), '[0,0]') self.assertEqual(str(PyMPQInterval(PyMPQScalar(0), PyMPQScalar(0))), '[0,0]') self.assertEqual( str(PyMPQInterval(PyMPQScalar(0, 1), PyMPQScalar(0, 1))), '[0,0]') self.assertEqual(str(PyMPQInterval(-1, 1, 2, 2)), '[-1/2,1/2]') self.assertEqual(str(PyMPQInterval(PyMPQ(-1, 2), PyMPQ(1, 2))), '[-1/2,1/2]') self.assertEqual( str(PyMPQInterval(PyMPQScalar(-1, 2), PyMPQScalar(1, 2))), '[-1/2,1/2]') self.assertEqual(str(PyMPQInterval.top()), '[-1/0,1/0]') self.assertEqual(str(PyMPQInterval.bottom()), '[1,-1]')
def test_initialization_assignment_conversion(self): self.assertEqual(str(PyMPQ()), '0') self.assertEqual(str(PyMPQ(0)), '0') self.assertEqual(str(PyMPQ(0.5)), '1/2') self.assertEqual(str(PyMPQ(0, 1)), '0') self.assertEqual(str(PyMPQ(1, 2)), '1/2') self.assertEqual(str(PyMPQ(2, 4)), '1/2')
def test_cmp(self): self.assertTrue(PyMPQInterval.bottom() < PyMPQInterval()) self.assertFalse(PyMPQInterval() < PyMPQInterval.bottom()) self.assertTrue(PyMPQInterval() < PyMPQInterval(-1, 1, 2, 2)) self.assertFalse(PyMPQInterval(-1, 1, 2, 2) < PyMPQInterval()) self.assertTrue(PyMPQInterval(-1, 1, 2, 2) < PyMPQInterval.top()) self.assertFalse(PyMPQInterval.top() < PyMPQInterval(-1, 1, 2, 2)) self.assertFalse(PyMPQInterval.bottom() == PyMPQInterval()) self.assertTrue(PyMPQInterval() == PyMPQInterval(0, 0)) self.assertFalse(PyMPQInterval() == PyMPQInterval(-1, 1, 2, 2)) self.assertTrue( PyMPQInterval(-1, 1, 2, 2) == PyMPQInterval( PyMPQ(-1, 2), PyMPQ(1, 2))) self.assertFalse(PyMPQInterval(-1, 1, 2, 2) == PyMPQInterval.top()) self.assertFalse(PyMPQInterval.bottom() > PyMPQInterval()) self.assertTrue(PyMPQInterval() > PyMPQInterval.bottom()) self.assertFalse(PyMPQInterval() > PyMPQInterval(-1, 1, 2, 2)) self.assertTrue(PyMPQInterval(-1, 1, 2, 2) > PyMPQInterval()) self.assertFalse(PyMPQInterval(-1, 1, 2, 2) > PyMPQInterval.top()) self.assertTrue(PyMPQInterval.top() > PyMPQInterval(-1, 1, 2, 2)) self.assertTrue( PyMPQInterval(-3, -1) <= PyMPQInterval(PyMPQScalar.init_infty(-1), PyMPQScalar(-1)))
def test_neg(self): self.assertEqual(-PyMPQInterval.bottom(), PyMPQInterval.bottom()) self.assertEqual(-PyMPQInterval(-1, 2), PyMPQInterval(-2, 1)) self.assertEqual(-PyMPQInterval(), PyMPQInterval(0, 0)) self.assertEqual(-PyMPQInterval(-1, 1, 2, 2), PyMPQInterval(-PyMPQ(1, 2), PyMPQ(1, 2))) self.assertEqual(-PyMPQInterval(1, 1, 2, 2), PyMPQInterval(-PyMPQ(1, 2), -PyMPQ(1, 2))) self.assertEqual(-PyMPQInterval(1, 1, 2, 2), PyMPQInterval(PyMPQ(-1, 2), PyMPQ(-1, 2))) self.assertEqual(-PyMPQInterval(1, 2), PyMPQInterval(-2, -1)) self.assertEqual(-PyMPQInterval.top(), PyMPQInterval.top())
def test_comparison(self): self.assertTrue(PyMPQ(1, 4) < PyMPQ(1, 2)) self.assertTrue(PyMPQ(1, 4) <= PyMPQ(1, 2)) self.assertTrue(PyMPQ(1, 2) <= PyMPQ(1, 2)) self.assertTrue(PyMPQ(1, 2) == PyMPQ(1, 2)) self.assertTrue(PyMPQ(1, 4) != PyMPQ(1, 2)) self.assertTrue(PyMPQ(1, 2) >= PyMPQ(1, 2)) self.assertTrue(PyMPQ(1, 2) >= PyMPQ(1, 4)) self.assertTrue(PyMPQ(1, 2) > PyMPQ(1, 4))
def test_arithmetic(self): self.assertEqual(PyMPQ(1, 2) + PyMPQ(1, 4), PyMPQ(3, 4)) self.assertEqual(PyMPQ(1, 2) - PyMPQ(1, 4), PyMPQ(1, 4)) self.assertEqual(PyMPQ(1, 2) * PyMPQ(1, 4), PyMPQ(1, 8)) self.assertEqual(-PyMPQ(1, 2), PyMPQ(-1, 2)) self.assertEqual(-PyMPQ(2, 4), PyMPQ(-1, 2)) self.assertEqual(abs(PyMPQ(-1, 2)), PyMPQ(abs(-1), abs(2)))
def test_deepcopy(self): q0 = PyMPQ(1, 2) q1 = deepcopy(q0) q2 = q0 self.assertNotEqual(id(q0), id(q1)) self.assertEqual(id(q0), id(q2))