]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_ttable.c
doc: Add `show ipv6 rpf X:X::X:X` command to docs
[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 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);
103
104 /* insert new row at beginning */
105 ttable_insert_row(tt, 0, "%s|%s||%d|%lf", "converting", "vegetarians",
106 2, 2015.0);
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);
112
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);
121
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);
129
130 /* set alignment */
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);
137
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);
144
145 /* set padding */
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);
152
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);
159
160 /* restyle */
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;
165 ttable_restyle(tt);
166
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);
173
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);
179
180 /* delete table */
181 ttable_del(tt);
182 }