-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_storage.ml
209 lines (183 loc) · 6.52 KB
/
test_storage.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
(* Tests for CS 4 final exam, Winter 2019, part A. *)
open Storage
open OUnit2
(*** Utility functions. ***)
(* Expect an Invalid_argument exception. *)
let expect_invalid_arg msg thunk =
assert_bool msg
(try (thunk (); false)
with Invalid_argument _ -> true)
(* Expect a Failure exception. *)
let expect_failure msg thunk =
assert_bool msg
(try (thunk (); false)
with Failure _ -> true)
module Test(S: Storage) =
struct
(* Test if a storage grid is completely unoccupied. *)
let all_unoccupied nrows ncols =
let grid = S.make nrows ncols in
try
begin
for r = 0 to nrows - 1 do
for c = 0 to ncols - 1 do
if S.get grid (r, c) <> None then
raise Exit
done
done;
true
end
with Exit -> false
(* Test if doing `get` on a location that was `set` returns the value set.
* Also check that no other location was affected. *)
let get_set nrows ncols =
let row = Random.int nrows in
let col = Random.int ncols in
let value = Random.int 10 in
let grid = S.set (S.make nrows ncols) (row, col) value in
try
begin
for r = 0 to nrows - 1 do
for c = 0 to ncols - 1 do
if r = row && c = col then
(if S.get grid (r, c) <> Some value then raise Exit)
else
(if S.get grid (r, c) <> None then raise Exit)
done
done;
true
end
with Exit -> false
(* Try to set a location off the board. *)
let bad_set_1 nrows ncols =
let row = -1 in
let col = Random.int ncols in
let value = Random.int 10 in
ignore (S.set (S.make nrows ncols) (row, col) value)
(* Try to set a location off the board. *)
let bad_set_2 nrows ncols =
let row = Random.int nrows in
let col = -1 in
let value = Random.int 10 in
ignore (S.set (S.make nrows ncols) (row, col) value)
(* Try to set a location off the board. *)
let bad_set_3 nrows ncols =
let row = nrows in
let col = Random.int ncols in
let value = Random.int 10 in
ignore (S.set (S.make nrows ncols) (row, col) value)
(* Try to set a location off the board. *)
let bad_set_4 nrows ncols =
let row = Random.int nrows in
let col = ncols in
let value = Random.int 10 in
ignore (S.set (S.make nrows ncols) (row, col) value)
(* Try to set a negative number. *)
let bad_set_5 nrows ncols =
let row = Random.int nrows in
let col = Random.int ncols in
let value = -1 in
ignore (S.set (S.make nrows ncols) (row, col) value)
(* Test if doing `remove` on a location that was `set` clears the value.
* Also check that no other location was affected. *)
let remove_has_loc nrows ncols =
let row = Random.int nrows in
let col = Random.int ncols in
let value = Random.int 10 in
let grid = S.set (S.make nrows ncols) (row, col) value in
let grid' = S.remove grid (row, col) in
try
begin
for r = 0 to nrows - 1 do
for c = 0 to ncols - 1 do
if r = row && c = col then
begin
if S.get grid' (r, c) <> None then raise Exit;
if S.has_loc grid' (r, c) then raise Exit;
end
else
(if S.get grid (r, c) <> None then raise Exit)
done
done;
true
end
with Exit -> false
end
module SI = ImpStorage
module SF = FunStorage
module TI = Test(ImpStorage)
module TF = Test(FunStorage)
(*** The tests. ***)
let all_tests = "all_tests" >:::
[
"make" >:: (fun _ ->
begin
(* Invalid invocations of `make`. *)
expect_invalid_arg "make 1" (fun _ -> ignore (SI.make (-1) (-1)));
expect_invalid_arg "make 2" (fun _ -> ignore (SF.make (-1) (-1)));
expect_invalid_arg "make 3" (fun _ -> ignore (SI.make (-1) (0)));
expect_invalid_arg "make 4" (fun _ -> ignore (SF.make (-1) (0)));
expect_invalid_arg "make 5" (fun _ -> ignore (SI.make 0 (-1)));
expect_invalid_arg "make 6" (fun _ -> ignore (SF.make 0 (-1)));
expect_invalid_arg "make 7" (fun _ -> ignore (SI.make 0 0));
expect_invalid_arg "make 8" (fun _ -> ignore (SF.make 0 0));
(* Valid invocations of `make`.
* All valid locations start out unoccupied. *)
assert_bool "make 9" (TI.all_unoccupied 3 4);
assert_bool "make 10" (TF.all_unoccupied 3 4);
assert_bool "make 11" (TI.all_unoccupied 8 8);
assert_bool "make 12" (TF.all_unoccupied 8 8);
end
);
"set/get" >:: (fun _ ->
begin
assert_bool "set/get 1" (TI.get_set 3 4);
assert_bool "set/get 2" (TF.get_set 3 4);
assert_bool "set/get 3" (TI.get_set 5 5);
assert_bool "set/get 4" (TF.get_set 7 8);
assert_bool "set/get 5" (TI.get_set 7 8);
assert_bool "set/get 6" (TF.get_set 5 5);
end
);
"bad set" >:: (fun _ ->
begin
expect_invalid_arg "bad set 1 I"
(fun _ -> TI.bad_set_1 3 4);
expect_invalid_arg "bad set 2 I"
(fun _ -> TI.bad_set_2 3 4);
expect_invalid_arg "bad set 3 I"
(fun _ -> TI.bad_set_3 3 4);
expect_invalid_arg "bad set 4 I"
(fun _ -> TI.bad_set_4 3 4);
expect_invalid_arg "bad set 5 I"
(fun _ -> TI.bad_set_5 3 4);
expect_invalid_arg "bad set 1 F"
(fun _ -> TF.bad_set_1 3 4);
expect_invalid_arg "bad set 2 F"
(fun _ -> TF.bad_set_2 3 4);
expect_invalid_arg "bad set 3 F"
(fun _ -> TF.bad_set_3 3 4);
expect_invalid_arg "bad set 4 F"
(fun _ -> TF.bad_set_4 3 4);
expect_invalid_arg "bad set 5 F"
(fun _ -> TF.bad_set_5 3 4);
end
);
"remove/has_loc" >:: (fun _ ->
begin
assert_bool "remove/has_loc 1 I" (TI.remove_has_loc 3 4);
assert_bool "remove/has_loc 2 I" (TI.remove_has_loc 4 5);
assert_bool "remove/has_loc 3 I" (TI.remove_has_loc 8 8);
assert_bool "remove/has_loc 1 F" (TF.remove_has_loc 4 5);
assert_bool "remove/has_loc 2 F" (TF.remove_has_loc 4 5);
assert_bool "remove/has_loc 3 F" (TF.remove_has_loc 8 8);
end
);
]
let run_tests () =
begin
Random.self_init ();
Printf.printf "\nRUNNING STORAGE TESTS...\n\n";
run_test_tt_main all_tests;
end
let _ = run_tests ()