are_1.2.0_16239a8b/ada-util/regtests/util-dates-tests.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
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
-----------------------------------------------------------------------
--  util-dates-tests - Test for dates
--  Copyright (C) 2018, 2019, 2020 Stephane Carrez
--  Written by Stephane Carrez (Stephane.Carrez@gmail.com)
--
--  Licensed under the Apache License, Version 2.0 (the "License");
--  you may not use this file except in compliance with the License.
--  You may obtain a copy of the License at
--
--      http://www.apache.org/licenses/LICENSE-2.0
--
--  Unless required by applicable law or agreed to in writing, software
--  distributed under the License is distributed on an "AS IS" BASIS,
--  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
--  See the License for the specific language governing permissions and
--  limitations under the License.
-----------------------------------------------------------------------
with Util.Test_Caller;
with Util.Dates.ISO8601;
package body Util.Dates.Tests is

   package Caller is new Util.Test_Caller (Test, "Dates");

   procedure Add_Tests (Suite : in Util.Tests.Access_Test_Suite) is
   begin
      Caller.Add_Test (Suite, "Test Util.Dates.ISO8601.Image",
                       Test_ISO8601_Image'Access);
      Caller.Add_Test (Suite, "Test Util.Dates.ISO8601.Value",
                       Test_ISO8601_Value'Access);
      Caller.Add_Test (Suite, "Test Util.Dates.ISO8601.Value (Errors)",
                       Test_ISO8601_Error'Access);
      Caller.Add_Test (Suite, "Test Util.Dates.Is_Same_Day",
                       Test_Is_Same_Day'Access);
      Caller.Add_Test (Suite, "Test Util.Dates.Get_Day_Count",
                       Test_Get_Day_Count'Access);
   end Add_Tests;

   --  ------------------------------
   --  Test converting a date in ISO8601.
   --  ------------------------------
   procedure Test_ISO8601_Image (T : in out Test) is
      T1 : constant Ada.Calendar.Time := Ada.Calendar.Formatting.Time_Of (1980, 1, 2, 10, 30, 23);
   begin
      Util.Tests.Assert_Equals (T, "1980-01-02", ISO8601.Image (T1),
                                "Invalid Image");

      Util.Tests.Assert_Equals (T, "1980", ISO8601.Image (T1, ISO8601.YEAR),
                                "Invalid Image (YEAR precision)");

      Util.Tests.Assert_Equals (T, "1980-01", ISO8601.Image (T1, ISO8601.MONTH),
                                "Invalid Image (MONTH precision)");

      Util.Tests.Assert_Equals (T, "1980-01-02", ISO8601.Image (T1, ISO8601.DAY),
                                "Invalid Image (DAY precision)");

      Util.Tests.Assert_Equals (T, "1980-01-02T10", ISO8601.Image (T1, ISO8601.HOUR),
                                "Invalid Image (DAY precision)");

      Util.Tests.Assert_Equals (T, "1980-01-02T10:30", ISO8601.Image (T1, ISO8601.MINUTE),
                                "Invalid Image (MINUTE precision)");

      Util.Tests.Assert_Equals (T, "1980-01-02T10:30:23", ISO8601.Image (T1, ISO8601.SECOND),
                                "Invalid Image (SECOND precision)");

   end Test_ISO8601_Image;

   --  ------------------------------
   --  Test converting a string in ISO8601 into a date.
   --  ------------------------------
   procedure Test_ISO8601_Value (T : in out Test) is
      Date   : Ada.Calendar.Time;
   begin
      Date := ISO8601.Value ("1980");
      Util.Tests.Assert_Equals (T, "1980-01-01", ISO8601.Image (Date));

      Date := ISO8601.Value ("1980-02");
      Util.Tests.Assert_Equals (T, "1980-02", ISO8601.Image (Date, ISO8601.MONTH));

      Date := ISO8601.Value ("1980-03-04");
      Util.Tests.Assert_Equals (T, "1980-03-04", ISO8601.Image (Date));

      Date := ISO8601.Value ("1980-03-04");
      Util.Tests.Assert_Equals (T, "1980-03-04", ISO8601.Image (Date));

      Date := ISO8601.Value ("1980-12-31");
      Util.Tests.Assert_Equals (T, "1980-12-31", ISO8601.Image (Date));

      Date := ISO8601.Value ("19801231");
      Util.Tests.Assert_Equals (T, "1980-12-31", ISO8601.Image (Date));

      Date := ISO8601.Value ("1980-12-31T11:23");
      Util.Tests.Assert_Equals (T, "1980-12-31T11:23", ISO8601.Image (Date, ISO8601.MINUTE));

      Date := ISO8601.Value ("1980-12-31T11:23:34");
      Util.Tests.Assert_Equals (T, "1980-12-31T11:23:34", ISO8601.Image (Date, ISO8601.SECOND));

      Date := ISO8601.Value ("1980-12-31T11:23:34.123");
      Util.Tests.Assert_Equals (T, "1980-12-31T11:23:34.123+00:00",
                                ISO8601.Image (Date, ISO8601.SUBSECOND));

      Date := ISO8601.Value ("1980-12-31T11:23:34.123+04:30");
      --  The date was normalized in GMT
      Util.Tests.Assert_Equals (T, "1980-12-31T06:53:34.123+00:00",
                                ISO8601.Image (Date, ISO8601.SUBSECOND));
   end Test_ISO8601_Value;

   --  ------------------------------
   --  Test value convertion errors.
   --  ------------------------------
   procedure Test_ISO8601_Error (T : in out Test) is
      procedure Check (Date : in String);

      procedure Check (Date : in String) is
      begin
         declare
            Unused : constant Ada.Calendar.Time := ISO8601.Value (Date);
         begin
            T.Fail ("No exception raised for " & Date);

         end;

      exception
         when Constraint_Error =>
            null;
      end Check;

   begin
      Check ("");
      Check ("1980-");
      Check ("1980:02:03");
      Check ("1980-02-03u33:33");
      Check ("1980-02-03u33");
      Check ("1980-13-44");
      Check ("1980-12-00");
      Check ("1980-12-03T25:34");
      Check ("1980-12-03T10x34");
      Check ("1980-12-03T10:34p");
      Check ("1980-12-31T11:23:34123");
      Check ("1980-12-31T11:23:34,1");
      Check ("1980-12-31T11:23:34,12");
      Check ("1980-12-31T11:23:34x123");
      Check ("1980-12-31T11:23:34.1234");
      Check ("1980-12-31T11:23:34Zp");
      Check ("1980-12-31T11:23:34+2");
      Check ("1980-12-31T11:23:34+23x");
      Check ("1980-12-31T11:23:34+99");
      Check ("1980-12-31T11:23:34+10:0");
      Check ("1980-12-31T11:23:34+10:03x");
   end Test_ISO8601_Error;

   --  ------------------------------
   --  Test Is_Same_Day operation.
   --  ------------------------------
   procedure Test_Is_Same_Day (T : in out Test) is
      procedure Check (D1, D2   : in String;
                       Same_Day : in Boolean);

      procedure Check (D1, D2   : in String;
                       Same_Day : in Boolean) is
         T1 : constant Ada.Calendar.Time := ISO8601.Value (D1);
         T2 : constant Ada.Calendar.Time := ISO8601.Value (D2);
      begin
         T.Assert (Same_Day = Is_Same_Day (T1, T2), "Invalid Is_Same_Day for " & D1 & " " & D2);
         T.Assert (Same_Day = Is_Same_Day (T2, T1), "Invalid Is_Same_Day for " & D2 & " " & D1);
      end Check;

   begin
      Check ("1980-12-31T11:23:34.123", "1980-12-31T10:23:34.123", True);
      Check ("1980-12-31T11:23:34.123", "1980-12-30T10:23:34.123", False);
      Check ("1980-12-31T00:00:00", "1980-12-31T23:59:59", True);
   end Test_Is_Same_Day;

   --  ------------------------------
   --  Test Get_Day_Count operation.
   --  ------------------------------
   procedure Test_Get_Day_Count (T : in out Test) is
   begin
      Util.Tests.Assert_Equals (T, 366, Natural (Get_Day_Count (2020)));
      Util.Tests.Assert_Equals (T, 365, Natural (Get_Day_Count (1983)));
      Util.Tests.Assert_Equals (T, 366, Natural (Get_Day_Count (2000)));
      Util.Tests.Assert_Equals (T, 365, Natural (Get_Day_Count (2001)));
   end Test_Get_Day_Count;

end Util.Dates.Tests;