rvm 1.08
|
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 // #define ERR_OUT(s) std::cerr << s 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00106 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00107 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00108 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00109 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00121 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00122 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00123 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00124 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00136 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00137 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00138 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00139 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00151 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00152 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00153 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00154 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00166 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00167 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00168 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00169 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00181 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00182 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00183 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00184 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00196 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00197 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00198 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00199 std::cout << " t.duration() = " << t.duration() << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00213 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00214 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00215 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00216 std::cout << " t.duration() = " << t.duration() << std::endl; 00217 std::cout << " t.eta(0) = " << t.eta(0) << std::endl; 00218 std::cout << " t.eta(1) = " << t.eta(1) << std::endl; 00219 std::cout << " t.eta(10) = " << t.eta(10) << std::endl; 00220 std::cout << " t.eta(50) = " << t.eta(50) << std::endl; 00221 std::cout << " t.eta(75) = " << t.eta(75) << std::endl; 00222 std::cout << " t.eta(93) = " << t.eta(93) << std::endl; 00223 std::cout << " t.eta(99) = " << t.eta(99) << std::endl; 00224 std::cout << " t.eta(100) = " << t.eta(100) << std::endl; 00225 std::cout << " t.eta(101) = " << t.eta(101) << std::endl; 00226 std::cout << " t.eta(107) = " << t.eta(107) << std::endl; 00227 std::cout << " t.eta(125) = " << t.eta(125) << std::endl; 00228 std::cout << " t.eta(150) = " << t.eta(150) << std::endl; 00229 std::cout << " t.eta(190) = " << t.eta(190) << std::endl; 00230 std::cout << " t.eta(199) = " << t.eta(199) << std::endl; 00231 std::cout << " t.eta(200) = " << t.eta(200) << std::endl; 00232 std::cout << " t.eta(0,100) = " << t.eta(0,100) << std::endl; 00233 std::cout << " t.eta(1,100) = " << t.eta(1,100) << std::endl; 00234 std::cout << " t.eta(10,100) = " << t.eta(10,100) << std::endl; 00235 std::cout << " t.eta(50,100) = " << t.eta(50,100) << std::endl; 00236 std::cout << " t.eta(75,100) = " << t.eta(75,100) << std::endl; 00237 std::cout << " t.eta(93,100) = " << t.eta(93,100) << std::endl; 00238 std::cout << " t.eta(99,100) = " << t.eta(99,100) << std::endl; 00239 std::cout << " t.eta(100,100) = " << t.eta(100,100) << std::endl; 00240 std::cout << " t.eta(101,100) = " << t.eta(101,100) << std::endl; 00241 std::cout << " t.eta(107,100) = " << t.eta(107,100) << std::endl; 00242 std::cout << " t.eta(125,100) = " << t.eta(125,100) << std::endl; 00243 std::cout << " t.eta(150,100) = " << t.eta(150,100) << std::endl; 00244 std::cout << " t.eta(190,100) = " << t.eta(190,100) << std::endl; 00245 std::cout << " t.eta(199,100) = " << t.eta(199,100) << std::endl; 00246 std::cout << " t.eta(200,100) = " << t.eta(200,100) << std::endl; 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 std::cout << "t.start_value() = " << t.start_value() << std::endl; 00290 std::cout << " t.stop_value() = " << t.stop_value() << std::endl; 00291 std::cout << " t.started_at() = " << t.started_at() << std::endl; 00292 std::cout << " t.stopped_at() = " << t.stopped_at() << std::endl; 00293 std::cout << " t.duration() = " << t.duration() << std::endl; 00294 std::cout << " t.eta(0) = " << t.eta(0) << std::endl; 00295 std::cout << " t.eta(1) = " << t.eta(1) << std::endl; 00296 std::cout << " t.eta(10) = " << t.eta(10) << std::endl; 00297 std::cout << " t.eta(50) = " << t.eta(50) << std::endl; 00298 std::cout << " t.eta(75) = " << t.eta(75) << std::endl; 00299 std::cout << " t.eta(93) = " << t.eta(93) << std::endl; 00300 std::cout << " t.eta(99) = " << t.eta(99) << std::endl; 00301 std::cout << " t.eta(100) = " << t.eta(100) << std::endl; 00302 std::cout << " t.eta(101) = " << t.eta(101) << std::endl; 00303 std::cout << " t.eta(107) = " << t.eta(107) << std::endl; 00304 std::cout << " t.eta(125) = " << t.eta(125) << std::endl; 00305 std::cout << " t.eta(150) = " << t.eta(150) << std::endl; 00306 std::cout << " t.eta(190) = " << t.eta(190) << std::endl; 00307 std::cout << " t.eta(199) = " << t.eta(199) << std::endl; 00308 std::cout << " t.eta(200) = " << t.eta(200) << std::endl; 00309 std::cout << " t.eta(0,100) = " << t.eta(0,100) << std::endl; 00310 std::cout << " t.eta(1,100) = " << t.eta(1,100) << std::endl; 00311 std::cout << " t.eta(10,100) = " << t.eta(10,100) << std::endl; 00312 std::cout << " t.eta(50,100) = " << t.eta(50,100) << std::endl; 00313 std::cout << " t.eta(75,100) = " << t.eta(75,100) << std::endl; 00314 std::cout << " t.eta(93,100) = " << t.eta(93,100) << std::endl; 00315 std::cout << " t.eta(99,100) = " << t.eta(99,100) << std::endl; 00316 std::cout << " t.eta(100,100) = " << t.eta(100,100) << std::endl; 00317 std::cout << " t.eta(101,100) = " << t.eta(101,100) << std::endl; 00318 std::cout << " t.eta(107,100) = " << t.eta(107,100) << std::endl; 00319 std::cout << " t.eta(125,100) = " << t.eta(125,100) << std::endl; 00320 std::cout << " t.eta(150,100) = " << t.eta(150,100) << std::endl; 00321 std::cout << " t.eta(190,100) = " << t.eta(190,100) << std::endl; 00322 std::cout << " t.eta(199,100) = " << t.eta(199,100) << std::endl; 00323 std::cout << " t.eta(200,100) = " << t.eta(200,100) << std::endl; 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