copyright
[PyX/mjg.git] / test / unit / test_unit.py
blob11e04b898cba7a0535414626d60f24a8889aa664
1 import sys
2 if sys.path[0] != "../..":
3 sys.path.insert(0, "../..")
5 import unittest
7 import types
8 from pyx import *
10 class UnitTestCase(unittest.TestCase):
12 def testTrueUnits(self):
13 self.failUnlessAlmostEqual(unit.tom(unit.t_m*42), 42)
14 self.failUnlessAlmostEqual(unit.tocm(unit.t_cm*42), 42)
15 self.failUnlessAlmostEqual(unit.tomm(unit.t_mm*42), 42)
16 self.failUnlessAlmostEqual(unit.toinch(unit.t_inch*42), 42)
17 self.failUnlessAlmostEqual(unit.topt(unit.t_pt*42), 42)
19 def testUserUnits(self):
20 unit.set(uscale=2)
21 self.failUnlessAlmostEqual(unit.tom(unit.m*42), 84)
22 self.failUnlessAlmostEqual(unit.tocm(unit.cm*42), 84)
23 self.failUnlessAlmostEqual(unit.tomm(unit.mm*42), 84)
24 self.failUnlessAlmostEqual(unit.toinch(unit.inch*42), 84)
25 self.failUnlessAlmostEqual(unit.topt(unit.pt*42), 84)
26 unit.set(uscale=1)
28 def testVisualUnits(self):
29 unit.set(vscale=3)
30 self.failUnlessAlmostEqual(unit.tom(unit.v_m*42), 126)
31 self.failUnlessAlmostEqual(unit.tocm(unit.v_cm*42), 126)
32 self.failUnlessAlmostEqual(unit.tomm(unit.v_mm*42), 126)
33 self.failUnlessAlmostEqual(unit.toinch(unit.v_inch*42), 126)
34 self.failUnlessAlmostEqual(unit.topt(unit.v_pt*42), 126)
35 unit.set(vscale=1)
37 def testWidthUnits(self):
38 unit.set(wscale=4)
39 self.failUnlessAlmostEqual(unit.tom(unit.w_m*42), 168)
40 self.failUnlessAlmostEqual(unit.tocm(unit.w_cm*42), 168)
41 self.failUnlessAlmostEqual(unit.tomm(unit.w_mm*42), 168)
42 self.failUnlessAlmostEqual(unit.toinch(unit.w_inch*42), 168)
43 self.failUnlessAlmostEqual(unit.topt(unit.w_pt*42), 168)
44 unit.set(wscale=1)
46 def testTeXUnits(self):
47 unit.set(xscale=5)
48 self.failUnlessAlmostEqual(unit.tom(unit.x_m*42), 210)
49 self.failUnlessAlmostEqual(unit.tocm(unit.x_cm*42), 210)
50 self.failUnlessAlmostEqual(unit.tomm(unit.x_mm*42), 210)
51 self.failUnlessAlmostEqual(unit.toinch(unit.x_inch*42), 210)
52 self.failUnlessAlmostEqual(unit.topt(unit.x_pt*42), 210)
53 unit.set(xscale=1)
55 def testMixedUnits(self):
56 unit.set(uscale=2, vscale=3, wscale=4, xscale=5)
57 self.failUnlessAlmostEqual(unit.tom(42*unit.t_m + 42*unit.m + 42*unit.v_m + 42*unit.w_m + 42*unit.x_m), 630)
58 self.failUnlessAlmostEqual(unit.tocm(42*unit.t_cm + 42*unit.cm + 42*unit.v_cm + 42*unit.w_cm + 42*unit.x_cm), 630)
59 self.failUnlessAlmostEqual(unit.toinch(42*unit.t_inch + 42*unit.inch + 42*unit.v_inch + 42*unit.w_inch + 42*unit.x_inch), 630)
60 self.failUnlessAlmostEqual(unit.topt(42*unit.t_pt + 42*unit.pt + 42*unit.v_pt + 42*unit.w_pt + 42*unit.x_pt), 630)
61 unit.set(uscale=1, vscale=1, wscale=1, xscale=1)
63 def testCompareUnits(self):
64 assert unit.cm*41 < unit.cm*42, "comparision error"
65 assert unit.cm*42 == unit.cm*42, "numbers should be equal (might be unstable)"
66 assert unit.cm*43 > unit.cm*42, "comparision error"
67 assert unit.cm*41 < 42, "comparision error"
68 assert unit.cm*42 == 42, "numbers should be equal (might be unstable)"
69 assert unit.cm*43 > 42, "comparision error"
70 assert 42 > unit.cm*41, "comparision error"
71 assert 42 == unit.cm*42, "numbers should be equal (might be unstable)"
72 assert 42 < unit.cm*43, "comparision error"
75 if __name__ == "__main__":
76 unittest.main()