rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-tstamp.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #include <iostream>
4 #include <iomanip>
5 #include <string>
6 #include <cassert>
7 
8 #ifdef HAVE_TIME_H
9 #include <time.h>
10 #endif
11 #ifdef HAVE_SYS_TYPES_H
12 #include <sys/types.h>
13 #endif
14 
15 #include "asserts.h"
16 #include "error.h"
17 #include "estring.h"
18 #include "tstamp.h"
19 
20 void test1(void)
21 {
22  timestamp t;
23  std::string str;
24  estring estr;
25 
26  estr.width(4);
27  estr.align(estring::right);
28  estr.left_fillchar('0');
29  estr = t.year();
30  str += estr.fmt_str();
31 
32  str += "-";
33 
34  estr.width(2);
35  estr.align(estring::right);
36  estr.left_fillchar('0');
37  estr = t.month();
38  str += estr.fmt_str();
39 
40  str += "-";
41 
42  estr.width(2);
43  estr.align(estring::right);
44  estr.left_fillchar('0');
45  estr = t.day();
46  str += estr.fmt_str();
47 
48  str += ".";
49 
50  estr.width(2);
51  estr.align(estring::right);
52  estr.left_fillchar('0');
53  estr = t.hour();
54  str += estr.fmt_str();
55 
56  estr.width(2);
57  estr.align(estring::right);
58  estr.left_fillchar('0');
59  estr = t.minute();
60  str += estr.fmt_str();
61 
62  estr.width(2);
63  estr.align(estring::right);
64  estr.left_fillchar('0');
65  estr = t.second();
66  str += estr.fmt_str();
67 
68  // cerr << "t.str() = " << t.str() << endl;
69  assert(t.str(timestamp::resolution_second) == str);
70 }
71 
72 void test2(void)
73 {
74  time_t t;
75  struct tm* l = 0;
76  timestamp ts;
77  int year = 0;
78  int month = 0;
79  int day = 0;
80  int hour = 0;
81  int minute = 0;
82  int second = 0;
83 
84  t = time(0);
85  l = localtime(&t);
86  ts.set();
87  year = l->tm_year + 1900;
88  month = l->tm_mon + 1;
89  day = l->tm_mday;
90  hour = l->tm_hour;
91  minute = l->tm_min;
92  second = l->tm_sec;
93 
94  assert(ts.year() == year);
95  assert(ts.month() == month);
96  assert(ts.day() == day);
97  assert(ts.hour() == hour);
98  assert(ts.minute() == minute);
99  assert(ts.second() == second);
100 }
101 
102 void test3(void)
103 {
104  timestamp t;
105  bool thrown;
106 
107  thrown = false; try { t.assign(""); } catch(...) { thrown = true; }
108  assert(thrown);
109 
110  thrown = false; try { t.assign("a"); } catch(...) { thrown = true; }
111  assert(thrown);
112 
113  thrown = false; try { t.assign("2"); } catch(...) { thrown = true; }
114  assert(thrown);
115 
116  thrown = false; try { t.assign("20"); } catch(...) { thrown = true; }
117  assert(thrown);
118 
119  thrown = false; try { t.assign("200"); } catch(...) { thrown = true; }
120  assert(thrown);
121 
122  thrown = false; try { t.assign("2003"); } catch(...) { thrown = true; }
123  assert(!thrown);
124  assert(t.year() == 2003);
125  assert(t.month() == 1);
126  assert(t.day() == 1);
127  assert(t.hour() == 0);
128  assert(t.minute() == 0);
129  assert(t.second() == 0);
130  assert(t.resolution() == timestamp::resolution_year);
131 
132  thrown = false; try { t.assign("2a03"); } catch(...) { thrown = true; }
133  assert(thrown);
134 
135  thrown = false; try { t.assign("2003-"); } catch(...) { thrown = true; }
136  assert(thrown);
137 
138  thrown = false; try { t.assign("2003-1"); } catch(...) { thrown = true; }
139  assert(thrown);
140 
141  thrown = false; try { t.assign("2003-10"); } catch(...) { thrown = true; }
142  assert(!thrown);
143  assert(t.year() == 2003);
144  assert(t.month() == 10);
145  assert(t.day() == 1);
146  assert(t.hour() == 0);
147  assert(t.minute() == 0);
148  assert(t.second() == 0);
150 
151  thrown = false; try { t.assign("2003-1b"); } catch(...) { thrown = true; }
152  assert(thrown);
153 
154  thrown = false; try { t.assign("2003-19"); } catch(...) { thrown = true; }
155  assert(thrown);
156 
157  thrown = false; try { t.assign("2003-10-"); } catch(...) { thrown = true; }
158  assert(thrown);
159 
160  thrown = false; try { t.assign("2003-10-1"); } catch(...) { thrown = true; }
161  assert(thrown);
162 
163  thrown = false; try { t.assign("2003-10-19"); } catch(...) { thrown = true; }
164  assert(!thrown);
165  assert(t.year() == 2003);
166  assert(t.month() == 10);
167  assert(t.day() == 19);
168  assert(t.hour() == 0);
169  assert(t.minute() == 0);
170  assert(t.second() == 0);
171  assert(t.resolution() == timestamp::resolution_day);
172 
173  thrown = false; try { t.assign("2003-10-19."); } catch(...) { thrown = true; }
174  assert(thrown);
175 
176  thrown = false; try { t.assign("2003-10-19.2"); } catch(...) { thrown = true; }
177  assert(thrown);
178 
179  thrown = false; try { t.assign("2003-10-19.24"); } catch(...) { thrown = true; }
180  assert(thrown);
181 
182  thrown = false; try { t.assign("2003-10-19.23"); } catch(...) { thrown = true; }
183  assert(!thrown);
184  assert(t.year() == 2003);
185  assert(t.month() == 10);
186  assert(t.day() == 19);
187  assert(t.hour() == 23);
188  assert(t.minute() == 0);
189  assert(t.second() == 0);
190  assert(t.resolution() == timestamp::resolution_hour);
191 
192  thrown = false; try { t.assign("2003-10-19.230"); } catch(...) { thrown = true; }
193  assert(thrown);
194 
195  thrown = false; try { t.assign("2003-10-19.2307"); } catch(...) { thrown = true; }
196  assert(!thrown);
197  assert(t.year() == 2003);
198  assert(t.month() == 10);
199  assert(t.day() == 19);
200  assert(t.hour() == 23);
201  assert(t.minute() == 7);
202  assert(t.second() == 0);
204 
205  thrown = false; try { t.assign("2003-10-19.23070"); } catch(...) { thrown = true; }
206  assert(thrown);
207 
208  thrown = false; try { t.assign("2003-10-19.230708"); } catch(...) { thrown = true; }
209  assert(!thrown);
210  assert(t.year() == 2003);
211  assert(t.month() == 10);
212  assert(t.day() == 19);
213  assert(t.hour() == 23);
214  assert(t.minute() == 7);
215  assert(t.second() == 8);
217 
218  thrown = false; try { t.assign("2003-10-19.2307089"); } catch(...) { thrown = true; }
219  assert(thrown);
220 }
221 
222 int main(int argc, char const * argv[])
223 {
224  try {
225  test1();
226  test2();
227  test3();
228  }
229  catch(error e) {
230  std::cerr << e;
231  assert(0);
232  }
233  catch(...) {
234  std::cerr << err_unknown;
235  assert(0);
236  }
237  return(0);
238 }
Timestamp object.
Definition: tstamp.h:13
int year(void) const
Return the timestamp year.
Definition: tstamp.cc:438
int day(void) const
Return the timestamp day.
Definition: tstamp.cc:426
An extended string class.
Definition: estring.h:52
int month(void) const
Return the timestamp month.
Definition: tstamp.cc:432
Right-justified.
Definition: estring.h:66
int main(int argc, char const *argv[])
Definition: test-tstamp.cc:222
char left_fillchar(const char a_char)
Set the fill character used to padd the left side of a formatted string.
Definition: estring.cc:603
#define err_unknown
Definition: error.h:114
void assign(const timestamp &a_t)
Set the timestamp to the value of another timestamp.
Definition: tstamp.cc:79
An error class.
Definition: error.h:72
value_type fmt_str(void)
Generate a formatted string.
Definition: estring.cc:688
void test3(void)
Definition: test-tstamp.cc:102
int second(void) const
Return the timestamp second.
Definition: tstamp.cc:408
void resolution(resolution_type a_r)
Set the timestamp resolution.
Definition: tstamp.cc:302
void set(void)
Set the timestamp to the current time and date.
Definition: tstamp.cc:58
size_type width(const size_type a_l)
Set the width of a formatted string.
Definition: estring.cc:446
int hour(void) const
Return the timestamp hour.
Definition: tstamp.cc:420
alignment align(const alignment a_alignment)
Set the alignment used for formatted strings.
Definition: estring.cc:471
const std::string str(void) const
Generate a string.
Definition: tstamp.cc:387
void test2(void)
Definition: test-tstamp.cc:72
void test1(void)
Definition: test-tstamp.cc:20
int minute(void) const
Return the timestamp minute.
Definition: tstamp.cc:414