Natools

Check-in [702a3d8ebc]
Login
Overview
Comment:constant_indefinite_ordered_map_tests: add tests for the new indexing and iterator features
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 702a3d8ebc6fd36a4284aee978c30ecc9e2d853a
User & Date: nat on 2014-11-29 18:23:15
Other Links: manifest | tags
Context
2014-12-02
22:07
s_expressions: add usual lexicographical comparison operator on Atoms, so that clients don't have to explicitly use Ada.Streams."<" check-in: 62ca3fd9c9 user: nat tags: trunk
2014-11-29
18:23
constant_indefinite_ordered_map_tests: add tests for the new indexing and iterator features check-in: 702a3d8ebc user: nat tags: trunk
2014-11-28
22:37
constant_indefinite_ordered_maps: add Ada 2012 iterator and indexing interfaces check-in: 327414fdc7 user: nat tags: trunk
Changes

Modified tests/natools-constant_indefinite_ordered_map_tests.adb from [b7057b0d58] to [e3dd7affd1].

24
25
26
27
28
29
30


31
32
33
34
35
36
37
     Constant_Indefinite_Ordered_Maps
        (Key_Type     => String,
         Element_Type => Integer);


   function Image (Map : Test_Maps.Unsafe_Maps.Map) return String;



   function Sample_Map return Test_Maps.Unsafe_Maps.Map;

   function Sample_Map return Test_Maps.Updatable_Map
     is (Test_Maps.Create (Sample_Map));


   ------------------------------







>
>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
     Constant_Indefinite_Ordered_Maps
        (Key_Type     => String,
         Element_Type => Integer);


   function Image (Map : Test_Maps.Unsafe_Maps.Map) return String;

   function Image (Map : Test_Maps.Updatable_Map) return String;

   function Sample_Map return Test_Maps.Unsafe_Maps.Map;

   function Sample_Map return Test_Maps.Updatable_Map
     is (Test_Maps.Create (Sample_Map));


   ------------------------------
61
62
63
64
65
66
67






