Back to index

lightning-sunbird  0.9+nobinonly
regression-utils.c
Go to the documentation of this file.
00001 #include "ical.h"
00002 
00003 #include <stdlib.h>  /* abort() */
00004 #include <string.h>  /* strcmp() */
00005 
00006 static char ictt_str[1024];
00007 int VERBOSE = 0;
00008 int QUIET = 0;
00009 
00010 const char* ical_timet_string(const time_t t)
00011 {
00012     struct tm stm = *(gmtime(&t));    
00013 
00014     sprintf(ictt_str,"%02d-%02d-%02d %02d:%02d:%02d Z",stm.tm_year+1900,
00015            stm.tm_mon+1,stm.tm_mday,stm.tm_hour,stm.tm_min,stm.tm_sec);
00016 
00017     return ictt_str;
00018     
00019 }
00020 
00021 const char* ictt_as_string(struct icaltimetype t)
00022 {
00023     const char *zone = icaltimezone_get_tzid((icaltimezone *)t.zone);
00024 
00025     if (icaltime_is_utc(t))
00026        sprintf(ictt_str,"%02d-%02d-%02d %02d:%02d:%02d Z UTC",
00027        t.year,t.month,t.day, t.hour,t.minute,t.second);
00028     else
00029        sprintf(ictt_str,"%02d-%02d-%02d %02d:%02d:%02d %s",
00030        t.year,t.month,t.day, t.hour,t.minute,t.second,
00031        zone == NULL? "(floating)": zone);
00032 
00033     return ictt_str;
00034 }
00035 
00036 char* icaltime_as_ctime(struct icaltimetype t)
00037 {
00038     time_t tt;
00039  
00040     tt = icaltime_as_timet(t);
00041     sprintf(ictt_str,"%s",ctime(&tt));
00042 
00043     return ictt_str;
00044 }
00045 
00046 
00048 static int die_on_errors = 0;
00049 static int testnumber    = 0;
00050 static int failed        = 0;
00051 static int current_set   = 0;
00052 
00053 static struct {int set; int test;} failed_tests[1024];
00054 
00055 void die_on_errors_set(int val) {
00056   die_on_errors = 1;
00057 }
00058 
00059 void _ok(char* test_name, int success, char *file, int linenum, char *test) {
00060   testnumber++;
00061 
00062   if (!QUIET || (QUIET && !success))
00063   printf("%sok %d - %s\n", (success)?"" : "not ", testnumber, test_name);
00064   if (!success) {
00065     failed_tests[failed].set  = current_set;
00066     failed_tests[failed].test = testnumber;
00067     failed++;
00068 
00069     printf("# test failed: \"%s\"\n", test);
00070     printf("#          at: %s:%-d\n", file, linenum);
00071   }
00072 
00073   if (die_on_errors == 1 && !success) {
00074     abort();
00075   }
00076 }
00077 
00078 void _is(char* test_name, const char* str1, const char* str2, char *file, int linenum) {
00079   int diff;
00080   
00081   if (str1 == NULL || str2 == NULL) {
00082     diff = 1;
00083   } else {
00084     diff = strcmp(str1, str2);
00085   }
00086 
00087   if (!test_name) test_name = "()";
00088 
00089   _ok(test_name, (diff==0), file, linenum, "");
00090   
00091   if (diff) {
00092     printf("#      got: %s\n", str1 ? str1 : "(null)");
00093     printf("# expected: %s\n", str2 ? str2 : "(null)");
00094   }
00095 }
00096 
00097 void _int_is(char* test_name, int i1, int i2, char *file, int linenum) {
00098   _ok(test_name, (i1==i2), file, linenum, "");
00099   
00100   if (i1!=i2) {
00101     printf("#      got: %d\n", i1);
00102     printf("# expected: %d\n", i2);
00103   }
00104 }
00105 
00106 
00107 void verbose(int newval) {
00108   VERBOSE = newval;
00109 }
00110 
00111 void test_start(int numtests) {
00112   if (numtests) {
00113     if (!QUIET)
00114     printf("1..%-d\n", numtests);
00115   } else {
00116     if (!QUIET)
00117     printf("1..\n");
00118   }
00119 }
00120 
00121 void test_header(char *header, int set) {
00122   if (!QUIET)
00123   printf("########## %-40s (%d) ##########\n", header, set);
00124   current_set = set;
00125 }
00126 
00127 void test_end(void) {
00128   int pct;
00129 
00130   if (failed) {
00131     int i, oldset = 0;
00132     
00133     pct = ((testnumber - failed)*100)/testnumber;
00134     printf("\n        Failed %d/%d tests, %2d%% okay\n", failed, testnumber, pct);
00135     printf("\n        Failed tests:\n          ");
00136     for (i = 0; i < failed; i++) {
00137       int this_set = failed_tests[i].set;
00138       char *prefix = "";
00139       if (this_set != oldset) {
00140        prefix = "\n          ";
00141        oldset = this_set;
00142       }
00143       
00144       printf("%s%d/%d ", prefix, this_set, failed_tests[i].test);
00145     }
00146     printf("\n");
00147       
00148   } else {
00149     printf("\n        All Tests Successful.\n");
00150   }
00151 }
00152 
00153 
00154 void test_run(char *test_name, 
00155              void (*test_fcn)(void), 
00156              int do_test, int headeronly) 
00157 {
00158   static int test_set = 1;
00159 
00160   if (headeronly || do_test == 0 || do_test == test_set)
00161     test_header(test_name, test_set);
00162   
00163   if (!headeronly && (do_test==0 || do_test == test_set)) {
00164     (*test_fcn)();
00165     if (!QUIET)
00166     printf("\n");
00167   }
00168   test_set++;
00169 }