be save on late evaluations
[PyX/mjg.git] / test / unit / test_unit.py
blob9dc762e32dc85245634d0b35ec1ac4311bd7f79b
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(unit.length("42 t m") + unit.length("42 u m") + unit.length("42 v m") + unit.length("42 w m") + unit.length("42 x m")), 630)
58 self.failUnlessAlmostEqual(unit.tocm(unit.length("42 t cm") + unit.length("42 u cm") + unit.length("42 v cm") + unit.length("42 w cm") + unit.length("42 x cm")), 630)
59 self.failUnlessAlmostEqual(unit.tomm(unit.length("42 t mm") + unit.length("42 u mm") + unit.length("42 v mm") + unit.length("42 w mm") + unit.length("42 x mm")), 630)
60 self.failUnlessAlmostEqual(unit.toinch(unit.length("42 t inch") + unit.length("42 u inch") + unit.length("42 v inch") + unit.length("42 w inch") + unit.length("42 x inch")), 630)
61 self.failUnlessAlmostEqual(unit.topt(unit.length("42 t pt") + unit.length("42 u pt") + unit.length("42 v pt") + unit.length("42 w pt") + unit.length("42 x pt")), 630)
62 unit.set(uscale=1, vscale=1, wscale=1, xscale=1)
64 def testCompareUnits(self):
65 assert unit.cm(41) < unit.cm(42), "comparision error"
66 assert unit.cm(42) == unit.cm(42), "numbers should be equal (might be unstable)"
67 assert unit.cm(43) > unit.cm(42), "comparision error"
68 assert unit.cm(41) < 42, "comparision error"
69 assert unit.cm(42) == 42, "numbers should be equal (might be unstable)"
70 assert unit.cm(43) > 42, "comparision error"
71 assert 42 > unit.cm(41), "comparision error"
72 assert 42 == unit.cm(42), "numbers should be equal (might be unstable)"
73 assert 42 < unit.cm(43), "comparision error"
76 if __name__ == "__main__":
77 unittest.main()