2 if sys
.path
[0] != "../..":
3 sys
.path
.insert(0, "../..")
8 from pyx
.graph
.axis
.tick
import rational
11 class RationalTestCase(unittest
.TestCase
):
13 def RationalEqual(self
, num
, denom
, r
):
14 self
.failUnlessEqual(num
*r
.denom
, r
.num
*denom
)
16 def testRationalInitSeq(self
):
17 self
.RationalEqual(1, 1, rational((2, 2)))
18 self
.RationalEqual(1, 1, rational([2, 2]))
20 def testRationalInitString(self
):
21 self
.RationalEqual(1, 1, rational("1"))
22 self
.RationalEqual(11, 10, rational("1.1"))
23 self
.RationalEqual(12345, 1000, rational("12.345"))
24 self
.RationalEqual(1, 1, rational("1."))
25 self
.RationalEqual(1, 10, rational(".1"))
26 self
.RationalEqual(1, 1, rational("1e+0"))
27 self
.RationalEqual(11, 10, rational("1.1e-0"))
28 self
.RationalEqual(10, 1, rational("1.e+1"))
29 self
.RationalEqual(1, 100, rational(".1e-1"))
30 self
.RationalEqual(-1, 1, rational("-1"))
31 self
.RationalEqual(-11, 10, rational("-1.1"))
32 self
.RationalEqual(-1, 1, rational("-1."))
33 self
.RationalEqual(-1, 10, rational("-.1"))
34 self
.RationalEqual(-1, 1, rational("-1e0"))
35 self
.RationalEqual(-11, 10, rational("-1.1e-0"))
36 self
.RationalEqual(-10, 1, rational("-1.e+1"))
37 self
.RationalEqual(-1, 100, rational("-.1e-1"))
38 self
.RationalEqual(-100000000000000000000, 1, rational("-1e+20"))
39 self
.RationalEqual(-1, 100000000000000000000, rational("-1e-20"))
41 self
.RationalEqual(1234, 1, rational(" 1234"))
42 self
.failUnlessRaises(ValueError, rational
, "12 34")
43 self
.failUnlessRaises(ValueError, rational
, "1 2.34")
44 self
.failUnlessRaises(ValueError, rational
, "12 .34")
45 self
.failUnlessRaises(ValueError, rational
, "12. 34")
46 self
.failUnlessRaises(ValueError, rational
, "12.3 4")
47 self
.RationalEqual(1234, 100, rational("12.34 "))
48 self
.RationalEqual(1234, 1, rational(" +1234"))
49 self
.RationalEqual(-1234, 1, rational(" -1234"))
50 self
.failUnlessRaises(ValueError, rational
, " + 1234")
51 self
.failUnlessRaises(ValueError, rational
, " - 1234")
52 self
.failUnlessRaises(ValueError, rational
, "12.34 e0")
53 self
.failUnlessRaises(ValueError, rational
, "12.34e 0")
54 self
.RationalEqual(1234, 10000, rational("12.34e-2"))
55 self
.RationalEqual(1234, 1000, rational("12.34E-1"))
56 self
.RationalEqual(1234, 100, rational("12.34e0 "))
57 self
.RationalEqual(1234, 10, rational("12.34E+1"))
58 self
.RationalEqual(1234, 1, rational("12.34e+2"))
59 self
.failUnlessRaises(ValueError, rational
, "12.34e -0")
60 self
.failUnlessRaises(ValueError, rational
, "12.34e+ 0")
61 self
.failUnlessRaises(ValueError, rational
, "12.34e- 0")
62 self
.failUnlessRaises(ValueError, rational
, "12.34e +0")
63 self
.failUnlessRaises(ValueError, rational
, "12.34.56")
64 self
.failUnlessRaises(ValueError, rational
, "12e34.56")
66 def testRationalInitStrings(self
):
67 self
.RationalEqual(1, 2, rational("1/2"))
68 self
.RationalEqual(1, 2, rational("1.1/2.2"))
69 self
.RationalEqual(1, 2, rational("1./2."))
70 self
.RationalEqual(1, 2, rational(".1/.2"))
71 self
.RationalEqual(1, 2, rational("1e+0/2e+0"))
72 self
.RationalEqual(1, 2, rational("1.1e-0/2.2e-0"))
73 self
.RationalEqual(1, 2, rational("1.e+1/2.e+1"))
74 self
.RationalEqual(1, 2, rational(".1e-1/.2e-1"))
75 self
.RationalEqual(1, 2, rational("-1/-2"))
76 self
.RationalEqual(1, 2, rational("-1.1/-2.2"))
77 self
.RationalEqual(1, 2, rational("-1./-2."))
78 self
.RationalEqual(1, 2, rational("-.1/-.2"))
79 self
.RationalEqual(1, 2, rational("-1e0/-2e0"))
80 self
.RationalEqual(1, 2, rational("-1.1e-0/-2.2e-0"))
81 self
.RationalEqual(1, 2, rational("-1.e+1/-2.e+1"))
82 self
.RationalEqual(1, 2, rational("-.1e-1/-.2e-1"))
84 def testRationalInitNumber(self
):
85 self
.RationalEqual(1, 1, rational(1))
86 self
.RationalEqual(11, 10, rational(1.1))
87 self
.RationalEqual(1, 1, rational(1.))
88 self
.RationalEqual(1, 10, rational(.1))
89 self
.RationalEqual(1, 1, rational(1e+0))
90 self
.RationalEqual(11, 10, rational(1.1e-0))
91 self
.RationalEqual(10, 1, rational(1.e
+1))
92 self
.RationalEqual(1, 100, rational(.1e-1))
93 self
.RationalEqual(-1, 1, rational(-1))
94 self
.RationalEqual(-11, 10, rational(-1.1))
95 self
.RationalEqual(-1, 1, rational(-1.))
96 self
.RationalEqual(-1, 10, rational(-.1))
97 self
.RationalEqual(-1, 1, rational(-1e0
))
98 self
.RationalEqual(-11, 10, rational(-1.1e-0))
99 self
.RationalEqual(-10, 1, rational(-1.e
+1))
100 self
.RationalEqual(-1, 100, rational(-.1e-1))
103 if __name__
== "__main__":