add UnicodeEngine (MultiEngineText and axis texters returning MultiEngineText), texte...
[PyX.git] / test / unit / test_data.py
blob90adef317106c424597a1a7f02a42961cfaa37cb
1 import sys
2 if sys.path[0] != "../..":
3 sys.path.insert(0, "../..")
5 import unittest
7 import io
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.assertEqual(mydata.columndata[0], [1, 2])
15 self.assertEqual(mydata.columns["a"], [1, 4])
16 self.assertEqual(mydata.columndata[2], [2, 5])
17 self.assertEqual("c" in list(mydata.columns.keys()), 0)
19 def testValues(self):
20 mydata = data.values(a=[1, 4])
21 self.assertEqual(mydata.columns["a"], [1, 4])
22 self.assertEqual("c" in list(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.assertEqual(mydata.columns["a"], [1, 2])
28 self.assertAlmostEqual(mydata2.columns["a"][0], 2.0)
29 self.assertAlmostEqual(mydata2.columns["a"][1], 4.0)
30 self.assertAlmostEqual(mydata2.columns["b"][0], 2.0)
31 self.assertAlmostEqual(mydata2.columns["b"][1], 8.0)
32 self.assertAlmostEqual(mydata2.columns["c"][0], 4.0)
33 self.assertAlmostEqual(mydata2.columns["c"][1], 32.0)
34 mydata3 = data.data(mydata2, a="b", b="2*c")
35 self.assertEqual(mydata3.columns["a"], mydata2.columns["b"])
36 self.assertAlmostEqual(mydata3.columns["b"][0], 2*mydata2.columns["c"][0])
37 self.assertAlmostEqual(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.assertEqual(mydata.columndata[0], [1, 2])
45 self.assertAlmostEqual(mydata2.columns["b"][0], 2.0)
46 self.assertAlmostEqual(mydata2.columns["b"][1], 4.0)
47 self.assertAlmostEqual(mydata2.columns["c"][0], 2.0)
48 self.assertAlmostEqual(mydata2.columns["c"][1], 8.0)
49 self.assertAlmostEqual(mydata2.columns["d"][0], 1.0)
50 self.assertAlmostEqual(mydata2.columns["d"][1], 4.0)
52 def testFile(self):
53 testfile = io.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.assertEqual(mydata.columns["row"], [1, 2, 3, 4])
60 self.assertAlmostEqual(mydata.columns["a"][0], 0.0)
61 self.assertAlmostEqual(mydata.columns["a"][1], 1.0)
62 self.assertAlmostEqual(mydata.columns["a"][2], 2.0)
63 self.assertAlmostEqual(mydata.columns["a"][3], 3.0)
64 self.assertEqual(mydata.columns["b"][0], None)
65 self.assertEqual(mydata.columns["b"][1], "eins")
66 self.assertEqual(mydata.columns["b"][2], "2")
67 self.assertEqual(mydata.columns["b"][3], "x\"x")
68 testfile = io.StringIO("""#a
78 9""")
79 mydata = data.file(testfile, title="title", skiphead=3, skiptail=2, every=2, row=0)
80 self.assertEqual(mydata.columns["row"], [4, 6, 8])
81 self.assertEqual(mydata.title, "title")
83 def testSec(self):
84 testfile = io.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.assertEqual(mydata.columns["sec"], ["sec1", "sec2"])
100 self.assertEqual(mydata.columns["a"], ["a1", "a4"])
101 self.assertEqual(mydata.columns["b"], ["a2", "a5"])
102 self.assertEqual(mydata.columns["c"], ["a3", None])
103 self.assertAlmostEqual(mydata.columns["d"][0], 2.0)
104 self.assertAlmostEqual(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.assertEqual(mydata.columns["x"][i], i)
110 self.assertEqual(mydata.columns["y"][i], -i)
113 if __name__ == "__main__":
114 unittest.main()