00001 #include "config.h"
00002
00003 #include <iostream>
00004 #include <cassert>
00005
00006 #ifdef HAVE_UNISTD_H
00007 #include <unistd.h>
00008 #endif
00009
00010 #include "asserts.h"
00011 #include "error.h"
00012 #include "timer.h"
00013
00014
00015 #define ERR_OUT(s)
00016
00017 template<class T>
00018 T absolute(const T& num)
00019 {
00020 if (num < 0) {
00021 return(-num);
00022 }
00023 return(num);
00024 }
00025
00026 void test1(void)
00027 {
00028 bool done = false;
00029 int retry = 0;
00030
00031 while (!done && (retry++ < 3)) {
00032 timer t;
00033
00034 t.start();
00035 sleep(5);
00036 t.stop();
00037
00038 if (t.duration() != "00:05.0") {
00039 std::cout << "Once in a blue moon this test fails -- I don't know why."
00040 << std::endl;
00041 std::cout << "t.duration() = " << t.duration() << std::endl;
00042 std::cout << "t.duration() should be 00:05.0" << std::endl;
00043 }
00044 if (t.duration() == "00:05.0") {
00045 done = true;
00046 }
00047 }
00048 assert(done);
00049 }
00050
00051 void test2(void)
00052 {
00053 timer t;
00054 timer::duration_type d1, d2, d;
00055 int retry = 0;
00056
00057 d1 = 0.0;
00058 while ((d1 != 5.0) && (retry++ < 10)) {
00059 t.start();
00060 sleep(5);
00061 t.stop();
00062
00063 d1 = t.duration_secs();
00064 }
00065
00066 if (d1 != 5.0) {
00067 std::cout << "Once in a blue moon this test fails -- I don't know why."
00068 << std::endl;
00069 std::cout << "d1 = " << d1 << std::endl;
00070 std::cout << "d1 should be 5.0" << std::endl;
00071 }
00072 d = absolute(d1-5.0);
00073 assert(d < 1.0);
00074
00075 d1 = t.duration_mins();
00076 d2 = t.duration_secs()/60;
00077 d = absolute(d1-d2);
00078 assert(d < 1.0);
00079
00080 d1 = t.duration_hours();
00081 d2 = t.duration_secs()/60/60;
00082 d = absolute(d1-d2);
00083 assert(d < 1.0);
00084
00085 d1 = t.duration_days();
00086 d2 = t.duration_secs()/60/60/24;
00087 d = absolute(d1-d2);
00088 assert(d < 1.0);
00089
00090 d1 = t.duration_years();
00091 d2 = t.duration_secs()/60/60/24/365;
00092 d = absolute(d1-d2);
00093 assert(d < 1.0);
00094 }
00095
00096 void test3(void)
00097 {
00098 timer::value_type base_value = 1067356170;
00099 timer::value_type start_value;
00100 timer::value_type stop_value;
00101 timer t(base_value, base_value+5);
00102
00103 t.use_localtime(false);
00104
00105
00106
00107
00108
00109
00110
00111 assert(t.start_value() == base_value);
00112 assert(t.stop_value() == base_value+5);
00113 assert(t.started_at() == "2003 Oct 28 15:49:30");
00114 assert(t.stopped_at() == "2003 Oct 28 15:49:35");
00115 assert(t.duration() == "00:05.0");
00116
00117 t.assign(base_value, base_value+(5*60));
00118 t.use_localtime(false);
00119
00120
00121
00122
00123
00124
00125
00126 assert(t.start_value() == base_value);
00127 assert(t.stop_value() == base_value+(5*60));
00128 assert(t.started_at() == "2003 Oct 28 15:49:30");
00129 assert(t.stopped_at() == "2003 Oct 28 15:54:30");
00130 assert(t.duration() == "05:00.0");
00131
00132 t.assign(base_value, base_value+(5*60*60));
00133 t.use_localtime(false);
00134
00135
00136
00137
00138
00139
00140
00141 assert(t.start_value() == base_value);
00142 assert(t.stop_value() == base_value+(5*60*60));
00143 assert(t.started_at() == "2003 Oct 28 15:49:30");
00144 assert(t.stopped_at() == "2003 Oct 28 20:49:30");
00145 assert(t.duration() == "05:00:00.0");
00146
00147 t.assign(base_value, base_value+(3*60*60*24));
00148 t.use_localtime(false);
00149
00150
00151
00152
00153
00154
00155
00156 assert(t.start_value() == base_value);
00157 assert(t.stop_value() == base_value+(3*60*60*24));
00158 assert(t.started_at() == "2003 Oct 28 15:49:30");
00159 assert(t.stopped_at() == "2003 Oct 31 15:49:30");
00160 assert(t.duration() == "3d 00:00:00.0");
00161
00162 t.assign(base_value, base_value+(5*60*60*24));
00163 t.use_localtime(false);
00164
00165
00166
00167
00168
00169
00170
00171 assert(t.start_value() == base_value);
00172 assert(t.stop_value() == base_value+(5*60*60*24));
00173 assert(t.started_at() == "2003 Oct 28 15:49:30");
00174 assert(t.stopped_at() == "2003 Nov 2 15:49:30");
00175 assert(t.duration() == "5d 00:00:00.0");
00176
00177 t.assign(base_value, base_value+(64*60*60*24));
00178 t.use_localtime(false);
00179
00180
00181
00182
00183
00184
00185
00186 assert(t.start_value() == base_value);
00187 assert(t.stop_value() == base_value+(64*60*60*24));
00188 assert(t.started_at() == "2003 Oct 28 15:49:30");
00189 assert(t.stopped_at() == "2003 Dec 31 15:49:30");
00190 assert(t.duration() == "64d 00:00:00.0");
00191
00192 t.assign(base_value, base_value+(65*60*60*24));
00193 t.use_localtime(false);
00194
00195
00196
00197
00198
00199
00200
00201 assert(t.start_value() == base_value);
00202 assert(t.stop_value() == base_value+(65*60*60*24));
00203 assert(t.started_at() == "2003 Oct 28 15:49:30");
00204 assert(t.stopped_at() == "2004 Jan 1 15:49:30");
00205 assert(t.duration() == "65d 00:00:00.0");
00206
00207 start_value = base_value;
00208 stop_value = base_value +5;
00209 t.assign(start_value, stop_value);
00210 t.use_localtime(false);
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248 assert(t.start_value() == start_value);
00249 assert(t.stop_value() == stop_value);
00250 assert(t.started_at() == "2003 Oct 28 15:49:30");
00251 assert(t.stopped_at() == "2003 Oct 28 15:49:35");
00252 assert(t.duration() == "00:05.0");
00253 assert(t.eta(0) == "??:??.?");
00254 assert(t.eta(1) == "08:15.0");
00255 assert(t.eta(10) == "00:45.0");
00256 assert(t.eta(50) == "00:05.0");
00257 assert(t.eta(75) == "00:01.6");
00258 assert(t.eta(93) == "00:00.3");
00259 assert(t.eta(99) == "00:00.0");
00260 assert(t.eta(100) == "00:00.0");
00261 assert(t.eta(101) == "-00:00.0");
00262 assert(t.eta(107) == "-00:00.3");
00263 assert(t.eta(125) == "-00:01.0");
00264 assert(t.eta(150) == "-00:01.6");
00265 assert(t.eta(190) == "-00:02.3");
00266 assert(t.eta(199) == "-00:02.4");
00267 assert(t.eta(200) == "-00:02.5");
00268 assert(t.eta(0,100) == "??:??.?");
00269 assert(t.eta(1,100) == "08:15.0");
00270 assert(t.eta(10,100) == "00:45.0");
00271 assert(t.eta(50,100) == "00:05.0");
00272 assert(t.eta(75,100) == "00:01.6");
00273 assert(t.eta(93,100) == "00:00.3");
00274 assert(t.eta(99,100) == "00:00.0");
00275 assert(t.eta(100,100) == "00:00.0");
00276 assert(t.eta(101,100) == "-00:00.0");
00277 assert(t.eta(107,100) == "-00:00.3");
00278 assert(t.eta(125,100) == "-00:01.0");
00279 assert(t.eta(150,100) == "-00:01.6");
00280 assert(t.eta(190,100) == "-00:02.3");
00281 assert(t.eta(199,100) == "-00:02.4");
00282 assert(t.eta(200,100) == "-00:02.5");
00283
00284 start_value = base_value;
00285 stop_value = base_value +5*60*60*24*365 +6*60*60*24 +7*60*60 +8*60 +9;
00286 t.assign(start_value, stop_value);
00287 t.use_localtime(false);
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 assert(t.start_value() == 1067356170);
00326 assert(t.stop_value() == 1225580259);
00327 assert(t.started_at() == "2003 Oct 28 15:49:30");
00328 assert(t.stopped_at() == "2008 Nov 1 22:57:39");
00329 assert(t.duration() == "5y 6d 07:08:09.0");
00330 assert(t.eta(0) == "??:??.?");
00331 assert(t.eta(1) == "496y 258d 10:26:51.0");
00332 assert(t.eta(10) == "45y 56d 16:13:21.0");
00333 assert(t.eta(50) == "5y 6d 07:08:09.0");
00334 assert(t.eta(75) == "1y 245d 10:22:43.0");
00335 assert(t.eta(93) == "137d 20:09:00.0");
00336 assert(t.eta(99) == "18d 11:57:03.1");
00337 assert(t.eta(100) == "00:00.0");
00338 assert(t.eta(101) == "-18d 03:09:35.1");
00339 assert(t.eta(107) == "-119d 19:18:28.6");
00340 assert(
00341 (t.eta(125) == "-1y 1d 06:13:37.8")
00342 || (t.eta(125) == "-1y 1d 06:13:37.7")
00343 );
00344 assert(t.eta(150) == "-1y 245d 10:22:43.0");
00345 assert(t.eta(190) == "-2y 137d 10:57:32.6");
00346 assert(t.eta(199) == "-2y 181d 01:08:16.5");
00347 assert(t.eta(200) == "-2y 185d 15:34:04.5");
00348 assert(t.eta(0,100) == "??:??.?");
00349 assert(t.eta(1,100) == "496y 258d 10:26:51.0");
00350 assert(t.eta(10,100) == "45y 56d 16:13:21.0");
00351 assert(t.eta(50,100) == "5y 6d 07:08:09.0");
00352 assert(t.eta(75,100) == "1y 245d 10:22:43.0");
00353 assert(t.eta(93,100) == "137d 20:09:00.0");
00354 assert(t.eta(99,100) == "18d 11:57:03.1");
00355 assert(t.eta(100,100) == "00:00.0");
00356 assert(t.eta(101,100) == "-18d 03:09:35.1");
00357 assert(t.eta(107,100) == "-119d 19:18:28.6");
00358 assert(
00359 (t.eta(125,100) == "-1y 1d 06:13:37.8")
00360 || (t.eta(125,100) == "-1y 1d 06:13:37.7")
00361 );
00362 assert(t.eta(150,100) == "-1y 245d 10:22:43.0");
00363 assert(t.eta(190,100) == "-2y 137d 10:57:32.6");
00364 assert(t.eta(199,100) == "-2y 181d 01:08:16.5");
00365 assert(t.eta(200,100) == "-2y 185d 15:34:04.5");
00366 }
00367
00368 int main(int argc, char const * argv[])
00369 {
00370 try {
00371 test1();
00372 test2();
00373 test3();
00374 }
00375 catch(error e) {
00376 std::cerr << e;
00377 assert(0);
00378 }
00379 catch(...) {
00380 std::cerr << err_unknown;
00381 assert(0);
00382 }
00383 return(0);
00384 }
00385