Back to index

lightning-sunbird  0.9+nobinonly
regression-recur.c
Go to the documentation of this file.
00001 /* -*- Mode: C -*-
00002   ======================================================================
00003   FILE: regression-recur.c
00004   CREATOR: ebusboom 8jun00
00005   
00006   DESCRIPTION:
00007 
00008   (C) COPYRIGHT 1999 Eric Busboom 
00009   http://www.softwarestudio.org
00010 
00011   The contents of this file are subject to the Mozilla Public License
00012   Version 1.0 (the "License"); you may not use this file except in
00013   compliance with the License. You may obtain a copy of the License at
00014   http://www.mozilla.org/MPL/
00015  
00016   Software distributed under the License is distributed on an "AS IS"
00017   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00018   the License for the specific language governing rights and
00019   limitations under the License.
00020 
00021   ======================================================================*/
00022 
00023 #include <assert.h>
00024 #include <string.h> /* for strdup */
00025 #include <stdlib.h> /* for malloc */
00026 #include <stdio.h>  /* for printf */
00027 #include <time.h>   /* for time() */
00028 #include <signal.h> /* for signal */
00029 #ifndef WIN32
00030 #include <unistd.h> /* for alarm */
00031 #endif
00032 
00033 #include "ical.h"
00034 #include "icalss.h"
00035 #include "regression.h"
00036 
00037 extern int VERBOSE;
00038 
00039 #ifdef WIN32
00040 #define snprintf     _snprintf
00041 #define strcasecmp   stricmp
00042 #endif
00043 
00044 
00045 static void sig_alrm(int i){
00046     fprintf(stderr,"Could not get lock on file\n");
00047     exit(1);
00048 }
00049 
00050 /* Get the expected result about the purpose of the property*/
00051 
00052 static int get_expected_numevents(icalcomponent *c)
00053 {
00054     icalproperty *p;
00055     const char* note = 0;
00056     int num_events = 0;
00057 
00058     if(c != 0){
00059         for(p = icalcomponent_get_first_property(c,ICAL_X_PROPERTY);
00060             p!= 0;
00061             p = icalcomponent_get_next_property(c,ICAL_X_PROPERTY)){
00062             if(strcmp(icalproperty_get_x_name(p),"X-EXPECT-NUMEVENTS")==0){
00063              note = icalproperty_get_x(p);
00064             }
00065         }
00066     } 
00067     
00068     if(note != 0){
00069       num_events = atoi(note);
00070     }
00071     
00072     
00073     return num_events;
00074 }
00075 
00076 
00077 
00078 static void recur_callback(icalcomponent *comp,
00079                         struct icaltime_span *span,
00080                         void *data)
00081 {
00082   int *num_recurs = data;
00083 
00084   if (VERBOSE) {
00085     printf("recur: %s", ctime(&span->start));
00086     printf("       %s", ctime(&span->end));
00087   }
00088   *num_recurs = *num_recurs + 1;
00089 }
00090 
00091 void test_recur_file()
00092 {
00093     icalset *cin = 0;
00094     struct icaltimetype next;
00095     icalcomponent *itr;
00096     icalproperty *desc, *dtstart, *rrule;
00097     struct icalrecurrencetype recur;
00098     icalrecur_iterator* ritr;
00099     time_t tt;
00100     char* file; 
00101     int num_recurs_found = 0;
00102        
00103     icalerror_set_error_state(ICAL_PARSE_ERROR, ICAL_ERROR_NONFATAL);
00104        
00105 #ifndef WIN32
00106     signal(SIGALRM,sig_alrm);
00107 #endif
00108     file = getenv("ICAL_RECUR_FILE");
00109     if (!file)
00110       file = "../../test-data/recur.txt";
00111        
00112 #ifndef WIN32
00113     alarm(15); /* to get file lock */
00114 #endif
00115     cin = icalfileset_new(file);
00116 #ifndef WIN32
00117     alarm(0);
00118 #endif
00119        
00120     ok("opening file with recurring events", (cin!=NULL));
00121     assert(cin!=NULL);
00122        
00123     for (itr = icalfileset_get_first_component(cin);
00124        itr != 0;
00125        itr = icalfileset_get_next_component(cin)){
00126       int badcomp = 0;
00127       int expected_events = 0;
00128       char msg[128];
00129 
00130 
00131       struct icaltimetype start = icaltime_null_time();
00132       struct icaltimetype startmin = icaltime_from_timet(1,0);
00133       struct icaltimetype endmax = icaltime_null_time();
00134       const char *desc_str = "malformed component";
00135 
00136       desc = icalcomponent_get_first_property(itr,ICAL_DESCRIPTION_PROPERTY);
00137       dtstart = icalcomponent_get_first_property(itr,ICAL_DTSTART_PROPERTY);
00138       rrule = icalcomponent_get_first_property(itr,ICAL_RRULE_PROPERTY);
00139       if (desc) {
00140        desc_str = icalproperty_get_description(desc);
00141       }
00142       
00143       ok((char*)desc_str, !(desc == 0 || dtstart == 0 || rrule == 0));
00144 
00145       if (desc == 0 || dtstart == 0 || rrule == 0) {
00146        badcomp = 1;
00147        if (VERBOSE) {
00148          printf("\n******** Error in input component ********\n");
00149          printf("The following component is malformed:\n %s\n", desc_str);
00150        }
00151        continue;
00152       }
00153       if (VERBOSE) {
00154        printf("\n\n#### %s\n",desc_str);
00155        printf("#### %s\n",icalvalue_as_ical_string(icalproperty_get_value(rrule)));
00156       }
00157       
00158       recur = icalproperty_get_rrule(rrule);
00159       start = icalproperty_get_dtstart(dtstart);
00160       
00161       ritr = icalrecur_iterator_new(recur,start);
00162       
00163       tt = icaltime_as_timet(start);
00164       
00165       if (VERBOSE)
00166        printf("#### %s\n",ctime(&tt ));
00167       
00168       icalrecur_iterator_free(ritr);
00169       
00170       for(ritr = icalrecur_iterator_new(recur,start),
00171            next = icalrecur_iterator_next(ritr); 
00172          !icaltime_is_null_time(next);
00173          next = icalrecur_iterator_next(ritr)){
00174        
00175        tt = icaltime_as_timet(next);
00176        
00177        if (VERBOSE)
00178          printf("  %s",ctime(&tt ));             
00179        
00180       }
00181 
00182       icalrecur_iterator_free(ritr);
00183       num_recurs_found = 0;
00184       expected_events = get_expected_numevents(itr);
00185 
00186       icalcomponent_foreach_recurrence(itr, startmin, endmax, 
00187                                    recur_callback, &num_recurs_found);
00188       
00189       sprintf(msg,"   expecting total of %d events", expected_events);
00190       int_is(msg, num_recurs_found, expected_events);
00191     }
00192     
00193     icalset_free(cin);
00194 }