File: 06backtrack.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 (90 lines) | stat: -rw-r--r-- 2,464 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
#!/usr/bin/perl -w

use strict;

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

use DateTime;
use DateTime::Set;

#======================================================================
# backtracking
#====================================================================== 

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

sub test {
    my $iterator = $_[0]->iterator;
    my @res;
    for (1..3) {
        my $tmp = $iterator->next;
        push @res, $tmp->ymd if defined $tmp;
    }
    return join( ' ', @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 ] );

# ------------- test a simple recurrence

my $month_callback = sub {
            $_[0]->truncate( to => 'month' );
            $_[0]->add( months => 1 );
            return $_[0];
        };
my $recurr_months = DateTime::Set->from_recurrence(
    recurrence => $month_callback,
    start => $t1,
);
is( test($recurr_months), '1810-09-01 1810-10-01 1810-11-01',
    "months" );

# --------- test a more complex recurrence

my $day_15_callback = sub {
        my $after = $_[0]->day >= 15;
        $_[0]->set( day => 15 );
        $_[0]->truncate( to => 'day' );
        $_[0]->add( months => 1 ) if $after;
        return $_[0];
    };
my $recurr_day_15 = DateTime::Set->from_recurrence( 
    recurrence => $day_15_callback, 
    start => $t1,
);
is( test($recurr_day_15), '1810-09-15 1810-10-15 1810-11-15',
    "recurr day 15" );

# ---------- test operations with recurrences

my $recurr_day_1_15 = $recurr_day_15 ->union( $recurr_months );
is( test($recurr_day_1_15), '1810-09-01 1810-09-15 1810-10-01',
    "union of recurrences: recurr day 1,15" );

# ---------- test add() to a recurrence

my $days_15 = $recurr_months->clone->add( days => 14 );
is( test($days_15), '1810-09-15 1810-10-15 1810-11-15',
    "days_15" );

# check that $recurr_months is still there
is( test($recurr_months), '1810-09-01 1810-10-01 1810-11-01',
    "months is still there" );

my $days_20 = $recurr_months->clone->add( days => 19 );
is( test($days_20), '1810-09-20 1810-10-20 1810-11-20',
    "days_20" );

# ---------- test operations with recurrences + add

my $days_15_and_20 = $days_15 ->union( $days_20 );

is( test($days_15_and_20), '1810-09-15 1810-09-20 1810-10-15',
    "days_15_and_20" );

1;