1 (* Verify shared dictionary.
2 * $Id: test_ancient_dict_verify.ml,v 1.1 2006-10-06 15:03:47 rich Exp $
8 let argv = Array.to_list Sys.argv
10 let wordsfile, datafile =
12 | [_; wordsfile; datafile] ->
15 failwith (sprintf "usage: %s wordsfile datafile"
19 let fd = openfile datafile [O_RDWR] 0o644 in
22 let arraysize = 256 (* one element for each character *)
24 type t = Not_Found | Exists of t array | Not_Exists of t array;;
25 let tree : t array Ancient.ancient = Ancient.get md 0
26 let tree = Ancient.follow tree
28 let word_exists word =
30 let tree = ref tree in
31 let len = String.length word in
32 for i = 0 to len-2; do
34 let c = Char.code c in
35 match (!tree).(c) with
36 | Not_Found -> raise Not_found
38 | Not_Exists tree' -> tree := tree'
41 (* Final character. *)
42 let c = word.[len-1] in
43 let c = Char.code c in
44 match (!tree).(c) with
46 | Not_Exists _ -> false
52 (* Read in the words and keep in a local list. *)
54 let chan = open_in wordsfile in
56 let word = input_line chan in
57 if word <> "" then words := word :: !words;
60 (try loop () with End_of_file -> ());
62 let words = List.rev !words in
64 (* Verify that the number of words in the tree is the same as the
65 * number of words in the words file.
67 let nr_expected = List.length words in
71 for i = 0 to arraysize-1 do
75 c := !c + 1 + count tree
83 if nr_expected <> nr_actual then
85 "verify failed: expected %d words but counted %d in tree"
86 nr_expected nr_actual);
88 (* Check each word exists in the tree. *)
91 if not (word_exists word) then
92 failwith (sprintf "verify failed: word '%s' missing from tree" word)
97 (* Garbage collect - good way to check we haven't broken anything. *)
100 printf "Verification succeeded.\n"