3 with Ada
.Text_IO
; use Ada
.Text_IO
;
5 with GNAT
.Sets
; use GNAT
.Sets
;
8 function Hash
(Key
: Integer) return Bucket_Range_Type
;
10 package Integer_Sets
is new Membership_Sets
11 (Element_Type
=> Integer,
21 -- Ensure that none of the elements in the range Low_Elem .. High_Elem are
22 -- present in set S, and that the set's length is 0.
24 procedure Check_Locked_Mutations
26 S
: in out Membership_Set
);
27 -- Ensure that all mutation operations of set S are locked
29 procedure Check_Present
34 -- Ensure that all elements in the range Low_Elem .. High_Elem are present
37 procedure Check_Unlocked_Mutations
39 S
: in out Membership_Set
);
40 -- Ensure that all mutation operations of set S are unlocked
46 -- Add elements in the range Low_Elem .. High_Elem in set S
48 procedure Test_Contains
51 Init_Size
: Positive);
52 -- Verify that Contains properly identifies that elements in the range
53 -- Low_Elem .. High_Elem are within a set. Init_Size denotes the initial
56 procedure Test_Create
;
57 -- Verify that all set operations fail on a non-created set
62 Init_Size
: Positive);
63 -- Verify that Delete properly removes elements in the range Low_Elem ..
64 -- High_Elem from a set. Init_Size denotes the initial size of the set.
66 procedure Test_Is_Empty
;
67 -- Verify that Is_Empty properly returns this status of a set
69 procedure Test_Iterate
;
70 -- Verify that iterators properly manipulate mutation operations
72 procedure Test_Iterate_Empty
;
73 -- Verify that iterators properly manipulate mutation operations of an
76 procedure Test_Iterate_Forced
79 Init_Size
: Positive);
80 -- Verify that an iterator that is forcefully advanced by Next properly
81 -- unlocks the mutation operations of a set. Init_Size denotes the initial
85 -- Verify that Size returns the correct size of a set
97 Siz
: constant Natural := Size
(S
);
100 for Elem
in Low_Elem
.. High_Elem
loop
101 if Contains
(S
, Elem
) then
102 Put_Line
("ERROR: " & Caller
& ": extra element" & Elem
'Img);
107 Put_Line
("ERROR: " & Caller
& ": wrong size");
108 Put_Line
("expected: 0");
109 Put_Line
("got :" & Siz
'Img);
113 ----------------------------
114 -- Check_Locked_Mutations --
115 ----------------------------
117 procedure Check_Locked_Mutations
119 S
: in out Membership_Set
)
124 Put_Line
("ERROR: " & Caller
& ": Delete: no exception raised");
129 Put_Line
("ERROR: " & Caller
& ": Delete: unexpected exception");
134 Put_Line
("ERROR: " & Caller
& ": Destroy: no exception raised");
139 Put_Line
("ERROR: " & Caller
& ": Destroy: unexpected exception");
144 Put_Line
("ERROR: " & Caller
& ": Insert: no exception raised");
149 Put_Line
("ERROR: " & Caller
& ": Insert: unexpected exception");
151 end Check_Locked_Mutations
;
157 procedure Check_Present
168 for Exp_Elem
in Low_Elem
.. High_Elem
loop
171 if Elem
/= Exp_Elem
then
172 Put_Line
("ERROR: " & Caller
& ": Check_Present: wrong element");
173 Put_Line
("expected:" & Exp_Elem
'Img);
174 Put_Line
("got :" & Elem
'Img);
178 -- At this point all elements should have been accounted for. Check for
181 while Has_Next
(Iter
) loop
184 ("ERROR: " & Caller
& ": Check_Present: extra element" & Elem
'Img);
188 when Iterator_Exhausted
=>
192 & "Check_Present: incorrect number of elements");
195 ------------------------------
196 -- Check_Unlocked_Mutations --
197 ------------------------------
199 procedure Check_Unlocked_Mutations
201 S
: in out Membership_Set
)
206 end Check_Unlocked_Mutations
;
212 function Hash
(Key
: Integer) return Bucket_Range_Type
is
214 return Bucket_Range_Type
(Key
);
227 for Elem
in Low_Elem
.. High_Elem
loop
236 procedure Test_Contains
239 Init_Size
: Positive)
241 Low_Bogus
: constant Integer := Low_Elem
- 1;
242 High_Bogus
: constant Integer := High_Elem
+ 1;
244 S
: Membership_Set
:= Create
(Init_Size
);
247 Populate
(S
, Low_Elem
, High_Elem
);
249 -- Ensure that the elements are contained in the set
251 for Elem
in Low_Elem
.. High_Elem
loop
252 if not Contains
(S
, Elem
) then
254 ("ERROR: Test_Contains: element" & Elem
'Img & " not in set");
258 -- Ensure that arbitrary elements which were not inserted in the set are
259 -- not contained in the set.
261 if Contains
(S
, Low_Bogus
) then
263 ("ERROR: Test_Contains: element" & Low_Bogus
'Img & " in set");
266 if Contains
(S
, High_Bogus
) then
268 ("ERROR: Test_Contains: element" & High_Bogus
'Img & " in set");
278 procedure Test_Create
is
285 -- Ensure that every routine defined in the API fails on a set which
286 -- has not been created yet.
289 Flag
:= Contains
(S
, 1);
290 Put_Line
("ERROR: Test_Create: Contains: no exception raised");
295 Put_Line
("ERROR: Test_Create: Contains: unexpected exception");
300 Put_Line
("ERROR: Test_Create: Delete: no exception raised");
305 Put_Line
("ERROR: Test_Create: Delete: unexpected exception");
310 Put_Line
("ERROR: Test_Create: Insert: no exception raised");
315 Put_Line
("ERROR: Test_Create: Insert: unexpected exception");
319 Flag
:= Is_Empty
(S
);
320 Put_Line
("ERROR: Test_Create: Is_Empty: no exception raised");
325 Put_Line
("ERROR: Test_Create: Is_Empty: unexpected exception");
330 Put_Line
("ERROR: Test_Create: Iterate: no exception raised");
335 Put_Line
("ERROR: Test_Create: Iterate: unexpected exception");
340 Put_Line
("ERROR: Test_Create: Size: no exception raised");
345 Put_Line
("ERROR: Test_Create: Size: unexpected exception");
353 procedure Test_Delete
356 Init_Size
: Positive)
359 S
: Membership_Set
:= Create
(Init_Size
);
362 Populate
(S
, Low_Elem
, High_Elem
);
364 -- Delete all even elements
366 for Elem
in Low_Elem
.. High_Elem
loop
367 if Elem
mod 2 = 0 then
372 -- Ensure that all remaining odd elements are present in the set
374 for Elem
in Low_Elem
.. High_Elem
loop
375 if Elem
mod 2 /= 0 and then not Contains
(S
, Elem
) then
376 Put_Line
("ERROR: Test_Delete: missing element" & Elem
'Img);
380 -- Delete all odd elements
382 for Elem
in Low_Elem
.. High_Elem
loop
383 if Elem
mod 2 /= 0 then
388 -- At this point the set should be completely empty
391 (Caller
=> "Test_Delete",
393 Low_Elem
=> Low_Elem
,
394 High_Elem
=> High_Elem
);
403 procedure Test_Is_Empty
is
404 S
: Membership_Set
:= Create
(8);
407 if not Is_Empty
(S
) then
408 Put_Line
("ERROR: Test_Is_Empty: set is not empty");
414 Put_Line
("ERROR: Test_Is_Empty: set is empty");
419 if not Is_Empty
(S
) then
420 Put_Line
("ERROR: Test_Is_Empty: set is not empty");
430 procedure Test_Iterate
is
434 S
: Membership_Set
:= Create
(5);
439 -- Obtain an iterator. This action must lock all mutation operations of
442 Iter_1
:= Iterate
(S
);
444 -- Ensure that every mutation routine defined in the API fails on a set
445 -- with at least one outstanding iterator.
447 Check_Locked_Mutations
448 (Caller
=> "Test_Iterate",
451 -- Obtain another iterator
453 Iter_2
:= Iterate
(S
);
455 -- Ensure that every mutation is still locked
457 Check_Locked_Mutations
458 (Caller
=> "Test_Iterate",
461 -- Exhaust the first itertor
463 while Has_Next
(Iter_1
) loop
467 -- Ensure that every mutation is still locked
469 Check_Locked_Mutations
470 (Caller
=> "Test_Iterate",
473 -- Exhaust the second itertor
475 while Has_Next
(Iter_2
) loop
479 -- Ensure that all mutation operations are once again callable
481 Check_Unlocked_Mutations
482 (Caller
=> "Test_Iterate",
488 ------------------------
489 -- Test_Iterate_Empty --
490 ------------------------
492 procedure Test_Iterate_Empty
is
495 S
: Membership_Set
:= Create
(5);
498 -- Obtain an iterator. This action must lock all mutation operations of
503 -- Ensure that every mutation routine defined in the API fails on a set
504 -- with at least one outstanding iterator.
506 Check_Locked_Mutations
507 (Caller
=> "Test_Iterate_Empty",
510 -- Attempt to iterate over the elements
512 while Has_Next
(Iter
) loop
516 ("ERROR: Test_Iterate_Empty: element" & Elem
'Img & " exists");
519 -- Ensure that all mutation operations are once again callable
521 Check_Unlocked_Mutations
522 (Caller
=> "Test_Iterate_Empty",
526 end Test_Iterate_Empty
;
528 -------------------------
529 -- Test_Iterate_Forced --
530 -------------------------
532 procedure Test_Iterate_Forced
535 Init_Size
: Positive)
539 S
: Membership_Set
:= Create
(Init_Size
);
542 Populate
(S
, Low_Elem
, High_Elem
);
544 -- Obtain an iterator. This action must lock all mutation operations of
549 -- Ensure that every mutation routine defined in the API fails on a set
550 -- with at least one outstanding iterator.
552 Check_Locked_Mutations
553 (Caller
=> "Test_Iterate_Forced",
556 -- Forcibly advance the iterator until it raises an exception
559 for Guard
in Low_Elem
.. High_Elem
+ 1 loop
564 ("ERROR: Test_Iterate_Forced: Iterator_Exhausted not raised");
566 when Iterator_Exhausted
=>
569 Put_Line
("ERROR: Test_Iterate_Forced: unexpected exception");
572 -- Ensure that all mutation operations are once again callable
574 Check_Unlocked_Mutations
575 (Caller
=> "Test_Iterate_Forced",
579 end Test_Iterate_Forced
;
585 procedure Test_Size
is
586 S
: Membership_Set
:= Create
(6);
593 Put_Line
("ERROR: Test_Size: wrong size");
594 Put_Line
("expected: 0");
595 Put_Line
("got :" & Siz
'Img);
602 Put_Line
("ERROR: Test_Size: wrong size");
603 Put_Line
("expected: 2");
604 Put_Line
("got :" & Siz
'Img);
611 Put_Line
("ERROR: Test_Size: wrong size");
612 Put_Line
("expected: 6");
613 Put_Line
("got :" & Siz
'Img);
619 -- Start of processing for Operations