-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.pir
169 lines (146 loc) · 3.36 KB
/
test.pir
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# this is the test program for the forth implementation targeting parrot.
# this script can be passed the names of any number of test files. each test is
# a series of input/output pairs, with optional comments that start with #s.
#
# the first non-blank, non-comment line is considered the first input. the line
# immediately following that is the first output line. the output can be either
# the stack (where the elements are joined by a space) or the message of a
# thrown exception.
.loadlib 'io_ops'
.sub 'main' :main
.param pmc args
$S0 = shift args
load_language 'forth'
.local pmc it
it = iter args
next_file:
unless it goto done
$S0 = shift it
test($S0)
goto next_file
done:
end
.end
#
# test(filename)
#
# Test a particular filename: read it, parse it, compare the input/output.
#
.sub 'test'
.param string filename
.local pmc file
file = new 'FileHandle'
file.'open'(filename)
.local string input, expected
.local int num_of_tests
num_of_tests = 0
next_test:
input = next_line(file)
if null input goto done
if input == "" goto next_test
expected = next_line(file)
if null expected goto missing_output
inc num_of_tests
is(input, expected, num_of_tests)
goto next_test
done:
print "1.."
print num_of_tests
print "\n"
file.'close'()
.return()
missing_output:
print "Missing test output for test #"
inc num_of_tests
print num_of_tests
print "\n"
exit 1
.end
.sub 'next_line' :anon
.param pmc file
.local string line
next_line:
line = file.'readline'()
if line == '' goto end_of_file
$S0 = substr line, 0, 1
if $S0 == "\n" goto next_line
if $S0 == "\r" goto next_line
if $S0 == "#" goto next_line
line = chomp(line)
.return (line)
end_of_file:
null line
.return (line)
.end
.sub 'chomp' :anon
.param string str
$I0 = index str, "\r"
if $I0 < 0 goto L1
str = substr str, 0, $I0
L1:
$I1 = index str, "\n"
if $I1 < 0 goto L2
str = substr str, 0, $I1
L2:
.return (str)
.end
#
# is(forth code, expected output, test number)
#
# An individual test. Execute the forth code and compare one of the following:
# 1) the first line of stdout
# 2) the stack
# 3) the exception message
#
.sub 'is'
.param string input
.param string expected
.param int test_num
.local pmc forth
forth = compreg 'forth'
.local pmc interp, stdout
stdout = getstdout
.local pmc fh
fh = new 'StringHandle'
fh.'open'('dummy', 'wr')
setstdout fh
push_eh exception
$P0 = forth(input)
.local pmc stack
stack = $P0()
pop_eh
setstdout stdout
.local string output
output = fh.'readline'()
if output != "" goto compare
output = join " ", stack
goto compare
exception:
.local pmc except
.get_results (except)
setstdout stdout
output = except
compare:
if output == expected goto ok
print "not ok "
print test_num
print "\n"
print "# Failed test\n"
print "# got: '"
print output
print "'\n"
print "# expected: '"
print expected
print "'\n"
.return()
ok:
print "ok "
print test_num
print "\n"
.return()
.end
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: