Skip to content

Commit 2eda708

Browse files
committed
Add tests to test compiler and emitted byte code.
1 parent de690d1 commit 2eda708

135 files changed

Lines changed: 15849 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/bytecode/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
output

tests/bytecode/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
This directory contains the framework and test files for testing the byte code
2+
output of the Micro Python compiler.
3+
4+
You need to first build the 'cpy' executable in the directory micropython/unix-cpy/.
5+
This executable is a minimal version of Micro Python which compiles a single source
6+
file and outputs the corresponding byte code.
7+
8+
The output of Micro Python is checked against CPython 3.3.
9+
10+
To run the tests use:
11+
12+
./run-tests

tests/bytecode/check.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import sys
2+
name = sys.argv[1].split('/')[-1].split('.')[0]
3+
with open(sys.argv[1]) as f:
4+
lines_correct = [l.strip('\n') for l in f.readlines()]
5+
lines_me = [l.strip('\n') for l in sys.stdin.readlines()]
6+
if len(lines_me) != len(lines_correct):
7+
if len(lines_me) == 0:
8+
print('{:<20}: no output'.format(name))
9+
elif lines_me[0].find('syntax error') >= 0:
10+
print('{:<20}: syntax error'.format(name))
11+
elif lines_me[0].find(' cannot be compiled') >= 0:
12+
print('{:<20}: compile error: {}'.format(name, lines_me[0]))
13+
else:
14+
print('{:<20}: mismatch in number of lines'.format(name))
15+
else:
16+
total = len(lines_me)
17+
same = 0
18+
bad_num_fields = 0
19+
bad_2 = 0
20+
bad_3 = 0
21+
jump_op = ['JUMP_FORWARD', 'JUMP_ABSOLUTE', 'POP_JUMP_IF_FALSE', 'POP_JUMP_IF_TRUE', 'SETUP_LOOP']
22+
jump_abs_op = ['JUMP_FORWARD', 'JUMP_ABSOLUTE']
23+
for i in range(total):
24+
if lines_me[i] == lines_correct[i]:
25+
same += 1
26+
else:
27+
# line is different
28+
line_me = lines_me[i].strip().split(' ', 2)
29+
line_correct = lines_correct[i].strip().split(' ', 2)
30+
allow = False
31+
if len(line_me) != len(line_correct):
32+
bad_num_fields += 1
33+
elif len(line_me) == 2:
34+
if line_me[0] == line_correct[0] == 'stacksize':
35+
allow = True
36+
else:
37+
bad_2 += 1
38+
else:
39+
assert(len(line_me) == 3)
40+
if line_me[0] == line_correct[0] and line_me[1] in jump_abs_op and line_correct[1] in jump_abs_op:
41+
allow = True
42+
elif line_me[0] == line_correct[0] and line_me[1] == line_correct[1] in jump_op:
43+
allow = True
44+
else:
45+
bad_3 += 1
46+
#if not allow:
47+
# print(line_me, 'vs', line_correct)
48+
49+
bad_str = ''
50+
if bad_num_fields > 0:
51+
bad_str += ', {} bad num fields'.format(bad_num_fields)
52+
if bad_2 > 0:
53+
bad_str += ', {} bad 2-field'.format(bad_2)
54+
if bad_3 > 0:
55+
bad_str += ', {} bad 3-field'.format(bad_3)
56+
print('{:<20}: {:>6} lines, {:>5.1f}% correct{}'.format(name, total, 100 * same / total, bad_str))

tests/bytecode/mp-tests/assert1.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
assert x
2+
assert x, 'test'

tests/bytecode/mp-tests/assign1.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[] = ()
2+
[] = []
3+
a = b
4+
(a) = b
5+
a, b = c, d
6+
a, b, c = d, e, f
7+
a, b, c, d = e, f, g, h
8+
#(a, b) = c, d
9+
#a, b = (c, d)
10+
#(a, b) = (c, d)

tests/bytecode/mp-tests/assign2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
*a, = b
2+
a, *b = c
3+
a, *b, = c
4+
a, *b, c = d
5+
6+
[*a] = b
7+
[*a,] = b
8+
[a, *b] = c
9+
#[a, *b,] = c
10+
#[a, *b, c] = d
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[] = ()
2+
x += 1
3+
x.y += 1
4+
x.f().y += 1
5+
x[1] += 2

tests/bytecode/mp-tests/call1.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
f(a, b=c)

tests/bytecode/mp-tests/class1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class C:
2+
pass
3+
C()

tests/bytecode/mp-tests/class2.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class A:
2+
x = 1
3+
y = x + z
4+
A()

0 commit comments

Comments
 (0)