Skip to content

Commit fce2a74

Browse files
committed
add windows os support
1 parent 37dc53c commit fce2a74

File tree

1 file changed

+39
-46
lines changed

1 file changed

+39
-46
lines changed

src/sys/stm_tests.ml

Lines changed: 39 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ struct
2222
type sut = unit
2323

2424
let (/) = Filename.concat
25-
25+
2626
let update_map_name map_name k v = Map_names.add k v (Map_names.remove k map_name)
2727

2828
let arb_cmd _s =
@@ -49,18 +49,15 @@ struct
4949
if path = []
5050
then Some (File f)
5151
else None
52-
| Directory d ->
52+
| Directory d ->
5353
(match path with
5454
| [] -> Some (Directory d)
5555
| hd :: tl ->
5656
(match Map_names.find_opt hd d.fs_map with
5757
| None -> None
5858
| Some fs -> find_opt fs tl))
5959

60-
let mem fs path =
61-
match find_opt fs path with
62-
| None -> false
63-
| Some _ -> true
60+
let mem fs path = find_opt fs path <> None
6461

6562
let rec mkdir fs path new_dir_name perm =
6663
match fs with
@@ -145,14 +142,26 @@ struct
145142
then fs
146143
else touch fs path new_file_name perm
147144

148-
let init_sut () = ignore(Sys.command ("rm -rf " ^ (static_path / "sandbox_root") ^ " && mkdir " ^ (static_path / "sandbox_root")))
149-
150-
let cleanup _ = ignore (Sys.command ("rm -r -d -f " ^ static_path ^ "/sandbox_root"))
145+
let init_sut () =
146+
match Sys.os_type with
147+
| "Unix" -> ignore (Sys.command ("rm -rf " ^ (static_path / "sandbox_root") ^ " && mkdir " ^ (static_path / "sandbox_root")))
148+
| "Win32" ->
149+
ignore (Sys.command (
150+
"powershell -Command \"Remove-Item -Path " ^ (static_path / "sandbox_root") ^ " -Recurse -Force -ErrorAction Ignore \"
151+
& mkdir " ^ (static_path / "sandbox_root")))
152+
| v -> failwith ("Sys tests not working with " ^ v)
153+
154+
let cleanup _ =
155+
match Sys.os_type with
156+
| "Unix" -> ignore (Sys.command ("rm -rf " ^ (static_path / "sandbox_root")))
157+
| "Win32" -> ignore (Sys.command ("powershell -Command \"Remove-Item '" ^ (static_path / "sandbox_root") ^ "' -Recurse -Force\""))
158+
| v -> failwith ("Sys tests not working with " ^ v)
151159

152160
let precond _c _s = true
153161

162+
let p path = (List.fold_left (/) (static_path / "sandbox_root") path)
163+
154164
let run c _file_name =
155-
let p path = static_path / "sandbox_root" / (List.fold_left (/) "" path) in
156165
match c with
157166
| File_exists (path) -> Res (bool, Sys.file_exists (p path))
158167
| Mkdir (path, new_dir_name, perm) ->
@@ -162,12 +171,19 @@ struct
162171
| Readdir (path) ->
163172
Res (result (array string) exn, protect (Sys.readdir) (p path))
164173
| Touch (path, new_file_name, _perm) ->
165-
Res (unit, ignore(Sys.command ("touch " ^ (p path) / new_file_name ^ " 2>/dev/null")))
174+
(match Sys.os_type with
175+
| "Unix" -> Res (unit, ignore(Sys.command ("touch " ^ (p path) / new_file_name ^ " 2>/dev/null")))
176+
| "Win32" -> Res (unit, ignore(Sys.command ("type nul >> \"" ^ (p path / new_file_name) ^ "\"")))
177+
| v -> failwith ("Sys tests not working with " ^ v))
178+
179+
let fs_is_a_dir fs = match fs with | Directory _ -> true | File _ -> false
166180

167-
let is_a_dir fs = match fs with | Directory _ -> true | File _ -> false
181+
let path_is_a_dir fs path =
182+
match find_opt fs path with
183+
| None -> false
184+
| Some target_fs -> fs_is_a_dir target_fs
168185

169186
let postcond c (fs: filesys) res =
170-
let p path = static_path / "sandbox_root" / (String.concat "/" path) in
171187
match c, res with
172188
| File_exists (path), Res ((Bool,_),b) -> b = mem fs path
173189
| Mkdir (path, new_dir_name, _perm), Res ((Result (Unit,Exn),_), res) ->
@@ -179,17 +195,9 @@ struct
179195
(s = (p complete_path) ^ ": Permission denied") ||
180196
(s = (p complete_path) ^ ": File exists" && mem fs complete_path) ||
181197
(s = (p complete_path) ^ ": No such file or directory" && not (mem fs path)) ||
182-
(s = (p complete_path) ^ ": Not a directory" &&
183-
(match find_opt fs complete_path with
184-
| None -> true
185-
| Some target_fs -> not (is_a_dir target_fs)))
198+
(s = (p complete_path) ^ ": Not a directory" && not (path_is_a_dir fs complete_path))
186199
| _ -> false)
187-
| Ok () ->
188-
let is_existing_is_a_dir =
189-
(match find_opt fs path with
190-
| None -> false
191-
| Some target_fs -> is_a_dir target_fs) in
192-
not (mem fs complete_path) && mem fs path && is_existing_is_a_dir)
200+
| Ok () -> not (mem fs complete_path) && mem fs path && path_is_a_dir fs path)
193201
| Rmdir (path, delete_dir_name), Res ((Result (Unit,Exn),_), res) ->
194202
let complete_path = (path @ [delete_dir_name]) in
195203
(match res with
@@ -198,41 +206,27 @@ struct
198206
| Sys_error s ->
199207
(s = (p complete_path) ^ ": Directory not empty" && not (readdir fs complete_path = Some [])) ||
200208
(s = (p complete_path) ^ ": No such file or directory" && not (mem fs complete_path)) ||
201-
(s = (p complete_path) ^ ": Not a directory" &&
202-
(match find_opt fs complete_path with
203-
| None -> true
204-
| Some target_fs -> not (is_a_dir target_fs)))
209+
(s = (p complete_path) ^ ": Not a directory" && not (path_is_a_dir fs complete_path))
205210
| _ -> false)
206211
| Ok () ->
207212
let is_empty = readdir fs complete_path = Some [] in
208-
let is_a_dir =
209-
(match find_opt fs complete_path with
210-
| None -> false
211-
| Some target_fs -> is_a_dir target_fs) in
212-
mem fs complete_path && is_empty && is_a_dir)
213+
mem fs complete_path && is_empty && path_is_a_dir fs complete_path)
213214
| Readdir (path), Res ((Result (Array String,Exn),_), res) ->
214215
(match res with
215216
| Error err ->
216217
(match err with
217218
| Sys_error s ->
218219
(s = (p path) ^ ": Permission denied") ||
219220
(s = (p path) ^ ": No such file or directory" && not (mem fs path)) ||
220-
(s = (p path) ^ ": Not a directory" &&
221-
(match find_opt fs path with
222-
| None -> true
223-
| Some target_fs -> not (is_a_dir target_fs)))
221+
(s = (p path) ^ ": Not a directory" && not (path_is_a_dir fs path))
224222
| _ -> false)
225223
| Ok array_of_subdir ->
226224
let sut = List.sort (fun a b -> -(String.compare a b)) (Array.to_list array_of_subdir) in
227225
let same_result =
228226
(match readdir fs path with
229227
| None -> false
230228
| Some l -> List.sort (fun a b -> -(String.compare a b)) l = sut) in
231-
let is_a_dir =
232-
(match find_opt fs path with
233-
| None -> true
234-
| Some target_fs -> is_a_dir target_fs) in
235-
same_result && mem fs path && is_a_dir)
229+
same_result && mem fs path && path_is_a_dir fs path)
236230
| Touch (_path, _new_dir_name, _perm), Res ((Unit,_),_) -> true
237231
| _,_ -> false
238232
end
@@ -241,8 +235,7 @@ module Sys_seq = STM_sequential.Make(SConf)
241235
module Sys_dom = STM_domain.Make(SConf)
242236

243237
;;
244-
QCheck_base_runner.run_tests_main
245-
(let count = 1000 in
246-
[Sys_seq.agree_test ~count ~name:"STM Sys test sequential";
247-
(* Sys_dom.neg_agree_test_par ~count ~name:"STM Sys test parallel" *)
248-
])
238+
QCheck_base_runner.run_tests_main [
239+
Sys_seq.agree_test ~count:1000 ~name:"STM Sys test sequential";
240+
Sys_dom.agree_test_par ~count:100 ~name:"STM Sys test parallel"
241+
]

0 commit comments

Comments
 (0)