Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

[r1813]: trunk / Toss / Solver / StructureTest.ml Maximize Restore History

Download this file

StructureTest.ml    457 lines (380 with data), 12.5 kB

  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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
open OUnit
open Structure
let struc_of_string s =
StructureParser.parse_structure Lexer.lex (Lexing.from_string s)
let test_parse ?msg ?result s =
let result = match result with
None -> s | Some s -> s in
assert_equal ?msg ~printer:(fun x->x) (Aux.normalize_spaces result)
(Aux.normalize_spaces (Structure.str (struc_of_string (s))))
let test_sprint ?msg s =
assert_equal ?msg ~printer:(fun x->x) s
(Structure.sprint (struc_of_string s))
let test_incident_in_struc struc e =
let tuple_str ?(with_paren=true) struc tp =
if Array.length tp = 1 && not with_paren then elem_str struc tp.(0) else
"(" ^ (String.concat ", "
(Array.to_list (Array.map (elem_str struc) tp))) ^ ")" in
let inc_str (rname, tps) =
rname ^ " {" ^ (String.concat ", " (List.map (tuple_str struc) tps)) ^"}" in
(String.concat "; " (List.map inc_str (incident struc e)))
let test_incident s result =
let struc = struc_of_string s in
assert_equal ~printer:(String.concat " | ") result
(List.map (test_incident_in_struc struc)
(Structure.elements struc))
let test_del s en result =
let struc = struc_of_string s in
let e = find_elem struc en in
assert_equal ~printer:(fun x->x) result
(Structure.str (del_elem struc e))
let tests = "Structure" >::: [
"parse 1" >::
(fun () ->
test_parse "[a, b | R (a, b) | ]";
test_parse "[a, b | R (a, b) ; .s = a | ]";
);
"parse 2" >::
(fun () ->
test_parse
~result:"[c, a, b | P (b); Q {a; b}; R {(a, b); (b, b)} | ]"
"[ c | R { (a, b); (b, b) } ; Q { a; b } ; P { b } | ]";
);
"parse 3" >::
(fun () ->
test_parse
~result:"[a | P (a) | f {a->1.3}]"
"[ a | P{a} | f { a-> 1.3 } ]";
test_parse
~result:"[a | P (a) | vx {a->0.}; vy {a->0.}; x {a->0.}; y {a->0.}]"
"[ a | P{a} | - ]";
test_parse
~result:"[a, b, c | | f {a->1.3, b->2., c->2.}]"
"[ | | f { a-> 1.3, b->2, c->3.3 } ; f { c -> 2 } ]";
test_parse
~result:"[a, b, c | R (a, b) | f {a->1.3, b->2., c->3.3}; g {b->2.}]"
"[ | R (a, b) | f { a-> 1.3, b->2, c->3.3 } ; g { b -> 2 } ]"
);
"parse range" >::
(fun () ->
test_parse
~result:("[e2, e3, e4 | | nbr {e2->2., e3->3., e4->4.}; x {e2->0., " ^
"e3->15., e4->30.}; y {e2->0., e3->0., e4->0.} ]")
"[ 2 - 4 | | - ]";
);
"incident" >::
(fun () ->
test_incident "[a, b | R (a, b) | ]"
["R {(a, b)}"; "R {(a, b)}"];
test_incident "[a, b | R { (a, b) }; P { a } | ]"
["R {(a, b)}; P {(a)}"; "R {(a, b)}"];
);
"del" >::
(fun () ->
test_del "[ a, b | R { (a, b) } | ]" "a" "[b | R:2 {} | ]";
test_del
"[ a, b, c | R { (a, b); (b, c) }; P { a } | ]" "a"
"[b, c | P:1 {}; R (b, c) | ]";
let struc = struc_of_string
"[ a, b, c | R { (a, b); (b, c) }; P { a } | ]" in
test_incident (str (del_elem struc (find_elem struc "a")))
["R {(b, c)}"; "R {(b, c)}"]
);
"parse TicTacToe" >::
(fun () ->
test_parse
"[ | P:1 {}; Q:1 {} | ] \"
. . .
. . .
. . .
\"";
test_parse
"[ | Q:1 {} | ] \"
. P .
P . .
. . .
\"";
);
"parse Chess" >::
(fun () ->
test_parse
"[ | | ] \"
... ... ... ...
BR BN.BB BQ.BK BB.BN BR.
... ... ... ...
BP.BP BP.BP BP.BP BP.BP
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
WP WP.WP WP.WP WP.WP WP.
... ... ... ...
WR.WN WB.WQ WK.WB WN.WR
\"";
test_parse
"[ | BB:1 {}; WN:1 {} | ] \"
... ... ... ...
BR BN. BQ.BK ...BN BR.
... ... ... ...
BP. BP.BP ...BP BP.BP
... ... ... ...
BP. ...BP ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
... ... ... ...
WP WP.WP WP.WP WP.WP WP.
... ... ... ...
WR. WB.WQ WK.WB ...WR
\"";
);
"parse Go" >::
(fun () ->
test_parse ~msg:"Go 19x19"
"[ | | ] \"
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
..................B....................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.....................W.................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
. . . . . . . . . . . . . . . . . . .
.......................................................
\"";
test_parse ~msg:"Go 9x9"
"[ | B:1 {}; W:1 {} | ] \"
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
. . . . . . . . .
.........................
\"";
);
"parse board long names" >::
(fun () ->
test_parse ~msg:"Unique by 1 character"
"[ | Alpha:1 {}; Beta:1 {} | ] \"
Al Be
Al
. Be
\"";
(* "A" not in signature, because its name is recoverable from board *)
test_parse ~msg:"Single character + unique by 1 character"
"[ | Alpha:1 {}; Beta:1 {} | ] \"
Al Be
A
. Be
\"";
test_parse ~msg:"Single character + unique by 2 characters"
"[ | Alpha:1 {}; Ampr:1 {} | ] \"
Al Am
A
. Am
\"";
test_parse ~msg:"Unique by 3 characters"
"[ | Aleph:1 {}; Alpha:1 {} | ] \"
AlpAle
Ale
. Alp
\"";
);
"parse board optional long predicates" >::
(fun () ->
test_parse ~msg:"3 predicates"
"[ | Alpha:1 {}; Beta:1 {}; Gamma:1 {} | ] \"
? ?Be
#AlBe?
\"";
test_parse ~msg:"2 predicates"
"[ | Alpha:1 {}; Beta:1 {} | ] \"
? ?Be
#AlBe?
\"";
);
"parse board signature effects" >::
(fun () ->
(* Unfortunately we need to print negated and optional
predicates in the signature, although they can be deduced by
looking at the board. *)
test_parse ~msg:"recreate negated"
~result:"[ | A:1 {}; G:1 {} | ] \"
? ?B
#A B?
\""
"[ | G:1 {} | ] \"
? ?B
#A B?
\"";
test_parse ~msg:"recreate optional"
~result:"[ | A:1 {}; B:1 {}; G:1 {} | ] \"
? ?B
#A .
\""
"[ | G:1 {} | ] \"
? ?B
#A .
\"";
);
"parse board with hole" >::
(fun () ->
test_parse
"[ | P:1 {}; Q:1 {} | ] \"
. . .
. * .
. . .
\"";
test_parse
"[ | Q:1 {} | ] \"
. P .
P * .
. . .
\"";
);
"parse more rels" >::
(fun () ->
test_parse
"[a2, b3 | D (a2, b3); P:1 {}; Q:1 {} | ] \"
. . .
. . .
. . .
\"";
test_parse
"[a2, b3 | D (a2, b3); Q:1 {} | ] \"
. P .
P . .
. . .
\"";
);
"struc from trs" >::
(fun () ->
let s = "
Load state library:/basic.
New function ''id'' {natural number} as natural number.
New variable n as natural number.
Let id n be n.
Elements from 0 to 5 at id {}, id {}.
New function ''succ'' {natural number} as a natural number list.
Let succ n be [n, n+1].
Relation R on from 0 to 4 given by succ {}.
" in
let (_, st) = struc_from_trs s in
assert_equal ~printer:(fun x -> x)
("[1, 2, 3, 4, 5, 6 | R {(1, 2); (2, 3); (3, 4); (4, 5); (5, 6)} | " ^
"x {1->0., 2->1., 3->2., 4->3., 5->4., 6->5.}; " ^
"y {1->0., 2->1., 3->2., 4->3., 5->4., 6->5.} ]")
(Aux.normalize_spaces (str (clear_names st)));
);
"sprint simple" >::
(fun () ->
test_sprint
"[a2, b3 | D (a2, b3); Q:1 {} |
] \"
. P .
P . .
. . .
\"";
test_sprint
"[c, a, b | P (b); Q {a; b}; R {(a, b); (b, b)} | f {a->2.}]"
);
"sprint long" >::
(fun () ->
test_sprint
"[c, a, b |
D {(c, c); (c, a); (c, b); (b, a); (b, b)}; P {c; b}; Q {a; b};
R {(c, c); (a, c); (a, b); (b, c); (b, b)}
| f {a->2., b->3., c->4.}; g {a->1., c->5.}
]"
);
"compare_diff" >::
(fun () ->
assert_equal ~printer:(fun (_,x)->x)
(true, "equal")
(compare_diff
(struc_of_string "[a,b | R(a,b) | ]")
(struc_of_string "[a,b | R(a,b) | ]"));
assert_equal ~printer:(fun (_,x)->x)
(true, "equal")
(compare_diff
(struc_of_string "[a,b,c | R(a,b) | f {a->2.}; g{b->3.} ]")
(struc_of_string "[a,b,c | R(a,b) | f {a->2.}; g{b->3.} ]"));
assert_equal ~printer:(fun (_,x)->x)
(false, "Relation tuple R(a, b) not found in the second structure")
(compare_diff
(struc_of_string "[a,b | R(a,b) | ]")
(struc_of_string "[a,b | R(b,a) | ]"));
assert_equal ~printer:(fun (_,x)->x)
(false, "Function g(b)->3. is 4. instead in the second structure")
(compare_diff
(struc_of_string "[a,b,c | R(a,b) | f {a->2.}; g{b->3.} ]")
(struc_of_string "[a,b,c | R(a,b) | f {a->2.}; g{b->4.} ]"));
assert_equal ~printer:(fun (_,x)->x)
(false, "Relation R not found in the second structure")
(compare_diff
(struc_of_string "[a,b | R(a,b) | ]")
(struc_of_string "[a,b | C(a,b) | ]"));
assert_equal ~printer:(fun (_,x)->x)
(false, "Element b not found in the second structure")
(compare_diff
(struc_of_string "[a,b | P(a) | ]")
(struc_of_string "[a | P(a) | ]"));
assert_equal ~printer:(fun (_,x)->x)
(false, "Relation C not found in the first structure")
(compare_diff
(struc_of_string "[a,b | R(a,b) | ]")
(struc_of_string "[a,b | R(a,b); C(a,b) | ]"));
assert_equal ~printer:(fun (_,x)->x)
(false, "Relation tuple R(a, c) not found in the first structure")
(compare_diff
(struc_of_string "[a,b,c | R(a,b) | ]")
(struc_of_string "[a,b,c | R{(a,b); (a,c)} | ]"));
);
]