File: test_lists.adb

package info (click to toggle)
libaunit 1.03-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 324 kB
  • ctags: 8
  • sloc: ada: 1,552; makefile: 71
file content (387 lines) | stat: -rw-r--r-- 11,313 bytes parent folder | download | duplicates (5)
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
with AUnit.Test_Cases.Registration;
use AUnit.Test_Cases.Registration;

with AUnit.Assertions; use AUnit.Assertions;

with AUnit.Lists;
package body Test_Lists is

   package Integer_Lists is new AUnit.Lists (Integer);
   use Integer_Lists;

   L : List;

   procedure Set_Up (T : in out Test_Case) is
   begin
      if not Before (L) then
         Back (L);
      end if;
   end Set_Up;

   procedure Tear_Down (T : in out Test_Case) is
   begin
      Wipe_Out (L);
   end Tear_Down;


   -- Test Routines:

   procedure Test_Creation (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Assert (Before (L), "Cursor not properly set on initialization");
      Assert (Empty (L), "Initial list not empty");
   end Test_Creation;

   procedure Test_Back (T : in out AUnit.Test_Cases.Test_Case'Class) is
      I : Natural;
   begin
      Extend (L, 1);
      Extend (L, 2);

      Finish (L);
      Assert (Is_Last (L), "Finish did not put cursor at end of list");

      I := Index (L);
      Back (L);
      Assert
        (Index (L) = I - 1,
          "Cursor not moved backwards: expected " &
          Integer'Image (I - 1) & ", got " & Integer'Image (Index (L)));
   end Test_Back;

   procedure Test_Finish (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Finish (L);
      Assert (Before (L), "Finish on empty list /= Before");

      Extend (L, 1);
      Finish (L);
      Assert
        (Is_Last (L),
         "Finish failed to place cursor on last element of the list");
   end Test_Finish;


   procedure Test_Forth (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Extend (L, 1);
      Extend (L, 2);
      Forth (L);
      Assert
        (Index (L) = 1,
         "Forth failed to advance cursor: expected " &
         Integer'Image (1) &
         " got " &
         Integer'Image (Index (L)));
      Forth (L);
      Assert
        (Index (L) = 2,
         "Forth failed to advance cursor: expected " &
         Integer'Image (2) &
         " got " &
         Integer'Image (Index (L)));
   end Test_Forth;

   procedure Test_Go_I_Th (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Extend (L, 1);
      Extend (L, 2);
      Extend (L, 3);

      for I in 0 .. Count (L) + 1 loop
         Go_I_Th (L, I);
         Assert
           (Index (L) = I,
            "Go_I_Th failed to place cursor correctly: expected " &
            Integer'Image (I) &
            " got " &
            Integer'Image (Index (L)));
      end loop;
   end Test_Go_I_Th;

   procedure Test_Move (T : in out AUnit.Test_Cases.Test_Case'Class) is
      I : Natural;
   begin
      Extend (L, 1);
      Extend (L, 2);
      Extend (L, 3);

      Move (L, 4);
      Assert (Off (L), "Move beyond end of list did not result in Off");

      Move (L, -4);
      Assert (Off (L), "Move before beginning of list did not result in Off");

      Start (L);
      I := Index (L);
      Move (L, 2);
      Assert (not Off (L), "Test written incorrectly: expected not Off");
      Assert
        (Index (L) = I + 2,
         "Move failed to position cursor: expected " &
         Integer'Image (I + 2) &
         " got " &
         Integer'Image (Index (L)));

      Finish (L);
      I := Index (L);
      Move (L, -2);
      Assert (not Off (L), "Test written incorrectly: expected not Off");
      Assert
        (Index (L) = I + (-2),
         "Move failed to position cursor: expected " &
         Integer'Image (I + (-2)) &
         " got " &
         Integer'Image (Index (L)));

      Start (L);
      Move (L, -1);
      Assert
        (Before (L), "Move prior to first element failed to indicate Before");

      Finish (L);
      Move (L, 1);
      Assert
        (After (L), "Move beyond last element failed to indicate After");
   end Test_Move;

   procedure Test_Start (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Start (L);
      Assert (After (L), "Start on empty list failed to indicate After");

      Extend (L, 1);
      Extend (L, 2);
      Finish (L);
      Start (L);
      Assert
        (Is_First (L), "Start on non-empty list failed to indicate Is_First");
   end Test_Start;

   procedure Test_Put_Front (T : in out AUnit.Test_Cases.Test_Case'Class) is
      Old_Count : Natural := 0;
   begin
      Put_Front (L, 1);
      Assert
        (Count (L) = Old_Count + 1,
         "Put_Front failed to increment count on initial list");
      Assert
        (First (L) = 1,
         "Put_Front inserted element incorrectly on empty list");

      Old_Count := Count (L);
      Put_Front (L, 2);
      Assert
        (Count (L) = Old_Count + 1,
         "Put_Front failed to increment count on non-empty list");
      Assert
        (First (L) = 2,
         "Put_Front inserted element incorrectly on non-empty list");
   end Test_Put_Front;


   procedure Test_Put_Left (T : in out AUnit.Test_Cases.Test_Case'Class) is
      Old_Count, Old_Index : Natural;
   begin
      Extend (L, 1);
      Start (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Put_Left (L, 2);
      Assert
        (Count (L) = Old_Count + 1,
         "Put_Left failed to increment Count for single element list");
      Assert
        (Index (L) = Old_Index + 1,
         "Put_Left failed to adjust index for single element list");

      Finish (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Put_Left (L, 3);
      Assert
        (Count (L) = Old_Count + 1,
         "Put_Left failed to increment Count for multi-element list");
      Assert
        (Index (L) = Old_Index + 1,
         "Put_Left failed to adjust index for multi-element list");
   end Test_Put_Left;

   procedure Test_Put_Right (T : in out AUnit.Test_Cases.Test_Case'Class) is
      Old_Count, Old_Index : Natural;
   begin
      Extend (L, 1);
      Start (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Put_Right (L, 2);
      Assert
        (Count (L) = Old_Count + 1,
         "Put_Right failed to increment Count for single element list");
      Assert
        (Index (L) = Old_Index,
         "Put_Right failed to maintain index for single element list");

      Start (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Put_Right (L, 3);
      Assert
        (Count (L) = Old_Count + 1,
         "Put_Right failed to increment Count for multi-element list");
      Assert
        (Index (L) = Old_Index,
         "Put_Right failed to maintain index for multi-element list");
   end Test_Put_Right;

   procedure Test_Replace (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Extend (L, 1);
      Start (L);
      Replace (L, 2);
      Assert (Item (L) = 2, "Replace failed for one element list");

      Extend (L, 1);
      Finish (L);
      Replace (L, 3);
      Assert (Item (L) = 3, "Replace failed at end of list");

      Extend (L, 1);
      Start (L);
      Forth (L);
      Replace (L, 4);
      Assert (Item (L) = 4, "Replace failed in middle of list");
   end Test_Replace;

   procedure Test_Remove (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      Extend (L, 1);
      Extend (L, 2);
      Extend (L, 3);

      Start (L);
      Forth (L);
      Remove (L);
      Assert
        (Count (L) = 2,
         "Remove failed to adjust Count when removing from middle of list");

      Finish (L);
      Remove (L);
      Assert
        (Count (L) = 1,
         "Remove failed to adjust Count when removing from end of list");

      Start (L);
      Remove (L);
      Assert (Empty (L), "Removal of last element failed to empty list");
      Assert (After (L), "Removal of last element failed to indicate After");
   end Test_Remove;

   procedure Test_Remove_Left (T : in out AUnit.Test_Cases.Test_Case'Class) is
      Old_Count, Old_Index : Natural;
   begin
      Extend (L, 1);
      Extend (L, 2);
      Extend (L, 3);
      Extend (L, 4);

      Finish (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Remove_Left (L);
      Assert
        (Count (L) = Old_Count - 1,
         "Remove_Left failed to adjust Count when removing before last element");
      Assert
        (Index (L) = Old_Index - 1,
         "Remove_Left failed to adjust Index when removing before last element");

      Start (L);
      Forth (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Remove_Left (L);
      Assert
        (Count (L) = Old_Count - 1,
         "Remove_Left failed to adjust Count when removing first element");
      Assert
        (Index (L) = Old_Index - 1,
         "Remove_Left failed to adjust Index when removing first element");
   end Test_Remove_Left;

   procedure Test_Remove_Right (T : in out AUnit.Test_Cases.Test_Case'Class) is
      Old_Count, Old_Index : Natural;
   begin
      Extend (L, 1);
      Extend (L, 2);
      Extend (L, 3);
      Extend (L, 4);

      Start (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Remove_Right (L);
      Assert
        (Count (L) = Old_Count - 1,
         "Remove_Right failed to adjust Count when removing after first element");
      Assert
        (Index (L) = Old_Index,
         "Remove_Right failed to maintain Index when removing after first element");

      Finish (L);
      Back (L);
      Old_Count := Count (L);
      Old_Index := Index (L);
      Remove_Right (L);
      Assert
        (Count (L) = Old_Count - 1,
         "Remove_Right failed to adjust Count when removing last element");
      Assert
        (Index (L) = Old_Index,
         "Remove_Right failed to maintain Index when removing last element");
   end Test_Remove_Right;

   procedure Test_Wipe_Out (T : in out AUnit.Test_Cases.Test_Case'Class) is
   begin
      for I in 1 .. 10 loop
         Extend (L, I);
      end loop;

      Wipe_Out (L);
      Assert (Empty (L), "Wipe_Out failed to empty list");

      Wipe_Out (L);
   exception
      when others =>
         Assert (False, "Wipe_Out fails when called on empty list");
   end Test_Wipe_Out;

   --  Register test routines to call:
   procedure Register_Tests (T : in out Test_Case) is
   begin
      --  Repeat for each test routine.
      Register_Routine (T, Test_Creation'Access, "Test Creation");
      Register_Routine (T, Test_Back'Access, "Test Back");
      Register_Routine (T, Test_Finish'Access, "Test Finish");
      Register_Routine (T, Test_Forth'Access, "Test Forth");
      Register_Routine (T, Test_Go_I_Th'Access, "Test Go_I_Th");
      Register_Routine (T, Test_Move'Access, "Test Move");
      Register_Routine (T, Test_Start'Access, "Test Start");
      Register_Routine (T, Test_Put_Front'Access, "Test Put_Front");
      Register_Routine (T, Test_Put_Left'Access, "Test Put_Left");
      Register_Routine (T, Test_Put_Right'Access, "Test Put_Right");
      Register_Routine (T, Test_Replace'Access, "Test Replace");
      Register_Routine (T, Test_Remove'Access, "Test Remove");
      Register_Routine (T, Test_Remove_Left'Access, "Test Remove_Left");
      Register_Routine (T, Test_Remove_Right'Access, "Test Remove_Right");
      Register_Routine (T, Test_Wipe_Out'Access, "Test Wipe_Out");
   end Register_Tests;

   --  Identifier of test case:
   function Name (T : Test_Case) return String_Access is
   begin
      return  new String'("Test_Lists");
   end Name;

end Test_Lists;