Skip to content

Commit 769f9fb

Browse files
authored
Simulation test for greedy regalloc priority queue (oxcaml#2274)
1 parent 29c1e2d commit 769f9fb

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

tests/backend/greedy/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(tests
2+
(names pqueue)
3+
(libraries ocamloptcomp))

tests/backend/greedy/pqueue.ml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
open Regalloc_gi_utils
2+
3+
module Int_min = struct
4+
include Int
5+
6+
let compare l r = Int.compare r l
7+
end
8+
9+
module Test (I : Order with type t = int) = struct
10+
11+
module Q = Make_max_priority_queue (I)
12+
module M = Map.Make (I)
13+
14+
let equal l r = I.compare l r = 0
15+
16+
let enq map i d =
17+
M.update i (function
18+
| None -> Some (M.singleton d 1)
19+
| Some ds -> Some (M.update d (function
20+
| None -> Some 1
21+
| Some j -> Some (j + 1)) ds)) map
22+
23+
let deq map i d =
24+
M.update i (function
25+
| None -> assert false
26+
| Some ds when M.equal equal ds (M.singleton d 1) -> None
27+
| Some ds -> Some (M.update d (function
28+
| None | Some 0 -> assert false
29+
| Some 1 -> None
30+
| Some j -> Some (j - 1)) ds)) map
31+
32+
let check_top q m =
33+
if Q.is_empty q then assert (M.is_empty !m)
34+
else (let Q.{ priority = qi; data = qd } = Q.get q in
35+
let mi, mds = M.max_binding !m in
36+
assert (qi = mi);
37+
assert (M.mem qd mds))
38+
39+
let enq q m =
40+
let i = Random.int 100 in
41+
let d = Random.int 1_000 in
42+
Q.add q ~priority:i ~data:d;
43+
m := enq !m i d;
44+
check_top q m
45+
46+
let deq q m =
47+
let Q.{ priority = qi; data = qd } = Q.get q in
48+
let Q.{ priority = qi'; data = qd' } = Q.get_and_remove q in
49+
assert (qi = qi');
50+
assert (qd = qd');
51+
m := deq !m qi qd;
52+
check_top q m
53+
54+
let () =
55+
Random.init 42;
56+
let q = Q.make ~initial_capacity:10 in
57+
let m = ref M.empty in
58+
for _ = 0 to 1_000_000 do
59+
(* Bias towards adding elements. *)
60+
if Random.int 100 < 55 then enq q m
61+
else if Q.is_empty q then assert (M.is_empty !m)
62+
else deq q m
63+
done;
64+
while not (Q.is_empty q) do deq q m done;
65+
assert (M.is_empty !m)
66+
;;
67+
end
68+
69+
module Int_max_test = Test (Int)
70+
module Int_min_test = Test (Int_min)

tests/backend/greedy/pqueue.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(* Intentionally blank *)

0 commit comments

Comments
 (0)