summaryrefslogtreecommitdiff
path: root/mbld/subtest.myr
blob: f631895d05713f44c2d23d728f9248efc1b8f9a3 (plain)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
use std
use bio
use regex

use "types"
use "util"

pkg bld =
	const showsub	: (b : build#, cmd : byte[:], \
		f : std.fd, logfd : std.fd, \
		failed : byte[:][:]# -> std.option(bool))
;;

var planpat
var headpat
var footpat

const __init__ = {
	planpat = std.try(regex.compile("MTEST\\s+(-?\\d+)\\s*"))
	headpat = std.try(regex.compile("test\\s+(.*)<<{!\\s*"))
	footpat = std.try(regex.compile("!}>>\\s*(ok|fail|timing)\\s*(.*)\\s*"))
}

const showsub = {b, cmd, fd, logfd, failed
	var f, log
	var res

	f = bio.mkfile(fd, bio.Rd)
	log = bio.mkfile(logfd, bio.Wr)
	res = `std.None
	match bio.readln(f)
	| `std.Err `bio.Eof:
		-> `std.None
	| `std.Err e:
		std.fatal("error reading subfile: {}\n", e)
	| `std.Ok ln:
		match testplan(ln)
		| `std.None:
			bio.write(log, ln)
			showraw(fd, logfd)
		| `std.Some ntests:
			res = `std.Some showtests(b, cmd, failed, \
				f, log, ntests)
		;;

		std.slfree(ln)
	;;
	bio.close(f)
	bio.close(log)
	-> res
}

const showraw = {f, log
	var buf : byte[:]

	buf = std.slalloc(64*std.KiB)
	while true
		match std.read(f, buf[:])
		| `std.Ok 0:	break
		| `std.Ok n:	std.write(log, buf[:n])
		| `std.Err e:	std.fatal("error writing log: {}\n", e)
		;;
	;;
	std.slfree(buf)
}

const showtests = {b, cmd, failed, f, log, ntests
	var curtest
	var nresults
	var ok

	if ntests == 0
		mbldput("FAIL: missing tests\n")
		-> false
	;;
	ok = true
	curtest = ""
	nresults = 0
	mbldput("\n")
	for ln : bio.byline(f)
		ln = std.strstrip(ln)
		match testhead(ln)
		| `std.None:
		| `std.Some t:	
			starttest(&curtest, t)
			bio.put(log, "RUN {}\n", t)
			continue
		;;

		match testfoot(ln)
		| `std.None:
		| `std.Some `Timing (niter, avg, stddev):
			showbench(b, &curtest, &nresults, niter, avg, stddev)
			continue
		| `std.Some `Pass:
			passtest(b, &curtest, &nresults)
			continue
		| `std.Some `Fail m:
			failtest(b, cmd, &curtest, failed, &nresults, m)
			ok = false
			continue
		;;

		match testplan(ln)
		| `std.None:
		| `std.Some n:
			if curtest.len == 0
				if !checktests(ntests, nresults)
					ok = false
				;;
				ntests = n
				nresults = 0
				continue
			;;
		;;

		bio.put(log, "\t{}\n", ln)
	;;
	if !checktests(ntests, nresults)
		ok = false
	;;
	-> ok
}

const checktests = {ntests, nresults
	if ntests > 0 && ntests != nresults
		mbldput("mismatched test count: expected {}, got {}\n", ntests, nresults)
		-> false
	;;
	-> true
}

const starttest = {curtest, t
	if curtest#.len != 0
		std.fatal("malformed input: test {} nested in {}\n", t, curtest)
	;;
	mbldput("\trun {}:\t", std.strstrip(t))
	curtest# = t
}

const passtest = {b, curtest, nresults
	donetest(b, curtest, nresults)
	mbldput("PASS\n")
}

const showbench = {b, curtest, nresults, niter, avg, stddev
	var scale, unit
	donetest(b, curtest, nresults)
	(scale, unit) = displayscale(avg)
	std.put("BENCH:\t{}{} (σ^2: {})\n", avg*scale, unit, stddev*scale);
}

const units = [
	"s",
	"ms",
	"μs",
	"ns",
]
const displayscale = {val
	var scale

	scale = 1.0
	for var i = 0; i < units.len; i++
		if val*scale > 1.0
			-> (scale, units[i])
		;;
		scale *= 1000.0
	;;
	-> (scale, units[units.len - 1])
}

const failtest = {b, cmd, curtest, failed, nresults, msg
	var p

	p = std.pathcat(cmd, curtest#)
	donetest(b, curtest, nresults)
	mbldput("FAIL {}\n", msg)
	std.slpush(failed, p)
}

const donetest = {b, curtest, nresults
	if curtest#.len == 0
		std.fatal("malformed input: test ended without start\n")
	;;
	std.slfree(curtest#)
	curtest# = ""
	nresults#++
}

const testplan = {ln
	var ntests

	match regex.exec(planpat, ln)
	| `std.None:
		-> `std.None
	| `std.Some m:
		ntests = std.get(std.intparse(m[1]))
		regex.matchfree(m)
		-> `std.Some ntests
	;;
}

const testhead = {ln
	var t

	match regex.exec(headpat, ln)
	| `std.Some m:
		t = std.sldup(m[1])
		regex.matchfree(m)
		-> `std.Some t
	| `std.None:
		-> `std.None
	;;
}

const testfoot = {ln
	match regex.exec(footpat, ln)
	| `std.Some m:
		match m[1]
		| "timing":	-> parsetiming(m[2])
		| "ok":		-> `std.Some `Pass
		| "fail":	-> `std.Some `Fail std.sldup(m[2])
		| junk:		-> `std.Some `Fail std.fmt("garbled : {}", junk)
		;;
	| `std.None:
		-> `std.None
	;;
}

const parsetiming = {tm
	var niter, avg, stddev
	var sp, buf : byte[:][3]

	sp = std.bstrtok(buf[:], tm)
	if sp.len != 3
		-> `std.None
	;;

	match std.intparse(sp[0])
	| `std.Some n:	niter = n
	| `std.None:	-> `std.None
	;;

	match std.flt64parse(sp[1])
	| `std.Some n:	avg = n
	| `std.None:	-> `std.None
	;;

	match std.flt64parse(sp[2])
	| `std.Some n:	stddev = n
	| `std.None:	-> `std.None
	;;

	-> `std.Some `Timing (niter, avg, stddev)
}