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
|
-- { dg-do run }
-- { dg-options "-gnata" }
with Ada.Strings.Hash;
with Ada.Containers.Hashed_Sets;
with Ada.Containers.Hashed_Maps;
with Ada.Containers.Indefinite_Hashed_Sets;
with Ada.Containers.Indefinite_Hashed_Maps;
procedure Containers2 is
-- Check that Cursors of the hashed containers follow the correct
-- predefined equality rules - that two Cursors to the same element
-- are equal, one one is obtained through, for example, iteration,
-- and the other is obtained through a search
subtype Definite_Name is String (1 .. 5);
type Named_Item is
record
Name : Definite_Name;
Item : Integer := 0;
end record;
function Equivalent_Item (Left, Right: Named_Item) return Boolean
is (Left.Name = Right.Name);
function DI_Hash (Item: Named_Item) return Ada.Containers.Hash_Type
is (Ada.Strings.Hash (Item.Name));
package HS is new Ada.Containers.Hashed_Sets
(Element_Type => Named_Item,
Hash => DI_Hash,
Equivalent_Elements => Equivalent_Item);
package IHS is new Ada.Containers.Indefinite_Hashed_Sets
(Element_Type => Named_Item,
Hash => DI_Hash,
Equivalent_Elements => Equivalent_Item);
package HM is new Ada.Containers.Hashed_Maps
(Key_Type => Definite_Name,
Element_Type => Integer,
Hash => Ada.Strings.Hash,
Equivalent_Keys => "=");
package IHM is new Ada.Containers.Indefinite_Hashed_Maps
(Key_Type => Definite_Name,
Element_Type => Integer,
Hash => Ada.Strings.Hash,
Equivalent_Keys => "=");
Item_Data : constant array (1 .. 5) of Named_Item
:= ((Name => "ABCDE", others => <>),
(Name => "FGHIJ", others => <>),
(Name => "KLMNO", others => <>),
(Name => "PQRST", others => <>),
(Name => "UVWXY", others => <>));
use type HS.Cursor;
use type IHS.Cursor;
use type HM.Cursor;
use type IHM.Cursor;
type HS_Cursor_Vec is array (Item_Data'Range) of HS.Cursor;
type IHS_Cursor_Vec is array (Item_Data'Range) of IHS.Cursor;
type HM_Cursor_Vec is array (Item_Data'Range) of HM.Cursor;
type IHM_Cursor_Vec is array (Item_Data'Range) of IHM.Cursor;
HSC : HS.Set;
IHSC : IHS.Set;
HMC : HM.Map;
IHMC : IHM.Map;
HS_Create_Cursors : HS_Cursor_Vec;
IHS_Create_Cursors : IHS_Cursor_Vec;
HM_Create_Cursors : HM_Cursor_Vec;
IHM_Create_Cursors : IHM_Cursor_Vec;
HS_Index : HS.Cursor;
IHS_Index : IHS.Cursor;
HM_Index : HM.Cursor;
IHM_Index : IHM.Cursor;
HS_Find : HS.Cursor;
IHS_Find : IHS.Cursor;
HM_Find : HM.Cursor;
IHM_Find : IHM.Cursor;
Inserted : Boolean;
begin
for I in Item_Data'Range loop
HSC.Insert (New_Item => Item_Data(I),
Position => HS_Create_Cursors(I),
Inserted => Inserted);
pragma Assert (Inserted);
IHSC.Insert (New_Item => Item_Data(I),
Position => IHS_Create_Cursors(I),
Inserted => Inserted);
pragma Assert (Inserted);
HMC.Insert (New_Item => Item_Data(I).Item,
Key => Item_Data(I).Name,
Position => HM_Create_Cursors(I),
Inserted => Inserted);
pragma Assert (Inserted);
IHMC.Insert (New_Item => Item_Data(I).Item,
Key => Item_Data(I).Name,
Position => IHM_Create_Cursors(I),
Inserted => Inserted);
pragma Assert (Inserted);
end loop;
HS_Index := HSC.First;
IHS_Index := IHSC.First;
HM_Index := HMC.First;
IHM_Index := IHMC.First;
for I in Item_Data'Range loop
pragma Assert (HS.Has_Element (HS_Index));
pragma Assert (IHS.Has_Element (IHS_Index));
pragma Assert (HM.Has_Element (HM_Index));
pragma Assert (IHM.Has_Element (IHM_Index));
HS_Find := HSC.Find (Item_Data(I));
pragma Assert (HS_Create_Cursors(I) = HS_Index);
pragma Assert (HS_Find = HS_Index);
IHS_Find := IHSC.Find (Item_Data(I));
pragma Assert (IHS_Create_Cursors(I) = IHS_Index);
pragma Assert (IHS_Find = IHS_Index);
HM_Find := HMC.Find (Item_Data(I).Name);
pragma Assert (HM_Create_Cursors(I) = HM_Index);
pragma Assert (HM_Find = HM_Index);
IHM_Find := IHMC.Find (Item_Data(I).Name);
pragma Assert (IHM_Create_Cursors(I) = IHM_Index);
pragma Assert (IHM_Find = IHM_Index);
HS.Next (HS_Index);
IHS.Next (IHS_Index);
HM.Next (HM_Index);
IHM.Next (IHM_Index);
end loop;
end;
|