Natools

Check-in [c6d912c37b]
Login
Overview
Comment:string_slice_tests: full-coverage test suite for string slices
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: c6d912c37b06f95cf1b0821c74401f4545d2a6f0
User & Date: nat on 2013-09-28 19:31:24
Other Links: manifest | tags
Context
2013-09-29
14:29
chunked_str0ings-tests: add a few more tests to improve coverage check-in: 2536bbf623 user: nat tags: trunk
2013-09-28
19:31
string_slice_tests: full-coverage test suite for string slices check-in: c6d912c37b user: nat tags: trunk
2013-09-27
18:29
string_slices: new package implementing copyless substrings check-in: 0fe763f79a user: nat tags: trunk
Changes

Added tests/natools-string_slice_tests.adb version [bab2542056].



























































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
------------------------------------------------------------------------------
-- Copyright (c) 2013, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

with Ada.Exceptions;
with Natools.String_Slices;

package body Natools.String_Slice_Tests is

   Parent_String : constant String (11 .. 54)
     := "The quick brown fox jumps over the lazy dog.";

   procedure No_Fail
     (Report : in out NT.Reporter'Class;
      Name : in String;
      Slice : in String_Slices.Slice);
      --  Report lack-of-exception test failure and dump Slice

   ------------------------------
   -- Local helper subprograms --
   ------------------------------

   procedure No_Fail
     (Report : in out NT.Reporter'Class;
      Name : in String;
      Slice : in String_Slices.Slice) is
   begin
      Report.Item (Name, NT.Fail);
      Report.Info ("No exception has been raised.");
      Report.Info ("Final value: "
        & String_Slices.Image (Slice.Get_Range)
        & " """ & Slice.To_String & '"');
   end No_Fail;


   ----------------------
   -- Test collections --
   ----------------------

   procedure All_Tests (Report : in out NT.Reporter'Class) is
   begin
      Report.Section ("String_Range tests");
      Range_Tests (Report);
      Report.End_Section;

      Slice_Tests (Report);
   end All_Tests;


   procedure Range_Tests (Report : in out NT.Reporter'Class) is
   begin
      Test_Is_In (Report);
      Test_Is_Subrange (Report);
      Test_Range_Image (Report);
      Test_Set_Length (Report);
   end Range_Tests;


   procedure Slice_Tests (Report : in out NT.Reporter'Class) is
   begin
      Test_Slice_Relations (Report);
      Test_Invalid_Subslices (Report);

      Test_Conversions (Report);
      Test_Extensions (Report);
      Test_Incoming_Range (Report);
      Test_Invalid_Extensions (Report);
      Test_Null_Slice (Report);
      Test_Outgoing_Range (Report);
      Test_Subslices (Report);
   end Slice_Tests;



   ----------------------
   -- Individual tests --
   ----------------------

   procedure Test_Conversions (Report : in out NT.Reporter'Class) is
      Name : constant String := "Functions To_Slice, To_String and Export";
   begin
      declare
         S : constant String_Slices.Slice := String_Slices.To_Slice (Name);
         Str : constant String := String_Slices.To_String (S);
         Exported : String (101 .. 100 + Name'Length);
      begin
         S.Export (Exported);

         if Str /= Name or Exported /= Name then
            Report.Item (Name, NT.Fail);
            Report.Info ('"' & Str & """ instead of """ & Name & '"');
         else
            Report.Item (Name, NT.Success);
         end if;
      end;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Conversions;


   procedure Test_Extensions (Report : in out NT.Reporter'Class) is
      Name : constant String := "Slice extensions";
   begin
      declare
         use type String_Slices.Slice;

         Parent : constant String_Slices.Slice
           := String_Slices.To_Slice (Parent_String);
         Extended : constant String_Slices.Slice
           := Parent.Subslice (String_Slices.To_Range (33, 50));
         Small : String_Slices.Slice := Parent.Subslice (20, 50);
      begin
         if Small.Parent /= Parent then
            Report.Item (Name, NT.Fail);
            Report.Info ("Small.Parent /= Parent");
            return;
         end if;

         if Small.Extend (Extended.Get_Range) /= Extended then
            Report.Item (Name, NT.Fail);
            Report.Info ("Small.Extend /= Extended");
            return;
         end if;

         Small.Extend (Extended.First, Extended.Last);
         if Small /= Extended then
            Report.Item (Name, NT.Fail);
            Report.Info ("Extended Small /= Extended");
            return;
         end if;

         if String_Slices.Null_Slice.Parent /= String_Slices.Null_Slice then
            Report.Item (Name, NT.Fail);
            Report.Info ("Null_Slice.Parent /= Null_Slice");
            return;
         end if;

         if String_Slices.Null_Slice.Duplicate /= String_Slices.Null_Slice then
            Report.Item (Name, NT.Fail);
            Report.Info ("Null_Slice.Duplicate /= Null_Slice");
            return;
         end if;
      end;

      Report.Item (Name, NT.Success);
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Extensions;


   procedure Test_Incoming_Range (Report : in out NT.Reporter'Class) is
      Name : constant String := "Range conservation through To_Slice";
   begin
      declare
         use type String_Slices.String_Range;

         Biased_String : constant String (11 .. 16) := "qwerty";
         S : constant String_Slices.Slice
           := String_Slices.To_Slice (Biased_String);
      begin
         if S.Get_Range /= String_Slices.Get_Range (Biased_String) then
            Report.Item (Name, NT.Fail);
            Report.Info ("String range: "
              & String_Slices.Image (String_Slices.Get_Range (Biased_String)));
            Report.Info ("Slice range: "
              & String_Slices.Image (S.Get_Range));
         else
            Report.Item (Name, NT.Success);
         end if;
      end;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Incoming_Range;


   procedure Test_Invalid_Extensions (Report : in out NT.Reporter'Class) is
      Name : constant String := "Invalid extensions";
   begin
      declare
         Parent : constant String_Slices.Slice
           := String_Slices.To_Slice (Parent_String);
         Small : String_Slices.Slice
           := Parent.Subslice (String_Slices.To_Range (30, 50));
      begin
         declare
            Extended : String_Slices.Slice;
         begin
            Extended := Small.Extend (1, 10);
            No_Fail (Report, Name, Extended);
            return;
         exception
            when Constraint_Error => null;
            when Error : others =>
               Report.Item (Name, NT.Fail);
               Report.Info ("Wrong exception "
                 & Ada.Exceptions.Exception_Name (Error)
                 & "has been raised.");
               return;
         end;

         begin
            Small.Extend (100, 150);
            No_Fail (Report, Name, Small);
            return;
         exception
            when Constraint_Error => null;
            when Error : others =>
               Report.Item (Name, NT.Fail);
               Report.Info ("Wrong exception "
                 & Ada.Exceptions.Exception_Name (Error)
                 & "has been raised.");
               return;
         end;
      end;

      Report.Item (Name, NT.Success);
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Invalid_Extensions;


   procedure Test_Invalid_Subslices (Report : in out NT.Reporter'Class) is
      Parent : constant String_Slices.Slice
        := String_Slices.To_Slice (Parent_String);
      Template : constant String_Slices.Slice := Parent.Subslice (22, 40);
      Slice : String_Slices.Slice;
   begin
      Report.Section ("Invalid subslices");

      declare
         Name : constant String := "Subslice too large";
      begin
         Slice := Template.Subslice (String_Slices.To_Range (32, 45));
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Subslice of null slice";
      begin
         Slice := String_Slices.Null_Slice.Subslice (32, 45);
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Restrict too large";
      begin
         Slice := Template;
         Slice.Restrict (String_Slices.To_Range (15, 30));
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Restrict of null slice";
         Default_Slice : String_Slices.Slice;
      begin
         Default_Slice.Restrict (15, 30);
         No_Fail (Report, Name, Default_Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Set_First too small";
      begin
         Slice := Template;
         Slice.Set_First (15);
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Set_Last too large";
      begin
         Slice := Template;
         Slice.Set_Last (45);
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Set_Length too large";
      begin
         Slice := Template;
         Slice.Set_Length (Template.Length + 5);
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      declare
         Name : constant String := "Extend beyond parent";
      begin
         Slice := Template;
         Slice.Extend (String_Slices.To_Range (1, 50));
         No_Fail (Report, Name, Slice);
      exception
         when Constraint_Error => Report.Item (Name, NT.Success);
         when Error : others =>
            Report.Item (Name, NT.Fail);
            Report.Info ("Wrong exception "
              & Ada.Exceptions.Exception_Name (Error)
              & "has been raised.");
      end;

      Report.End_Section;
   end Test_Invalid_Subslices;


   procedure Test_Is_In (Report : in out NT.Reporter'Class) is
      Name : constant String := "Function Is_In";
   begin
      declare
         Interval : constant String_Slices.String_Range := (3, 5);
         Before : constant Boolean := String_Slices.Is_In (2, Interval);
         First : constant Boolean := String_Slices.Is_In (3, Interval);
         Inside : constant Boolean := String_Slices.Is_In (5, Interval);
         Last : constant Boolean := String_Slices.Is_In (7, Interval);
         After : constant Boolean := String_Slices.Is_In (8, Interval);
      begin
         Report.Item (Name, NT.To_Result
           ((not Before) and First and Inside and Last and (not After)));

         if Before then
            Report.Info ("2 in [3, 7]");
         end if;

         if not First then
            Report.Info ("3 not in [3, 7]");
         end if;

         if not Inside then
            Report.Info ("5 not in [3, 7]");
         end if;

         if not Last then
            Report.Info ("7 not in [3, 7]");
         end if;

         if After then
            Report.Info ("8 in [3, 7]");
         end if;
      end;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Is_In;


   procedure Test_Is_Subrange (Report : in out NT.Reporter'Class) is
      procedure Check
        (Left, Right : in String_Slices.String_Range;
         Expected : in Boolean);

      Name : constant String := "Function Is_Subrange";
      Result : Boolean := True;

      procedure Check
        (Left, Right : in String_Slices.String_Range;
         Expected : in Boolean) is
      begin
         if String_Slices.Is_Subrange (Left, Right) /= Expected then
            if Result then
               Report.Item (Name, NT.Fail);
            end if;

            Report.Info ("Is_Subrange ("
              & String_Slices.Image (Left)
              & ", "
              & String_Slices.Image (Right)
              & ") should return "
              & Boolean'Image (Expected));

            Result := False;
         end if;
      end Check;
   begin
      Check ((3, 5), (1, 2), False);
      Check ((3, 5), (1, 3), False);
      Check ((3, 5), (1, 5), False);
      Check ((3, 5), (1, 7), True);
      Check ((3, 5), (1, 9), True);
      Check ((3, 5), (3, 1), False);
      Check ((3, 5), (3, 3), False);
      Check ((3, 5), (3, 5), True);
      Check ((3, 5), (3, 7), True);
      Check ((3, 5), (5, 2), False);
      Check ((3, 5), (5, 3), False);
      Check ((3, 5), (5, 5), False);
      Check ((3, 5), (7, 1), False);
      Check ((3, 5), (7, 3), False);
      Check ((3, 5), (9, 3), False);

      Check ((3, 1), (1, 2), False);
      Check ((3, 1), (1, 3), True);
      Check ((3, 1), (1, 5), True);
      Check ((3, 1), (3, 1), True);
      Check ((3, 1), (3, 3), True);
      Check ((3, 1), (5, 3), False);

      if Result then
         Report.Item (Name, NT.Success);
      end if;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Is_Subrange;


   procedure Test_Null_Slice (Report : in out NT.Reporter'Class) is
      procedure Check_Null (S : in String);

      Name : constant String := "Null slice to empty string";
      Result : Boolean := True;

      procedure Check_Null (S : in String) is
      begin
         if S /= "" then
            if Result then
               Report.Item (Name, NT.Fail);
               Result := False;
            end if;

            Report.Info ("Empty string expected, got """ & S & '"');
         end if;
      end Check_Null;
   begin
      declare
         Default_Slice : String_Slices.Slice;
      begin
         String_Slices.Null_Slice.Query (Check_Null'Access);
         Default_Slice.Query (Check_Null'Access);
         Check_Null (String_Slices.Null_Slice.To_String);
         Check_Null (Default_Slice.To_String);
      end;

      if Result then
         Report.Item (Name, NT.Success);
      end if;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Null_Slice;


   procedure Test_Outgoing_Range (Report : in out NT.Reporter'Class) is
      use type String_Slices.String_Range;

      procedure Check_Range (S : in String);

      Name : constant String
        := "Range conservation through To_String and Query";
      Ref_Range : constant String_Slices.String_Range := (10, 21);
      Result : Boolean := True;

      procedure Check_Range (S : in String) is
      begin
         if String_Slices.Get_Range (S) /= Ref_Range then
            Report.Item (Name, NT.Fail);
            Report.Info ("Queried string range: "
              & String_Slices.Image (String_Slices.Get_Range (S))
              & ", expected "
              & String_Slices.Image (Ref_Range));
            Result := False;
         end if;
      end Check_Range;
   begin
      declare
         Slice : String_Slices.Slice := String_Slices.To_Slice (Name);
      begin
         Slice.Restrict (Ref_Range);
         Slice.Query (Check_Range'Access);

         if Result then
            if String_Slices.Get_Range (Slice.To_String) /= Ref_Range then
               Report.Item (Name, NT.Fail);
               Report.Info ("To_String range: "
                 & String_Slices.Image
                     (String_Slices.Get_Range (Slice.To_String))
                 & ", expected "
                 & String_Slices.Image (Ref_Range));
            else
               Report.Item (Name, NT.Success);
            end if;
         end if;
      end;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Outgoing_Range;


   procedure Test_Range_Image (Report : in out NT.Reporter'Class) is
      procedure Check_String (Result, Reference : in String);

      Name : constant String := "Image of String_Range";
      Success : Boolean := True;

      procedure Check_String (Result, Reference : in String) is
      begin
         if Result /= Reference then
            if Success then
               Report.Item (Name, NT.Fail);
               Success := False;
            end if;

            Report.Info
              ("Result """ & Result & """, expected """ & Reference & '"');
         end if;
      end Check_String;
   begin
      declare
         Range1 : constant String_Slices.String_Range := (10, 0);
         Range2 : constant String_Slices.String_Range := (16, 5);
         Range3 : constant String_Slices.String_Range := (5, 1);
      begin
         Check_String (String_Slices.Image (Range1), "empty at 10");
         Check_String (String_Slices.Image (Range2), "[16, 20]");
         Check_String (String_Slices.Image (Range3), "[5]");
      end;

      if Success then
         Report.Item (Name, NT.Success);
      end if;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Range_Image;


   procedure Test_Set_Length (Report : in out NT.Reporter'Class) is
      Name : constant String := "Procedure Set_Length";
   begin
      declare
         R : String_Slices.String_Range := (10, 10);
      begin
         String_Slices.Set_Length (R, 20);

         if R.Length /= 20 then
            Report.Item (Name, NT.Fail);
            Report.Info ("Unexpected length" & Integer'Image (R.Length));
         else
            Report.Item (Name, NT.Success);
         end if;
      end;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Set_Length;


   procedure Test_Slice_Relations (Report : in out NT.Reporter'Class) is
      Section_Name : constant String := "Slice relations";
   begin
      declare
         Parent : constant String_Slices.Slice
           := String_Slices.To_Slice (Parent_String);
         Beginning : constant String_Slices.Slice
           := String_Slices.Subslice (Parent, String_Slices.To_Range (11, 30));
         Empty : constant String_Slices.Slice
           := String_Slices.Subslice (Parent, String_Slices.To_Range (25, 0));
         Ending : constant String_Slices.Slice
           := String_Slices.Subslice (Parent, 30, Parent.Last);
         First_Word : String_Slices.Slice;
         Dupe : constant String_Slices.Slice
           := String_Slices.Duplicate (Beginning);
      begin
         First_Word := Beginning;
         First_Word.Restrict (11, 13);

         Report.Section (Section_Name);

         declare
            Name : constant String := "Is_Subslice (Beginning, Parent)";
         begin
            Report.Item (Name, NT.To_Result
              (String_Slices.Is_Subslice (Beginning, Parent)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "Is_Subslice (First_Word, Beginning)";
         begin
            Report.Item (Name, NT.To_Result
              (String_Slices.Is_Subslice (First_Word, Beginning)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "Is_Subslice (First_Word, Parent)";
         begin
            Report.Item (Name, NT.To_Result
              (String_Slices.Is_Subslice (First_Word, Parent)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "not Is_Subslice (Beginning, Ending)";
         begin
            Report.Item (Name, NT.To_Result
              (not String_Slices.Is_Subslice (Beginning, Ending)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "Is_Related (Beginning, Ending)";
         begin
            Report.Item (Name, NT.To_Result
              (String_Slices.Is_Related (Beginning, Ending)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "not Is_Related (Beginning, Dupe)";
         begin
            Report.Item (Name, NT.To_Result
              (not String_Slices.Is_Related (Beginning, Dupe)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "Is_Empty (Empty)";
         begin
            Report.Item (Name, NT.To_Result (String_Slices.Is_Empty (Empty)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         declare
            Name : constant String := "not Is_Null (Empty)";
         begin
            Report.Item (Name, NT.To_Result
              (not String_Slices.Is_Null (Empty)));
         exception
            when Error : others => Report.Report_Exception (Name, Error);
         end;

         Report.End_Section;
      end;
   exception
      when Error : others => Report.Report_Exception (Section_Name, Error);
   end Test_Slice_Relations;


   procedure Test_Subslices (Report : in out NT.Reporter'Class) is
      procedure Report_Fail;
      procedure Check_Range
        (Slice : in String_Slices.Slice;
         First, Last : in Natural);
      procedure Check_Contents
        (Slice : in String_Slices.Slice;
         Expected : in String);
      procedure Check_Empty
        (Slice : in String_Slices.Slice;
         Name : in String);

      Name : constant String := "Subslices";
      Success : Boolean := True;

      procedure Report_Fail is
      begin
         if Success then
            Report.Item (Name, NT.Fail);
            Success := False;
         end if;
      end Report_Fail;

      procedure Check_Range
        (Slice : in String_Slices.Slice;
         First, Last : in Natural) is
      begin
         if Slice.First /= First or Slice.Last /= Last then
            Report_Fail;
            Report.Info ("Slice range "
              & String_Slices.Image (Slice.Get_Range)
              & ", expected "
              & String_Slices.Image (String_Slices.To_Range (First, Last)));
         end if;
      end Check_Range;

      procedure Check_Contents
        (Slice : in String_Slices.Slice;
         Expected : in String) is
      begin
         if Slice.To_String /= Expected then
            Report_Fail;
            Report.Info ("Slice contains """ & Slice.To_String & '"');
            Report.Info ("Expected """ & Expected & '"');
         end if;
      end Check_Contents;

      procedure Check_Empty
        (Slice : in String_Slices.Slice;
         Name : in String) is
      begin
         if Slice.Is_Null then
            Report_Fail;
            Report.Info ("Unexpected null slice after " & Name);
         elsif not Slice.Is_Empty then
            Report_Fail;
            Report.Info ("Unexpected non-empty slice after " & Name);
            Report.Info ("Contents: """ & Slice.To_String & '"');
         end if;
      end Check_Empty;
   begin
      declare
         procedure Check_Both
           (S : in String_Slices.Slice;
            First, Last : in Natural);

         procedure Check_Both
           (S : in String_Slices.Slice;
            First, Last : in Natural) is
         begin
            Check_Range (S, First, Last);
            Check_Contents (S, Parent_String (First .. Last));
         end Check_Both;

         Parent : constant String_Slices.Slice
           := String_Slices.To_Slice (Parent_String);
         Slice : String_Slices.Slice;
      begin
         Slice := Parent.Subslice (String_Slices.To_Range (22, 33));
         Check_Both (Slice, 22, 33);

         Slice.Set_First (25);
         Check_Both (Slice, 25, 33);

         Slice.Set_Last (30);
         Check_Both (Slice, 25, 30);

         Slice.Set_Length (3);
         Check_Both (Slice, 25, 27);

         Slice := Parent.Subslice (15, 0);
         Check_Empty (Slice, "Subslice");

         Slice := Parent;
         Slice.Restrict (String_Slices.To_Range (40, 0));
         Check_Empty (Slice, "Restrict");

         Slice := Parent;
         Slice.Set_First (60);
         Check_Empty (Slice, "Set_First");

         Slice := Parent;
         Slice.Set_Last (10);
         Check_Empty (Slice, "Set_Last");

         Slice := Parent.Subslice (String_Slices.To_Range (22, 33));
         Slice.Set_Length (0);
         Check_Empty (Slice, "Set_Length");
      end;

      declare
         Sub_Null : constant String_Slices.Slice
           := String_Slices.Null_Slice.Subslice (10, 0);
      begin
         Check_Range (Sub_Null, 10, 9);

         if not Sub_Null.Is_Null then
            Report_Fail;
            Report.Info ("Null_Slice.Subslice is not null: """
              & Sub_Null.To_String & '"');
         end if;
      end;

      if Success then
         Report.Item (Name, NT.Success);
      end if;
   exception
      when Error : others => Report.Report_Exception (Name, Error);
   end Test_Subslices;

end Natools.String_Slice_Tests;

Added tests/natools-string_slice_tests.ads version [963c091de4].

































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
------------------------------------------------------------------------------
-- Copyright (c) 2013, Natacha Porté                                        --
--                                                                          --
-- Permission to use, copy, modify, and distribute this software for any    --
-- purpose with or without fee is hereby granted, provided that the above   --
-- copyright notice and this permission notice appear in all copies.        --
--                                                                          --
-- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES --
-- WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         --
-- MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  --
-- ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   --
-- WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    --
-- ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  --
-- OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           --
------------------------------------------------------------------------------

------------------------------------------------------------------------------
-- Natools.String_Slice_Tests is a test suite for Natools.String_Slices.    --
------------------------------------------------------------------------------

with Natools.Tests;

package Natools.String_Slice_Tests is
   pragma Preelaborate (String_Slice_Tests);

   package NT renames Natools.Tests;

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

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

   procedure Test_Is_In (Report : in out NT.Reporter'Class);
   procedure Test_Is_Subrange (Report : in out NT.Reporter'Class);
   procedure Test_Range_Image (Report : in out NT.Reporter'Class);
   procedure Test_Set_Length (Report : in out NT.Reporter'Class);

   procedure Test_Conversions (Report : in out NT.Reporter'Class);
   procedure Test_Extensions (Report : in out NT.Reporter'Class);
   procedure Test_Incoming_Range (Report : in out NT.Reporter'Class);
   procedure Test_Invalid_Extensions (Report : in out NT.Reporter'Class);
   procedure Test_Invalid_Subslices (Report : in out NT.Reporter'Class);
   procedure Test_Null_Slice (Report : in out NT.Reporter'Class);
   procedure Test_Outgoing_Range (Report : in out NT.Reporter'Class);
   procedure Test_Slice_Relations (Report : in out NT.Reporter'Class);
   procedure Test_Subslices (Report : in out NT.Reporter'Class);

end Natools.String_Slice_Tests;

Modified tests/test_all.adb from [33dccb6b64] to [bc7229b77a].

19
20
21
22
23
24
25

26
27
28
29
30
31
32
-----------------------------------------------------------------------

with Ada.Command_Line;
with Ada.Text_IO;
with Natools.Chunked_Strings.Tests;
with Natools.Getopt_Long_Tests;
with Natools.Reference_Tests;

with Natools.Tests.Text_IO;

procedure Test_All is
   package Uneven_Chunked_Strings is new Natools.Chunked_Strings
     (Default_Allocation_Unit => 7,
      Default_Chunk_Size      => 15);
   package Uneven_Chunked_Strings_Tests is new Uneven_Chunked_Strings.Tests;







>







19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-----------------------------------------------------------------------

with Ada.Command_Line;
with Ada.Text_IO;
with Natools.Chunked_Strings.Tests;
with Natools.Getopt_Long_Tests;
with Natools.Reference_Tests;
with Natools.String_Slice_Tests;
with Natools.Tests.Text_IO;

procedure Test_All is
   package Uneven_Chunked_Strings is new Natools.Chunked_Strings
     (Default_Allocation_Unit => 7,
      Default_Chunk_Size      => 15);
   package Uneven_Chunked_Strings_Tests is new Uneven_Chunked_Strings.Tests;
61
62
63
64
65
66
67




68
69
70
71
72
73
74
   Report.Section ("Getopt_Long");
   Natools.Getopt_Long_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("References");
   Natools.Reference_Tests.All_Tests (Report);
   Report.End_Section;





   Natools.Tests.Text_IO.Print_Results (Report.Total_Results);

   declare
      Results : constant Natools.Tests.Result_Summary := Report.Total_Results;
   begin
      if Results (Natools.Tests.Fail) > 0 or







>
>
>
>







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
   Report.Section ("Getopt_Long");
   Natools.Getopt_Long_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("References");
   Natools.Reference_Tests.All_Tests (Report);
   Report.End_Section;

   Report.Section ("String_Slices");
   Natools.String_Slice_Tests.All_Tests (Report);
   Report.End_Section;

   Natools.Tests.Text_IO.Print_Results (Report.Total_Results);

   declare
      Results : constant Natools.Tests.Result_Summary := Report.Total_Results;
   begin
      if Results (Natools.Tests.Fail) > 0 or