]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_ttable.c
2 * ASCII table generator.
3 * Copyright (C) 2017 Cumulus Networks
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <termtable.h>
24 int main(int argc
, char **argv
)
28 struct ttable
*tt
= ttable_new(&ttable_styles
[TTSTYLE_ASCII
]);
30 /* test printf compatibility and dimension counters */
31 ttable_add_row(tt
, "%s|%s|%s", "Column 1", "Column 2", "Column 3");
32 assert(tt
->ncols
== 3);
33 assert(tt
->nrows
== 1);
34 table
= ttable_dump(tt
, "\n");
35 fprintf(stdout
, "%s\n", table
);
36 XFREE(MTYPE_TMP
, table
);
38 /* add new row with 1 column, assert that it is not added */
39 assert(ttable_add_row(tt
, "%s", "Garbage") == NULL
);
40 assert(tt
->ncols
== 3);
41 assert(tt
->nrows
== 1);
42 table
= ttable_dump(tt
, "\n");
43 fprintf(stdout
, "%s\n", table
);
44 XFREE(MTYPE_TMP
, table
);
46 /* add new row, assert that it is added */
47 assert(ttable_add_row(tt
, "%s|%s|%s", "a", "b", "c"));
48 assert(tt
->ncols
== 3);
49 assert(tt
->nrows
== 2);
50 table
= ttable_dump(tt
, "\n");
51 fprintf(stdout
, "%s\n", table
);
52 XFREE(MTYPE_TMP
, table
);
54 /* add empty row, assert that it is added */
55 assert(ttable_add_row(tt
, "||"));
56 assert(tt
->ncols
== 3);
57 assert(tt
->nrows
== 3);
58 table
= ttable_dump(tt
, "\n");
59 fprintf(stdout
, "%s\n", table
);
60 XFREE(MTYPE_TMP
, table
);
62 /* delete 1st row, assert that it is removed */
63 ttable_del_row(tt
, 0);
64 assert(tt
->ncols
== 3);
65 assert(tt
->nrows
== 2);
66 table
= ttable_dump(tt
, "\n");
67 fprintf(stdout
, "%s\n", table
);
68 XFREE(MTYPE_TMP
, table
);
70 /* delete last row, assert that it is removed */
71 ttable_del_row(tt
, 0);
72 assert(tt
->ncols
== 3);
73 assert(tt
->nrows
== 1);
74 table
= ttable_dump(tt
, "\n");
75 fprintf(stdout
, "%s\n", table
);
76 XFREE(MTYPE_TMP
, table
);
78 /* delete the remaining row, check dumping an empty table */
79 ttable_del_row(tt
, 0);
80 assert(tt
->ncols
== 0);
81 assert(tt
->nrows
== 0);
82 table
= ttable_dump(tt
, "\n");
83 fprintf(stdout
, "%s\n", table
);
84 XFREE(MTYPE_TMP
, table
);
87 ttable_add_row(tt
, "%s|%s||%s|%9d", "slick", "black", "triple", 1337);
88 assert(tt
->ncols
== 5);
89 assert(tt
->nrows
== 1);
90 table
= ttable_dump(tt
, "\n");
91 fprintf(stdout
, "%s\n", table
);
92 XFREE(MTYPE_TMP
, table
);
95 ttable_add_row(tt
, "%s|%s||%s|%s",
96 "nebula dusk session streets twilight pioneer beats yeah",
97 "prarie dog", "cornmeal", ":O -*_-*");
98 assert(tt
->ncols
== 5);
99 assert(tt
->nrows
== 2);
100 table
= ttable_dump(tt
, "\n");
101 fprintf(stdout
, "%s\n", table
);
102 XFREE(MTYPE_TMP
, table
);
104 /* insert new row at beginning */
105 ttable_insert_row(tt
, 0, "%s|%s||%d|%lf", "converting", "vegetarians",
107 assert(tt
->ncols
== 5);
108 assert(tt
->nrows
== 3);
109 table
= ttable_dump(tt
, "\n");
110 fprintf(stdout
, "%s\n", table
);
111 XFREE(MTYPE_TMP
, table
);
113 /* insert new row at end */
114 ttable_insert_row(tt
, tt
->nrows
- 1, "%s|%s||%d|%ld", "converting",
115 "vegetarians", 1, 2003L);
116 assert(tt
->ncols
== 5);
117 assert(tt
->nrows
== 4);
118 table
= ttable_dump(tt
, "\n");
119 fprintf(stdout
, "%s\n", table
);
120 XFREE(MTYPE_TMP
, table
);
122 /* insert new row at middle */
123 ttable_insert_row(tt
, 1, "%s|%s||%s|%ld", "she", "pioneer", "aki", 1l);
124 assert(tt
->ncols
== 5);
125 assert(tt
->nrows
== 5);
126 table
= ttable_dump(tt
, "\n");
127 fprintf(stdout
, "%s\n", table
);
128 XFREE(MTYPE_TMP
, table
);
131 ttable_align(tt
, 0, 1, 2, 2, LEFT
);
132 assert(tt
->ncols
== 5);
133 assert(tt
->nrows
== 5);
134 table
= ttable_dump(tt
, "\n");
135 fprintf(stdout
, "%s\n", table
);
136 XFREE(MTYPE_TMP
, table
);
138 ttable_align(tt
, 0, 1, 5, 1, RIGHT
);
139 assert(tt
->ncols
== 5);
140 assert(tt
->nrows
== 5);
141 table
= ttable_dump(tt
, "\n");
142 fprintf(stdout
, "%s\n", table
);
143 XFREE(MTYPE_TMP
, table
);
146 ttable_pad(tt
, 0, 1, 1, 1, RIGHT
, 2);
147 assert(tt
->ncols
== 5);
148 assert(tt
->nrows
== 5);
149 table
= ttable_dump(tt
, "\n");
150 fprintf(stdout
, "%s\n", table
);
151 XFREE(MTYPE_TMP
, table
);
153 ttable_pad(tt
, 0, 0, 5, 4, LEFT
, 2);
154 assert(tt
->ncols
== 5);
155 assert(tt
->nrows
== 5);
156 table
= ttable_dump(tt
, "\n");
157 fprintf(stdout
, "%s\n", table
);
158 XFREE(MTYPE_TMP
, table
);
161 tt
->style
.cell
.border
.bottom_on
= false;
162 tt
->style
.cell
.border
.top_on
= false;
163 tt
->style
.cell
.border
.right_on
= false;
164 tt
->style
.cell
.border
.left_on
= false;
167 /* top & double bottom border for top row */
168 ttable_rowseps(tt
, 0, BOTTOM
, true, '-');
169 ttable_rowseps(tt
, 1, TOP
, true, '-');
170 table
= ttable_dump(tt
, "\n");
171 fprintf(stdout
, "%s\n", table
);
172 XFREE(MTYPE_TMP
, table
);
174 /* column separators for leftmost column */
175 ttable_colseps(tt
, 0, RIGHT
, true, '|');
176 table
= ttable_dump(tt
, "\n");
177 fprintf(stdout
, "%s\n", table
);
178 XFREE(MTYPE_TMP
, table
);