Debian Codegraph @

regression/size.sml

raw
1 fun 'a printSize (name: string, min_max: (IntInf.int * IntInf.int) option,
2 value: 'a, use: 'a -> unit): unit =
3 (print "The size of "
4 ; print name
5 ; print " is"
6 ; let
7 val size = MLton.size value
8 in
9 case min_max of
10 NONE => (print " = "
11 ; print (IntInf.toString size)
12 ; print " bytes.\n")
13 | SOME (min, max) =>
14 if min <= size andalso size <= max
15 then (print " >= "
16 ; print (IntInf.toString min)
17 ; print " bytes and <= "
18 ; print (IntInf.toString max)
19 ; print " bytes.\n")
20 else (print " = "
21 ; print (IntInf.toString size)
22 ; print " bytes.\n")
23 end
24 ; use value)
25
26 fun chk (x, y) =
27 if x = y
28 then ()
29 else raise Fail "bug"
30
31 val l = [1, 2, 3, 4]
32
33 val _ =
34 (printSize ("a char", NONE, #"c", fn _ => ())
35 ; printSize ("an int list of length 4", NONE,
36 List.tabulate (4, fn i => i + 1), fn l =>
37 chk (foldl (op +) 0 l, 10))
38 ; printSize ("a string of length 10", NONE,
39 CharVector.tabulate (10, fn i => chr (ord #"0" + i)), fn s =>
40 chk (CharVector.foldl (fn (c,s) => ord c + s) 0 s, 525))
41 ; printSize ("an int array of length 10", NONE,
42 Array.tabulate (10, fn i => i), fn a =>
43 chk (Array.foldl (op +) 0 a, 45))
44 ; printSize ("a double array of length 10", NONE,
45 Array.tabulate (10, real), fn a =>
46 chk (Real.floor (Array.foldl (op +) 0.0 a), 45))
47 ; printSize ("an array of length 10 of 2-ples of ints", NONE,
48 Array.tabulate (10, fn i => (i, i + 1)), fn a =>
49 chk (Array.foldl (fn ((a,b),s) => a + b + s) 0 a, 100))
50 ; printSize ("a useless function", NONE,
51 fn _ => 13, fn f => ())
52 ; let
53 val l = List.tabulate (8, fn i =>
54 if i mod 2 = 1
55 then NONE
56 else SOME (List.tabulate (i, fn i => i + 1)))
57 in
58 ()
59 ; printSize ("an int list option (SOME)", NONE,
60 List.nth (l, 4), fn lo =>
61 chk (case lo of NONE => 0 | SOME l => foldl (op +) 0 l, 10))
62 ; printSize ("an int list option (NONE)", NONE,
63 List.nth (l, 5), fn lo =>
64 chk (case lo of NONE => 0 | SOME l => foldl (op +) 0 l, 0))
65 end)
66
67 local
68 open MLton.Cont
69 in
70 val rc: int option t option ref = ref NONE
71 val _ =
72 case callcc (fn k: int option t => (rc := SOME k; throw (k, NONE))) of
73 NONE => ()
74 | SOME i => print (concat [Int.toString i, "\n"])
75 end
76
77 val _ = printSize ("a continuation option ref",
78 case !rc of
79 NONE => NONE
80 | SOME _ => SOME (4000, 6000),
81 rc, fn rc =>
82 case !rc of
83 NONE => ()
84 | SOME k => (rc := NONE; MLton.Cont.throw (k, SOME 13)))