Skip to content

Commit 77d7365

Browse files
committed
Rework generator based on stats
1 parent d9f67c0 commit 77d7365

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

src/sys/stm_tests.ml

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -140,58 +140,57 @@ struct
140140

141141
let (/) = Filename.concat
142142

143-
(* var gen_existing_path : filesys -> path Gen.t *)
144-
let rec gen_existing_path fs =
143+
(* var existing_contents : filesys -> path list * path list *)
144+
let rec existing_contents fs : path list * path list =
145145
match fs with
146-
| Model.File -> Gen.return []
146+
| Model.File -> [[]],[]
147147
| Model.Directory d ->
148-
(match Model.Map_names.bindings d.fs_map with
149-
| [] -> Gen.return []
150-
| bindings -> Gen.(oneofl bindings >>= fun (n, sub_fs) ->
151-
Gen.oneof [
152-
Gen.return [n];
153-
Gen.map (fun l -> n::l) (gen_existing_path sub_fs)]
154-
)
155-
)
156-
157-
(* var gen_existing_pair : filesys -> (path * string) option Gen.t *)
158-
let rec gen_existing_pair fs = match fs with
159-
| Model.File -> Gen.return None (*failwith "no sandbox directory"*)
160-
| Model.Directory d ->
161-
(match Model.Map_names.bindings d.fs_map with
162-
| [] -> Gen.return None
163-
| bindings ->
164-
Gen.(oneofl bindings >>= fun (n, sub_fs) ->
165-
oneof [
166-
return (Some ([],n));
167-
map (function None -> Some ([],n)
168-
| Some (path,name) -> Some (n::path,name)) (gen_existing_pair sub_fs)]
169-
)
170-
)
171-
172-
let name_gen = Gen.oneofl ["aaa" ; "bbb" ; "ccc" ; "ddd" ; "eee"]
173-
let path_gen s = Gen.(oneof [gen_existing_path s; list_size (int_bound 5) name_gen]) (* can be empty *)
174-
let pair_gen s =
175-
let fresh_pair_gen = Gen.(pair (list_size (int_bound 5) name_gen)) name_gen in
176-
Gen.(oneof [
177-
fresh_pair_gen;
178-
(gen_existing_pair s >>= function None -> fresh_pair_gen
179-
| Some (p,_) -> map (fun n -> (p,n)) name_gen);
180-
(gen_existing_pair s >>= function None -> fresh_pair_gen
181-
| Some (p,n) -> return (p,n));
182-
])
148+
let bindings = Model.Map_names.bindings d.fs_map in
149+
let files, dirs = List.partition (fun p -> snd p = Model.File) bindings in
150+
let sub_res =
151+
List.map (fun (n,sub_fs) ->
152+
let sub_files, sub_dirs = existing_contents sub_fs in
153+
List.map (fun l -> n::l) sub_files,
154+
List.map (fun l -> n::l) sub_dirs) dirs in
155+
let files = List.map (fun (n,_) -> [n]) files in
156+
List.concat (files :: List.map fst sub_res),
157+
[]::List.concat (List.map snd sub_res)
158+
159+
let name_gen = Gen.oneofl ["aaa" ; "bbb" ; "ccc" ; "ddd" ; "eee"; "fff"; "ggg"; "hhh"; "iii"]
183160

184161
let arb_cmd s =
162+
let files, dirs = existing_contents s in
163+
let gen_file = Gen.oneofl files in
164+
let gen_file_sep = Gen.oneofl (List.filter_map Model.separate_path files) in
165+
let gen_dir = Gen.oneofl dirs in
166+
let gen_dir_sep = Gen.oneofl (List.filter_map Model.separate_path dirs) in
167+
let gen_arb_path = Gen.(list_size (int_bound 5) name_gen) in
168+
let gen_arb_path_sep = Gen.(pair (list_size (int_bound 4) name_gen) name_gen) in
185169
QCheck.make ~print:show_cmd
186-
Gen.(oneof [
187-
map (fun path -> File_exists path) (path_gen s);
188-
map (fun path -> Is_directory path) (path_gen s);
189-
map (fun (path,new_dir_name) -> Remove (path, new_dir_name)) (pair_gen s);
190-
map2 (fun old_path new_path -> Rename (old_path, new_path)) (path_gen s) (path_gen s);
191-
map (fun (path,new_dir_name) -> Mkdir (path, new_dir_name)) (pair_gen s);
192-
map (fun (path,delete_dir_name) -> Rmdir (path, delete_dir_name)) (pair_gen s);
193-
map (fun path -> Readdir path) (path_gen s);
194-
map (fun (path,new_file_name) -> Mkfile (path, new_file_name)) (pair_gen s);
170+
Gen.(
171+
if files = []
172+
then
173+
oneof [
174+
map2 (fun path new_file_name -> Mkfile (path, new_file_name)) gen_dir name_gen;
175+
map2 (fun path new_dir_name -> Mkdir (path, new_dir_name)) gen_dir name_gen;
176+
]
177+
else
178+
frequency [
179+
1,map (fun path -> File_exists path) (frequency [8,gen_file; 1,gen_dir; 1,gen_arb_path]);
180+
1,map (fun path -> Is_directory path) (frequency [1,gen_file; 8,gen_dir; 1,gen_arb_path]);
181+
1,map (fun (path,file_name) -> Remove (path, file_name)) (if List.length dirs > 1
182+
then frequency [8,gen_file_sep; 1,gen_dir_sep; 1,gen_arb_path_sep]
183+
else frequency [1,gen_file_sep; 1,gen_arb_path_sep]);
184+
1,map (fun (old_path,new_path) -> Rename (old_path, new_path)) (frequency [5,(pair gen_file gen_arb_path);
185+
5,(pair gen_dir gen_arb_path);
186+
1,(pair gen_arb_path gen_arb_path);
187+
]);
188+
3,map2 (fun path new_dir_name -> Mkdir (path, new_dir_name)) (frequency [1,gen_file; 8,gen_dir; 1,gen_arb_path]) name_gen;
189+
1,map (fun (path,dir_name) -> Rmdir (path, dir_name)) (if List.length dirs > 1
190+
then frequency [1,gen_file_sep; 8,gen_dir_sep; 1,gen_arb_path_sep]
191+
else gen_arb_path_sep);
192+
1,map (fun path -> Readdir path) (frequency [1,gen_file; 8,gen_dir; 1,gen_arb_path]);
193+
3,map2 (fun path new_file_name -> Mkfile (path, new_file_name)) gen_dir name_gen;
195194
])
196195

197196
let sandbox_root = "_sandbox"

0 commit comments

Comments
 (0)