-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.ml
202 lines (160 loc) · 5.75 KB
/
test.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
type status =
| CTError
| RTError
| NoError
let status_of_string = function
| "fail"
| "RT error" -> RTError
| "CT error" -> CTError
| _ -> NoError
let string_of_status = function
| RTError -> "RT error"
| CTError -> "CT error"
| NoError -> "No error"
(** Ocaml representation of test files *)
(* All strings are enforced to be trimmed. *)
(* The expected string *)
type t =
{ name : string
; description : string
; params : string list
; status : status
; src : string
; expected : string }
let test_regexp =
Str.regexp "NAME:\\|DESCRIPTION:\\|PARAMS:\\|STATUS:\\|SRC:\\|EXPECTED:\\|END"
let read_test filename =
if Sys.file_exists filename
then
let content = CCIO.(with_in filename read_all) in
let open Str in
let get_opt s dflt = function
| Delim s' :: Text content :: rest when s = s' ->
String.trim content, rest
| all -> dflt, all
in
let toks = full_split test_regexp content in
let name, toks = get_opt "NAME:" Filename.(chop_extension @@ basename filename) toks in
let description, toks = get_opt "DESCRIPTION:" "" toks in
let params, toks =
let params_string, toks = get_opt "PARAMS:" "" toks in
String.(List.map trim @@ split_on_char ',' params_string), toks
in
let status, toks = get_opt "STATUS:" "ok" toks in
match toks with
| Delim "SRC:" :: Text src ::
Delim "EXPECTED:" :: Text expected :: ( [] | Delim "END" :: _ ) ->
Some { name ; description ; params ; status = status_of_string status ;
src ; expected = String.trim expected }
| _ ->
Printf.fprintf stderr "Wrong format in test file %s" filename;
None
else
(Printf.fprintf stderr "Test file %s not found." filename ; None)
let (let*) = Result.bind
let string_match =
let open Alcotest in
let matches pat s =
try let _ = Str.(search_forward (regexp pat) s 0) in true
with Not_found -> false
in
testable (pp string) matches
let status =
let open Alcotest in
testable Fmt.(using string_of_status string) (=)
(** Test pairs giving access to the first component when testing the second component *)
let dep_pair : type a b. a Alcotest.testable -> (a -> b Alcotest.testable) -> (a * b) Alcotest.testable =
fun cmp1 cmp2 ->
let open Alcotest in
let cmp_pair (x1, x2) (y1, y2) = equal cmp1 x1 y1 && equal (cmp2 x1) x2 y2 in
testable (fun fmt p -> pp (pair cmp1 (cmp2 (fst p))) fmt p) cmp_pair
(* Testing the result of running a test *)
let compare_results =
let cmp_res = function
| NoError -> Alcotest.string
| _ -> string_match
in
dep_pair status cmp_res
(** Helper functions on strings and processes *)
let filter_lines pred s =
CCString.split ~by:"\n" s
|> List.filter pred
|> String.concat "\n"
let is_comment_line s =
not (CCString.prefix ~pre:"|" s)
let process_output out =
String.trim out |> filter_lines is_comment_line
(** Calling the compiler (clang) and assembler (nasm) *)
(* Produce a result out of the return data from the compiler/assembler *)
let wrap_result (out, err, retcode) =
if retcode = 0 && String.equal "" err
then (print_string out ; Ok ())
else Error (CTError, out ^ err)
(* Find out current architecture (only supporting Linux/OS X for now) *)
let bin_format =
let out, _ , _ = CCUnix.call "uname -s" in
let arch = String.trim out in
match arch with
| "Linux" -> "elf64"
| "Darwin" -> "macho64"
| _ -> Fmt.failwith "Unknown architecture %s" arch
let clang ~compile_flags runtime basefile =
wrap_result @@ CCUnix.call "clang %s -o %s.run %s %s.o" compile_flags basefile runtime basefile
let nasm basefile =
wrap_result @@ CCUnix.call "nasm -f %s -o %s.o %s.s" bin_format basefile basefile
type compiler = string -> out_channel -> unit
let make_test
~compile_flags
runtime
~(compiler:compiler)
~oracle
filename =
match read_test filename with
| None -> Alcotest.failf "Could not open or parse test %s" filename
| Some test ->
let exec () =
let base = Filename.chop_extension filename in
let exe = base ^ ".run" in
let res =
let* () =
try Ok (CCIO.with_out (base ^ ".s") (compiler test.src))
with e -> Error (CTError, Printexc.to_string e)
in
let* () = nasm base in
let* () = clang ~compile_flags runtime base in
let out, err, retcode = CCUnix.call ~env:(Array.of_list test.params) "./%s" exe in
if retcode = 0 then
Ok (process_output out)
else Error (RTError, out ^ err)
in
let res = match res with
| Ok out -> NoError, out
| Error err -> err
in
let expected =
let i_oracle = CCString.find ~sub:"|ORACLE" test.expected in
match oracle with
| Some interp when test.status = NoError && i_oracle <> -1 ->
let prefix = CCString.sub test.expected 0 (max (i_oracle - 1) 0) in
let status , output = interp test.src in
status , prefix ^ output
| _ -> test.status, test.expected
in
let open Alcotest in
check compare_results test.name expected res
in test.name, exec
let testfiles_in_dir dir =
CCUnix.with_process_in ("find " ^ dir ^ " -name '*.bbc'") ~f:CCIO.read_lines_l
let name_from_file filename =
let open Filename in
dirname filename ^ "::" ^ basename (chop_extension filename)
let tests_from_dir ?(compile_flags="-g") ~runtime ~compiler ?oracle dir =
let open Alcotest in
let to_test testfile =
let testname, exec_test = make_test ~compile_flags runtime ~compiler ~oracle testfile in
name_from_file testfile, [test_case testname `Quick exec_test]
in
List.map to_test @@ testfiles_in_dir dir
|> CCList.sort (fun (s1,_) (s2,_) -> String.compare s1 s2)
(* Use as follow: *)
(* run "Tests" @@ List.map tests_from_dir [ "failing"; "tests"] *)