1 (* Verify shared dictionary. *)
6 let argv = Array.to_list Sys.argv
8 let wordsfile, datafile =
10 | [_; wordsfile; datafile] ->
13 failwith (sprintf "usage: %s wordsfile datafile"
17 let fd = openfile datafile [O_RDWR] 0o644 in
20 let arraysize = 256 (* one element for each character *)
22 type t = Not_Found | Exists of t array | Not_Exists of t array;;
23 let tree : t array Ancient.ancient = Ancient.get md 0
24 let tree = Ancient.follow tree
26 let word_exists word =
28 let tree = ref tree in
29 let len = String.length word in
30 for i = 0 to len-2; do
32 let c = Char.code c in
33 match (!tree).(c) with
34 | Not_Found -> raise Not_found
36 | Not_Exists tree' -> tree := tree'
39 (* Final character. *)
40 let c = word.[len-1] in
41 let c = Char.code c in
42 match (!tree).(c) with
44 | Not_Exists _ -> false
50 (* Read in the words and keep in a local list. *)
52 let chan = open_in wordsfile in
54 let word = input_line chan in
55 if word <> "" then words := word :: !words;
58 (try loop () with End_of_file -> ());
60 let words = List.rev !words in
62 (* Verify that the number of words in the tree is the same as the
63 * number of words in the words file.
65 let nr_expected = List.length words in
69 for i = 0 to arraysize-1 do
73 c := !c + 1 + count tree
81 if nr_expected <> nr_actual then
83 "verify failed: expected %d words but counted %d in tree"
84 nr_expected nr_actual);
86 (* Check each word exists in the tree. *)
89 if not (word_exists word) then
90 failwith (sprintf "verify failed: word '%s' missing from tree" word)
95 (* Garbage collect - good way to check we haven't broken anything. *)
98 printf "Verification succeeded.\n"