annotate test/testsamples.py @ 377:9667d78ba79e

Switched to xml for project description
author Windel Bouwman
date Fri, 11 Apr 2014 15:47:50 +0200
parents 19eacf4f7270
children 6df89163e114
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 = """
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
9 mov sp, 0x30000 ; setup stack pointer
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
10 BL sample_start ; Branch to sample start
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
11 local_loop:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
12 B local_loop
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
13 """
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
14
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
15 class Samples:
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
16 def testPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
17 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
18 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
19 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
20 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
21 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
22 io.print("Hello world");
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
23 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
24 """
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
25 self.do(snippet, "Hello world")
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
26
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
27 def testForLoopPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
28 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
29 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
30 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
31 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
32 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
33 var int i;
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
34 for (i=0; i<10; i = i + 1)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
35 {
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
36 io.print2("A = ", i);
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
37 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
38 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
39 """
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
40 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
41 self.do(snippet, res)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
42
374
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
43 def testIfStatement(self):
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
44 snippet = """
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
45 module sample;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
46 import io;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
47 function void start()
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
48 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
49 var int i;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
50 i = 13;
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
51 if (i*7 < 100)
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
52 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
53 io.print("Wow");
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
54 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
55 else
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
56 {
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
57 io.print("Outch");
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
58 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
59 }
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
60 """
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
61 res = "Wow"
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
62 self.do(snippet, res)
72a3b646d543 Added if statement sample
Windel Bouwman
parents: 372
diff changeset
63
372
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
64 def testParameterPassing4(self):
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
65 snippet = """
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
66 module sample;
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
67 import io;
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
68 function void dump(int a, int b, int c, int d)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
69 {
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
70 io.print2("a=", a);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
71 io.print2("b=", b);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
72 io.print2("c=", c);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
73 io.print2("d=", d);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
74 }
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
75 function void start()
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
76 {
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
77 dump(4,55,66,0x1337);
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
78 }
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
79 """
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
80 res = "a=0x{0:08X}\n".format(4)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
81 res += "b=0x{0:08X}\n".format(55)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
82 res += "c=0x{0:08X}\n".format(66)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
83 res += "d=0x{0:08X}\n".format(0x1337)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
84 self.do(snippet, res)
68841f9ab96c Added yet another test snippet
Windel Bouwman
parents: 366
diff changeset
85
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
86 def testGlobalVariable(self):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
87 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
88 module sample;
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
89 import io;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
90 var int G;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
91 function void do1()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
92 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
93 G = G + 1;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
94 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
95 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
96 function void do5()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
97 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
98 G = G + 5;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
99 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
100 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
101 function void start()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
102 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
103 G = 0;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
104 do1();
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
105 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
106 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
107 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
108 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
109 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
110 """
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
111 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
112 self.do(snippet, res)
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
113
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
114
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
115 class TestSamplesOnVexpress(unittest.TestCase, Samples):
375
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
116 def setUp(self):
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
117 if not has_qemu():
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
118 self.skipTest('Not running qemu tests')
19eacf4f7270 Started on memory manager
Windel Bouwman
parents: 374
diff changeset
119
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
120 def do(self, src, expected_output):
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
121 # Construct binary file from snippet:
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
122 o1 = assemble(io.StringIO(startercode), 'arm')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
123 o2 = c3compile([
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
124 relpath('..', 'kernel', 'src', 'io.c3'),
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
125 relpath('..', 'kernel', 'arch', 'vexpressA9.c3'),
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
126 io.StringIO(src)], [], 'arm')
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
127 layout = {'code': 0x10000, 'data': 0x20000}
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
128 o3 = link([o1, o2], layout)
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
129
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
130 sample_filename = 'testsample.bin'
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
131 with open(sample_filename, 'wb') as f:
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
132 f.write(o3.get_section('code').data)
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
133
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
134 # Check bin file exists:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
135 self.assertTrue(os.path.isfile(sample_filename))
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
136
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
137 # Run bin file in emulator:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
138 res = runQemu(sample_filename, machine='vexpress-a9')
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
139 os.remove(sample_filename)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
140 self.assertEqual(expected_output, res)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
141
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
142
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
143 if __name__ == '__main__':
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
144 unittest.main()
377
9667d78ba79e Switched to xml for project description
Windel Bouwman
parents: 375
diff changeset
145