1 (* Very basic tests of Ancient module shared functionality.
2 * $Id: test_ancient_shared.ml,v 1.2 2006-09-27 18:39:44 rich Exp $
11 phone : string option;
12 marital_status : marital_status;
15 and marital_status = Single | Married | Divorced
18 eprintf "compacting ... %!";
20 let stat = Gc.stat () in
21 let live_words = stat.Gc.live_words in
22 eprintf "live words = %d (%d MB)\n%!"
23 live_words (live_words * 8 / 1024 / 1024)
25 let random_string () =
26 let n = 1 + Random.int 20 in
27 let str = String.create n in
29 let c = 97 + Random.int 26 in
35 let random_string_option () =
36 if Random.int 3 = 1 then None else Some (random_string ())
38 let random_marital_status () =
39 match Random.int 3 with
44 let rec output_data chan data =
45 let n = Array.length data in
47 output_item chan data.(i)
50 and output_item chan item =
51 fprintf chan "id = %d\n%!" item.id;
52 fprintf chan "\tname = %s\n%!" item.name;
53 fprintf chan "\tdob = %s\n%!" item.dob;
54 fprintf chan "\taddress = %s\n%!" item.address;
55 fprintf chan "\tphone = %s\n%!"
56 (match item.phone with
58 | Some str -> "Some " ^ str);
59 fprintf chan "\tmarital_status = %s\n%!"
60 (string_of_marital_status item.marital_status)
62 and string_of_marital_status status =
65 | Married -> "Married"
66 | Divorced -> "Divorced"
69 match List.tl (Array.to_list Sys.argv) with
70 | ["read"; share_filename; print_filename] ->
71 (* Read data in filename and print. *)
72 let fd = Unix.openfile share_filename [Unix.O_RDWR] 0 in
73 let md = Ancient.attach fd in
75 eprintf "After attaching %s ...\n" share_filename;
78 let data : item array Ancient.ancient = Ancient.get md 0 in
79 eprintf "After getting ...\n";
82 let chan = open_out print_filename in
83 output_data chan (Ancient.follow data);
87 eprintf "After detaching ...\n";
90 | ["write"; share_filename; print_filename] ->
91 (* Generate random data and write to filename, also print it. *)
92 eprintf "Before allocating data on OCaml heap ...\n";
98 name = random_string ();
99 dob = random_string ();
100 address = random_string ();
101 phone = random_string_option ();
102 marital_status = random_marital_status () }
104 eprintf "After allocating data on OCaml heap ...\n";
107 let chan = open_out print_filename in
108 output_data chan data;
112 Unix.openfile share_filename
113 [Unix.O_CREAT;Unix.O_TRUNC;Unix.O_RDWR] 0o644 in
114 let md = Ancient.attach fd in
116 ignore (Ancient.share md 0 data);
117 eprintf "After sharing data to %s ...\n" share_filename;
121 eprintf "After detaching ...\n";
125 failwith "test_ancient_shared"