add normpath.split unit tests
[PyX/mjg.git] / test / unit / test_path.py
blob2c3ebcda0dc11c243e710577fb2bf82837ea4455
1 import unittest
3 from pyx import *
4 from pyx.path import *
6 epsilon = 1e-5
7 def isEqual(l1, l2):
8 return abs(unit.topt(l1-l2))<epsilon
12 class NormpathTestCase(unittest.TestCase):
13 def testsplit(self):
14 p = path(moveto(0,0), lineto(1,0), moveto(2,0), lineto(3,0))
15 np = normpath(p)
17 # one split parameter
18 sp = np.split([0])
19 assert len(sp)==2 and sp[0] is None and isEqual(sp[1].arclength(), 2)
21 sp = np.split([0.5])
22 assert len(sp)==2 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 1.5)
24 sp = np.split([1])
25 assert len(sp)==2 and isEqual(sp[0].arclength(), 1) and isEqual(sp[1].arclength(), 1)
27 sp = np.split([1.5])
28 assert len(sp)==2 and isEqual(sp[0].arclength(), 1.5) and isEqual(sp[1].arclength(), 0.5)
30 sp = np.split([2])
31 assert len(sp)==2 and isEqual(sp[0].arclength(), 2) and sp[1] is None
33 # two split parameters
34 sp = np.split([0, 0.5])
35 assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 1.5)
37 sp = np.split([0, 1])
38 assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclength(), 1) and isEqual(sp[2].arclength(), 1)
40 sp = np.split([0, 1.5])
41 assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclength(), 1.5) and isEqual(sp[2].arclength(), 0.5)
43 sp = np.split([0, 2])
44 assert len(sp)==3 and sp[0] is None and isEqual(sp[1].arclength(), 2) and sp[2] is None
46 sp = np.split([0.5, 1])
47 assert len(sp)==3 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 1)
49 sp = np.split([0.5, 1.5])
50 assert len(sp)==3 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 1) and isEqual(sp[2].arclength(), 0.5)
52 sp = np.split([0.5, 2])
53 assert len(sp)==3 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 1.5) and sp[2] is None
55 sp = np.split([1, 1.5])
56 assert len(sp)==3 and isEqual(sp[0].arclength(), 1) and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5)
58 sp = np.split([1, 2])
59 assert len(sp)==3 and isEqual(sp[0].arclength(), 1) and isEqual(sp[1].arclength(), 1) and sp[2] is None
61 sp = np.split([1.5, 2])
62 assert len(sp)==3 and isEqual(sp[0].arclength(), 1.5) and isEqual(sp[1].arclength(), 0.5) and sp[2] is None
64 # three split parameters
65 sp = np.split([0, 0.5, 1])
66 assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 1)
68 sp = np.split([0, 0.5, 1.5])
69 assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 1) and isEqual(sp[3].arclength(), 0.5)
71 sp = np.split([0, 0.5, 2])
72 assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 1.5) and sp[3] is None
74 sp = np.split([0, 1, 1.5])
75 assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclength(), 1) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 0.5)
77 sp = np.split([0, 1, 2])
78 assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclength(), 1) and isEqual(sp[2].arclength(), 1) and sp[3] is None
81 sp = np.split([0, 1.5, 2])
82 assert len(sp)==4 and sp[0] is None and isEqual(sp[1].arclength(), 1.5) and isEqual(sp[2].arclength(), 0.5) and sp[3] is None
84 sp = np.split([0.5, 1, 1.5])
85 assert len(sp)==4 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 0.5)
87 sp = np.split([0.5, 1.5, 2])
88 assert len(sp)==4 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 1) and isEqual(sp[2].arclength(), 0.5) and sp[3] is None
90 sp = np.split([1, 1.5, 2])
91 assert len(sp)==4 and isEqual(sp[0].arclength(), 1) and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and sp[3] is None
94 # four split parameters
95 sp = np.split([0, 0.5, 1, 1.5])
96 assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 0.5) and isEqual(sp[4].arclength(), 0.5)
98 sp = np.split([0, 0.5, 1, 2])
99 assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 1) and sp[4] is None
101 sp = np.split([0, 0.5, 1.5, 2])
102 assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 1) and isEqual(sp[3].arclength(), 0.5) and sp[4] is None
104 sp = np.split([0, 1, 1.5, 2])
105 assert len(sp)==5 and sp[0] is None and isEqual(sp[1].arclength(), 1) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 0.5) and sp[4] is None
107 sp = np.split([0.5, 1, 1.5, 2])
108 assert len(sp)==5 and isEqual(sp[0].arclength(), 0.5) and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 0.5) and sp[4] is None
111 # five split parameters
112 sp = np.split([0, 0.5, 1, 1.5, 2])
113 assert len(sp)==6 and sp[0] is None and isEqual(sp[1].arclength(), 0.5) and isEqual(sp[2].arclength(), 0.5) and isEqual(sp[3].arclength(), 0.5) and isEqual(sp[4].arclength(), 0.5) and sp[5] is None
116 suite = unittest.TestSuite((unittest.makeSuite(NormpathTestCase, 'test'),))
118 if __name__ == "__main__":
119 runner = unittest.TextTestRunner()
120 runner.run(suite)