68
69
70
71
72
73
74
   begin
      Append (Result, "(");
      Map.Iterate (Process'Access);
      Append (Result, ")");
      return To_String (Result);
   end Image;








   function Sample_Map return Test_Maps.Unsafe_Maps.Map is
      Result : Test_Maps.Unsafe_Maps.Map;
   begin
      for I in 0 .. 9 loop
         Result.Insert
           ((1 => '1',







>
>
>
>
>
>







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
   begin
      Append (Result, "(");
      Map.Iterate (Process'Access);
      Append (Result, ")");
      return To_String (Result);
   end Image;


   function Image (Map : Test_Maps.Updatable_Map) return String is
   begin
      return Image (Map.To_Unsafe_Map);
   end Image;


   function Sample_Map return Test_Maps.Unsafe_Maps.Map is
      Result : Test_Maps.Unsafe_Maps.Map;
   begin
      for I in 0 .. 9 loop
         Result.Insert
           ((1 => '1',
94
95
96
97
98
99
100



101
102
103
104
105
106
107
















































































































































































































108
109
110
111
112
113
114
      Consistency (Report);
      Cursor_Operations (Report);
      Direct_Access (Report);
      Empty_Map (Report);
      Iterations (Report);
      Map_Updates (Report);
      Unsafe_Map_Roundtrip (Report);



   end All_Tests;



   ----------------------
   -- Individual Tests --
   ----------------------

















































































































































































































   procedure Consistency (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Consistency checks");
   begin
      if Test_Maps.Has_Element (Test_Maps.No_Element) then
         Test.Fail ("No_Element has an element");
      end if;







>
>
>







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
      Consistency (Report);
      Cursor_Operations (Report);
      Direct_Access (Report);
      Empty_Map (Report);
      Iterations (Report);
      Map_Updates (Report);
      Unsafe_Map_Roundtrip (Report);
      Ada_2012_Indexing (Report);
      Ada_2012_Iteration (Report);
      Ada_2012_Errors (Report);
   end All_Tests;



   ----------------------
   -- Individual Tests --
   ----------------------

   procedure Ada_2012_Errors (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Errors in Ada 2012 extensions");
   begin
      declare
         Map : Test_Maps.Updatable_Map := Sample_Map;
         Fixed_Map : constant Test_Maps.Updatable_Map := Sample_Map;
         Empty_Map : constant Test_Maps.Updatable_Map
           := Test_Maps.Create (Test_Maps.Unsafe_Maps.Empty_Map);
         I : Integer;
      begin
         for Position in Empty_Map.Iterate loop
            Test.Fail ("Found element in empty map:");
            Test.Info ("  (" & Test_Maps.Key (Position)
              & " ->" & Integer'Image (Test_Maps.Element (Position)) & ')');
         end loop;

         for Position in reverse Empty_Map.Iterate loop
            Test.Fail ("Found element in reverse empty map:");
            Test.Info ("  (" & Test_Maps.Key (Position)
              & " ->" & Integer'Image (Test_Maps.Element (Position)) & ')');
         end loop;

         begin
            I := Fixed_Map ("#1");
            Test.Fail ("Found value " & Integer'Image (I) & " for key ""#1""");
         exception
            when Constraint_Error => null;
            when Error : others =>
               Test.Fail ("Unexpected exception for value ""#1""");
               Test.Report_Exception (Error, NT.Fail);
         end;

         begin
            I := Fixed_Map (Fixed_Map.Find ("#2"));
            Test.Fail ("Found value " & Integer'Image (I) & " for key ""#2""");
         exception
            when Constraint_Error => null;
            when Error : others =>
               Test.Fail ("Unexpected exception with value for ""#2""");
               Test.Report_Exception (Error, NT.Fail);
         end;

         begin
            I := Fixed_Map (Map.Find ("20"));
            Test.Fail ("Found value " & Integer'Image (I)
              & " for key ""20"" in foreign map");
         exception
            when Program_Error => null;
            when Error : others =>
               Test.Fail ("Unexpected exception with for foreign cursor");
               Test.Report_Exception (Error, NT.Fail);
         end;

         begin
            Map ("#3") := 93;
            Test.Fail ("Found node for key ""#3""");
         exception
            when Constraint_Error => null;
            when Error : others =>
               Test.Fail ("Unexpected exception for value ""#3""");
               Test.Report_Exception (Error, NT.Fail);
         end;

         begin
            Fixed_Map (Fixed_Map.Find ("#4")) := 94;
            Test.Fail ("Found node for key ""#4""");
         exception
            when Constraint_Error => null;
            when Error : others =>
               Test.Fail ("Unexpected exception with node for ""#4""");
               Test.Report_Exception (Error, NT.Fail);
         end;

         begin
            Map (Fixed_Map.Find ("20")) := 95;
            Test.Fail ("Found node for key ""20"" in foreign map");
         exception
            when Program_Error => null;
            when Error : others =>
               Test.Fail ("Unexpected exception with node for foreign cursor");
               Test.Report_Exception (Error, NT.Fail);
         end;
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end Ada_2012_Errors;


   procedure Ada_2012_Indexing (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Ada 2012 user-defined indexing");

      procedure Test_Constant (Map : in Test_Maps.Updatable_Map);

      procedure Test_Constant (Map : in Test_Maps.Updatable_Map) is
         I : Integer;
      begin
         I := Map ("25");
         if I /= 25 then
            Test.Fail ("Unexpacted value" & Integer'Image (I)
              & " for Map (""25"")");
         end if;

         I := Map (Map.Find ("12"));
         if I /= 12 then
            Test.Fail ("Unexpacted value" & Integer'Image (I)
              & " for Map (""12"")");
         end if;
      end Test_Constant;
   begin
      declare
         Map : Test_Maps.Updatable_Map := Sample_Map;
         I : Integer;
      begin
         I := Map ("15");
         if I /= 15 then
            Test.Fail ("Unexpacted value" & Integer'Image (I)
              & " for Map (""15"")");
         end if;

         Map ("23") := 2;
         I := Map.Element ("23");
         if I /= 2 then
            Test.Fail ("Unexpected value" & Integer'Image (I)
              & " for updated Map (""23"")");
            Test.Info ("Full map: " & Image (Map));
         end if;

         Test_Constant (Map);
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end Ada_2012_Indexing;


   procedure Ada_2012_Iteration (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Ada 2012 user-defined iteration");
   begin
      declare
         Map : Test_Maps.Updatable_Map := Sample_Map;
         Expected, Direction : Integer;
         Abort_Loop : Boolean := False;

         procedure Test_Element (Element : in Integer);

         procedure Test_Element (Element : in Integer) is
         begin
            if Expected /= Element then
               Test.Fail ("Got element" & Integer'Image (Element)
                 & ", expected" & Integer'Image (Expected));
               Test.Info ("Current map: " & Image (Map));
               Abort_Loop := True;
            end if;

            Expected := Expected + Direction;
         end Test_Element;
      begin
         Direction := 1;
         Expected := 10;
         Abort_Loop := False;
         for Element of Map loop
            Test_Element (Element);
            exit when Abort_Loop;
         end loop;

         Direction := -1;
         Expected := 29;
         Abort_Loop := False;
         for Element of reverse Map loop
            Test_Element (Element);
            exit when Abort_Loop;
         end loop;

         Expected := 59;
         Direction := -1;
         for Element of Map loop
            Element := Expected;
            Expected := Expected + Direction;
         end loop;

         Direction := 1;
         Expected := 40;
         Abort_Loop := False;
         for Element of reverse Map loop
            Test_Element (Element);
            exit when Abort_Loop;
         end loop;

         Direction := 1;
         Expected := 50;
         Abort_Loop := False;
         for Position in reverse Map.Iterate (Map.Find ("19")) loop
            Test_Element (Test_Maps.Element (Position));
            exit when Abort_Loop;
         end loop;

         Direction := -1;
         Expected := 50;
         Abort_Loop := False;
         for Position in Map.Iterate (Map.Find ("19")) loop
            Test_Element (Test_Maps.Element (Position));
            exit when Abort_Loop;
         end loop;
      end;
   exception
      when Error : others => Test.Report_Exception (Error);
   end Ada_2012_Iteration;


   procedure Consistency (Report : in out NT.Reporter'Class) is
      Test : NT.Test := Report.Item ("Consistency checks");
   begin
      if Test_Maps.Has_Element (Test_Maps.No_Element) then
         Test.Fail ("No_Element has an element");
      end if;

Modified tests/natools-constant_indefinite_ordered_map_tests.ads from [3835e3fb87] to [18ef90ce9e].

19
20
21
22
23
24
25



26
27
28
29
30
31
32
33
34
package Natools.Constant_Indefinite_Ordered_Map_Tests is
   pragma Preelaborate;

   package NT renames Natools.Tests;

   procedure All_Tests (Report : in out NT.Reporter'Class);




   procedure Consistency (Report : in out NT.Reporter'Class);
   procedure Cursor_Operations (Report : in out NT.Reporter'Class);
   procedure Direct_Access (Report : in out NT.Reporter'Class);
   procedure Empty_Map (Report : in out NT.Reporter'Class);
   procedure Iterations (Report : in out NT.Reporter'Class);
   procedure Map_Updates (Report : in out NT.Reporter'Class);
   procedure Unsafe_Map_Roundtrip (Report : in out NT.Reporter'Class);

end Natools.Constant_Indefinite_Ordered_Map_Tests;







>
>
>









19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package Natools.Constant_Indefinite_Ordered_Map_Tests is
   pragma Preelaborate;

   package NT renames Natools.Tests;

   procedure All_Tests (Report : in out NT.Reporter'Class);

   procedure Ada_2012_Errors (Report : in out NT.Reporter'Class);
   procedure Ada_2012_Indexing (Report : in out NT.Reporter'Class);
   procedure Ada_2012_Iteration (Report : in out NT.Reporter'Class);
   procedure Consistency (Report : in out NT.Reporter'Class);
   procedure Cursor_Operations (Report : in out NT.Reporter'Class);
   procedure Direct_Access (Report : in out NT.Reporter'Class);
   procedure Empty_Map (Report : in out NT.Reporter'Class);
   procedure Iterations (Report : in out NT.Reporter'Class);
   procedure Map_Updates (Report : in out NT.Reporter'Class);
   procedure Unsafe_Map_Roundtrip (Report : in out NT.Reporter'Class);

end Natools.Constant_Indefinite_Ordered_Map_Tests;