test-tstamp.cc

Go to the documentation of this file.
00001 #include "config.h"
00002 
00003 #include <iostream>
00004 #include <iomanip>
00005 #include <string>
00006 #include <cassert>
00007 
00008 #ifdef HAVE_TIME_H
00009 #include <time.h>
00010 #endif
00011 #ifdef HAVE_SYS_TYPES_H
00012 #include <sys/types.h>
00013 #endif
00014 
00015 #include "asserts.h"
00016 #include "error.h"
00017 #include "estring.h"
00018 #include "tstamp.h"
00019 
00020 void test1(void)
00021 {
00022         timestamp t;
00023         std::string str;
00024         estring estr;
00025 
00026         estr.width(4);
00027         estr.align(estring::right);
00028         estr.left_fillchar('0');
00029         estr = t.year();
00030         str += estr.fmt_str();
00031 
00032         str += "-";
00033 
00034         estr.width(2);
00035         estr.align(estring::right);
00036         estr.left_fillchar('0');
00037         estr = t.month();
00038         str += estr.fmt_str();
00039 
00040         str += "-";
00041 
00042         estr.width(2);
00043         estr.align(estring::right);
00044         estr.left_fillchar('0');
00045         estr = t.day();
00046         str += estr.fmt_str();
00047 
00048         str += ".";
00049 
00050         estr.width(2);
00051         estr.align(estring::right);
00052         estr.left_fillchar('0');
00053         estr = t.hour();
00054         str += estr.fmt_str();
00055 
00056         estr.width(2);
00057         estr.align(estring::right);
00058         estr.left_fillchar('0');
00059         estr = t.minute();
00060         str += estr.fmt_str();
00061 
00062         estr.width(2);
00063         estr.align(estring::right);
00064         estr.left_fillchar('0');
00065         estr = t.second();
00066         str += estr.fmt_str();
00067 
00068         // cerr << "t.str() = " << t.str() << endl;
00069         assert(t.str(timestamp::resolution_second) == str);
00070 }
00071 
00072 void test2(void)
00073 {
00074         time_t t;
00075         struct tm* l = 0;
00076         timestamp ts;
00077         int year = 0;
00078         int month = 0;
00079         int day = 0;
00080         int hour = 0;
00081         int minute = 0;
00082         int second = 0;
00083 
00084         t = time(0);
00085         l = localtime(&t);
00086         ts.set();
00087         year = l->tm_year + 1900;
00088         month = l->tm_mon + 1;
00089         day = l->tm_mday;
00090         hour = l->tm_hour;
00091         minute = l->tm_min;
00092         second = l->tm_sec;
00093 
00094         assert(ts.year() == year);
00095         assert(ts.month() == month);
00096         assert(ts.day() == day);
00097         assert(ts.hour() == hour);
00098         assert(ts.minute() == minute);
00099         assert(ts.second() == second);
00100 }
00101 
00102 void test3(void)
00103 {
00104         timestamp t;
00105         bool thrown;
00106 
00107         thrown = false; try { t.assign(""); } catch(...) { thrown = true; }
00108         assert(thrown);
00109 
00110         thrown = false; try { t.assign("a"); } catch(...) { thrown = true; }
00111         assert(thrown);
00112 
00113         thrown = false; try { t.assign("2"); } catch(...) { thrown = true; }
00114         assert(thrown);
00115 
00116         thrown = false; try { t.assign("20"); } catch(...) { thrown = true; }
00117         assert(thrown);
00118 
00119         thrown = false; try { t.assign("200"); } catch(...) { thrown = true; }
00120         assert(thrown);
00121 
00122         thrown = false; try { t.assign("2003"); } catch(...) { thrown = true; }
00123         assert(!thrown);
00124         assert(t.year() == 2003);
00125         assert(t.month() == 1);
00126         assert(t.day() == 1);
00127         assert(t.hour() == 0);
00128         assert(t.minute() == 0);
00129         assert(t.second() == 0);
00130         assert(t.resolution() == timestamp::resolution_year);
00131 
00132         thrown = false; try { t.assign("2a03"); } catch(...) { thrown = true; }
00133         assert(thrown);
00134 
00135         thrown = false; try { t.assign("2003-"); } catch(...) { thrown = true; }
00136         assert(thrown);
00137 
00138         thrown = false; try { t.assign("2003-1"); } catch(...) { thrown = true; }
00139         assert(thrown);
00140 
00141         thrown = false; try { t.assign("2003-10"); } catch(...) { thrown = true; }
00142         assert(!thrown);
00143         assert(t.year() == 2003);
00144         assert(t.month() == 10);
00145         assert(t.day() == 1);
00146         assert(t.hour() == 0);
00147         assert(t.minute() == 0);
00148         assert(t.second() == 0);
00149         assert(t.resolution() == timestamp::resolution_month);
00150 
00151         thrown = false; try { t.assign("2003-1b"); } catch(...) { thrown = true; }
00152         assert(thrown);
00153 
00154         thrown = false; try { t.assign("2003-19"); } catch(...) { thrown = true; }
00155         assert(thrown);
00156 
00157         thrown = false; try { t.assign("2003-10-"); } catch(...) { thrown = true; }
00158         assert(thrown);
00159 
00160         thrown = false; try { t.assign("2003-10-1"); } catch(...) { thrown = true; }
00161         assert(thrown);
00162 
00163         thrown = false; try { t.assign("2003-10-19"); } catch(...) { thrown = true; }
00164         assert(!thrown);
00165         assert(t.year() == 2003);
00166         assert(t.month() == 10);
00167         assert(t.day() == 19);
00168         assert(t.hour() == 0);
00169         assert(t.minute() == 0);
00170         assert(t.second() == 0);
00171         assert(t.resolution() == timestamp::resolution_day);
00172 
00173         thrown = false; try { t.assign("2003-10-19."); } catch(...) { thrown = true; }
00174         assert(thrown);
00175 
00176         thrown = false; try { t.assign("2003-10-19.2"); } catch(...) { thrown = true; }
00177         assert(thrown);
00178 
00179         thrown = false; try { t.assign("2003-10-19.24"); } catch(...) { thrown = true; }
00180         assert(thrown);
00181 
00182         thrown = false; try { t.assign("2003-10-19.23"); } catch(...) { thrown = true; }
00183         assert(!thrown);
00184         assert(t.year() == 2003);
00185         assert(t.month() == 10);
00186         assert(t.day() == 19);
00187         assert(t.hour() == 23);
00188         assert(t.minute() == 0);
00189         assert(t.second() == 0);
00190         assert(t.resolution() == timestamp::resolution_hour);
00191 
00192         thrown = false; try { t.assign("2003-10-19.230"); } catch(...) { thrown = true; }
00193         assert(thrown);
00194 
00195         thrown = false; try { t.assign("2003-10-19.2307"); } catch(...) { thrown = true; }
00196         assert(!thrown);
00197         assert(t.year() == 2003);
00198         assert(t.month() == 10);
00199         assert(t.day() == 19);
00200         assert(t.hour() == 23);
00201         assert(t.minute() == 7);
00202         assert(t.second() == 0);
00203         assert(t.resolution() == timestamp::resolution_minute);
00204 
00205         thrown = false; try { t.assign("2003-10-19.23070"); } catch(...) { thrown = true; }
00206         assert(thrown);
00207 
00208         thrown = false; try { t.assign("2003-10-19.230708"); } catch(...) { thrown = true; }
00209         assert(!thrown);
00210         assert(t.year() == 2003);
00211         assert(t.month() == 10);
00212         assert(t.day() == 19);
00213         assert(t.hour() == 23);
00214         assert(t.minute() == 7);
00215         assert(t.second() == 8);
00216         assert(t.resolution() == timestamp::resolution_second);
00217 
00218         thrown = false; try { t.assign("2003-10-19.2307089"); } catch(...) { thrown = true; }
00219         assert(thrown);
00220 }
00221 
00222 int main(int argc, char *argv[])
00223 {
00224         try {
00225                 test1();
00226                 test2();
00227                 test3();
00228         }
00229         catch(error e) {
00230                 std::cerr << e;
00231                 assert(0);
00232         }
00233         catch(...) {
00234                 std::cerr << err_unknown;
00235                 assert(0);
00236         }
00237         return(0);
00238 }

Generated on Thu Jun 5 11:12:58 2008 for rvm by  doxygen 1.5.1