summaryrefslogtreecommitdiff
path: root/lib/testr/testr.myr
blob: 4a087f3105691a406078f0e88632884b40c3f85c (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
use std

pkg testr =
	type ctx = struct
		ok	: bool
		reason	: byte[:]
		jmpbuf	: std.jmpbuf#
	;;

	type spec = struct
		name	: byte[:]
		fn	: (ctx : ctx# -> void)
	;;

	const run	: (specs : spec[:] -> void)
	const bench	: (specs : spec[:] -> void)
	const ok	: (ctx : ctx# -> void)
	const fail	: (ctx : ctx#, msg : byte[:], args : ... -> void)
	const check	: (ctx : ctx#, cond : bool, msg : byte[:], args : ... -> void)
	generic eq	: (ctx : ctx#, a : @t, b : @t -> void) :: std.equatable @t
	generic neq	: (ctx : ctx#, a : @t, b : @t -> void) :: std.equatable @t

	const softfail	: (ctx : ctx#, msg : byte[:], args : ... -> void)
;;

const bench = {specs
	var subset

	subset = subtests()
	std.put("MTEST {}\n", specs.len)
	for s : specs
		benchspec(&s, subset)
	;;
	std.htfree(subset)
}

const run = {specs
	var subset

	subset = subtests()
	std.put("MTEST {}\n", specs.len)
	for s : specs
		testspec(&s, subset)
	;;
	std.htfree(subset)
}

const ok = {ctx
	/* nothing to do here */
}

const check = {ctx, cond, msg, args
	var ap
	
	if !cond
		ap = std.vastart(&args)
		failv(ctx, msg, &ap)
	;;
}

generic eq = {ctx, a, b
	var b0 : byte[128]
	var b1 : byte[128]

	if !std.eq(a, b)
		fail(ctx, "{e} != {e}\n",
		     std.bfmt(b0[:], "{}", a),
		     std.bfmt(b1[:], "{}", b))
	;;
}

generic neq = {ctx, a, b
	var b0 : byte[128]
	var b1 : byte[128]

	if std.eq(a, b)
		fail(ctx, "{e} != {e}\n",
		     std.bfmt(b0[:], "{}", a),
		     std.bfmt(b1[:], "{}", b))
	;;
}

const fail = {ctx, msg, args
	var ap
	
	ap = std.vastart(&args)
	failv(ctx, msg, &ap)
}

const failv = {ctx, msg, ap
	softfailv(ctx, msg, ap)
	std.longjmp(ctx.jmpbuf)
}

const softfail = {ctx, msg, args
	var ap
	
	ap = std.vastart(&args)
	softfailv(ctx, msg, &ap)
}

const softfailv = {ctx, msg, ap
	/* keep the first failure */
	if ctx.ok
		ctx.ok = false
		ctx.reason = std.fmtv(msg, ap)
	;;
}

const benchspec = {ts, sub
	var ctx : ctx
	var avg, m, d, n, nsamp
	var start, dt
	var dorun, jmpbuf

	ctx.ok = true
	ctx.reason = ""
	ctx.jmpbuf = &jmpbuf

	avg = 0.0;
	m = 0.0;
	n = 0.0;
	nsamp = 0
	dorun = matchtest(ts.name, sub)
	std.put("test {} <<{{!\n", ts.name)
	if !std.setjmp(&jmpbuf) && dorun
		/* estimate samples */
		start = std.now()
		ts.fn(&ctx)
		dt = (std.now() - start : flt64)

		if dt == 0.0
			nsamp = 1000_000
		else
			nsamp = (100_000.0/dt : int64)
			nsamp = std.max(10, nsamp)
		;;

		for var i = 0; i < nsamp; i++
			n +=1.0;
			start = std.now()
			ts.fn(&ctx)
			dt = (std.now() - start : flt64)/1_000_000.0
			d = (dt - avg);
			avg = avg + d/n;
			m = m + d*(dt - avg);
		;;
	;;

	if !dorun
		std.put("!}}>> skip filtered\n", nsamp, avg, m)
	elif ctx.ok
		std.put("!}}>> timing {} {} {}\n", nsamp, avg, m)
	else
		std.put("!}}>> fail {}\n", ctx.reason)
		std.slfree(ctx.reason)
	;;
}

const testspec = {ts, sub
	var ctx : ctx
	var jmpbuf

	ctx.ok = true
	ctx.reason = ""
	ctx.jmpbuf = &jmpbuf
	if !matchtest(ts.name, sub)
		-> void
	;;

	std.put("test {} <<{{!\n", ts.name)
	if !std.setjmp(&jmpbuf)
		ts.fn(&ctx)
	;;

	if ctx.ok
		std.put("!}}>> ok\n")
	else
		std.put("!}}>> fail {}\n", ctx.reason)
		std.slfree(ctx.reason)
	;;
}

const subtests = {
	var subset

	subset = std.mkht()
	match std.getenv("MTEST_SUBSET")
	| `std.None:	/* no subtests */
	| `std.Some "":	/* no subtests */
	| `std.Some subs:
		for s : std.bysplit(subs, ",")
			std.htput(subset, s, true)
		;;
	;;
	-> subset
}

const matchtest = {name, sub
	-> std.htcount(sub) == 0 || std.hthas(sub, name)
}