annotate test/testsamples.py @ 396:fb3c1f029b30

Added baselexer into c3 lexer
author Windel Bouwman
date Tue, 27 May 2014 22:19:32 +0200
parents d056b552d3f4
children c0d9837acde8
rev   line source
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
1 import unittest
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
2 import os
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
3 import io
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
4 from testemulation import runQemu, has_qemu
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
5 from testzcc import relpath
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
6 from ppci.buildfunctions import assemble, c3compile, link
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
7
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
8 startercode = """
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
9 section reset
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
10 mov sp, 0x30000 ; setup stack pointer
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
11 BL sample_start ; Branch to sample start
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
12 local_loop:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
13 B local_loop
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
14 """
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
15
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
16 modarchcode = """
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
17 module arch;
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
18
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
19 function void putc(int c)
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
20 {
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
21 var int *UART0DR;
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
22 UART0DR = cast<int*>(0x10009000); // UART0 DR register
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
23 *UART0DR = c;
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
24 }
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
25
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
26 """
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
27
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
28 arch_mmap = """
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
29 MEMORY image LOCATION=0x10000 SIZE=0x10000 {
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
30 SECTION(reset)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
31 SECTION(code)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
32 }
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
33
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
34 MEMORY ram LOCATION=0x20000 SIZE=0x10000 {
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
35 SECTION(data)
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
36 }
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
37 """
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
38
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
39 class Samples:
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
40 def testPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
41 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
42 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
43 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
44 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
45 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
46 io.print("Hello world");
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
47 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
48 """
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
49 self.do(snippet, "Hello world")
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
50
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
51 def testForLoopPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
52 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
53 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
54 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
55 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
56 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
57 var int i;
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
58 for (i=0; i<10; i = i + 1)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
59 {
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
60 io.print2("A = ", i);
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
61 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
62 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
63 """
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
64 res = "".join("A = 0x{0:08X}\n".format(a) for a in range(10))
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
65 self.do(snippet, res)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
66
374
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
67 def testIfStatement(self):
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
68 snippet = """
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
69 module sample;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
70 import io;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
71 function void start()
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
72 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
73 var int i;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
74 i = 13;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
75 if (i*7 < 100)
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
76 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
77 io.print("Wow");
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
78 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
79 else
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
80 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
81 io.print("Outch");
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
82 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
83 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
84 """
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
85 res = "Wow"
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
86 self.do(snippet, res)
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
87
372
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
88 def testParameterPassing4(self):
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
89 snippet = """
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
90 module sample;
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
91 import io;
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
92 function void dump(int a, int b, int c, int d)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
93 {
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
94 io.print2("a=", a);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
95 io.print2("b=", b);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
96 io.print2("c=", c);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
97 io.print2("d=", d);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
98 }
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
99 function void start()
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
100 {
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
101 dump(4,55,66,0x1337);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
102 }
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
103 """
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
104 res = "a=0x{0:08X}\n".format(4)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
105 res += "b=0x{0:08X}\n".format(55)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
106 res += "c=0x{0:08X}\n".format(66)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
107 res += "d=0x{0:08X}\n".format(0x1337)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
108 self.do(snippet, res)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
109
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
110 def testGlobalVariable(self):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
111 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
112 module sample;
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
113 import io;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
114 var int G;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
115 function void do1()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
116 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
117 G = G + 1;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
118 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
119 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
120 function void do5()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
121 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
122 G = G + 5;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
123 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
124 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
125 function void start()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
126 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
127 G = 0;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
128 do1();
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
129 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
130 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
131 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
132 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
133 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
134 """
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
135 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13])
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
136 self.do(snippet, res)
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
137
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
138
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
139 class TestSamplesOnVexpress(unittest.TestCase, Samples):
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
140 def setUp(self):
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
141 if not has_qemu():
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
142 self.skipTest('Not running qemu tests')
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
143
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
144 def do(self, src, expected_output):
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
145 # Construct binary file from snippet:
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
146 o1 = assemble(io.StringIO(startercode), 'arm')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
147 o2 = c3compile([
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
148 relpath('..', 'kernel', 'src', 'io.c3'),
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 377
diff changeset
149 io.StringIO(modarchcode),
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
150 io.StringIO(src)], [], 'arm')
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
151 o3 = link([o2, o1], io.StringIO(arch_mmap), 'arm')
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
152
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
153 img_data = o3.get_image('image')
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
154 sample_filename = 'testsample.bin'
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
155 with open(sample_filename, 'wb') as f:
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
156 f.write(img_data)
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
157
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
158 # Check bin file exists:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
159 self.assertTrue(os.path.isfile(sample_filename))
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
160
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
161 # Run bin file in emulator:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
162 res = runQemu(sample_filename, machine='vexpress-a9')
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
163 os.remove(sample_filename)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
164 self.assertEqual(expected_output, res)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
165
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
166
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
167 # TODO: test samples on thumb target..
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
168
173e20a47fda Added linker description loader
Windel Bouwman
parents: 381
diff changeset
169
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
170 if __name__ == '__main__':
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
171 unittest.main()
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
172