Merge branch '1.3.x'
[catacomb-python] / t / t-rat.py
diff --git a/t/t-rat.py b/t/t-rat.py
new file mode 100644 (file)
index 0000000..9fd15d4
--- /dev/null
@@ -0,0 +1,94 @@
+### -*-python-*-
+###
+### Testing rational arithmetic functionality
+###
+### (c) 2019 Straylight/Edgeware
+###
+
+###----- Licensing notice ---------------------------------------------------
+###
+### This file is part of the Python interface to Catacomb.
+###
+### Catacomb/Python is free software: you can redistribute it and/or
+### modify it under the terms of the GNU General Public License as
+### published by the Free Software Foundation; either version 2 of the
+### License, or (at your option) any later version.
+###
+### Catacomb/Python is distributed in the hope that it will be useful, but
+### WITHOUT ANY WARRANTY; without even the implied warranty of
+### MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+### General Public License for more details.
+###
+### You should have received a copy of the GNU General Public License
+### along with Catacomb/Python.  If not, write to the Free Software
+### Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
+### USA.
+
+###--------------------------------------------------------------------------
+### Imported modules.
+
+import catacomb as C
+import unittest as U
+import testutils as T
+
+###--------------------------------------------------------------------------
+class TestRat (U.TestCase):
+
+  def check_rat(me, k):
+    R = k.RING
+
+    ## Check that exact true division in the base ring gives base-ring
+    ## elements.
+    a, b, c = R(19), R(5), R(8)
+    m = a*b
+    q = m/b
+    me.assertEqual(type(q), R)
+    me.assertEqual(q, a)
+
+    ## Check that inexact division gives a fraction.
+    f = (m + c)/b
+    me.assertNotEqual(type(f), R)
+    me.assertNotEqual(f, a)
+    r = f - q
+    me.assertEqual(b*r, c)
+
+    ## More complicated arithmetic.
+    u, v = a/b, a/c
+    me.assertEqual((u + v)*(b*c), a*(b + c))
+    me.assertEqual((u - v)*(b*c), a*(c - b))
+
+    ## Ordering.
+    me.assertTrue(b < c)
+    me.assertTrue(b/a < c/a)
+    me.assertFalse(c/a < b/a)
+    me.assertTrue(b/a <= c/a)
+    me.assertFalse(c/a <= b/a)
+    me.assertFalse(b/a >= c/a)
+    me.assertTrue(c/a >= b/a)
+    me.assertFalse(b/a > c/a)
+    me.assertTrue(c/a > b/a)
+
+  def test_intrat(me):
+    me.check_rat(C.IntRat)
+
+    ## Check string conversions.
+    u = C.MP(5)/C.MP(4)
+    me.assertEqual(str(u), "5/4")
+    me.assertEqual(repr(u), "IntRat(5, 4)")
+
+  def test_gfrat(me):
+    me.check_rat(C.GFRat)
+
+    ## Check string conversions.
+    u = C.GF(5)/C.GF(4)
+    me.assertEqual(str(u), "0x5/0x4")
+    me.assertEqual(repr(u), "GFRat(0x5, 0x4)")
+
+  def test_cross(me):
+    u = C.MP(5)/C.MP(3)
+    v = C.GF(5)/C.GF(3)
+    me.assertRaises(TypeError, T.add, u, v)
+
+###----- That's all, folks --------------------------------------------------
+
+if __name__ == "__main__": U.main()