rvm  1.11
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
test-table.cc
Go to the documentation of this file.
1 #include "config.h"
2 
3 #ifdef HAVE_UNISTD_H
4 #include <unistd.h>
5 #endif
6 
7 #include <iostream>
8 #include <fstream>
9 #include <cassert>
10 
11 #include "asserts.h"
12 #include "error.h"
13 #include "estring.h"
14 #include "table.h"
15 
16 // #define ERR_OUT(s) std::cerr << s
17 #define ERR_OUT(s)
18 
20 {
21 public:
22  bool open(const std::string& a_filename)
23  {
24  if (m_in.is_open())
25  m_in.close();
26  m_in.clear();
27  m_in.open(a_filename.c_str());
28  if (!m_in.is_open() || !m_in) {
29  return(false);
30  }
31  return(true);
32  }
33 
34  bool close(void)
35  {
36  m_in.close();
37  return(true);
38  }
39 
40  bool is_open(void)
41  {
42  bool value;
43 
44  value = m_in.is_open();
45  return(value);
46  }
47 
48  bool get_line(void)
49  {
50  char buffer[1024] = { 0 };
51 
52  if (!m_in.getline(buffer,1024)) {
53  return(false);
54  }
55  try {
56  m_str = buffer;
57  }
58  catch(...) {
59  std::cerr
60  << "*** ERROR: check_output::get_line() - Out of memory?"
61  << std::endl;
62  return(false);
63  }
64  return(true);
65  }
66 
67  const std::string& str(void) const
68  {
69  return(m_str);
70  }
71 
72  bool remove(const std::string& a_filename)
73  {
74  if (unlink(a_filename.c_str()) != 0) {
75  return(false);
76  }
77  return(true);
78  }
79 
80 private:
81  std::string m_str;
82  std::ifstream m_in;
83 };
84 
86 
87 void test1(void)
88 {
89  table_cell_base tcb;
90 
91  assert(tcb.x == table_cell_base::npos);
92  assert(tcb.y == table_cell_base::npos);
93 
94  table_cell_base tcb2;
95 
96  assert(tcb == tcb);
97  assert(!(tcb == tcb2));
98 
99  std::ofstream out;
100  out.open("./test-table.txt");
101  assert(out.is_open());
102  tcb.write(out,0,10);
103  out.close();
104 
105  assert(check.open("./test-table.txt"));
106  assert(!check.get_line());
107  assert(check.close());
108  assert(check.remove("./test-table.txt"));
109 }
110 
111 void test2(void)
112 {
113  table_cell_estring tce;
114  estring estr;
115 
116  estr = "Hello World";
117  tce.assign(estr);
118 
119  assert(tce.height() == 1);
120  assert(tce.width() == estr.size());
121 
122  table_cell_estring tce2(estr);
123 
124  assert(tce2.height() == 1);
125  assert(tce2.width() == estr.size());
126 
127  table_cell_estring tce3(tce);
128 
129  assert(tce3.height() == 1);
130  assert(tce3.width() == estr.size());
131 
132  std::ofstream out;
133  out.open("./test-table.txt");
134  assert(out.is_open());
135  tce.write(out,0,15);
136  out << std::endl;
137  tce.write(out,1,15);
138  out << std::endl;
139  out.close();
140 
141  assert(check.open("./test-table.txt"));
142  assert(check.get_line() && check.str() == "Hello World ");
143  assert(check.get_line() && check.str() == " ");
144  assert(!check.get_line());
145  assert(check.close());
146  assert(check.remove("./test-table.txt"));
147 }
148 
149 void test3(void)
150 {
151  {
152  table t;
153 
154  assert(t.ncols() == 0);
155  assert(t.nrows() == 0);
156  assert(t.width() == 0);
157  assert(t.height() == 0);
158 
159  std::ofstream out;
160  out.open("./test-table.txt");
161  assert(out.is_open());
162  out << t;
163  out.close();
164 
165  assert(check.open("./test-table.txt"));
166  assert(!check.get_line());
167  assert(check.close());
168  assert(check.remove("./test-table.txt"));
169  }
170 
171  {
172  table t;
173 
174  t.resize(1,1);
175 
176  assert(t.ncols() == 1);
177  assert(t.nrows() == 1);
178  assert(t.col_width(0) == 0);
179  assert(t.row_width() == 0);
180  assert(t.col_height() == 0);
181  assert(t.row_height(0) == 0);
182  assert(t.width() == 0);
183  assert(t.height() == 0);
184 
185  std::ofstream out;
186  out.open("./test-table.txt");
187  assert(out.is_open());
188  out << t;
189  out.close();
190 
191  assert(check.open("./test-table.txt"));
192  assert(!check.get_line());
193  assert(check.close());
194  assert(check.remove("./test-table.txt"));
195 
196  t.assign(0,0,"Hello World");
197 
198  assert(t.ncols() == 1);
199  assert(t.nrows() == 1);
200  assert(t.col_width(0) == 11);
201  assert(t.row_width() == 11);
202  assert(t.col_height() == 1);
203  assert(t.row_height(0) == 1);
204  assert(t.width() == 11);
205  assert(t.height() == 1);
206 
207  out.open("./test-table.txt");
208  assert(out.is_open());
209  out << t;
210  out.close();
211 
212  assert(check.open("./test-table.txt"));
213  assert(check.get_line() && check.str() == "Hello World");
214  assert(!check.get_line());
215  assert(check.close());
216  assert(check.remove("./test-table.txt"));
217 
218  t.resize(2,1);
219 
220  assert(t.ncols() == 2);
221  assert(t.nrows() == 1);
222  assert(t.col_width(0) == 11);
223  assert(t.row_width() == 11);
224  assert(t.col_height() == 1);
225  assert(t.row_height(0) == 1);
226  assert(t.width() == 11);
227  assert(t.height() == 1);
228 
229  out.open("./test-table.txt");
230  assert(out.is_open());
231  out << t;
232  out.close();
233 
234  assert(check.open("./test-table.txt"));
235  assert(check.get_line() && check.str() == "Hello World");
236  assert(!check.get_line());
237  assert(check.close());
238  assert(check.remove("./test-table.txt"));
239 
240  t.assign(0,0,"Hello ");
241  t.assign(1,0,"World");
242 
243  assert(t.ncols() == 2);
244  assert(t.nrows() == 1);
245  assert(t.col_width(0) == 6);
246  assert(t.col_width(1) == 5);
247  assert(t.row_width() == 11);
248  assert(t.col_height() == 1);
249  assert(t.row_height(0) == 1);
250  assert(t.width() == 11);
251  assert(t.height() == 1);
252 
253  out.open("./test-table.txt");
254  assert(out.is_open());
255  out << t;
256  out.close();
257 
258  assert(check.open("./test-table.txt"));
259  assert(check.get_line() && check.str() == "Hello World");
260  assert(!check.get_line());
261  assert(check.close());
262  assert(check.remove("./test-table.txt"));
263 
264  t.resize(2,2);
265 
266  assert(t.ncols() == 2);
267  assert(t.nrows() == 2);
268  assert(t.col_width(0) == 6);
269  assert(t.col_width(1) == 5);
270  assert(t.col_height() == 1);
271  assert(t.row_height(0) == 1);
272  assert(t.row_height(1) == 0);
273  assert(t.width() == 11);
274  assert(t.height() == 1);
275 
276  out.open("./test-table.txt");
277  assert(out.is_open());
278  out << t;
279  out.close();
280 
281  assert(check.open("./test-table.txt"));
282  assert(check.get_line() && check.str() == "Hello World");
283  assert(!check.get_line());
284  assert(check.close());
285  assert(check.remove("./test-table.txt"));
286 
287  t.assign(0,1,"Hi ");
288  t.assign(1,1,"There");
289 
290  assert(t.ncols() == 2);
291  assert(t.nrows() == 2);
292  assert(t.col_width(0) == 6);
293  assert(t.col_width(1) == 5);
294  assert(t.row_width() == 11);
295  assert(t.col_height() == 2);
296  assert(t.row_height(0) == 1);
297  assert(t.row_height(1) == 1);
298  assert(t.width() == 11);
299  assert(t.height() == 2);
300 
301  out.open("./test-table.txt");
302  assert(out.is_open());
303  out << t;
304  out.close();
305 
306  assert(check.open("./test-table.txt"));
307  assert(check.get_line() && check.str() == "Hello World");
308  assert(check.get_line() && check.str() == "Hi There");
309  assert(!check.get_line());
310  assert(check.close());
311  assert(check.remove("./test-table.txt"));
312 
313  }
314 
315  {
316  table sub_t(2,2);
317 
318  sub_t.assign(0,0," A ");
319  sub_t.assign(1,0," B ");
320  sub_t.assign(1,1," D ");
321 
322  table t(2,2);
323 
324  t.assign(0,0,"Hello ");
325  t.assign(1,0,"World");
326  t.assign(0,1,"Hi");
327  t.assign(1,1,sub_t);
328 
329  assert(t.ncols() == 2);
330  assert(t.nrows() == 2);
331  assert(t.col_width(0) == 6);
332  assert(t.col_width(1) == 6);
333  assert(t.row_width() == 12);
334  assert(t.col_height() == 3);
335  assert(t.row_height(0) == 1);
336  assert(t.row_height(1) == 2);
337  assert(t.width() == 12);
338  assert(t.height() == 3);
339 
340  std::ofstream out;
341  out.open("./test-table.txt");
342  assert(out.is_open());
343  out << t;
344  out.close();
345 
346  assert(check.open("./test-table.txt"));
347  assert(check.get_line() && check.str() == "Hello World ");
348  assert(check.get_line() && check.str() == "Hi A B ");
349  assert(check.get_line() && check.str() == " D ");
350  assert(!check.get_line());
351  assert(check.close());
352  assert(check.remove("./test-table.txt"));
353  }
354 
355  {
356  table t;
357 
358  assert(t.ncols() == 0);
359  assert(t.nrows() == 0);
360 
361  t.insert_row(0);
362 
363  assert(t.ncols() == 0);
364  assert(t.nrows() == 1);
365 
366  t.insert_col(0);
367 
368  assert(t.ncols() == 1);
369  assert(t.nrows() == 1);
370 
371  t.assign(0,0,"Hello");
372 
373  t.insert_col(t.ncols());
374 
375  assert(t.ncols() == 2);
376  assert(t.nrows() == 1);
377 
378  t.assign(1,0," ");
379 
380  t.insert_col(t.ncols());
381 
382  assert(t.ncols() == 3);
383  assert(t.nrows() == 1);
384 
385  t.assign(2,0,"World");
386 
387  t.insert_row(t.nrows());
388 
389  assert(t.ncols() == 3);
390  assert(t.nrows() == 2);
391 
392  t.assign(0,1,"Hi");
393  t.assign(1,1," ");
394  t.assign(2,1,"There");
395 
396  std::ofstream out;
397  out.open("./test-table.txt");
398  out << t;
399  out.close();
400 
401  assert(check.open("./test-table.txt"));
402  assert(check.get_line() && check.str() == "Hello World");
403  assert(check.get_line() && check.str() == "Hi There");
404  assert(!check.get_line());
405  assert(check.close());
406  assert(check.remove("./test-table.txt"));
407  }
408 }
409 
410 void test4(void)
411 {
412  table t, subt1, subt2;
413  char const * array1[] = {
414  "alces:/",
415  "alces:/export/",
416  "alces:/export/home/a1/",
417  "alces:/export/home/a2/",
418  "alces:/export/home/a3/",
419  "alces:/export/home/a4/",
420  "alces:/export/home/a5/",
421  "alces:/export/home/b1/",
422  "alces:/export/home/b2/",
423  "alces:/export/home/b3/",
424  "alces:/export/home/b4/",
425  "alces:/export/home/b5/",
426  "alces:/export/home/c1/",
427  "alces:/export/home/c2/",
428  "alces:/export/home/c3/",
429  "alces:/export/home/c4/",
430  "alces:/export/home/c5/",
431  "alces:/export/home/d1/",
432  "alces:/export/home/d2/",
433  "alces:/export/home/d3/",
434  "alces:/export/home/d4/",
435  "alces:/export/home/d5/",
436  "alces:/var/spool/",
437  0
438  };
439  char const * array2[] = {
440  "alces/",
441  "alces/export/",
442  "alces/export/home/a1/",
443  "alces/export/home/a2/",
444  "alces/export/home/a3/",
445  "alces/export/home/a4/",
446  "alces/export/home/a5/",
447  "alces/export/home/b1/",
448  "alces/export/home/b2/",
449  "alces/export/home/b3/",
450  "alces/export/home/b4/",
451  "alces/export/home/b5/",
452  "alces/export/home/c1/",
453  "alces/export/home/c2/",
454  "alces/export/home/c3/",
455  "alces/export/home/c4/",
456  "alces/export/home/c5/",
457  "alces/export/home/d1/",
458  "alces/export/home/d2/",
459  "alces/export/home/d3/",
460  "alces/export/home/d4/",
461  "alces/export/home/d5/",
462  "alces/var/spool/",
463  0
464  };
465  int c;
466  estring estr;
467 
468  for (c = 0; array1[c] != 0; ++c) {
469  subt1 << "From:" << " " << array1[c] << " " << "To:" << " " << array2[c]
470  << table_endl;
471  }
472 
473  estr = "Start Time:";
474  estr.align(estring::right);
475  subt2 << estr;
476 
477  subt2 << " ";
478 
479  estr = "2004 Mar 31 21:32:10";
480  estr.align(estring::right);
481  subt2 << estr;
482 
483  subt2 << " ";
484 
485  estr = "Total Files:";
486  estr.align(estring::right);
487  subt2 << estr;
488 
489  subt2 << " ";
490 
491  estr = "119,743";
492  estr.align(estring::right);
493  subt2 << estr;
494 
495  subt2 << " ";
496 
497  estr = "Total Size:";
498  estr.align(estring::right);
499  subt2 << estr;
500 
501  subt2 << " ";
502 
503  estr = "9.5GB";
504  estr.align(estring::right);
505  subt2 << estr;
506 
507  subt2 << table_endl;
508 
509 
510 
511  estr = "Stop Time:";
512  estr.align(estring::right);
513  subt2 << estr;
514 
515  subt2 << " ";
516 
517  estr = "2004 Mar 31 23:16:31";
518  estr.align(estring::right);
519  subt2 << estr;
520 
521  subt2 << " ";
522 
523  estr = "Xferd Files:";
524  estr.align(estring::right);
525  subt2 << estr;
526 
527  subt2 << " ";
528 
529  estr = "1";
530  estr.align(estring::right);
531  subt2 << estr;
532 
533  subt2 << " ";
534 
535  estr = "Xferd Size:";
536  estr.align(estring::right);
537  subt2 << estr;
538 
539  subt2 << " ";
540 
541  estr = "6.0KB";
542  estr.align(estring::right);
543  subt2 << estr;
544 
545  subt2 << table_endl;
546 
547 
548 
549  estr = "Duration:";
550  estr.align(estring::right);
551  subt2 << estr;
552 
553  subt2 << " ";
554 
555  estr = "01:34:21";
556  estr.align(estring::right);
557  subt2 << estr;
558 
559  subt2 << " ";
560 
561  estr = "Percent:";
562  estr.align(estring::right);
563  subt2 << estr;
564 
565  subt2 << " ";
566 
567  estr = "0.0%";
568  estr.align(estring::right);
569  subt2 << estr;
570 
571  subt2 << " ";
572 
573  estr = "Percent:";
574  estr.align(estring::right);
575  subt2 << estr;
576 
577  subt2 << " ";
578 
579  estr = "0.0%";
580  estr.align(estring::right);
581  subt2 << estr;
582 
583  subt2 << table_endl;
584 
585  t << "Status" << " " << "Stats" << table_endl;
586 
587  estr = "";
588  estr.fillchar('-');
589  t << estr << " " << estr << table_endl;
590 
591  t << "Ok" << " " << subt1 << table_endl;
592  t << table_skip << " " << table_skip << table_endl;
593  t << table_skip << " " << subt2 << table_endl;
594 
595  std::ofstream out;
596  out.open("./test-table.txt");
597  out << t;
598  out.close();
599 
600  assert(check.open("./test-table.txt"));
601  assert(check.get_line() && check.str() == "Status Stats ");
602  assert(check.get_line() && check.str() == "------ -----------------------------------------------------------------------");
603  assert(check.get_line() && check.str() == "Ok From: alces:/ To: alces/ ");
604  assert(check.get_line() && check.str() == " From: alces:/export/ To: alces/export/ ");
605  assert(check.get_line() && check.str() == " From: alces:/export/home/a1/ To: alces/export/home/a1/ ");
606  assert(check.get_line() && check.str() == " From: alces:/export/home/a2/ To: alces/export/home/a2/ ");
607  assert(check.get_line() && check.str() == " From: alces:/export/home/a3/ To: alces/export/home/a3/ ");
608  assert(check.get_line() && check.str() == " From: alces:/export/home/a4/ To: alces/export/home/a4/ ");
609  assert(check.get_line() && check.str() == " From: alces:/export/home/a5/ To: alces/export/home/a5/ ");
610  assert(check.get_line() && check.str() == " From: alces:/export/home/b1/ To: alces/export/home/b1/ ");
611  assert(check.get_line() && check.str() == " From: alces:/export/home/b2/ To: alces/export/home/b2/ ");
612  assert(check.get_line() && check.str() == " From: alces:/export/home/b3/ To: alces/export/home/b3/ ");
613  assert(check.get_line() && check.str() == " From: alces:/export/home/b4/ To: alces/export/home/b4/ ");
614  assert(check.get_line() && check.str() == " From: alces:/export/home/b5/ To: alces/export/home/b5/ ");
615  assert(check.get_line() && check.str() == " From: alces:/export/home/c1/ To: alces/export/home/c1/ ");
616  assert(check.get_line() && check.str() == " From: alces:/export/home/c2/ To: alces/export/home/c2/ ");
617  assert(check.get_line() && check.str() == " From: alces:/export/home/c3/ To: alces/export/home/c3/ ");
618  assert(check.get_line() && check.str() == " From: alces:/export/home/c4/ To: alces/export/home/c4/ ");
619  assert(check.get_line() && check.str() == " From: alces:/export/home/c5/ To: alces/export/home/c5/ ");
620  assert(check.get_line() && check.str() == " From: alces:/export/home/d1/ To: alces/export/home/d1/ ");
621  assert(check.get_line() && check.str() == " From: alces:/export/home/d2/ To: alces/export/home/d2/ ");
622  assert(check.get_line() && check.str() == " From: alces:/export/home/d3/ To: alces/export/home/d3/ ");
623  assert(check.get_line() && check.str() == " From: alces:/export/home/d4/ To: alces/export/home/d4/ ");
624  assert(check.get_line() && check.str() == " From: alces:/export/home/d5/ To: alces/export/home/d5/ ");
625  assert(check.get_line() && check.str() == " From: alces:/var/spool/ To: alces/var/spool/ ");
626  assert(check.get_line() && check.str() == " ");
627  assert(check.get_line() && check.str() == " Start Time: 2004 Mar 31 21:32:10 Total Files: 119,743 Total Size: 9.5GB");
628  assert(check.get_line() && check.str() == " Stop Time: 2004 Mar 31 23:16:31 Xferd Files: 1 Xferd Size: 6.0KB");
629  assert(check.get_line() && check.str() == " Duration: 01:34:21 Percent: 0.0% Percent: 0.0%");
630  assert(!check.get_line());
631  assert(check.close());
632  assert(check.remove("./test-table.txt"));
633 }
634 
635 int main(int argc, char const * argv[])
636 {
637  try {
638  test1();
639  test2();
640  test3();
641  test4();
642  }
643  catch(error e) {
644  std::cerr << e;
645  assert(0);
646  }
647  catch(...) {
648  std::cerr << err_unknown;
649  assert(0);
650  }
651  return(0);
652 }
653 
check_output check
Definition: test-table.cc:85
size_type col_height(void) const
Definition: table.cc:470
table & table_endl(table &a_table)
Definition: table.cc:853
bool is_open(void)
Definition: test-table.cc:40
virtual size_type height(void) const
Definition: table.cc:504
virtual void assign(const table &a_class)
Definition: table.cc:347
bool close(void)
Definition: test-table.cc:34
An extended string class.
Definition: estring.h:52
virtual void write(std::ostream &out, size_type a_line, size_type a_width) const
Definition: table.cc:46
size_type col_width(const size_type a_x) const
Definition: table.cc:431
virtual void write(std::ostream &out, size_type a_line, size_type a_width) const
Definition: table.cc:117
Right-justified.
Definition: estring.h:66
bool get_line(void)
Definition: test-table.cc:48
size_type y
Definition: table.h:36
std::string m_str
Definition: test-table.cc:81
static const size_type npos
Definition: table.h:17
table & table_skip(table &a_table)
Definition: table.cc:869
size_type row_height(const size_type a_y) const
Definition: table.cc:482
void test2(void)
Definition: test-table.cc:111
#define err_unknown
Definition: error.h:114
size_type x
Definition: table.h:35
virtual size_type width(void) const
Definition: table.cc:108
const std::string & str(void) const
Definition: test-table.cc:67
void test1(void)
Definition: test-table.cc:87
virtual void insert_col(const size_type a_x)
Definition: table.cc:332
const size_type nrows(void) const
Definition: table.cc:561
int main(int argc, char const *argv[])
Definition: test-table.cc:635
An error class.
Definition: error.h:72
virtual void insert_row(const size_type a_y)
Definition: table.cc:315
const size_type ncols(void) const
Definition: table.cc:549
virtual size_type height(void) const
Definition: table.cc:103
bool remove(const std::string &a_filename)
Definition: test-table.cc:72
void test3(void)
Definition: test-table.cc:149
virtual void assign(const table_cell_estring &a_class)
Definition: table.cc:90
virtual void resize(const size_type a_x, const size_type a_y)
Definition: table.cc:291
alignment align(const alignment a_alignment)
Set the alignment used for formatted strings.
Definition: estring.cc:471
size_type row_width(void) const
Definition: table.cc:454
void test4(void)
Definition: test-table.cc:410
std::ifstream m_in
Definition: test-table.cc:82
Definition: table.h:96
virtual size_type width(void) const
Definition: table.cc:524
bool open(const std::string &a_filename)
Definition: test-table.cc:22