3 ### Testing rational arithmetic functionality
5 ### (c) 2019 Straylight/Edgeware
8 ###----- Licensing notice ---------------------------------------------------
10 ### This file is part of the Python interface to Catacomb.
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.
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.
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,
27 ###--------------------------------------------------------------------------
34 ###--------------------------------------------------------------------------
35 class TestRat (U
.TestCase
):
40 ## Check that exact true division in the base ring gives base-ring
42 a
, b
, c
= R(19), R(5), R(8)
45 me
.assertEqual(type(q
), R
)
48 ## Check that inexact division gives a fraction.
50 me
.assertNotEqual(type(f
), R
)
51 me
.assertNotEqual(f
, a
)
53 me
.assertEqual(b
*r
, c
)
55 ## More complicated arithmetic.
57 me
.assertEqual((u
+ v
)*(b
*c
), a
*(b
+ c
))
58 me
.assertEqual((u
- v
)*(b
*c
), a
*(c
- b
))
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
)
71 ## Conversions from string.
72 me
.assertRaises(TypeError, T
.add
, f
, "3")
75 me
.check_rat(C
.IntRat
)
77 ## Check interaction with floating point.
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)
84 ## Check string conversions.
85 me
.assertEqual(str(u
), "5/4")
86 me
.assertEqual(repr(u
), "IntRat(5, 4)")
91 ## Check string conversions.
93 me
.assertRaises(TypeError, T
.add
, u
, 0.0)
94 me
.assertEqual(str(u
), "0x5/0x4")
95 me
.assertEqual(repr(u
), "GFRat(0x5, 0x4)")
100 me
.assertRaises(TypeError, T
.add
, u
, v
)
102 ###----- That's all, folks --------------------------------------------------
104 if __name__
== "__main__": U
.main()