-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtests0.ml
138 lines (108 loc) · 3.16 KB
/
tests0.ml
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
(* Testing the serialization of captured delimited continuations *)
(* If the code is given an argument, we load the previously serialized
continuation from the file
*)
open Delimcc
open Printf
let () = prerr_endline "\nInitializing tests0\n"
let save_state_file fname k =
let oc = open_out fname in
let () = output_delim_value oc k in
close_out oc
;;
let load_state_file fname =
let ic = open_in fname in
let v = Marshal.from_channel ic in
let () = close_in ic in
v
;;
(* establish global data *)
type 'a k = Done of 'a | K of Obj.t;;
let p0 : (int k) prompt = new_prompt ();;
let kempty = Obj.repr
(push_prompt p0 (fun () -> take_subcont p0 (fun sk () -> Obj.magic sk)));;
(* let () = show_val 5 kempty *)
(* Define the point for relativitization*)
let () = init_global_closure kempty
(*
let kempty1 = Obj.repr
(push_prompt p0 (fun () -> shift p0 (fun f -> Obj.magic f)));;
let krel = relativitize kempty1 true;;
*)
(* End of global data *)
(* Old test
let () =
let n = 1 in
let m = 2 in
let fn = ref (fun (x,y) u ->
prerr_int (m+n+u); prerr_float x; prerr_endline y) in
let f2 x = prerr_endline "f2"; x in
let f3 () = prerr_endline "f3"; n in
let () = Gc.minor () in
let fv = ((fun x -> fun y -> !fn (x,y)), f2, f3) in
let fvr = Obj.repr fv in
let () = prerr_endline "simple_test" in
let () = show_val 5 fvr in
let fv' = relativitize fvr true in
let () = show_val 5 fv' in
let () = show_val 5 (Obj.repr fv) in
let (fvn,f2n,f3n) = (Obj.obj fv') in
let () = fvn (7.0) "OK" 10; f2n (); f3n () in
let (fvn,f2n,f3n) = fv in
fvn (7.0) "OK" 10; f2n (); f3n (); ()
(* ; exit 0 *)
*)
(*
let krel = relativitize kempty true;;
let () = show_val 5 krel;;
let kempty =
let p = new_prompt () in
push_prompt p (fun () ->
take_subcont p (fun sk () -> Obj.repr sk)
);;
let kempty =
push_prompt p0 (fun () -> shift p0 (fun f -> Obj.repr f));;
let krel = relativitize kempty false;;
let () = save_state_file "/tmp/k0" krel;
print_endline "saved"
*)
let () = print_endline "shift test\n"
let appk1 k = let Done v = ((Obj.obj k):(int->int k)) 5
in v;;
let create_and_save () =
let k1 = let K v = push_prompt p0 (fun () ->
Done (shift p0 (fun (f: int -> int k) -> K (Obj.repr f))
+ 10))
in v
in
let () = show_val 5 k1 in
(*
let k1r = relativitize k1 false in
let () = show_val 5 k1r in
let () = print_endline "trying an application";
printf "the result is %d\n\n" (appk1 k1r) in
*)
let () = save_state_file "/tmp/k1" k1;
print_endline "saved" in
let () = print_endline "trying an application after saving";
printf "the result is %d\n\n" (appk1 k1) in
()
(*
let krest =
Obj.obj (absolutize (Obj.repr k1r))
;;
let () = show_val 5 krest;;
*)
let (need_saving,fname) = match Sys.argv with
| [|_|] -> (true,"/tmp/k1")
| [|_;fname|] -> printf "Loading from %s\n" fname; (false, fname)
let () = if need_saving then create_and_save () else ()
let () = print_endline "loading back\n"
let krest =
let v = load_state_file "/tmp/k1" in
let () = show_val 5 v in
Obj.obj (absolutize (Obj.repr v))
;;
let () = print_endline "trying an application";
printf "the result is %d\n\n" (appk1 krest)
;;