00001 #include "config.h"
00002
00003 #ifdef HAVE_UNISTD_H
00004 #include <unistd.h>
00005 #endif
00006
00007 #include <iostream>
00008 #include <fstream>
00009 #include <cassert>
00010
00011 #include "asserts.h"
00012 #include "error.h"
00013 #include "estring.h"
00014 #include "table.h"
00015
00016
00017 #define ERR_OUT(s)
00018
00019 class check_output
00020 {
00021 public:
00022 bool open(const std::string& a_filename)
00023 {
00024 if (m_in.is_open())
00025 m_in.close();
00026 m_in.clear();
00027 m_in.open(a_filename.c_str());
00028 if (!m_in.is_open() || !m_in) {
00029 return(false);
00030 }
00031 return(true);
00032 }
00033
00034 bool close(void)
00035 {
00036 m_in.close();
00037 return(true);
00038 }
00039
00040 bool is_open(void)
00041 {
00042 bool value;
00043
00044 value = m_in.is_open();
00045 return(value);
00046 }
00047
00048 bool get_line(void)
00049 {
00050 char buffer[1024] = { 0 };
00051
00052 if (!m_in.getline(buffer,1024)) {
00053 return(false);
00054 }
00055 try {
00056 m_str = buffer;
00057 }
00058 catch(...) {
00059 std::cerr
00060 << "*** ERROR: check_output::get_line() - Out of memory?"
00061 << std::endl;
00062 return(false);
00063 }
00064 return(true);
00065 }
00066
00067 const std::string& str(void) const
00068 {
00069 return(m_str);
00070 }
00071
00072 bool remove(const std::string& a_filename)
00073 {
00074 if (unlink(a_filename.c_str()) != 0) {
00075 return(false);
00076 }
00077 return(true);
00078 }
00079
00080 private:
00081 std::string m_str;
00082 std::ifstream m_in;
00083 };
00084
00085 check_output check;
00086
00087 void test1(void)
00088 {
00089 table_cell_base tcb;
00090
00091 assert(tcb.x == table_cell_base::npos);
00092 assert(tcb.y == table_cell_base::npos);
00093
00094 table_cell_base tcb2;
00095
00096 assert(tcb == tcb);
00097 assert(!(tcb == tcb2));
00098
00099 std::ofstream out;
00100 out.open("./test-table.txt");
00101 assert(out.is_open());
00102 tcb.write(out,0,10);
00103 out.close();
00104
00105 assert(check.open("./test-table.txt"));
00106 assert(!check.get_line());
00107 assert(check.close());
00108 assert(check.remove("./test-table.txt"));
00109 }
00110
00111 void test2(void)
00112 {
00113 table_cell_estring tce;
00114 estring estr;
00115
00116 estr = "Hello World";
00117 tce.assign(estr);
00118
00119 assert(tce.height() == 1);
00120 assert(tce.width() == estr.size());
00121
00122 table_cell_estring tce2(estr);
00123
00124 assert(tce2.height() == 1);
00125 assert(tce2.width() == estr.size());
00126
00127 table_cell_estring tce3(tce);
00128
00129 assert(tce3.height() == 1);
00130 assert(tce3.width() == estr.size());
00131
00132 std::ofstream out;
00133 out.open("./test-table.txt");
00134 assert(out.is_open());
00135 tce.write(out,0,15);
00136 out << std::endl;
00137 tce.write(out,1,15);
00138 out << std::endl;
00139 out.close();
00140
00141 assert(check.open("./test-table.txt"));
00142 assert(check.get_line() && check.str() == "Hello World ");
00143 assert(check.get_line() && check.str() == " ");
00144 assert(!check.get_line());
00145 assert(check.close());
00146 assert(check.remove("./test-table.txt"));
00147 }
00148
00149 void test3(void)
00150 {
00151 {
00152 table t;
00153
00154 assert(t.ncols() == 0);
00155 assert(t.nrows() == 0);
00156 assert(t.width() == 0);
00157 assert(t.height() == 0);
00158
00159 std::ofstream out;
00160 out.open("./test-table.txt");
00161 assert(out.is_open());
00162 out << t;
00163 out.close();
00164
00165 assert(check.open("./test-table.txt"));
00166 assert(!check.get_line());
00167 assert(check.close());
00168 assert(check.remove("./test-table.txt"));
00169 }
00170
00171 {
00172 table t;
00173
00174 t.resize(1,1);
00175
00176 assert(t.ncols() == 1);
00177 assert(t.nrows() == 1);
00178 assert(t.col_width(0) == 0);
00179 assert(t.row_width() == 0);
00180 assert(t.col_height() == 0);
00181 assert(t.row_height(0) == 0);
00182 assert(t.width() == 0);
00183 assert(t.height() == 0);
00184
00185 std::ofstream out;
00186 out.open("./test-table.txt");
00187 assert(out.is_open());
00188 out << t;
00189 out.close();
00190
00191 assert(check.open("./test-table.txt"));
00192 assert(!check.get_line());
00193 assert(check.close());
00194 assert(check.remove("./test-table.txt"));
00195
00196 t.assign(0,0,"Hello World");
00197
00198 assert(t.ncols() == 1);
00199 assert(t.nrows() == 1);
00200 assert(t.col_width(0) == 11);
00201 assert(t.row_width() == 11);
00202 assert(t.col_height() == 1);
00203 assert(t.row_height(0) == 1);
00204 assert(t.width() == 11);
00205 assert(t.height() == 1);
00206
00207 out.open("./test-table.txt");
00208 assert(out.is_open());
00209 out << t;
00210 out.close();
00211
00212 assert(check.open("./test-table.txt"));
00213 assert(check.get_line() && check.str() == "Hello World");
00214 assert(!check.get_line());
00215 assert(check.close());
00216 assert(check.remove("./test-table.txt"));
00217
00218 t.resize(2,1);
00219
00220 assert(t.ncols() == 2);
00221 assert(t.nrows() == 1);
00222 assert(t.col_width(0) == 11);
00223 assert(t.row_width() == 11);
00224 assert(t.col_height() == 1);
00225 assert(t.row_height(0) == 1);
00226 assert(t.width() == 11);
00227 assert(t.height() == 1);
00228
00229 out.open("./test-table.txt");
00230 assert(out.is_open());
00231 out << t;
00232 out.close();
00233
00234 assert(check.open("./test-table.txt"));
00235 assert(check.get_line() && check.str() == "Hello World");
00236 assert(!check.get_line());
00237 assert(check.close());
00238 assert(check.remove("./test-table.txt"));
00239
00240 t.assign(0,0,"Hello ");
00241 t.assign(1,0,"World");
00242
00243 assert(t.ncols() == 2);
00244 assert(t.nrows() == 1);
00245 assert(t.col_width(0) == 6);
00246 assert(t.col_width(1) == 5);
00247 assert(t.row_width() == 11);
00248 assert(t.col_height() == 1);
00249 assert(t.row_height(0) == 1);
00250 assert(t.width() == 11);
00251 assert(t.height() == 1);
00252
00253 out.open("./test-table.txt");
00254 assert(out.is_open());
00255 out << t;
00256 out.close();
00257
00258 assert(check.open("./test-table.txt"));
00259 assert(check.get_line() && check.str() == "Hello World");
00260 assert(!check.get_line());
00261 assert(check.close());
00262 assert(check.remove("./test-table.txt"));
00263
00264 t.resize(2,2);
00265
00266 assert(t.ncols() == 2);
00267 assert(t.nrows() == 2);
00268 assert(t.col_width(0) == 6);
00269 assert(t.col_width(1) == 5);
00270 assert(t.col_height() == 1);
00271 assert(t.row_height(0) == 1);
00272 assert(t.row_height(1) == 0);
00273 assert(t.width() == 11);
00274 assert(t.height() == 1);
00275
00276 out.open("./test-table.txt");
00277 assert(out.is_open());
00278 out << t;
00279 out.close();
00280
00281 assert(check.open("./test-table.txt"));
00282 assert(check.get_line() && check.str() == "Hello World");
00283 assert(!check.get_line());
00284 assert(check.close());
00285 assert(check.remove("./test-table.txt"));
00286
00287 t.assign(0,1,"Hi ");
00288 t.assign(1,1,"There");
00289
00290 assert(t.ncols() == 2);
00291 assert(t.nrows() == 2);
00292 assert(t.col_width(0) == 6);
00293 assert(t.col_width(1) == 5);
00294 assert(t.row_width() == 11);
00295 assert(t.col_height() == 2);
00296 assert(t.row_height(0) == 1);
00297 assert(t.row_height(1) == 1);
00298 assert(t.width() == 11);
00299 assert(t.height() == 2);
00300
00301 out.open("./test-table.txt");
00302 assert(out.is_open());
00303 out << t;
00304 out.close();
00305
00306 assert(check.open("./test-table.txt"));
00307 assert(check.get_line() && check.str() == "Hello World");
00308 assert(check.get_line() && check.str() == "Hi There");
00309 assert(!check.get_line());
00310 assert(check.close());
00311 assert(check.remove("./test-table.txt"));
00312
00313 }
00314
00315 {
00316 table sub_t(2,2);
00317
00318 sub_t.assign(0,0," A ");
00319 sub_t.assign(1,0," B ");
00320 sub_t.assign(1,1," D ");
00321
00322 table t(2,2);
00323
00324 t.assign(0,0,"Hello ");
00325 t.assign(1,0,"World");
00326 t.assign(0,1,"Hi");
00327 t.assign(1,1,sub_t);
00328
00329 assert(t.ncols() == 2);
00330 assert(t.nrows() == 2);
00331 assert(t.col_width(0) == 6);
00332 assert(t.col_width(1) == 6);
00333 assert(t.row_width() == 12);
00334 assert(t.col_height() == 3);
00335 assert(t.row_height(0) == 1);
00336 assert(t.row_height(1) == 2);
00337 assert(t.width() == 12);
00338 assert(t.height() == 3);
00339
00340 std::ofstream out;
00341 out.open("./test-table.txt");
00342 assert(out.is_open());
00343 out << t;
00344 out.close();
00345
00346 assert(check.open("./test-table.txt"));
00347 assert(check.get_line() && check.str() == "Hello World ");
00348 assert(check.get_line() && check.str() == "Hi A B ");
00349 assert(check.get_line() && check.str() == " D ");
00350 assert(!check.get_line());
00351 assert(check.close());
00352 assert(check.remove("./test-table.txt"));
00353 }
00354
00355 {
00356 table t;
00357
00358 assert(t.ncols() == 0);
00359 assert(t.nrows() == 0);
00360
00361 t.insert_row(0);
00362
00363 assert(t.ncols() == 0);
00364 assert(t.nrows() == 1);
00365
00366 t.insert_col(0);
00367
00368 assert(t.ncols() == 1);
00369 assert(t.nrows() == 1);
00370
00371 t.assign(0,0,"Hello");
00372
00373 t.insert_col(t.ncols());
00374
00375 assert(t.ncols() == 2);
00376 assert(t.nrows() == 1);
00377
00378 t.assign(1,0," ");
00379
00380 t.insert_col(t.ncols());
00381
00382 assert(t.ncols() == 3);
00383 assert(t.nrows() == 1);
00384
00385 t.assign(2,0,"World");
00386
00387 t.insert_row(t.nrows());
00388
00389 assert(t.ncols() == 3);
00390 assert(t.nrows() == 2);
00391
00392 t.assign(0,1,"Hi");
00393 t.assign(1,1," ");
00394 t.assign(2,1,"There");
00395
00396 std::ofstream out;
00397 out.open("./test-table.txt");
00398 out << t;
00399 out.close();
00400
00401 assert(check.open("./test-table.txt"));
00402 assert(check.get_line() && check.str() == "Hello World");
00403 assert(check.get_line() && check.str() == "Hi There");
00404 assert(!check.get_line());
00405 assert(check.close());
00406 assert(check.remove("./test-table.txt"));
00407 }
00408 }
00409
00410 void test4(void)
00411 {
00412 table t, subt1, subt2;
00413 char const * array1[] = {
00414 "alces:/",
00415 "alces:/export/",
00416 "alces:/export/home/a1/",
00417 "alces:/export/home/a2/",
00418 "alces:/export/home/a3/",
00419 "alces:/export/home/a4/",
00420 "alces:/export/home/a5/",
00421 "alces:/export/home/b1/",
00422 "alces:/export/home/b2/",
00423 "alces:/export/home/b3/",
00424 "alces:/export/home/b4/",
00425 "alces:/export/home/b5/",
00426 "alces:/export/home/c1/",
00427 "alces:/export/home/c2/",
00428 "alces:/export/home/c3/",
00429 "alces:/export/home/c4/",
00430 "alces:/export/home/c5/",
00431 "alces:/export/home/d1/",
00432 "alces:/export/home/d2/",
00433 "alces:/export/home/d3/",
00434 "alces:/export/home/d4/",
00435 "alces:/export/home/d5/",
00436 "alces:/var/spool/",
00437 0
00438 };
00439 char const * array2[] = {
00440 "alces/",
00441 "alces/export/",
00442 "alces/export/home/a1/",
00443 "alces/export/home/a2/",
00444 "alces/export/home/a3/",
00445 "alces/export/home/a4/",
00446 "alces/export/home/a5/",
00447 "alces/export/home/b1/",
00448 "alces/export/home/b2/",
00449 "alces/export/home/b3/",
00450 "alces/export/home/b4/",
00451 "alces/export/home/b5/",
00452 "alces/export/home/c1/",
00453 "alces/export/home/c2/",
00454 "alces/export/home/c3/",
00455 "alces/export/home/c4/",
00456 "alces/export/home/c5/",
00457 "alces/export/home/d1/",
00458 "alces/export/home/d2/",
00459 "alces/export/home/d3/",
00460 "alces/export/home/d4/",
00461 "alces/export/home/d5/",
00462 "alces/var/spool/",
00463 0
00464 };
00465 int c;
00466 estring estr;
00467
00468 for (c = 0; array1[c] != 0; ++c) {
00469 subt1 << "From:" << " " << array1[c] << " " << "To:" << " " << array2[c]
00470 << table_endl;
00471 }
00472
00473 estr = "Start Time:";
00474 estr.align(estring::right);
00475 subt2 << estr;
00476
00477 subt2 << " ";
00478
00479 estr = "2004 Mar 31 21:32:10";
00480 estr.align(estring::right);
00481 subt2 << estr;
00482
00483 subt2 << " ";
00484
00485 estr = "Total Files:";
00486 estr.align(estring::right);
00487 subt2 << estr;
00488
00489 subt2 << " ";
00490
00491 estr = "119,743";
00492 estr.align(estring::right);
00493 subt2 << estr;
00494
00495 subt2 << " ";
00496
00497 estr = "Total Size:";
00498 estr.align(estring::right);
00499 subt2 << estr;
00500
00501 subt2 << " ";
00502
00503 estr = "9.5GB";
00504 estr.align(estring::right);
00505 subt2 << estr;
00506
00507 subt2 << table_endl;
00508
00509
00510
00511 estr = "Stop Time:";
00512 estr.align(estring::right);
00513 subt2 << estr;
00514
00515 subt2 << " ";
00516
00517 estr = "2004 Mar 31 23:16:31";
00518 estr.align(estring::right);
00519 subt2 << estr;
00520
00521 subt2 << " ";
00522
00523 estr = "Xferd Files:";
00524 estr.align(estring::right);
00525 subt2 << estr;
00526
00527 subt2 << " ";
00528
00529 estr = "1";
00530 estr.align(estring::right);
00531 subt2 << estr;
00532
00533 subt2 << " ";
00534
00535 estr = "Xferd Size:";
00536 estr.align(estring::right);
00537 subt2 << estr;
00538
00539 subt2 << " ";
00540
00541 estr = "6.0KB";
00542 estr.align(estring::right);
00543 subt2 << estr;
00544
00545 subt2 << table_endl;
00546
00547
00548
00549 estr = "Duration:";
00550 estr.align(estring::right);
00551 subt2 << estr;
00552
00553 subt2 << " ";
00554
00555 estr = "01:34:21";
00556 estr.align(estring::right);
00557 subt2 << estr;
00558
00559 subt2 << " ";
00560
00561 estr = "Percent:";
00562 estr.align(estring::right);
00563 subt2 << estr;
00564
00565 subt2 << " ";
00566
00567 estr = "0.0%";
00568 estr.align(estring::right);
00569 subt2 << estr;
00570
00571 subt2 << " ";
00572
00573 estr = "Percent:";
00574 estr.align(estring::right);
00575 subt2 << estr;
00576
00577 subt2 << " ";
00578
00579 estr = "0.0%";
00580 estr.align(estring::right);
00581 subt2 << estr;
00582
00583 subt2 << table_endl;
00584
00585 t << "Status" << " " << "Stats" << table_endl;
00586
00587 estr = "";
00588 estr.fillchar('-');
00589 t << estr << " " << estr << table_endl;
00590
00591 t << "Ok" << " " << subt1 << table_endl;
00592 t << table_skip << " " << table_skip << table_endl;
00593 t << table_skip << " " << subt2 << table_endl;
00594
00595 std::ofstream out;
00596 out.open("./test-table.txt");
00597 out << t;
00598 out.close();
00599
00600 assert(check.open("./test-table.txt"));
00601 assert(check.get_line() && check.str() == "Status Stats ");
00602 assert(check.get_line() && check.str() == "------ -----------------------------------------------------------------------");
00603 assert(check.get_line() && check.str() == "Ok From: alces:/ To: alces/ ");
00604 assert(check.get_line() && check.str() == " From: alces:/export/ To: alces/export/ ");
00605 assert(check.get_line() && check.str() == " From: alces:/export/home/a1/ To: alces/export/home/a1/ ");
00606 assert(check.get_line() && check.str() == " From: alces:/export/home/a2/ To: alces/export/home/a2/ ");
00607 assert(check.get_line() && check.str() == " From: alces:/export/home/a3/ To: alces/export/home/a3/ ");
00608 assert(check.get_line() && check.str() == " From: alces:/export/home/a4/ To: alces/export/home/a4/ ");
00609 assert(check.get_line() && check.str() == " From: alces:/export/home/a5/ To: alces/export/home/a5/ ");
00610 assert(check.get_line() && check.str() == " From: alces:/export/home/b1/ To: alces/export/home/b1/ ");
00611 assert(check.get_line() && check.str() == " From: alces:/export/home/b2/ To: alces/export/home/b2/ ");
00612 assert(check.get_line() && check.str() == " From: alces:/export/home/b3/ To: alces/export/home/b3/ ");
00613 assert(check.get_line() && check.str() == " From: alces:/export/home/b4/ To: alces/export/home/b4/ ");
00614 assert(check.get_line() && check.str() == " From: alces:/export/home/b5/ To: alces/export/home/b5/ ");
00615 assert(check.get_line() && check.str() == " From: alces:/export/home/c1/ To: alces/export/home/c1/ ");
00616 assert(check.get_line() && check.str() == " From: alces:/export/home/c2/ To: alces/export/home/c2/ ");
00617 assert(check.get_line() && check.str() == " From: alces:/export/home/c3/ To: alces/export/home/c3/ ");
00618 assert(check.get_line() && check.str() == " From: alces:/export/home/c4/ To: alces/export/home/c4/ ");
00619 assert(check.get_line() && check.str() == " From: alces:/export/home/c5/ To: alces/export/home/c5/ ");
00620 assert(check.get_line() && check.str() == " From: alces:/export/home/d1/ To: alces/export/home/d1/ ");
00621 assert(check.get_line() && check.str() == " From: alces:/export/home/d2/ To: alces/export/home/d2/ ");
00622 assert(check.get_line() && check.str() == " From: alces:/export/home/d3/ To: alces/export/home/d3/ ");
00623 assert(check.get_line() && check.str() == " From: alces:/export/home/d4/ To: alces/export/home/d4/ ");
00624 assert(check.get_line() && check.str() == " From: alces:/export/home/d5/ To: alces/export/home/d5/ ");
00625 assert(check.get_line() && check.str() == " From: alces:/var/spool/ To: alces/var/spool/ ");
00626 assert(check.get_line() && check.str() == " ");
00627 assert(check.get_line() && check.str() == " Start Time: 2004 Mar 31 21:32:10 Total Files: 119,743 Total Size: 9.5GB");
00628 assert(check.get_line() && check.str() == " Stop Time: 2004 Mar 31 23:16:31 Xferd Files: 1 Xferd Size: 6.0KB");
00629 assert(check.get_line() && check.str() == " Duration: 01:34:21 Percent: 0.0% Percent: 0.0%");
00630 assert(!check.get_line());
00631 assert(check.close());
00632 assert(check.remove("./test-table.txt"));
00633 }
00634
00635 int main(int argc, char const * argv[])
00636 {
00637 try {
00638 test1();
00639 test2();
00640 test3();
00641 test4();
00642 }
00643 catch(error e) {
00644 std::cerr << e;
00645 assert(0);
00646 }
00647 catch(...) {
00648 std::cerr << err_unknown;
00649 assert(0);
00650 }
00651 return(0);
00652 }
00653