From 61aae6fc3c68b06086613e9ec7d2f0d3f7aabcd4 Mon Sep 17 00:00:00 2001 From: Kodi Date: Sun, 25 Jul 2010 21:16:19 +0000 Subject: [PATCH] [t/spec] More tests for Instants and Durations. git-svn-id: http://svn.pugscode.org/pugs@31823 c213334d-75ef-0310-aa23-eaa082d1ae64 --- .../instants-and-durations.t | 4 + S32-temporal/Date.t | 2 +- S32-temporal/DateTime-Instant-Duration.t | 79 +++++++++++++++++++ S32-temporal/DateTime.t | 8 +- 4 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 S32-temporal/DateTime-Instant-Duration.t diff --git a/S02-builtin_data_types/instants-and-durations.t b/S02-builtin_data_types/instants-and-durations.t index e88ba61219..eb79bb6e4d 100644 --- a/S02-builtin_data_types/instants-and-durations.t +++ b/S02-builtin_data_types/instants-and-durations.t @@ -23,6 +23,7 @@ isa_ok eval('now +300'), Instant, 'now is a term, not a function'; my $t1 = now; my $d = $t1 - $t0; + ok $t0 < $t1, 'later Instants are greater'; dies_ok { $t0 + $t1 }, 'Instant + Instant is illegal'; isa_ok $d, Duration, 'Instant - Instant ~~ Duration'; ok $d ~~ Real, 'Durations are Real'; @@ -32,10 +33,13 @@ isa_ok eval('now +300'), Instant, 'now is a term, not a function'; isa_ok $t0 - $d, Instant, 'Instant - Duration ~~ Instant'; is $t0 + ($t1 - $t0), $t1, 'Instant A + (Instant B - Instant A) == Instant A'; dies_ok { $d * $d }, 'Duration * Duration is illegal'; + dies_ok { $d ** 2 }, 'Duration ** Int is illegal'; isa_ok 2 * $d, Duration, 'Int * Duration ~~ Duration'; isa_ok $d / (2/3), Duration, 'Duration / Rat ~~ Duration'; } done_testing; +# See S32-temporal/DateTime-Instant-Duration.t for more. + # vim: ft=perl6 diff --git a/S32-temporal/Date.t b/S32-temporal/Date.t index 6e9ea503ea..0634ec5634 100644 --- a/S32-temporal/Date.t +++ b/S32-temporal/Date.t @@ -1,7 +1,7 @@ use v6; use Test; -# L +# L> plan *; diff --git a/S32-temporal/DateTime-Instant-Duration.t b/S32-temporal/DateTime-Instant-Duration.t new file mode 100644 index 0000000000..8c8d413e83 --- /dev/null +++ b/S32-temporal/DateTime-Instant-Duration.t @@ -0,0 +1,79 @@ +use v6; +use Test; + +=begin pod + +DateTime is the only means of constructing arbitrary Instants, +so we test some of the properties of Instants and Durations here +rather than in S02/instants-and-duration.t. + +=end pod + +sub dti(*%args) { DateTime.new(year => 1984, |%args).Instant } + +sub diff(%early = (), *%late) { + do dti(|%late) - dti(|%early) } + +sub days($n) { $n * 24 * 60 * 60 } + +plan *; + +# L + +isa_ok dti, Instant, 'DateTime.Instant returns an Instant'; +is dti, dti, 'Equal DateTimes yield equal Instants'; +is diff, 0, 'The difference of equal Instants is 0'; + +is diff(second => 5), 5, 'Instant subtraction (seconds)'; +is diff(second => 3.14159), 3.14159, 'Instant subtraction (non-integral seconds)'; +is diff(minute => 15), 15 * 60, 'Instant subtraction (minutes)'; +is diff(:hour(3), :minute(15), :second(33)), + 3*60*60 + 15*60 + 33, 'Instant subtraction (HMS)'; +is diff(day => 3), days(3), 'Instant subtraction (days)'; +is diff(month => 2), days(31), 'Instant subtraction (a month)'; +is diff(month => 3), days(31 + 29), 'Instant subtraction (Jan and Feb, leap year)'; +is diff({year => 1985}, month => 3), days(31 + 28), 'Instant subtraction (Jan and Feb, common year)'; +is diff(:year(1985), :month(3), :day(14)), + days(366 + 31 + 28 + 14), 'Instant subtraction (YMD)'; +is +(dti() - DateTime.new('1985-03-14T13:28:22').Instant), + days(366 + 31 + 28 + 14) + 13*60*60 + 28*60 + 22, 'Instant subtraction (YMDHMS)'; + +{ + my $a = DateTime.new(:year(2005), :month(1), :day(1), + :hour(2), :minute(22), :second(13.4)); + my $b = DateTime.new(:year(2004), :month(12), :day(31), + :hour(23), :minute(57), :second(8.5)); + my $expected-diff = 60 - 8.5 + 2*60 + 2*60*60 + 22*60 + 13.4; + is +($b.Instant - $a.Instant), $expected-diff, 'Instant subtraction (ugly case)'; + + $a .= clone(timezone => 3*60*60); + $b .= clone(timezone => 35*60 - 5); + is +($b.Instant - $a.Instant), 0.1, 'Instant subtraction (time zones)'; + + diff({:year(1997), :month(6), :day(30)}, + :year(1997), :month(7), :day(1)), + days(1) + 1, 'Instant subtraction (June 30 leap second)'; + $a .= clone(year => 2005, timezone => 0); + $b .= clone(year => 2006, timezone => 0); + is +($b.Instant - $a.Instant), $expected-diff + 1, 'Instant subtraction (December 31 leap second)'; + + $a = DateTime.new('2006-01-01T12:33:58+1234') + # In UTC, $a is 2005-12-31T23:59:58. + $b = DateTime.new('2006-01-01T12:44:03+1244'); + # In UTC, $b is 2006-01-01T00:00:03. + is +($b.Instant - $a.Instant), 6, 'Instant subtraction (leap second and time zones)'; + + $a .= clone(year => 1973); + $b .= clone(year => 2008); + is +($b.Instant - $a.Instant), 1_104_451_227, 'Instant subtraction (thirty-year span)'; + # I got this figure by adding 22 (the number of leap seconds + # between the two moments) to the difference of POSIX + # times. +} + +# L/DateTime.new(now)> + +# TODO + +done_testing; + +# vim: ft=perl6 diff --git a/S32-temporal/DateTime.t b/S32-temporal/DateTime.t index 5327862003..6e0fa1750c 100644 --- a/S32-temporal/DateTime.t +++ b/S32-temporal/DateTime.t @@ -142,8 +142,6 @@ lives_ok { dt year => 1999, month => 1, day => 1, hour => 1, minute => 59, second => 60.9, timezone => 2*60*60 }, 'Leap-second validation: Okay because of TZ; January 1 (second 60.9)'; -# TODO: DateTime.new(Instant $i, …) - # -------------------------------------------------------------------- # L/DateTime.new(time)> # -------------------------------------------------------------------- @@ -177,7 +175,7 @@ is show-dt(DateTime.new(946684800)), '0 0 0 1 1 2000 6', 'from POSIX at 2000-01- timezone => -(5*60*60 + 55*60), formatter => { .day ~ '/' ~ .month ~ '/' ~ .year ~ ' ' ~ .second ~ 's' ~ .minute ~ 'm' ~ .hour ~ 'h' }); - is ~$dt, '31/12/1999 59s4m18h', 'DateTime.new(Numeric) with time zone and formatter'; + is ~$dt, '31/12/1999 59s4m18h', 'DateTime.new(Int) with time zone and formatter'; } # -------------------------------------------------------------------- @@ -223,7 +221,7 @@ is DateTime.new('2009-12-31T22:33:44', 1 while time == $t; # loop until the next second $t = time; my $dt1 = DateTime.new($t); - my $dt2 = DateTime.now; # $dt1 and $dt2 might differ very occasionally + my $dt2 = DateTime.now.utc; # $dt1 and $dt2 might differ very occasionally is show-dt($dt1), show-dt($dt2), 'DateTime.now uses current time'; $t = time; @@ -236,8 +234,6 @@ is DateTime.new('2009-12-31T22:33:44', is ~$dt2, ~(($dt1.hour + 22) % 24), 'DateTime.now with time zone and formatter'; } -# TODO: DateTime.Instant - # -------------------------------------------------------------------- # L # --------------------------------------------------------------------