chiark / gitweb /
t/: Add a test suite.
[catacomb-python] / t / t-rat.py
1 ### -*-python-*-
2 ###
3 ### Testing rational arithmetic functionality
4 ###
5 ### (c) 2019 Straylight/Edgeware
6 ###
7
8 ###----- Licensing notice ---------------------------------------------------
9 ###
10 ### This file is part of the Python interface to Catacomb.
11 ###
12 ### Catacomb/Python is free software: you can redistribute it and/or
13 ### modify it under the terms of the GNU General Public License as
14 ### published by the Free Software Foundation; either version 2 of the
15 ### License, or (at your option) any later version.
16 ###
17 ### Catacomb/Python is distributed in the hope that it will be useful, but
18 ### WITHOUT ANY WARRANTY; without even the implied warranty of
19 ### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ### General Public License for more details.
21 ###
22 ### You should have received a copy of the GNU General Public License
23 ### along with Catacomb/Python.  If not, write to the Free Software
24 ### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
25 ### USA.
26
27 ###--------------------------------------------------------------------------
28 ### Imported modules.
29
30 import catacomb as C
31 import unittest as U
32 import testutils as T
33
34 ###--------------------------------------------------------------------------
35 class TestRat (U.TestCase):
36
37   def check_rat(me, k):
38     R = k.RING
39
40     ## Check that exact true division in the base ring gives base-ring
41     ## elements.
42     a, b, c = R(19), R(5), R(8)
43     m = a*b
44     q = m/b
45     me.assertEqual(type(q), R)
46     me.assertEqual(q, a)
47
48     ## Check that inexact division gives a fraction.
49     f = (m + c)/b
50     me.assertNotEqual(type(f), R)
51     me.assertNotEqual(f, a)
52     r = f - q
53     me.assertEqual(b*r, c)
54
55     ## More complicated arithmetic.
56     u, v = a/b, a/c
57     me.assertEqual((u + v)*(b*c), a*(b + c))
58     me.assertEqual((u - v)*(b*c), a*(c - b))
59
60     ## Ordering.
61     me.assertTrue(b < c)
62     me.assertTrue(b/a < c/a)
63     me.assertFalse(c/a < b/a)
64     me.assertTrue(b/a <= c/a)
65     me.assertFalse(c/a <= b/a)
66     me.assertFalse(b/a >= c/a)
67     me.assertTrue(c/a >= b/a)
68     me.assertFalse(b/a > c/a)
69     me.assertTrue(c/a > b/a)
70
71   def test_intrat(me):
72     me.check_rat(C.IntRat)
73
74     ## Check string conversions.
75     u = C.MP(5)/C.MP(4)
76     me.assertEqual(str(u), "5/4")
77     me.assertEqual(repr(u), "IntRat(5, 4)")
78
79   def test_gfrat(me):
80     me.check_rat(C.GFRat)
81
82     ## Check string conversions.
83     u = C.GF(5)/C.GF(4)
84     me.assertEqual(str(u), "0x5/0x4")
85     me.assertEqual(repr(u), "GFRat(0x5, 0x4)")
86
87   def test_cross(me):
88     u = C.MP(5)/C.MP(3)
89     v = C.GF(5)/C.GF(3)
90     me.assertRaises(TypeError, T.add, u, v)
91
92 ###----- That's all, folks --------------------------------------------------
93
94 if __name__ == "__main__": U.main()