This repository has been archived by the owner on Jun 9, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatchObjects_unit.ml
344 lines (303 loc) · 13.1 KB
/
matchObjects_unit.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
open MatchObjects
open Kaputt.Abbreviations
open TypesJS
open TestBaseData
open MatchTypes
open TraceTypes
let (|>) = Pervasives.(|>)
let test_data = {
funs1 = functab1;
funs2 = functab2;
facts1 = rich_facts_1;
facts2 = rich_facts_2;
pt1 = points_to_1;
pt2 = points_to_2;
noneq = IntIntSet.empty
}
let is_base_test_true (name, value) =
Test.make_simple_test ~title:("is_base - " ^ name) (fun () -> Assert.is_true (is_base value))
let is_base_test_false (name, value) =
Test.make_simple_test ~title:("is_base - " ^ name) (fun () -> Assert.is_false (is_base value))
let is_base_tests =
List.map is_base_test_true [
("undefined", OUndefined);
("boolean", OBoolean true);
("int", ONumberInt 42);
("float", ONumberFloat 3.14);
("string", OString "xyzzy");
("symbol", OSymbol "foobar");
("null", ONull);
] @
List.map is_base_test_false [
("function", OFunction (1, 2));
("object", OObject 3);
("other", OOther ("ty", 4))
]
module ValPairMap = Map.Make(struct
type t = jsval * jsval
let compare = Pervasives.compare
end);;
let print_reason = Fmt.to_to_string MatchTypes.pp_fun_match_failure
let match_functions_ins_ins_eq =
Test.make_simple_test ~title:"match_functions - equal instrumented code"
(fun () -> assert_is_None ~prn:print_reason (match_functions test_data 4 3))
let match_functions_ins_ins_neq =
Test.make_simple_test ~title:"match_functions - non-equal instrumented code"
(fun () -> assert_is_Some ~prn:print_reason (match_functions test_data 3 3))
let match_functions_nins_eq =
Test.make_simple_test ~title:"match_functions - equal non-instrumented code"
(fun () -> assert_is_None ~prn:print_reason (match_functions test_data 5 4))
let match_functions_nins_ins =
Test.make_simple_test ~title:"match_functions - instrumented vs. uninstrumented code"
(fun () -> assert_is_Some ~prn:print_reason (match_functions test_data 5 3))
let match_functions_ext_eq =
Test.make_simple_test ~title:"match_functions - equal external code"
(fun () -> assert_is_None ~prn:print_reason (match_functions test_data 0 0))
let match_functions_ext_neq =
Test.make_simple_test ~title:"match_functions - non-equal external code"
(fun () -> assert_is_Some ~prn:print_reason (match_functions test_data 0 1))
let match_functions_ext_ins =
Test.make_simple_test ~title:"match_functions - instrumented vs. external code"
(fun () -> assert_is_Some ~prn:print_reason (match_functions test_data 4 1))
let match_functions_nins_ext =
Test.make_simple_test ~title:"match_functions - uninstrumented vs. external code"
(fun () -> assert_is_Some ~prn:print_reason (match_functions test_data 5 1))
let match_functions_tests =
[match_functions_ins_ins_eq; match_functions_ins_ins_neq; match_functions_nins_eq;
match_functions_nins_ins; match_functions_ext_eq; match_functions_ext_neq;
match_functions_ext_ins; match_functions_nins_ext]
let testobj1 =
StringMap.empty
|> StringMap.add "f1" v1
|> StringMap.add "f2" obj1_cyc1
|> StringMap.add "ign1" vundef
|> StringMap.add "ign2" vnull
let testobj2 =
StringMap.empty
|> StringMap.add "f1" v1
|> StringMap.add "f2" obj2_cyc1
|> StringMap.add "ign1" vnull
|> StringMap.add "ign3" vundef
let testobj2' =
StringMap.empty
|> StringMap.add "f1" v1
|> StringMap.add "f2" obj2_cyc2
|> StringMap.add "ign1" vnull
|> StringMap.add "ign3" vundef
let test_tab =
ValPairMap.empty
|> ValPairMap.add (v1, v1) true
|> ValPairMap.add (v0, v0) true
|> ValPairMap.add (obj1_cyc1, obj2_cyc1) true
|> ValPairMap.add (obj1_cyc1, obj2_cyc2) false
|> ValPairMap.add (obj1_cyc2, obj2_cyc2) true
|> ValPairMap.add (obj1_cyc2, obj2_cyc3) false
module IIA = Assert.Set(IntIntSet)
(struct
type t = int * int
let to_string = Fmt.to_to_string (Fmt.pair Fmt.int Fmt.int)
end)
let same_data
{ funs1 = funs11;
funs2 = funs21;
facts1 = facts11;
facts2 = facts21;
pt1 = pt11;
pt2 = pt21;
noneq = noneq1 }
{ funs1 = funs12;
funs2 = funs22;
facts1 = facts12;
facts2 = facts22;
pt1 = pt12;
pt2 = pt22;
noneq = noneq2 } =
same_functions funs11 funs12;
same_functions funs21 funs22;
same_rich_facts facts11 facts12;
same_rich_facts facts21 facts22;
AssertVersionedReferenceMap.make_equal (=) (Fmt.to_to_string pp_jsval)
pt11 pt12;
AssertVersionedReferenceMap.make_equal (=) (Fmt.to_to_string pp_jsval)
pt21 pt22;
IIA.equal noneq1 noneq2
let matcher_stub seen_tab data cycle_set objeq vals =
Format.eprintf "Considering value pair %a@."
(Fmt.pair pp_jsval pp_jsval) vals;
same_data test_data data;
seen_tab := ValPairMap.add vals
(1 + try ValPairMap.find vals !seen_tab with Not_found -> 0)
!seen_tab;
try
if ValPairMap.find vals test_tab then None else Some (MatchTypes.Other "testing")
with
Not_found ->
Assert.fail_msg ("Trying to look up " ^
Fmt.to_to_string (Fmt.pair pp_jsval pp_jsval) vals)
let good_ignore = [ "ign1"; "ign2"; "ign3" ]
let pp_seen_map =
let open Fmt in
let open TypesJS in
using ValPairMap.bindings
(list (pair (pair pp_jsval pp_jsval) int));;
let eq_seen_map = ValPairMap.equal (=)
let assert_equal_seen_map = Assert.make_equal eq_seen_map (Fmt.to_to_string pp_seen_map)
let test_match_objects_raw_obj1_obj2_good_ignore =
Test.make_simple_test ~title:"match_objects_raw - obj1 vs. obj2, good ignore set"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty in
assert_is_None (match_objects_raw (matcher_stub seen) good_ignore test_data IntIntSet.empty objeq testobj1 testobj2);
assert_equal_seen_map
(ValPairMap.empty |> ValPairMap.add (v1, v1) 1 |> ValPairMap.add (obj1_cyc1, obj2_cyc1) 1)
!seen
)
let test_match_objects_raw_obj1_obj2_not_ignoring_2 =
Test.make_simple_test ~title:"match_objects_raw - obj1 vs. obj2, not ignoring ign2"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty in
assert_is_Some (match_objects_raw (matcher_stub seen) ["ign1";"ign3"] test_data IntIntSet.empty objeq testobj1 testobj2);
)
let test_match_objects_raw_obj1_obj2_not_ignoring_3 =
Test.make_simple_test ~title:"match_objects_raw - obj1 vs. obj2, not ignoring ign3"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty in
assert_is_Some (match_objects_raw (matcher_stub seen) ["ign1";"ign2"] test_data IntIntSet.empty objeq testobj1 testobj2);
)
let test_match_objects_raw_obj1_obj2_not_ignoring_1 =
Test.make_simple_test ~title:"match_objects_raw - obj1 vs. obj2, not ignoring ign1"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty in
Assert.raises ~msg:"Expected Not_found exception"
(fun () -> match_objects_raw (matcher_stub seen) ["ign3";"ign2"] test_data IntIntSet.empty objeq testobj1 testobj2)
)
let test_match_objects_raw_obj1_obj2'_good_ignore =
Test.make_simple_test ~title:"match_objects_raw - obj1 vs. obj2', good ignore set"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty in
assert_is_Some (match_objects_raw (matcher_stub seen) good_ignore test_data IntIntSet.empty objeq testobj1 testobj2');
Assert.equal_int 1 (ValPairMap.find (obj1_cyc1, obj2_cyc2) !seen)
)
let obj1desc_cyc1 =
simplefields [ ("next", obj1_cyc2); ("val", v0) ]
let test_match_objects_memo_cyc1_cyc1 =
Test.make_simple_test ~title:"match_objects_memo - cyc1 vs. cyc1, uncached"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty
and id1 = objectid_of_jsval obj1_cyc1
and id2 = objectid_of_jsval obj2_cyc1 in
assert_is_None ~msg:"Return value" (match_objects_memo (matcher_stub seen) ["toString"] test_data IntIntSet.empty objeq id1 id2);
assert_is_None ~msg:"Cached values" (IntIntMap.find (get_object_id id1, get_object_id id2) !objeq)
)
let test_match_objects_memo_cyc1_cyc2 =
Test.make_simple_test ~title:"match_objects_memo - cyc1 vs. cyc2, uncached"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty
and id1 = objectid_of_jsval obj1_cyc1
and id2 = objectid_of_jsval obj2_cyc2 in
assert_is_Some ~msg:"Return value" (match_objects_memo (matcher_stub seen) ["toString"] test_data IntIntSet.empty objeq id1 id2);
assert_is_Some ~msg:"Cached values" (IntIntMap.find (get_object_id id1, get_object_id id2) !objeq)
)
let test_match_objects_memo_cyc3_cyc3_seen_cache =
Test.make_simple_test ~title:"match_objects_memo - cyc3 vs. cyc3, seen case"
(fun () ->
let seen = ref ValPairMap.empty
and objeq = ref IntIntMap.empty
and id1 = objectid_of_jsval obj1_cyc3
and id2 = objectid_of_jsval obj2_cyc3 in
let cycle_seen = IntIntSet.empty |> IntIntSet.add (get_object_id id1, get_object_id id2) in
assert_is_None (match_objects_memo (matcher_stub seen) ["toString"] test_data cycle_seen objeq id1 id2)
)
let test_match_objects_memo_cyc3_cyc3_objeq_cache =
Test.make_simple_test ~title:"match_objects_memo - cyc3 vs. cyc3, objeq case"
(fun () ->
let seen = ref ValPairMap.empty
and id1 = objectid_of_jsval obj1_cyc3
and id2 = objectid_of_jsval obj2_cyc3
and cycle_seen = IntIntSet.empty in
let objeq = ref (IntIntMap.empty |> IntIntMap.add (get_object_id id1, get_object_id id2) None) in
assert_is_None (match_objects_memo (matcher_stub seen) ["toString"] test_data cycle_seen objeq id1 id2)
)
let test_match_objects_memo_cyc3_cyc3_objeq_cache_fail =
Test.make_simple_test ~title:"match_objects_memo - cyc3 vs. cyc3, objeq case (negative)"
(fun () ->
let seen = ref ValPairMap.empty
and id1 = objectid_of_jsval obj1_cyc3
and id2 = objectid_of_jsval obj2_cyc3
and cycle_seen = IntIntSet.empty
and msg = MatchTypes.Other "mark" in
let objeq = ref (IntIntMap.empty |> IntIntMap.add (get_object_id id1, get_object_id id2) (Some msg)) in
Assert.make_equal (=)
(function
None -> "None"
| Some (NonMatching (_, _, _)) -> "non-matching elements ..."
| Some (MissingOrig (n, _)) -> "missing orig called " ^ n ^ " at ..."
| Some (MissingXfrm (n, _)) -> "missing xfrm called " ^ n ^ " at ..."
| Some (Other msg) -> msg)
(Some msg)
(match_objects_memo (matcher_stub seen) ["toString"] test_data cycle_seen objeq id1 id2)
)
let test_match_raw_values_v1_v1 =
Test.make_simple_test ~title:"match_values_raw - equal ints"
(fun () ->
let objeq = ref IntIntMap.empty in
assert_is_None (match_values_raw test_data IntIntSet.empty objeq (v1, v1));
Assert.equal_int 0 (IntIntMap.cardinal !objeq))
let test_match_raw_values_cyc1_cyc1 =
Test.make_simple_test ~title:"match_values_raw - cyc1 vs. cyc1"
(fun () ->
let objeq = ref IntIntMap.empty in
assert_is_None (match_values_raw test_data IntIntSet.empty objeq (obj1_cyc1, obj2_cyc1)))
let test_match_raw_values_cyc1_cyc2 =
Test.make_simple_test ~title:"match_values_raw - cyc1 vs. cyc2"
(fun () ->
let objeq = ref IntIntMap.empty in
assert_is_Some (match_values_raw test_data IntIntSet.empty objeq (obj1_cyc1, obj2_cyc2)))
let test_match_values_v1_v1 =
Test.make_simple_test ~title:"match_values - equal ints"
(fun () ->
let objeq = ref IntIntMap.empty in
assert_is_None (match_values "XYZ" test_rt1 test_rt2 test_lf1 test_lf2 IntIntSet.empty v1 v1 objeq);
Assert.equal_int 0 (IntIntMap.cardinal !objeq))
let test_match_values_cyc1_cyc1 =
Test.make_simple_test ~title:"match_values - cyc1 vs. cyc1"
(fun () ->
let objeq = ref IntIntMap.empty in
assert_is_None (match_values "XYZ" test_rt1 test_rt2 test_lf1 test_lf2 IntIntSet.empty obj1_cyc1 obj2_cyc1 objeq))
let test_match_values_cyc1_cyc2 =
Test.make_simple_test ~title:"match_values - cyc1 vs. cyc2"
(fun () ->
let objeq = ref IntIntMap.empty in
assert_is_Some (match_values "XYZ" test_rt1 test_rt2 test_lf1 test_lf2 IntIntSet.empty obj1_cyc1 obj2_cyc2 objeq))
let tests = (is_base_tests @ [
match_functions_ins_ins_eq;
match_functions_ins_ins_neq;
match_functions_nins_eq;
match_functions_nins_ins;
match_functions_ext_eq;
match_functions_ext_neq;
match_functions_ext_ins;
match_functions_nins_eq;
test_match_objects_raw_obj1_obj2_good_ignore;
test_match_objects_raw_obj1_obj2_not_ignoring_1;
test_match_objects_raw_obj1_obj2_not_ignoring_2;
test_match_objects_raw_obj1_obj2_not_ignoring_3;
test_match_objects_raw_obj1_obj2'_good_ignore;
test_match_objects_memo_cyc1_cyc1;
test_match_objects_memo_cyc1_cyc2;
test_match_objects_memo_cyc3_cyc3_seen_cache;
test_match_objects_memo_cyc3_cyc3_objeq_cache;
test_match_objects_memo_cyc3_cyc3_objeq_cache_fail;
test_match_raw_values_v1_v1;
test_match_raw_values_cyc1_cyc1;
test_match_raw_values_cyc1_cyc2;
test_match_values_v1_v1;
test_match_values_cyc1_cyc1;
test_match_values_cyc1_cyc2
])