fixed a bug in deformer.parallel, reported by Andreas Matthias
[PyX/mjg.git] / test / unit / test_data.py
blob652814a62720839018f5da2bb9f47b7ee9b2ab75
1 import sys
2 if sys.path[0] != "../..":
3 sys.path.insert(0, "../..")
5 import unittest
7 import StringIO
8 from pyx.graph import data
10 class DataTestCase(unittest.TestCase):
12 def testPoints(self):
13 mydata = data.points([[1, 2, 3], [4, 5, 6]], a=1, b=2)
14 self.failUnlessEqual(mydata.columndata[0], [1, 2])
15 self.failUnlessEqual(mydata.columns["a"], [1, 4])
16 self.failUnlessEqual(mydata.columndata[2], [2, 5])
17 self.failUnlessEqual("c" in mydata.columns.keys(), 0)
19 def testValues(self):
20 mydata = data.values(a=[1, 4])
21 self.failUnlessEqual(mydata.columns["a"], [1, 4])
22 self.failUnlessEqual("c" in mydata.columns.keys(), 0)
24 def testData(self):
25 mydata = data.points([[1], [2]], a=1)
26 mydata2 = data.data(mydata, a="2*a", b="2*$1*a", c="4*$(i)*a*$(-1)", context={"i":1})
27 self.failUnlessEqual(mydata.columns["a"], [1, 2])
28 self.failUnlessAlmostEqual(mydata2.columns["a"][0], 2.0)
29 self.failUnlessAlmostEqual(mydata2.columns["a"][1], 4.0)
30 self.failUnlessAlmostEqual(mydata2.columns["b"][0], 2.0)
31 self.failUnlessAlmostEqual(mydata2.columns["b"][1], 8.0)
32 self.failUnlessAlmostEqual(mydata2.columns["c"][0], 4.0)
33 self.failUnlessAlmostEqual(mydata2.columns["c"][1], 32.0)
34 mydata3 = data.data(mydata2, a="b", b="2*c")
35 self.failUnlessEqual(mydata3.columns["a"], mydata2.columns["b"])
36 self.failUnlessAlmostEqual(mydata3.columns["b"][0], 2*mydata2.columns["c"][0])
37 self.failUnlessAlmostEqual(mydata3.columns["b"][1], 2*mydata2.columns["c"][1])
39 a = "nothing"
40 two = 2
41 f = lambda x: x*x
42 mydata = data.points([[1], [2]], a=1)
43 mydata2 = data.data(mydata, b="two*a", c="two*$1*a", d="f($1)", context=locals())
44 self.failUnlessEqual(mydata.columndata[0], [1, 2])
45 self.failUnlessAlmostEqual(mydata2.columns["b"][0], 2.0)
46 self.failUnlessAlmostEqual(mydata2.columns["b"][1], 4.0)
47 self.failUnlessAlmostEqual(mydata2.columns["c"][0], 2.0)
48 self.failUnlessAlmostEqual(mydata2.columns["c"][1], 8.0)
49 self.failUnlessAlmostEqual(mydata2.columns["d"][0], 1.0)
50 self.failUnlessAlmostEqual(mydata2.columns["d"][1], 4.0)
52 def testFile(self):
53 testfile = StringIO.StringIO("""#a
55 1 eins
56 2 "2"
57 3 x"x""")
58 mydata = data.file(testfile, row=0, a="a", b=2)
59 self.failUnlessEqual(mydata.columns["row"], [1, 2, 3, 4])
60 self.failUnlessAlmostEqual(mydata.columns["a"][0], 0.0)
61 self.failUnlessAlmostEqual(mydata.columns["a"][1], 1.0)
62 self.failUnlessAlmostEqual(mydata.columns["a"][2], 2.0)
63 self.failUnlessAlmostEqual(mydata.columns["a"][3], 3.0)
64 self.failUnlessEqual(mydata.columns["b"][0], None)
65 self.failUnlessEqual(mydata.columns["b"][1], "eins")
66 self.failUnlessEqual(mydata.columns["b"][2], "2")
67 self.failUnlessEqual(mydata.columns["b"][3], "x\"x")
68 testfile = StringIO.StringIO("""#a
78 9""")
79 mydata = data.file(testfile, title="title", skiphead=3, skiptail=2, every=2, row=0)
80 self.failUnlessEqual(mydata.columns["row"], [4, 6, 8])
81 self.failUnlessEqual(mydata.title, "title")
83 def testSec(self):
84 testfile = StringIO.StringIO("""[sec1]
85 opt1=a1
86 opt2=a2
87 val=1
88 val=2
90 [sec2]
91 opt1=a4
92 opt2=a5
93 val=2
94 val=1
96 [sec1]
97 opt3=a3""")
98 mydata = data.conffile(testfile, sec=0, a="opt1", b="opt2", c="opt3", d="val")
99 self.failUnlessEqual(mydata.columns["sec"], ["sec1", "sec2"])
100 self.failUnlessEqual(mydata.columns["a"], ["a1", "a4"])
101 self.failUnlessEqual(mydata.columns["b"], ["a2", "a5"])
102 self.failUnlessEqual(mydata.columns["c"], ["a3", None])
103 self.failUnlessAlmostEqual(mydata.columns["d"][0], 2.0)
104 self.failUnlessAlmostEqual(mydata.columns["d"][1], 1.0)
106 def testParamfunction(self):
107 mydata = data.paramfunction("k", 0, 9, "x, y = k, -k", points=10)
108 for i in range(10):
109 self.failUnlessEqual(mydata.columns["x"][i], i)
110 self.failUnlessEqual(mydata.columns["y"][i], -i)
113 if __name__ == "__main__":
114 unittest.main()