]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_ttable.c
Merge remote-tracking branch 'origin/stable/2.0'
[mirror_frr.git] / tests / lib / test_ttable.c
1 /*
2 * ASCII table generator.
3 * Copyright (C) 2017 Cumulus Networks
4 * Quentin Young
5 *
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)
9 * any later version.
10 *
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
14 * more details.
15 *
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
19 */
20 #include <zebra.h>
21 #include <termtable.h>
22 #include <memory.h>
23
24 int main(int argc, char **argv)
25 {
26 char *table;
27
28 struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_ASCII]);
29
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);
37
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);
45
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);
53
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);
61
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);
69
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);
77
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);
85
86 /* add new row */
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);
93
94 /* add bigger row */
95 ttable_add_row(tt, "%s|%s||%s|%s",
96 "nebula dusk session streets twilight "
97 "pioneer beats yeah",
98 "prarie dog", "cornmeal", ":O -*_-*");
99 assert(tt->ncols == 5);
100 assert(tt->nrows == 2);
101 table = ttable_dump(tt, "\n");
102 fprintf(stdout, "%s\n", table);
103 XFREE(MTYPE_TMP, table);
104
105 /* insert new row at beginning */
106 ttable_insert_row(tt, 0, "%s|%s||%d|%lf", "converting", "vegetarians",
107 2, 2015.0);
108 assert(tt->ncols == 5);
109 assert(tt->nrows == 3);
110 table = ttable_dump(tt, "\n");
111 fprintf(stdout, "%s\n", table);
112 XFREE(MTYPE_TMP, table);
113
114 /* insert new row at end */
115 ttable_insert_row(tt, tt->nrows - 1, "%s|%s||%d|%ld", "converting",
116 "vegetarians", 1, 2003L);
117 assert(tt->ncols == 5);
118 assert(tt->nrows == 4);
119 table = ttable_dump(tt, "\n");
120 fprintf(stdout, "%s\n", table);
121 XFREE(MTYPE_TMP, table);
122
123 /* insert new row at middle */
124 ttable_insert_row(tt, 1, "%s|%s||%s|%ld", "she", "pioneer", "aki", 1l);
125 assert(tt->ncols == 5);
126 assert(tt->nrows == 5);
127 table = ttable_dump(tt, "\n");
128 fprintf(stdout, "%s\n", table);
129 XFREE(MTYPE_TMP, table);
130
131 /* set alignment */
132 ttable_align(tt, 0, 1, 2, 2, LEFT);
133 assert(tt->ncols == 5);
134 assert(tt->nrows == 5);
135 table = ttable_dump(tt, "\n");
136 fprintf(stdout, "%s\n", table);
137 XFREE(MTYPE_TMP, table);
138
139 ttable_align(tt, 0, 1, 5, 1, RIGHT);
140 assert(tt->ncols == 5);
141 assert(tt->nrows == 5);
142 table = ttable_dump(tt, "\n");
143 fprintf(stdout, "%s\n", table);
144 XFREE(MTYPE_TMP, table);
145
146 /* set padding */
147 ttable_pad(tt, 0, 1, 1, 1, RIGHT, 2);
148 assert(tt->ncols == 5);
149 assert(tt->nrows == 5);
150 table = ttable_dump(tt, "\n");
151 fprintf(stdout, "%s\n", table);
152 XFREE(MTYPE_TMP, table);
153
154 ttable_pad(tt, 0, 0, 5, 4, LEFT, 2);
155 assert(tt->ncols == 5);
156 assert(tt->nrows == 5);
157 table = ttable_dump(tt, "\n");
158 fprintf(stdout, "%s\n", table);
159 XFREE(MTYPE_TMP, table);
160
161 /* restyle */
162 tt->style.cell.border.bottom_on = false;
163 tt->style.cell.border.top_on = false;
164 tt->style.cell.border.right_on = false;
165 tt->style.cell.border.left_on = false;
166 ttable_restyle(tt);
167
168 /* top & double bottom border for top row */
169 ttable_rowseps(tt, 0, BOTTOM, true, '-');
170 ttable_rowseps(tt, 1, TOP, true, '-');
171 table = ttable_dump(tt, "\n");
172 fprintf(stdout, "%s\n", table);
173 XFREE(MTYPE_TMP, table);
174
175 /* column separators for leftmost column */
176 ttable_colseps(tt, 0, RIGHT, true, '|');
177 table = ttable_dump(tt, "\n");
178 fprintf(stdout, "%s\n", table);
179 XFREE(MTYPE_TMP, table);
180
181 /* delete table */
182 ttable_del(tt);
183 }