File: 12iterator_intersection.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 (94 lines) | stat: -rw-r--r-- 2,356 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
#!/usr/bin/perl -w

use strict;

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

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

#======================================================================
# recurrence intersection
#====================================================================== 

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 ] );

# makes a set with month-day == 15 ( always 15 )
my $month_callback_1 = sub {
            $_[0]->add( days => -14 )
                 ->truncate( to => 'month' )
                 ->add( months => 1, days => 14 );
        };

# makes a set with month-day == 15 days from end-of-month ( 13, 14, 15 or 16 )
my $month_callback_2 = sub {
            $_[0]
                 ->add( days => 16 )
                 ->truncate( to => 'month' )
                 ->add( months => 1 )
                 ->add( days => -16 );
        };


my $months1 = DateTime::Set->from_recurrence( 
    recurrence => $month_callback_1, 
    start => $t1,
);

my $months2 = DateTime::Set->from_recurrence( 
    recurrence => $month_callback_2, 
    start => $t2,
);


my $iterator = $months1->iterator;
my @res = ();
for (1..5) {
        my $tmp = $iterator->next;
        push @res, $tmp->ymd if defined $tmp;
}
$res = join( ' ', @res );
ok( $res eq '1810-09-15 1810-10-15 1810-11-15 1810-12-15 1811-01-15',
        "iterations of month1 give $res" );

$iterator = $months2->iterator;
@res = ();
for (1..5) {
        my $tmp = $iterator->next;
        push @res, $tmp->ymd if defined $tmp;
}
$res = join( ' ', @res );
ok( $res eq '1810-12-16 1811-01-16 1811-02-13 1811-03-16 1811-04-15',
        "iterations of month2 give $res" );

my $m12 = $months1->intersection( $months2 );

$res = $m12->min;
$res = $res->ymd if ref($res);
ok( $res eq '1811-04-15', 
    "min() - got $res" );


$iterator = $m12->iterator;
@res = ();
for (1..3) {
        my $tmp = $iterator->next;
        push @res, $tmp->ymd if defined $tmp && ref($tmp);
}
$res = join( ' ', @res );
ok( $res eq '1811-04-15 1811-06-15 1811-09-15',
        "3 iterations give $res" );


1;