annotate test/testsamples.py @ 366:39bf68bf1891

Fix sample tests and deterministic build
author Windel Bouwman
date Fri, 21 Mar 2014 09:43:01 +0100
parents c49459768aaa
children 68841f9ab96c
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
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
4 from testemulation import runQemu
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
5 from testzcc import relpath
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
6 from ppci.tasks import TaskRunner
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
7 from ppci.buildtasks import Assemble, Compile, Link
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
8 from ppci.objectfile import ObjectFile
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
9
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
10 startercode = """
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
11 mov sp, 0x30000 ; setup stack pointer
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
12 BL sample_start ; Branch to sample start
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
13 local_loop:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
14 B local_loop
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
15 """
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
16
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
17 class Samples:
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
18 def testPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
19 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
20 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
21 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
22 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
23 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
24 io.print("Hello world");
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
25 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
26 """
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
27 self.do(snippet, "Hello world")
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
28
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
29 def testForLoopPrint(self):
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
30 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
31 module sample;
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
32 import io;
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
33 function void start()
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
34 {
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
35 var int i;
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
36 for (i=0; i<10; i = i + 1)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
37 {
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
38 io.print2("A = ", i);
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
39 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
40 }
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
41 """
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
42 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
43 self.do(snippet, res)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
44
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
45 def testGlobalVariable(self):
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
46 snippet = """
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
47 module sample;
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
48 import io;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
49 var int G;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
50 function void do1()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
51 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
52 G = G + 1;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
53 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
54 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
55 function void do5()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
56 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
57 G = G + 5;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
58 io.print2("G=", G);
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
59 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
60 function void start()
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
61 {
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
62 G = 0;
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
63 do1();
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
64 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
65 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
66 do1();
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
67 do5();
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
68 }
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
69 """
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
70 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
71 self.do(snippet, res)
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
72
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
73
358
5ef1cb1bb54f Fix nosetests
Windel Bouwman
parents: 355
diff changeset
74 class TestSamplesOnVexpress(unittest.TestCase, Samples):
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
75 def do(self, src, expected_output):
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
76 # Construct binary file from snippet:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
77 o1 = ObjectFile()
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
78 o2 = ObjectFile()
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
79 asmb = Assemble(io.StringIO(startercode), 'arm', o1)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
80 comp = Compile([
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
81 relpath('..', 'kernel', 'src', 'io.c3'),
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
82 relpath('..', 'kernel', 'arch', 'vexpressA9.c3'),
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
83 io.StringIO(src)], [], 'arm', o2)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
84 sample_filename = 'testsample.bin'
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
85 layout = {'code': 0x10000, 'data':0x20000}
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
86 link = Link([o1, o2], layout, sample_filename)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
87
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
88 # Create task executor:
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
89 runner = TaskRunner()
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
90 runner.add_task(asmb)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
91 runner.add_task(comp)
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
92 runner.add_task(link)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
93 runner.run_tasks()
366
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
94 # Check bin file exists:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
95 self.assertTrue(os.path.isfile(sample_filename))
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
96
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
97 # Run bin file in emulator:
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
98 res = runQemu(sample_filename, machine='vexpress-a9')
39bf68bf1891 Fix sample tests and deterministic build
Windel Bouwman
parents: 364
diff changeset
99 os.remove(sample_filename)
355
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
100 self.assertEqual(expected_output, res)
c2ddc8a36f5e Enabled optimization
Windel Bouwman
parents:
diff changeset
101
364
c49459768aaa Work on globals
Windel Bouwman
parents: 363
diff changeset
102
362
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
103 if __name__ == '__main__':
c05ab629976a Added CPUID for arm
Windel Bouwman
parents: 358
diff changeset
104 unittest.main()