fix of major todo synchronization issue if $c->hide_older_than option is set
[davical.git] / testing / test-RRULE-v2.php
blobb0a6e1b87ea5852a1b97c9dab69c6ef926422981
1 #!/usr/bin/php
2 <?php
4 if ( @file_exists('../../awl/inc/AWLUtilities.php') ) {
5 set_include_path('../inc:../htdocs:../../awl/inc');
7 else {
8 set_include_path('../inc:../htdocs:/usr/share/awl/inc');
10 require_once("always.php");
11 $c->dbg = array();
12 require_once("RRule-v2.php");
13 require_once('AwlQuery.php');
15 @header("Content-Type: text/plain");
17 echo <<<EOTXT
18 Testing the RRule v2 Library
20 EOTXT;
22 class RRuleTest {
23 var $dtstart;
24 var $recur;
25 var $description;
26 var $result_description;
27 var $PHP_time;
28 var $SQL_time;
30 function __construct( $description, $start, $recur, $result_description = null ) {
31 $this->description = $description;
32 $this->dtstart = $start;
33 $this->recur = $recur;
34 $this->result_description = $result_description;
35 $this->result_limit = 30;
38 function PHPTest() {
39 $result = '';
40 $start = microtime(true);
41 $rule = new RepeatRule( $this->dtstart, $this->recur );
42 $i = 0;
43 while( $date = $rule->next() ) {
44 if ( ($i++ % 4) == 0 ) $result .= "\n";
45 $result .= " " . $date->format('Y-m-d H:i:s');
46 if ( $i >= $this->result_limit ) break;
48 $this->PHP_time = microtime(true) - $start;
49 return $result;
52 function SQLTest() {
53 $result = '';
54 $sql = "SELECT event_instances::timestamp AS event_date FROM event_instances(:dtstart,:rrule) LIMIT ".$this->result_limit;
55 $start = microtime(true);
56 $qry = new AwlQuery($sql, array( ':dtstart' => $this->dtstart, ':rrule' => $this->recur) );
57 // printf( "%s\n", $qry->querystring);
58 if ( $qry->Exec("test") && $qry->rows() > 0 ) {
59 $i = 0;
60 while( $row = $qry->Fetch() ) {
61 if ( ($i++ % 4) == 0 ) $result .= "\n";
62 $result .= " " . $row->event_date;
65 $this->SQL_time = microtime(true) - $start;
66 return $result;
71 $tests = array(
72 new RRuleTest( "Daily for 7 days", "20061103T073000", "RRULE:FREQ=DAILY;COUNT=7" )
73 , new RRuleTest( "Weekly for 26 weeks", "20061102T100000", "RRULE:FREQ=WEEKLY;COUNT=26;INTERVAL=1;BYDAY=TH" )
74 , new RRuleTest( "Fortnightly for 4 events", "20061103T160000", "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=4" )
75 , new RRuleTest( "Fortnightly for 28 events", "20061103T160000", "RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=20071122T235900" )
76 , new RRuleTest( "3/wk for 5 weeks", "20081101T160000", "RRULE:FREQ=WEEKLY;COUNT=15;INTERVAL=1;BYDAY=MO,WE,FR" )
77 , new RRuleTest( "Monthly forever", "20061104T073000", "RRULE:FREQ=MONTHLY" )
78 , new RRuleTest( "Monthly, on the 1st monday, 2nd wednesday, 3rd friday and last sunday, forever", "20061117T073000", "RRULE:FREQ=MONTHLY;BYDAY=1MO,2WE,3FR,-1SU" )
79 , new RRuleTest( "The working days of each month", "20061107T113000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;UNTIL=20070101T000000" )
80 , new RRuleTest( "The last working day of each month", "20061107T113000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYSETPOS=-1;COUNT=30" )
81 , new RRuleTest( "Every working day", "20081020T103000", "RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;COUNT=30" )
82 , new RRuleTest( "Every working day", "20081020T110000", "RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=MO,TU,WE,TH,FR;COUNT=30" )
83 , new RRuleTest( "The last day of each month", "20110831", "RRULE:FREQ=MONTHLY;BYMONTHDAY=-1" )
84 , new RRuleTest( "1st Tuesday, 2nd Wednesday, 3rd Thursday & 4th Friday, every March, June, September, October and December (SQL is wrong)", "20081001T133000", "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=1TU,2WE,3TH,4FR;BYMONTH=3,6,9,10,12" )
85 , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=MONTHLY;INTERVAL=1;BYDAY=TU,FR;COUNT=30" )
86 , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=WEEKLY;INTERVAL=1;BYDAY=TU,FR;COUNT=30" )
87 , new RRuleTest( "Every tuesday and friday", "20081017T084500", "RRULE:FREQ=DAILY;INTERVAL=1;BYDAY=TU,FR;COUNT=30" )
88 , new RRuleTest( "Time zone 1", "19700315T030000", "FREQ=YEARLY;INTERVAL=1;BYDAY=3SU;BYMONTH=3" )
89 , new RRuleTest( "Time zone 2", "19700927T020000", "FREQ=YEARLY;INTERVAL=1;BYDAY=-1SU;BYMONTH=9" )
90 , new RRuleTest( "Time zone 3", "19810329T030000", "FREQ=YEARLY;BYMONTH=3;BYDAY=-1SU" )
91 , new RRuleTest( "Time zone 4", "20000404T010000", "FREQ=YEARLY;BYDAY=1SU;BYMONTH=4;COUNT=15" )
92 , new RRuleTest( "Six Working Days", "20110905", "FREQ=DAILY;BYDAY=MO,TU,WE,TH,FR;COUNT=6" )
93 , new RRuleTest( "Six Working Days", "20110905", "FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR;COUNT=6" )
96 foreach( $tests AS $k => $test ) {
97 echo "=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=\n";
98 echo "$test->dtstart - $test->recur\n";
99 echo "$test->description\n";
100 $php_result = $test->PHPTest();
101 $sql_result = $test->SQLTest();
102 if ( $php_result == $sql_result ) {
103 printf( 'PHP & SQL results are identical (-: P: %6.4lf & S: %6.4lf'."\n", $test->PHP_time, $test->SQL_time);
105 else {
106 printf( 'PHP & SQL results differ :-( P: %6.4lf & S: %6.4lf'."\n", $test->PHP_time, $test->SQL_time);
107 echo "PHP Result:\n$php_result\n\n";
108 echo "SQL Result:\n$sql_result\n\n"; // Still under development
113 exit(0);