gnatcoll_22.0.0_620c2f23/testsuite/tests/utils/str_checks/test.adb

  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
with GNATCOLL.Utils;
with Ada.Characters.Handling;   use Ada.Characters.Handling;
with Ada.Characters.Latin_1;    use Ada.Characters.Latin_1;
with Test_Assert;
with Ada.Text_IO;

function Test return Integer is

   package A renames Test_Assert;
   package IO renames Ada.Text_IO;

   procedure Test_Equality;
   procedure Test_Is_Whitespace;
   procedure Test_Starts_With;
   procedure Test_Ends_With;
   procedure Test_Is_Blank_Line;

   procedure Test_Equality is
      S1 : constant String := "abcdefg";
      S2 : constant String := To_Upper (S1);
      S3 : constant String := "hijklmn";
      S4 : constant String := S1 & S3;
      Empty : constant String := "";
   begin

      for CS in Boolean loop
         IO.Put_Line ("Test Equal with case sensitity set to: " & CS'Image);
         A.Assert (GNATCOLL.Utils.Equal (Empty, "", Case_Sensitive => CS)
                   = True,
                   Msg => "empty string");
         A.Assert (GNATCOLL.Utils.Equal (S1, S1, Case_Sensitive => CS)
                   = True,
                   Msg => "exact same string");
         A.Assert (GNATCOLL.Utils.Equal (S1, S2, Case_Sensitive => CS)
                   = not CS,
                   Msg => "compare to To_Upper copy");
         A.Assert (GNATCOLL.Utils.Equal (S1, S3, Case_Sensitive => CS)
                   = False,
                   Msg => "differnet strings");
         A.Assert (GNATCOLL.Utils.Equal (S4, S1, Case_Sensitive => CS)
                   = False,
                   Msg => "S4 compare to its substring S1");
      end loop;

      IO.Put_Line ("Test Case_Insensitive_Equal");
      A.Assert (GNATCOLL.Utils.Case_Insensitive_Equal (S1, S1) = True,
                Msg => "exact same string");
      A.Assert (GNATCOLL.Utils.Case_Insensitive_Equal (S1, S2) = True,
                Msg => "compare to To_Upper copy");
      A.Assert (GNATCOLL.Utils.Case_Insensitive_Equal (S1, S3) = False,
                Msg => "different string");

   end Test_Equality;

   procedure Test_Is_Whitespace is

      subtype Whitespace_Type is Character with
         Static_Predicate => Whitespace_Type in ' ' | LF | CR;
   begin
      IO.Put_Line ("Test Is_Whitespace");

      for Car in Whitespace_Type loop
         A.Assert (GNATCOLL.Utils.Is_Whitespace (Car) = True,
                   Msg => Character'Image (Car) & " is whitespace");
      end loop;

      A.Assert (GNATCOLL.Utils.Is_Whitespace ('a') = False,
                Msg => "is whitespace false");
      A.Assert (GNATCOLL.Utils.Is_Whitespace (NUL) = False,
                Msg => "is whitespace empty");
   end Test_Is_Whitespace;

   procedure Test_Starts_With is
   begin
      IO.Put_Line ("Test Starts_With");
      A.Assert (GNATCOLL.Utils.Starts_With ("ABCD", "ABC") = True,
                Msg => "starts_with true");
      A.Assert (GNATCOLL.Utils.Starts_With ("ABCD", "") = True,
                Msg => "starts_with empty => always true");

      A.Assert (GNATCOLL.Utils.Starts_With ("ABCD", "BCD") = False,
                Msg => "starts_with false");
      A.Assert (GNATCOLL.Utils.Starts_With ("ABCD", "BCDEFGH") = False,
                Msg => "starts_with false : pattern too long");
   end Test_Starts_With;

   procedure Test_Ends_With is
   begin
      IO.Put_Line ("Test Ends_With");
      A.Assert (GNATCOLL.Utils.Ends_With ("ABCD", "BCD") = True,
                Msg => "ends_with true");
      A.Assert (GNATCOLL.Utils.Ends_With ("ABCD", "") = True,
                Msg => "ends_with empty => always true");

      A.Assert (GNATCOLL.Utils.Ends_With ("ABCD", "ABC") = False,
                Msg => "ends_with false");
      A.Assert (GNATCOLL.Utils.Ends_With ("ABCD", "ABCDEFG") = False,
                Msg => "ends_with false : pattern too long");
   end Test_Ends_With;

   procedure Test_Is_Blank_Line is
      S1 : constant String := "       " & ASCII.CR;
      S2 : constant String := "AAAAA";
      S3 : constant String := S1 & ASCII.CR & ASCII.LF & S2;
   begin
      IO.Put_Line ("Test Is_Blank_Line");

      A.Assert (GNATCOLL.Utils.Is_Blank_Line ("", 0) = True,
                Msg => "empty string and Index = 0");
      A.Assert (GNATCOLL.Utils.Is_Blank_Line (S1, 0) = True,
                Msg => "only blank + Index = 0");
      A.Assert (GNATCOLL.Utils.Is_Blank_Line (S2, 0) = False,
                Msg => "Not blank + Index = 0");

      A.Assert (GNATCOLL.Utils.Is_Blank_Line ("", 5) = True,
                Msg => "empty string, Index > 0");

      A.Assert (GNATCOLL.Utils.Is_Blank_Line (S3, S1'Length + 3) = False,
                Msg => "not blank");

   end Test_Is_Blank_Line;

begin
   IO.New_Line;
   Test_Equality;
   IO.New_Line;
   Test_Is_Whitespace;
   IO.New_Line;
   Test_Starts_With;
   IO.New_Line;
   Test_Ends_With;
   IO.New_Line;
   Test_Is_Blank_Line;

   return A.Report;

end Test;