Natools

Diff
Login

Differences From Artifact [5e09af406b]:

To Artifact [6cf26c7206]:


10
11
12
13
14
15
16

17
18
19
20
21
22
23
-- 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.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;

with Natools.String_Slices.Slice_Sets;

package body Natools.String_Slice_Set_Tests is







>







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-- 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 Ada.Strings.Fixed;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;

with Natools.String_Slices.Slice_Sets;

package body Natools.String_Slice_Set_Tests is
100
101
102
103
104
105
106

107
108
109
110
111
112
113
   ----------------------
   -- Test collections --
   ----------------------

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

      Test_Interval_Arithmetic (Report);
      Test_Navigation (Report);
      Test_Tokenization (Report);
   end All_Tests;










>







101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
   ----------------------
   -- Test collections --
   ----------------------

   procedure All_Tests (Report : in out NT.Reporter'Class) is
   begin
      Test_Conversions (Report);
      Test_Exceptions (Report);
      Test_Interval_Arithmetic (Report);
      Test_Navigation (Report);
      Test_Tokenization (Report);
   end All_Tests;



281
282
283
284
285
286
287




















































































































































































































































































































































































































288
289
290
291
292
293
294

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





















































































































































































































































































































































































































   procedure Test_Interval_Arithmetic (Report : in out NT.Reporter'Class) is
      Name : constant String := "Interval arithmetic";
      Reported : Boolean := False;

      procedure Check_Set
        (Parent : in String;







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







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

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


   procedure Test_Exceptions (Report : in out NT.Reporter'Class) is
      Parent_Slice : constant String_Slices.Slice
        := String_Slices.To_Slice (Parent_String);
      Set : Slice_Sets.Slice_Set;
   begin
      Report.Section ("Exceptions raised");

      declare
         Name : constant String := "Add_Slice to null set";
      begin
         Set.Add_Slice (15, 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Exclude_Slice from null set";
      begin
         Set.Exclude_Slice (15, 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Include_Slice to null set";
      begin
         Set.Include_Slice (15, 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 null set";
      begin
         Set.Restrict (15, 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Subset of null set";
         New_Set : Slice_Sets.Slice_Set;
      begin
         New_Set := Set.Subset (15, 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Result: """ & New_Set.To_String & '"');
      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 := "Cut_Before in null set";
      begin
         Set.Cut_Before (15);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Find_Slice in null set";

         function Find_Anything (Slice : String) return Boolean;

         function Find_Anything (Slice : String) return Boolean is
            pragma Unreferenced (Slice);
         begin
            return True;
         end Find_Anything;

         Result : String_Slices.String_Range;
      begin
         Result := Set.Find_Slice (Find_Anything'Access);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Result: " & String_Slices.Image (Result));
      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;

      Set := Slice_Sets.To_Slice_Set (Parent_Slice);
      Set.Exclude_Slice (20, 29);

      declare
         Name : constant String := "Incorrect Index in Element";
         C : Character;
      begin
         C := Set.Element (25);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Return value: " & Character'Image (C));
      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 := "Include_Slice outside of parent string";
      begin
         Set.Include_Slice (Parent_String'Last + 10, Parent_String'Last + 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Add_Slice outside of parent string";
      begin
         Set.Add_Slice (Parent_String'Last + 10, Parent_String'Last + 19);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Add_Slice with overlapping range";
      begin
         Set.Add_Slice (15, 24);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Add_Slice with overlapping slice";
      begin
         Set.Add_Slice (Parent_Slice.Subslice (15, 24));
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Unrelated Add_Slice";
      begin
         Set.Add_Slice (String_Slices.To_Slice (Parent_String (22 .. 26)));
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Unrelated Include_Slice";
      begin
         Set.Include_Slice (String_Slices.To_Slice (Parent_String (22 .. 26)));
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Cut_Before before first";
      begin
         Set.Cut_Before (5);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Cut_Before after last";
      begin
         Set.Cut_Before (Parent_String'Last + 5);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Extender callback in Trim_Slices";

         function Extender (Slice : String) return String_Slices.String_Range;

         function Extender (Slice : String)
           return String_Slices.String_Range is
         begin
            return String_Slices.To_Range (Slice'First, Slice'Last + 1);
         end Extender;
      begin
         Set.Trim_Slices (Extender'Access);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
      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 := "Function Index with invalid From";
         N : Natural;
      begin
         N := Set.Index (Ada.Strings.Maps.To_Set (" "), 5);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Return value:" & Integer'Image (N));
      exception
         when Ada.Strings.Index_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;

      Set.Clear;

      declare
         Name : constant String := "Empty_Set.Next";
         N : Natural;
      begin
         N := Set.Next (15);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Return value:" & Integer'Image (N));
      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 := "Empty_Set.Previous";
         N : Natural;
      begin
         N := Set.Previous (15);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Return value:" & Integer'Image (N));
      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;

      Set.Include_Slice (Parent_String'First, Parent_String'Last);

      declare
         Name : constant String := "Invalid input Index for Next";
         N : Natural;
      begin
         N := Set.Next (5);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Return value:" & Integer'Image (N));
      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 := "Invalid input Index for Previous";
         N : Natural;
      begin
         N := Set.Previous (5);
         Report.Item (Name, NT.Fail);
         Report.Info ("No exception has been raised.");
         Report.Info ("Return value:" & Integer'Image (N));
      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_Exceptions;


   procedure Test_Interval_Arithmetic (Report : in out NT.Reporter'Class) is
      Name : constant String := "Interval arithmetic";
      Reported : Boolean := False;

      procedure Check_Set
        (Parent : in String;