Commit | Line | Data |
---|---|---|
7f918cf1 CE |
1 | (* assert.sml |
2 | * 2004 Matthew Fluet (mfluet@acm.org) | |
3 | * Ported to MLton threads. | |
4 | *) | |
5 | ||
6 | structure Assert: ASSERT = | |
7 | struct | |
8 | structure C = Critical | |
9 | val assertFlg = false | |
10 | ||
11 | fun fail msg = | |
12 | (C.atomicBegin (); | |
13 | TextIO.print (concat ["ASSERT: ", msg, "\n"]); | |
14 | OS.Process.exit OS.Process.failure) | |
15 | ||
16 | fun assert (msgs: (unit -> string) list, | |
17 | msg: unit -> string, | |
18 | f: unit -> bool): unit = | |
19 | if assertFlg andalso not (f () handle _ => false) | |
20 | then let | |
21 | val msgs = List.map (fn f => f ()) msgs | |
22 | val msg = concat [String.concatWith " " msgs, " :: ", msg ()] | |
23 | in | |
24 | fail msg | |
25 | end | |
26 | else () | |
27 | fun assert' (msg: string, f: unit -> bool): unit = | |
28 | assert ([], fn () => msg, f) | |
29 | ||
30 | datatype z = datatype MLton.Thread.AtomicState.t | |
31 | fun assertAtomic (msg: unit -> string, n: int option): unit = | |
32 | assert ([C.atomicMsg], msg, fn () => | |
33 | case MLton.Thread.atomicState () of | |
34 | Atomic m => (case n of NONE => true | SOME n => n = m) | |
35 | | NonAtomic => false) | |
36 | fun assertNonAtomic (msg: unit -> string): unit = | |
37 | assert ([C.atomicMsg], msg, fn () => | |
38 | case MLton.Thread.atomicState () of | |
39 | Atomic _ => false | |
40 | | NonAtomic => true) | |
41 | fun assertAtomic' (msg, n) = assertAtomic (fn () => msg, n) | |
42 | fun assertNonAtomic' msg = assertNonAtomic (fn () => msg) | |
43 | end |