@@@ py_buffer/freebin wip
[catacomb-python] / t / t-rat.py
CommitLineData
553d59fe
MW
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
30import catacomb as C
31import unittest as U
32import testutils as T
33
34###--------------------------------------------------------------------------
35class 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
ad52b46f
MW
71 ## Conversions from string.
72 me.assertRaises(TypeError, T.add, f, "3")
73
553d59fe
MW
74 def test_intrat(me):
75 me.check_rat(C.IntRat)
76
ad52b46f 77 ## Check interaction with floating point.
553d59fe 78 u = C.MP(5)/C.MP(4)
ad52b46f
MW
79 me.assertEqual(type(u + 0.0), float)
80 me.assertEqual(u, 1.25)
81 me.assertTrue(u < 1.26)
82 me.assertTrue(u > 1.24)
83
84 ## Check string conversions.
553d59fe
MW
85 me.assertEqual(str(u), "5/4")
86 me.assertEqual(repr(u), "IntRat(5, 4)")
87
88 def test_gfrat(me):
89 me.check_rat(C.GFRat)
90
91 ## Check string conversions.
92 u = C.GF(5)/C.GF(4)
ad52b46f 93 me.assertRaises(TypeError, T.add, u, 0.0)
553d59fe
MW
94 me.assertEqual(str(u), "0x5/0x4")
95 me.assertEqual(repr(u), "GFRat(0x5, 0x4)")
96
97 def test_cross(me):
98 u = C.MP(5)/C.MP(3)
99 v = C.GF(5)/C.GF(3)
100 me.assertRaises(TypeError, T.add, u, v)
101
102###----- That's all, folks --------------------------------------------------
103
104if __name__ == "__main__": U.main()