File: 04recurrence.t

package info (click to toggle)
libdatetime-set-perl 0.31-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 336 kB
  • sloc: perl: 2,972; makefile: 2
file content (176 lines) | stat: -rw-r--r-- 4,806 bytes parent folder | download | duplicates (7)
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
#!/usr/bin/perl -w

use strict;

use Test::More;
plan tests => 25;

use DateTime;
use DateTime::Duration;
use DateTime::Set;
# use warnings;

#======================================================================
# recurrence
#====================================================================== 

use constant INFINITY     =>       100 ** 100 ** 100 ;
use constant NEG_INFINITY => -1 * (100 ** 100 ** 100);

my $res;

my $t1 = new DateTime( year => '1810', month => '08', day => '22' );
my $t2 = new DateTime( year => '1810', month => '11', day => '24' );
my $s1 = DateTime::Set->from_datetimes( dates => [ $t1, $t2 ] );

my $month_callback = sub {
            $_[0]->truncate( to => 'month' );
            # warn " truncate = ".$_[0]->ymd;
            $_[0]->add( months => 1 );
            # warn " add = ".$_[0]->ymd;
            return $_[0];
        };

my $months = DateTime::Set->from_recurrence(
    recurrence => $month_callback,
);

# contains datetime, unbounded set
{
    my $t0 = $t2->clone->truncate( to => 'month' );
    my $t0_set = DateTime::Set->from_datetimes( dates => [ $t0 ] );
    is( $months->contains( $t1 ), 0, "does not contain datetime" );
    is( $months->contains( $t1, $t0 ), 0, "does not contain datetime list" );
    is( $months->contains( $t0 ), 1, "contains datetime" );

    is( $months->intersects( $t1 ), 0, "does not intersect datetime" );
    is( $months->intersects( $t1, $t0 ), 1, "intersects datetime list" );
    is( $months->intersects( $t0 ), 1, "intersects datetime" );

    ok( ! defined $months->contains( $months ) , 
        "contains - can't do it with both unbounded sets, returns undef" );
        
    is( $t0_set->intersects( $months ), 1, "intersects unbounded set" );
}

# "START"

$months = DateTime::Set->from_recurrence( 
    recurrence => $month_callback, 
    start => $t1,   # 1810-08-22
);

# contains datetime, semi-bounded set
{
    my $t0 = $t2->clone->truncate( to => 'month' );
    is( $months->contains( $t1 ), 0, "does not contain datetime" );
    is( $months->contains( $t1, $t0 ), 0, "does not contain datetime list" );
    is( $months->contains( $t0 ), 1, "contains datetime" );

    is( $months->intersects( $t1 ), 0, "does not intersect datetime" );
    is( $months->intersects( $t1, $t0 ), 1, "intersects datetime list" );
    is( $months->intersects( $t0 ), 1, "intersects datetime" );
}

$res = $months->min;
$res = $res->ymd if ref($res);
is( $res, '1810-09-01', 
    "min()" );
$res = $months->max;
# $res = $res->ymd if ref($res);
is( ref($res), 'DateTime::Infinite::Future',
    "max()" );

# "END"
$months = DateTime::Set->from_recurrence(
    recurrence => $month_callback,
    end => $t1,  # 1810-08-22
);
$res = $months->min;
# $res = $res->ymd if ref($res);
is( ref($res), 'DateTime::Infinite::Past',
    "min()" );

{
$res = $months->max;
$res = $res->ymd if ref($res);
is( $res, '1810-08-01',   
    "max()" );
}

is( $months->count, undef, "count" );

# "START+END"
$months = DateTime::Set->from_recurrence(
    recurrence => $month_callback,
    start => $t1,  # 1810-08-22
    end => $t2,    # 1810-11-24
);
$res = $months->min;
$res = $res->ymd if ref($res);
is( $res, '1810-09-01',
    "min()" );

{
$res = $months->max;
$res = $res->ymd if ref($res);
is( $res, '1810-11-01',
    "max()" );
}

# "START+END" at recurrence 
$t1->set( day => 1 );  # month=8
$t2->set( day => 1 );  # month=11
$months = DateTime::Set->from_recurrence(
    recurrence => $month_callback,
    start => $t1,
    end => $t2,
);
$res = $months->min;
$res = $res->ymd if ref($res);
is( $res, '1810-08-01',
    "min()" );

{
$res = $months->max;
$res = $res->ymd if ref($res);
is( $res, '1810-11-01',
    "max()" );
}

{
# verify that the set-span when backtracking is ok.
# This is _critical_ for doing correct intersections
$res = $months->intersection( DateTime->new( year=>1810, month=>11, day=>1 ) );
$res = $res->max;
$res = $res->ymd if ref($res);
is( $res, '1810-11-01',
    "intersection at the recurrence" );
}

# big set - "START+END" at recurrence
{
my $set = DateTime::SpanSet->from_spans(
    spans => [
        DateTime::Span->from_datetimes(
            start => new DateTime( year => '1950', month => '08', day => '22' ),
            end   => new DateTime( year => '2000', month => '08', day => '22' ),
        ),
        DateTime::Span->from_datetimes(
            start => new DateTime( year => '2350', month => '08', day => '22' ),
            end   => new DateTime( year => '2400', month => '08', day => '22' ),
        ),
    ],
);
my $months = DateTime::Set->from_recurrence(
    recurrence => $month_callback,
);
my $bounded = $months->intersection( $set );

# ok( ! defined $bounded->count, "will not count: there are too many elements" );
is( $bounded->count, 1200, "too many elements - iterate" );

}

1;