Mercurial > lcfOS
annotate test/testbintools.py @ 396:fb3c1f029b30
Added baselexer into c3 lexer
author | Windel Bouwman |
---|---|
date | Tue, 27 May 2014 22:19:32 +0200 |
parents | d056b552d3f4 |
children |
rev | line source |
---|---|
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
1 import unittest |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
2 import sys |
377 | 3 import io |
342 | 4 from ppci.target.arm.token import ArmToken |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
5 from ppci.linker import Linker |
377 | 6 from ppci.objectfile import ObjectFile, serialize, deserialize, load_object |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
7 from ppci import CompilerError |
377 | 8 from ppci.tasks import TaskRunner, TaskError |
9 from ppci.buildtasks import EmptyTask | |
381 | 10 from ppci.buildfunctions import link |
383 | 11 from ppci import layout |
342 | 12 |
13 | |
14 class TaskTestCase(unittest.TestCase): | |
377 | 15 @unittest.skip('api change') |
342 | 16 def testCircular(self): |
17 t1 = EmptyTask('t1') | |
18 t2 = EmptyTask('t2') | |
19 t1.add_dependency(t2) | |
20 with self.assertRaises(TaskError): | |
21 t2.add_dependency(t1) | |
22 | |
377 | 23 @unittest.skip('api change') |
342 | 24 def testCircularDeeper(self): |
25 t1 = EmptyTask('t1') | |
26 t2 = EmptyTask('t2') | |
27 t3 = EmptyTask('t3') | |
28 t1.add_dependency(t2) | |
29 t2.add_dependency(t3) | |
30 with self.assertRaises(TaskError): | |
31 t3.add_dependency(t1) | |
32 | |
377 | 33 @unittest.skip('api change') |
342 | 34 def testSort(self): |
35 t1 = EmptyTask('t1') | |
36 t2 = EmptyTask('t2') | |
37 runner = TaskRunner() | |
38 t1.add_dependency(t2) | |
39 runner.add_task(t1) | |
40 runner.add_task(t2) | |
41 runner.run_tasks() | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
42 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
43 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
44 class TokenTestCase(unittest.TestCase): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
45 def testSetBits(self): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
46 at = ArmToken() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
47 at[2:4] = 0b11 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
48 self.assertEqual(0xc, at.bit_value) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
49 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
50 def testSetBits(self): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
51 at = ArmToken() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
52 at[4:8] = 0b1100 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
53 self.assertEqual(0xc0, at.bit_value) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
54 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
55 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
56 class LinkerTestCase(unittest.TestCase): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
57 def testUndefinedReference(self): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
58 o1 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
59 o1.get_section('.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
60 o1.add_relocation('undefined_sym', 0, 'rel8', '.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
61 o2 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
62 with self.assertRaises(CompilerError): |
385 | 63 o3 = link([o1, o2], layout.Layout(), 'arm') |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
64 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
65 def testDuplicateSymbol(self): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
66 o1 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
67 o1.get_section('.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
68 o1.add_symbol('a', 0, '.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
69 o2 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
70 o2.get_section('.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
71 o2.add_symbol('a', 0, '.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
72 with self.assertRaises(CompilerError): |
385 | 73 o3 = link([o1, o2], layout.Layout(), 'arm') |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
74 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
75 def testRel8Relocation(self): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
76 o1 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
77 o1.get_section('.text').add_data(bytes([0]*100)) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
78 o1.add_relocation('a', 0, 'rel8', '.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
79 o2 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
80 o2.get_section('.text').add_data(bytes([0]*100)) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
81 o2.add_symbol('a', 24, '.text') |
385 | 82 o3 = link([o1, o2], layout.Layout(), 'arm') |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
83 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
84 def testSymbolValues(self): |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
85 o1 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
86 o1.get_section('.text').add_data(bytes([0]*108)) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
87 o1.add_symbol('b', 24, '.text') |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
88 o2 = ObjectFile() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
89 o2.get_section('.text').add_data(bytes([0]*100)) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
90 o2.add_symbol('a', 2, '.text') |
385 | 91 o3 = link([o1, o2], layout.Layout(), 'arm') |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
92 self.assertEqual(110, o3.find_symbol('a').value) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
93 self.assertEqual(24, o3.find_symbol('b').value) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
94 self.assertEqual(208, o3.get_section('.text').Size) |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
95 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
96 def testMemoryLayout(self): |
383 | 97 spec = """ |
98 MEMORY flash LOCATION=0x08000000 SIZE=0x3000 { | |
99 SECTION(code) | |
100 } | |
101 MEMORY flash LOCATION=0x20000000 SIZE=0x3000 { | |
102 SECTION(data) | |
103 } | |
104 """ | |
105 memory_layout = layout.load_layout(io.StringIO(spec)) | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
106 o1 = ObjectFile() |
383 | 107 o1.get_section('code').add_data(bytes([0]*108)) |
108 o1.add_symbol('b', 24, 'code') | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
109 o2 = ObjectFile() |
383 | 110 o2.get_section('code').add_data(bytes([0]*100)) |
111 o2.get_section('data').add_data(bytes([0]*100)) | |
112 o2.add_symbol('a', 2, 'data') | |
113 o2.add_symbol('c', 2, 'code') | |
385 | 114 o3 = link([o1, o2], memory_layout, 'arm') |
115 self.assertEqual(0x20000000+2, o3.get_symbol_value('a')) | |
116 self.assertEqual(0x08000000+24, o3.get_symbol_value('b')) | |
117 self.assertEqual(0x08000000+110, o3.get_symbol_value('c')) | |
383 | 118 self.assertEqual(208, o3.get_section('code').Size) |
119 self.assertEqual(100, o3.get_section('data').Size) | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
120 |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
121 |
377 | 122 class ObjectFileTestCase(unittest.TestCase): |
123 def makeTwins(self): | |
124 o1 = ObjectFile() | |
125 o2 = ObjectFile() | |
126 o2.get_section('code').add_data(bytes(range(55))) | |
127 o1.get_section('code').add_data(bytes(range(55))) | |
128 o1.add_relocation('A', 0x2, 'imm12_dumm', 'code') | |
129 o2.add_relocation('A', 0x2, 'imm12_dumm', 'code') | |
130 o1.add_symbol('A2', 0x90, 'code') | |
131 o2.add_symbol('A2', 0x90, 'code') | |
132 o1.add_symbol('A3', 0x90, 'code') | |
133 o2.add_symbol('A3', 0x90, 'code') | |
134 return o1, o2 | |
135 | |
136 def testEquality(self): | |
137 o1, o2 = self.makeTwins() | |
138 self.assertEqual(o1, o2) | |
139 | |
140 def testSaveAndLoad(self): | |
141 o1, o2 = self.makeTwins() | |
142 f1 = io.StringIO() | |
143 o1.save(f1) | |
144 f2 = io.StringIO(f1.getvalue()) | |
145 o3 = load_object(f2) | |
146 self.assertEqual(o3, o1) | |
147 | |
148 def testSerialization(self): | |
149 o1, o2 = self.makeTwins() | |
150 o3 = deserialize(serialize(o1)) | |
151 self.assertEqual(o3, o1) | |
152 | |
153 | |
383 | 154 class LayoutFileTestCase(unittest.TestCase): |
155 def testLayout1(self): | |
156 spec = """ | |
157 MEMORY flash LOCATION=0x1000 SIZE=0x3000 { | |
158 SECTION(code) | |
159 ALIGN(4) | |
160 } | |
161 """ | |
162 layout1 = layout.load_layout(io.StringIO(spec)) | |
163 layout2 = layout.Layout() | |
164 m = layout.Memory('flash') | |
165 m.location = 0x1000 | |
166 m.size = 0x3000 | |
167 m.add_input(layout.Section('code')) | |
168 m.add_input(layout.Align(4)) | |
169 layout2.add_memory(m) | |
170 self.assertEqual(layout2, layout1) | |
171 | |
172 | |
336
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
173 if __name__ == '__main__': |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
174 unittest.main() |
d1ecc493384e
Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
Windel Bouwman
parents:
diff
changeset
|
175 sys.exit() |