Mercurial > lcfOS
comparison 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 |
comparison
equal
deleted
inserted
replaced
365:98ff43cfdd36 | 366:39bf68bf1891 |
---|---|
1 import unittest | 1 import unittest |
2 import os | |
3 import io | |
2 from testemulation import runQemu | 4 from testemulation import runQemu |
3 from ppci.recipe import RecipeLoader | 5 from testzcc import relpath |
4 from ppci.tasks import TaskRunner | 6 from ppci.tasks import TaskRunner |
7 from ppci.buildtasks import Assemble, Compile, Link | |
8 from ppci.objectfile import ObjectFile | |
5 | 9 |
10 startercode = """ | |
11 mov sp, 0x30000 ; setup stack pointer | |
12 BL sample_start ; Branch to sample start | |
13 local_loop: | |
14 B local_loop | |
15 """ | |
6 | 16 |
7 class Samples: | 17 class Samples: |
8 def testPrint(self): | 18 def testPrint(self): |
9 snippet = """ | 19 snippet = """ |
10 module testsample; | 20 module sample; |
11 import io; | 21 import io; |
12 function void start() | 22 function void start() |
13 { | 23 { |
14 io.print("Hello world"); | 24 io.print("Hello world"); |
15 } | 25 } |
16 """ | 26 """ |
17 self.do(snippet, "Hello world") | 27 self.do(snippet, "Hello world") |
18 | 28 |
19 def testForLoopPrint(self): | 29 def testForLoopPrint(self): |
20 snippet = """ | 30 snippet = """ |
21 module testsample; | 31 module sample; |
22 import io; | 32 import io; |
23 function void start() | 33 function void start() |
24 { | 34 { |
25 var int i; | 35 var int i; |
26 for (i=0; i<10; i++) | 36 for (i=0; i<10; i = i + 1) |
27 { | 37 { |
28 io.print2("A = ", i); | 38 io.print2("A = ", i); |
29 } | 39 } |
30 } | 40 } |
31 """ | 41 """ |
32 res = "".join("A = 0x{0:08X}\n".format(a) for a in range(10)) | 42 res = "".join("A = 0x{0:08X}\n".format(a) for a in range(10)) |
33 self.do(snippet, res) | 43 self.do(snippet, res) |
34 | 44 |
35 def testForLoopPrint(self): | 45 def testGlobalVariable(self): |
36 snippet = """ | 46 snippet = """ |
37 module testglobal; | 47 module sample; |
38 import io; | 48 import io; |
39 var int G; | 49 var int G; |
40 function void do1() | 50 function void do1() |
41 { | 51 { |
42 G = G + 1; | 52 G = G + 1; |
50 function void start() | 60 function void start() |
51 { | 61 { |
52 G = 0; | 62 G = 0; |
53 do1(); | 63 do1(); |
54 do1(); | 64 do1(); |
55 do2(); | 65 do5(); |
56 do1(); | 66 do1(); |
57 do2(); | 67 do5(); |
58 } | 68 } |
59 """ | 69 """ |
60 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13]) | 70 res = "".join("G=0x{0:08X}\n".format(a) for a in [1,2,7,8,13]) |
61 self.do(snippet, res) | 71 self.do(snippet, res) |
62 | 72 |
63 | 73 |
64 class TestSamplesOnVexpress(unittest.TestCase, Samples): | 74 class TestSamplesOnVexpress(unittest.TestCase, Samples): |
65 def do(self, src, expected_output): | 75 def do(self, src, expected_output): |
76 # Construct binary file from snippet: | |
77 o1 = ObjectFile() | |
78 o2 = ObjectFile() | |
79 asmb = Assemble(io.StringIO(startercode), 'arm', o1) | |
80 comp = Compile([ | |
81 relpath('..', 'kernel', 'src', 'io.c3'), | |
82 relpath('..', 'kernel', 'arch', 'vexpressA9.c3'), | |
83 io.StringIO(src)], [], 'arm', o2) | |
84 sample_filename = 'testsample.bin' | |
85 layout = {'code': 0x10000, 'data':0x20000} | |
86 link = Link([o1, o2], layout, sample_filename) | |
87 | |
88 # Create task executor: | |
66 runner = TaskRunner() | 89 runner = TaskRunner() |
67 recipe_loader = RecipeLoader(runner) | 90 runner.add_task(asmb) |
68 # print(expected_output) | 91 runner.add_task(comp) |
69 return | 92 runner.add_task(link) |
70 # TODO: improve recipe loading?? | |
71 recipe_loader.load_dict({ | |
72 'link': { | |
73 'inputs': [ | |
74 ], | |
75 'layout': { | |
76 'code': 0x60010000, | |
77 'data': 0x60020000 | |
78 }, | |
79 'output': 'tst.bin' | |
80 } | |
81 }) | |
82 runner.add_task(Compile()) | |
83 runner.run_tasks() | 93 runner.run_tasks() |
84 res = runQemu('tst.bin', machine='vexpress-a9') | 94 # Check bin file exists: |
95 self.assertTrue(os.path.isfile(sample_filename)) | |
96 | |
97 # Run bin file in emulator: | |
98 res = runQemu(sample_filename, machine='vexpress-a9') | |
99 os.remove(sample_filename) | |
85 self.assertEqual(expected_output, res) | 100 self.assertEqual(expected_output, res) |
86 | 101 |
87 | 102 |
88 if __name__ == '__main__': | 103 if __name__ == '__main__': |
89 unittest.main() | 104 unittest.main() |