2
open Typerep_experimental.Std
6
(* Tests to make sure we can deserialize values we serialized *)
7
(* Tests to make sure we can deserialize values serialized by with sexp *)
8
(* Tests to make sure with sexp can deserialize values serialized by us *)
11
let t_of_sexp_of_sexp_of_t typerep value =
12
let `generic t_of_sexp = S.t_of_sexp typerep in
13
let `generic sexp_of_t = S.sexp_of_t typerep in
14
t_of_sexp (sexp_of_t value)
16
let check_untyped value typerep =
17
let `generic t_of_sexp = S.t_of_sexp typerep in
18
let `generic sexp_of_t = S.sexp_of_t typerep in
19
let `generic sexp_of_un =
20
Sexprep.Tagged.Sexp_of.of_typestruct (Type_struct.of_typerep typerep)
22
let `generic un_of_sexp =
23
Sexprep.Tagged.Of_sexp.of_typestruct (Type_struct.of_typerep typerep)
25
let new_value = t_of_sexp (sexp_of_un (un_of_sexp (sexp_of_t value))) in
26
Polymorphic_compare.equal new_value value
28
let check_typerep value typerep =
29
let str = t_of_sexp_of_sexp_of_t typerep value in
30
Polymorphic_compare.equal value str
32
let check_obj_typerep (type a) (value:a) (typerep:a Typerep.t) =
33
let `generic sexp_of_t = S.sexp_of_t typerep in
34
let sexp = sexp_of_t value in
35
let typestruct = Type_struct.of_typerep typerep in
36
let typerep_of_t = Type_struct.recreate_dynamically_typerep_for_test typerep in
37
let objstruct = Type_struct.of_typerep typerep_of_t in
38
let fail = ref false in
40
let `generic t_of_sexp = S.t_of_sexp typerep_of_t in
41
let obj_value = t_of_sexp sexp in
42
if not (Polymorphic_compare.equal obj_value value) then begin
44
Printf.printf "typestruct: %s\n"
45
(Sexp.to_string_hum (Type_struct.sexp_of_t typestruct));
46
Printf.printf "objstruct: %s\n"
47
(Sexp.to_string_hum (Type_struct.sexp_of_t objstruct));
48
Printf.printf "polymorphic equality failed obj_value: %S\n%!"
49
(Sexp.to_string_hum sexp)
53
let `generic sexp_of_t = S.sexp_of_t typerep_of_t in
54
let obj_sexp = sexp_of_t value in
55
let obj_str = Sexp.to_string_hum obj_sexp in
56
let value_str = Sexp.to_string_hum sexp in
57
if not (String.equal obj_str value_str) then begin
59
Printf.printf "typestruct: %s\n"
60
(Sexp.to_string_hum (Type_struct.sexp_of_t typestruct));
61
Printf.printf "objstruct: %s\n"
62
(Sexp.to_string_hum (Type_struct.sexp_of_t objstruct));
63
Printf.printf "sexp equality failed obj_value:\nvalue sexp:\n%s\nobj sexp\n%s\n%!"
70
let check value typerep =
71
if (check_typerep value typerep)
72
&& (check_untyped value typerep)
73
&& (check_obj_typerep value typerep)
76
let `generic sexp_of_t = S.sexp_of_t typerep in
77
let `generic t_of_sexp = S.t_of_sexp typerep in
78
let sexp = sexp_of_t value in
79
let second_sexp = sexp_of_t (t_of_sexp sexp) in
80
print_endline "sexp:";
81
print_endline (Sexp.to_string_hum sexp);
82
print_endline "sexp of (t of (sexp of t)):";
83
print_endline (Sexp.to_string_hum second_sexp);
87
let check_of_sexp value typerep sexp_of_t =
88
let `generic t_of_sexp = S.t_of_sexp typerep in
89
Polymorphic_compare.equal value (t_of_sexp (sexp_of_t value))
91
let check_of_t value typerep t_of_sexp =
92
let `generic sexp_of_t = S.sexp_of_t typerep in
93
Polymorphic_compare.equal value (t_of_sexp (sexp_of_t value))
97
type t = int with typerep, sexp
100
assert(check value M.typerep_of_t);
101
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
102
assert(check_of_t value M.typerep_of_t M.t_of_sexp)
105
let module M = struct
106
type t = int32 with typerep, sexp
108
let value = Int32.of_int_exn 5 in
109
assert(check value M.typerep_of_t);
110
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
111
assert(check_of_t value M.typerep_of_t M.t_of_sexp)
114
let module M = struct
115
type t = int64 with typerep, sexp
117
let value = Int64.of_int_exn 5 in
118
assert(check value M.typerep_of_t);
119
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
120
assert(check_of_t value M.typerep_of_t M.t_of_sexp)
123
let module M = struct
124
type t = char with typerep, sexp
127
assert(check value M.typerep_of_t);
128
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
129
assert(check_of_t value M.typerep_of_t M.t_of_sexp)
132
let module M = struct
133
type t = float with typerep, sexp
135
let value = 543.02 in
136
assert(check value M.typerep_of_t);
137
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
138
assert(check_of_t value M.typerep_of_t M.t_of_sexp)
141
let module M = struct
142
type t = string with typerep, sexp
144
let value = "Hello, world!" in
145
assert(check value M.typerep_of_t);
146
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
147
assert(check_of_t value M.typerep_of_t M.t_of_sexp)
150
let module M = struct
151
type t = bool with typerep, sexp
153
assert(check true M.typerep_of_t);
154
assert(check false M.typerep_of_t);
155
assert(check_of_sexp true M.typerep_of_t M.sexp_of_t);
156
assert(check_of_sexp false M.typerep_of_t M.sexp_of_t);
157
assert(check_of_t true M.typerep_of_t M.t_of_sexp);
158
assert(check_of_t false M.typerep_of_t M.t_of_sexp)
161
let module M = struct
162
type t = unit with typerep, sexp
164
assert(check () M.typerep_of_t);
165
assert(check_of_sexp () M.typerep_of_t M.sexp_of_t);
166
assert(check_of_t () M.typerep_of_t M.t_of_sexp)
169
let module M = struct
170
type 'a t = 'a option with typerep, sexp
172
assert(check None (M.typerep_of_t typerep_of_int));
173
assert(check (Some 5) (M.typerep_of_t typerep_of_int));
174
assert(check_of_sexp None
175
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
176
assert(check_of_sexp (Some 5)
177
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
178
assert(check_of_t None
179
(M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp));
180
assert(check_of_t (Some 5)
181
(M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp))
184
let module M = struct
185
type 'a t = 'a list with typerep, sexp
187
assert(check [] (M.typerep_of_t typerep_of_int));
188
assert(check [1;2;6;5;4;3] (M.typerep_of_t typerep_of_int));
189
assert(check_of_sexp [] (M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
190
assert(check_of_sexp [1;2;6;5;4;3]
191
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
192
assert(check_of_t [] (M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp));
193
assert(check_of_t [1;2;6;5;4;3]
194
(M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp))
197
let module M = struct
198
type 'a t = 'a array with typerep, sexp
200
assert(check [||] (M.typerep_of_t typerep_of_int));
201
assert(check [|1;2;6;5;4;3|] (M.typerep_of_t typerep_of_int));
202
assert(check_of_sexp [||]
203
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
204
assert(check_of_sexp [|1;2;6;5;4;3|]
205
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
206
assert(check_of_t [||] (M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp));
207
assert(check_of_t [|1;2;6;5;4;3|]
208
(M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp))
211
let module M = struct
212
type 'a t = 'a ref with typerep, sexp
214
assert(check (ref 6) (M.typerep_of_t typerep_of_int));
215
assert(check_of_sexp (ref 6)
216
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
217
assert(check_of_t (ref 6) (M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp))
220
let module M = struct
221
type 'a t = 'a lazy_t with typerep, sexp
223
let value = lazy 42 in
224
let typerep = M.typerep_of_t typerep_of_int in
225
let sexp_of_t = M.sexp_of_t sexp_of_int in
226
let t_of_sexp = M.t_of_sexp int_of_sexp in
227
let `generic sexp_of_x = S.sexp_of_t typerep in
228
let `generic x_of_sexp = S.t_of_sexp typerep in
229
assert (Int.equal (Lazy.force value) (Lazy.force (x_of_sexp (sexp_of_x value))));
230
assert (Int.equal (Lazy.force value) (Lazy.force (x_of_sexp (sexp_of_t value))));
231
assert (Int.equal (Lazy.force value) (Lazy.force (t_of_sexp (sexp_of_x value))));
236
let module M = struct
237
type 'a t = {foo:'a; bar:float} with typerep, sexp
239
assert(check {M.foo=5;bar=43.25} (M.typerep_of_t typerep_of_int));
240
assert(check_of_sexp {M.foo=5;bar=43.25}
241
(M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
242
assert( check_of_t {M.foo=5;bar=43.25}
243
(M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp))
247
let module M = struct
248
type ('a, 'b) t = ('a * 'b) with typerep, sexp
250
assert(check (5,45.67)
251
(M.typerep_of_t typerep_of_int typerep_of_float));
252
assert(check_of_sexp (5,45.67)
253
(M.typerep_of_t typerep_of_int typerep_of_float)
254
(M.sexp_of_t sexp_of_int sexp_of_float));
255
assert(check_of_t (5,45.67)
256
(M.typerep_of_t typerep_of_int typerep_of_float)
257
(M.t_of_sexp int_of_sexp float_of_sexp))
260
let module M = struct
261
type ('a, 'b, 'c) t = ('a * 'b * 'c) with typerep, sexp
263
assert(check (5,45,3.14159)
264
(M.typerep_of_t typerep_of_int typerep_of_int typerep_of_float));
265
assert(check_of_sexp (5,45,3.14159)
266
(M.typerep_of_t typerep_of_int typerep_of_int typerep_of_float)
267
(M.sexp_of_t sexp_of_int sexp_of_int sexp_of_float));
268
assert(check_of_t (5,45,3.14159)
269
(M.typerep_of_t typerep_of_int typerep_of_int typerep_of_float)
270
(M.t_of_sexp int_of_sexp int_of_sexp float_of_sexp))
274
let module M = struct
275
type ('a, 'b, 'c, 'd) t = ('a * 'b * 'c * 'd) with typerep, sexp
277
assert(check (5,45,3.14159,1.14159)
278
(M.typerep_of_t typerep_of_int typerep_of_int typerep_of_float typerep_of_float));
279
assert(check_of_sexp (5,45,3.14159,1.14159)
280
(M.typerep_of_t typerep_of_int typerep_of_int typerep_of_float typerep_of_float)
281
(M.sexp_of_t sexp_of_int sexp_of_int sexp_of_float sexp_of_float));
282
assert(check_of_t (5,45,3.14159,1.14159)
283
(M.typerep_of_t typerep_of_int typerep_of_int typerep_of_float typerep_of_float)
284
(M.t_of_sexp int_of_sexp int_of_sexp float_of_sexp float_of_sexp))
288
let module M = struct
289
type ('a, 'b, 'c, 'd, 'e) t = ('a * 'b * 'c * 'd * 'e) with typerep, sexp
291
assert(check (5,45,3.14159,1.14159,"hi")
292
(M.typerep_of_t typerep_of_int typerep_of_int
293
typerep_of_float typerep_of_float
295
assert(check_of_sexp (5,45,3.14159,1.14159,"hi")
296
(M.typerep_of_t typerep_of_int typerep_of_int
297
typerep_of_float typerep_of_float
299
(M.sexp_of_t sexp_of_int sexp_of_int
300
sexp_of_float sexp_of_float
302
assert(check_of_t (5,45,3.14159,1.14159,"hi")
303
(M.typerep_of_t typerep_of_int typerep_of_int
304
typerep_of_float typerep_of_float
306
(M.t_of_sexp int_of_sexp int_of_sexp
307
float_of_sexp float_of_sexp
312
let module M = struct
320
| Bab of ('a * 'a) with typerep, sexp
322
(* sexprep serialize and deserialize *)
323
assert(check M.Foo (M.typerep_of_t typerep_of_int));
324
assert(check (M.Bar 651) (M.typerep_of_t typerep_of_int));
325
assert(check (M.Bar "651") (M.typerep_of_t typerep_of_string));
326
assert(check M.Bee (M.typerep_of_t typerep_of_bool));
327
assert(check (M.Baz (651,54)) (M.typerep_of_t typerep_of_int));
328
assert(check (M.Bax (651,54)) (M.typerep_of_t typerep_of_int));
329
assert(check (M.Baa (651,54)) (M.typerep_of_t typerep_of_int));
330
assert(check (M.Bab (651,54)) (M.typerep_of_t typerep_of_int));
331
(* sexplib serialize; sexprep deserialize *)
332
assert(check_of_sexp M.Foo (M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
333
assert(check_of_sexp (M.Bar 651) (M.typerep_of_t typerep_of_int) (M.sexp_of_t sexp_of_int));
334
assert(check_of_sexp (M.Baz (651,54))
335
(M.typerep_of_t typerep_of_int)
336
(M.sexp_of_t sexp_of_int));
337
assert(check_of_sexp (M.Bax (651,54))
338
(M.typerep_of_t typerep_of_int)
339
(M.sexp_of_t sexp_of_int));
340
assert(check_of_sexp (M.Baa (651,54))
341
(M.typerep_of_t typerep_of_int)
342
(M.sexp_of_t sexp_of_int));
343
assert(check_of_sexp (M.Bab (651,54))
344
(M.typerep_of_t typerep_of_int)
345
(M.sexp_of_t sexp_of_int));
346
(* sexprep serialize; sexplib deserialize *)
347
assert(check_of_t M.Foo (M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp));
348
assert(check_of_t (M.Bar 651) (M.typerep_of_t typerep_of_int) (M.t_of_sexp int_of_sexp));
349
assert(check_of_t (M.Baz (651,54))
350
(M.typerep_of_t typerep_of_int)
351
(M.t_of_sexp int_of_sexp));
352
assert(check_of_t (M.Bax (651,54))
353
(M.typerep_of_t typerep_of_int)
354
(M.t_of_sexp int_of_sexp));
355
assert(check_of_t (M.Baa (651,54))
356
(M.typerep_of_t typerep_of_int)
357
(M.t_of_sexp int_of_sexp));
358
assert(check_of_t (M.Bab (651,54))
359
(M.typerep_of_t typerep_of_int)
360
(M.t_of_sexp int_of_sexp))
363
let module M = struct
371
(* sexprep serialize and deserialize *)
372
assert(check M.Foo M.typerep_of_t);
373
assert(check (M.Bar 651) M.typerep_of_t);
374
assert(check (M.Baz (651,54)) M.typerep_of_t);
375
assert(check (M.Bax (651,54)) M.typerep_of_t);
376
(* sexplib serialize; sexprep deserialize *)
377
assert(check_of_sexp M.Foo M.typerep_of_t M.sexp_of_t);
378
assert(check_of_sexp (M.Bar 651) M.typerep_of_t M.sexp_of_t);
379
assert(check_of_sexp (M.Baz (651,54)) M.typerep_of_t M.sexp_of_t);
380
assert(check_of_sexp (M.Bax (651,54)) M.typerep_of_t M.sexp_of_t);
381
(* sexprep serialize; sexplib deserialize *)
382
assert(check_of_t M.Foo M.typerep_of_t M.t_of_sexp);
383
assert(check_of_t (M.Bar 651) M.typerep_of_t M.t_of_sexp);
384
assert(check_of_t (M.Baz (651,54)) M.typerep_of_t M.t_of_sexp);
385
assert(check_of_t (M.Bax (651,54)) M.typerep_of_t M.t_of_sexp)
388
let module M = struct
389
type t = Leaf | Node of t * t with typerep,sexp
393
then M.Node (producer (n-1), producer (n-1))
396
let value = producer 15 in
397
assert(check value M.typerep_of_t);
398
assert(check_of_sexp value M.typerep_of_t M.sexp_of_t);
399
assert(check_of_t value M.typerep_of_t M.t_of_sexp);
402
let module M = struct
403
type 'a t = [ `Foo | `Bar of 'a ] with typerep
405
let typerep = M.typerep_of_t typerep_of_unit in
406
assert(check `Foo typerep) ;
407
assert(check (`Bar ()) typerep)
409
module Rev_option : sig
410
type 'a t with typerep
411
val of_option : 'a option -> 'a t
412
val register : unit -> unit
415
type 'a t = 'a option with typerep(abstract)
421
let t_of_sexp a_of_sexp sexp =
423
| Sexp.Atom ("enon" | "enoN") -> None
425
| Sexp.List [el ; Sexp.Atom ("emos" | "emoS")] -> Some (a_of_sexp el)
428
let sexp_of_t sexp_of_a a =
430
| None -> Sexp.Atom "enoN"
431
| Some a -> Sexp.List [sexp_of_a a ; Sexp.Atom "emoS"]
434
Type_struct.Generic.register1 (module struct
436
let compute = fun t -> Type_struct.Option t
437
end : Type_struct.Generic.S1);
438
S.Of_sexp.register1 (module struct
440
let compute = t_of_sexp
442
S.Sexp_of.register1 (module struct
444
let compute = sexp_of_t
449
let module A = struct
450
type t = int Rev_option.t with typerep
454
ignore (check (Rev_option.of_option None) A.typerep_of_t);
456
with S.Of_sexp.Not_implemented _ -> true);
457
Rev_option.register ();
458
assert (check_typerep (Rev_option.of_option None) A.typerep_of_t);
459
assert (check_typerep (Rev_option.of_option (Some 0)) A.typerep_of_t);
460
assert (check_typerep (Rev_option.of_option (Some 42)) A.typerep_of_t);
464
let module Rev_int : sig
465
type t = int with typerep
466
val register : unit -> unit
469
type t = int with typerep
473
exception Parse_error of Sexp.t with sexp
475
let t_of_sexp = function
477
let str' = String.copy str in
478
let len = String.length str in
479
let rec aux index index' =
480
if index >= len then () else begin
481
str'.[index] <- str.[index'];
482
aux (succ index) (pred index')
488
| Sexp.List _ as sexp -> raise (Parse_error sexp)
491
Type_struct.Generic.register0 (module struct
493
let compute = Type_struct.Int
494
end : Type_struct.Generic.S0);
495
S.Of_sexp.register typerep_of_t t_of_sexp
497
assert (check 421 Rev_int.typerep_of_t);
499
let `generic t_of_sexp = S.t_of_sexp Rev_int.typerep_of_t in
500
let `generic sexp_of_t = S.sexp_of_t Rev_int.typerep_of_t in
501
assert (Int.equal 421 (t_of_sexp (sexp_of_t 124)));