Index: tests/natools-time_keys-tests.adb ================================================================== --- tests/natools-time_keys-tests.adb +++ tests/natools-time_keys-tests.adb @@ -19,10 +19,16 @@ package body Natools.Time_Keys.Tests is function Image (Date : Ada.Calendar.Time) return String; + procedure Key_Test + (Test : in out NT.Test; + Time : in Ada.Calendar.Time; + Expected_Key : in String; + Max_Sub_Second_Digits : in Natural); + procedure Roundtrip_Test (Test : in out NT.Test; Time : in Ada.Calendar.Time; Expected_Key : in String); @@ -36,10 +42,27 @@ return Time_IO.RFC_3339.Image (Date => Date, Subsecond_Digits => Duration'Aft); end Image; + + procedure Key_Test + (Test : in out NT.Test; + Time : in Ada.Calendar.Time; + Expected_Key : in String; + Max_Sub_Second_Digits : in Natural) + is + Generated_Key : constant String := To_Key (Time, Max_Sub_Second_Digits); + begin + if Generated_Key /= Expected_Key then + Test.Fail ("Generated key """ & Generated_Key + & """, expected """ & Expected_Key & '"'); + Test.Info ("Time of generated key: " + & Image (To_Time (Generated_Key))); + end if; + end Key_Test; + procedure Roundtrip_Test (Test : in out NT.Test; Time : in Ada.Calendar.Time; Expected_Key : in String) @@ -67,10 +90,11 @@ ------------------------- procedure All_Tests (Report : in out NT.Reporter'Class) is begin Roundtrips (Report); + Subsecond_Rounding (Report); end All_Tests; ---------------------- @@ -110,6 +134,84 @@ "_0CV"); exception when Error : others => Test.Report_Exception (Error); end Roundtrips; + + procedure Subsecond_Rounding (Report : in out NT.Reporter'Class) is + Test : NT.Test := Report.Item ("Overflow in subsecond rounding"); + begin + if Duration'Small > 1.0 / 256.0 then + Test.Skip ("Not enough precision in Duration"); + return; + end if; + + Key_Test + (Test, + To_Time ("VV121231~V"), + "VV121231~", + 2); + + Key_Test + (Test, + To_Time ("VV121231~X"), + "VV121232", + 2); + + Key_Test + (Test, + To_Time ("VV124561~~V"), + "VV124561~~", + 3); + + Key_Test + (Test, + To_Time ("VV124561~~X"), + "VV124562", + 3); + + Key_Test + (Test, + Ada.Calendar.Formatting.Time_Of + (2015, 2, 2, 1, 1, 1, 255.0 / 256.0), + "VV22112", + 1); + + Key_Test + (Test, + Ada.Calendar.Formatting.Time_Of + (2015, 2, 2, 1, 58, 59, 255.0 / 256.0), + "VV221w", + 1); + + Key_Test + (Test, + Ada.Calendar.Formatting.Time_Of + (2015, 2, 2, 22, 59, 59, 255.0 / 256.0), + "VV22N", + 1); + + Key_Test + (Test, + Ada.Calendar.Formatting.Time_Of + (2015, 2, 28, 23, 59, 59, 255.0 / 256.0), + "VV31", + 1); + + Key_Test + (Test, + Ada.Calendar.Formatting.Time_Of + (2016, 2, 28, 23, 59, 59, 255.0 / 256.0), + "VW2T", + 1); + + Key_Test + (Test, + Ada.Calendar.Formatting.Time_Of + (2015, 12, 31, 23, 59, 59, 255.0 / 256.0), + "VW11", + 1); + exception + when Error : others => Test.Report_Exception (Error); + end Subsecond_Rounding; + end Natools.Time_Keys.Tests; Index: tests/natools-time_keys-tests.ads ================================================================== --- tests/natools-time_keys-tests.ads +++ tests/natools-time_keys-tests.ads @@ -21,7 +21,8 @@ package NT renames Natools.Tests; procedure All_Tests (Report : in out NT.Reporter'Class); procedure Roundtrips (Report : in out NT.Reporter'Class); + procedure Subsecond_Rounding (Report : in out NT.Reporter'Class); end Natools.Time_Keys.Tests;