-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sc_sub_copy_out.ml
85 lines (69 loc) · 1.76 KB
/
test_sc_sub_copy_out.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
value () = Random.init 2
;
value res f =
try `Ok (f ()) with [ e -> `Error e ]
;
value cmp_res f1 f2 =
(res f1) = (res f2)
;
module SC = Subarray_cat
;
module S = Subarray
;
open It_Ops
;
value old_sub_copy_out ~ofs ~len sc =
let sc_len = SC.length sc in
if ofs < 0 || len < 0 || ofs+len > sc_len
then invalid_arg "Subarray_cat.sub_copy_out"
else
S.of_array & Array.init len (fun i -> SC.get sc (ofs+i))
;
value new_sub_copy_out = SC.sub_copy_out
;
(*******************)
value random_subarray () =
let arr_len = Random.int 500 in
let arr = Array.init arr_len (fun _ -> Random.int 10000) in
let subarr_ofs = Random.int (arr_len + 1) in
let arr_avail = arr_len - subarr_ofs in
let subarr_len = Random.int (arr_avail + 1) in
S.of_array_sub arr subarr_ofs subarr_len
;
value random_sc () =
let count = Random.int 5 in
SC.make & Array.to_list & Array.init count (fun _ -> random_subarray ())
;
value test () =
for _sc's = 0 to 10000 do
let sc = random_sc () in
let sc_len = SC.length sc in
for _tests = 0 to 10000 do
let ofs = -3 + Random.int (sc_len + 6)
and len = -3 + Random.int (sc_len + 6) in
if cmp_res
(fun () -> old_sub_copy_out ~ofs ~len sc)
(fun () -> new_sub_copy_out ~ofs ~len sc)
then ()
else failwith "bad"
done
done
;
value bench () =
for _sc's = 0 to 0 do
let sc = random_sc () in
let sc_len = SC.length sc in
for _tests = 0 to 10000000 do
let ofs = -3 + Random.int (sc_len + 6)
and len = -3 + Random.int (sc_len + 6) in
ignore (res
(fun () -> old_sub_copy_out ~ofs ~len sc)
(*
(fun () -> new_sub_copy_out ~ofs ~len sc)
*)
)
done
done
;
value () = (bench (); Printf.printf "alloc = %f\n" (Gc.allocated_bytes ()))
;