]> git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_ttable.c
*: auto-convert to SPDX License IDs
[mirror_frr.git] / tests / lib / test_ttable.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * ASCII table generator.
4 * Copyright (C) 2017 Cumulus Networks
5 * Quentin Young
6 */
7 #include <zebra.h>
8 #include <termtable.h>
9 #include <memory.h>
10
11 int main(int argc, char **argv)
12 {
13 char *table;
14
15 struct ttable *tt = ttable_new(&ttable_styles[TTSTYLE_ASCII]);
16
17 /* test printf compatibility and dimension counters */
18 ttable_add_row(tt, "%s|%s|%s", "Column 1", "Column 2", "Column 3");
19 assert(tt->ncols == 3);
20 assert(tt->nrows == 1);
21 table = ttable_dump(tt, "\n");
22 fprintf(stdout, "%s\n", table);
23 XFREE(MTYPE_TMP, table);
24
25 /* add new row with 1 column, assert that it is not added */
26 assert(ttable_add_row(tt, "%s", "Garbage") == NULL);
27 assert(tt->ncols == 3);
28 assert(tt->nrows == 1);
29 table = ttable_dump(tt, "\n");
30 fprintf(stdout, "%s\n", table);
31 XFREE(MTYPE_TMP, table);
32
33 /* add new row, assert that it is added */
34 assert(ttable_add_row(tt, "%s|%s|%s", "a", "b", "c"));
35 assert(tt->ncols == 3);
36 assert(tt->nrows == 2);
37 table = ttable_dump(tt, "\n");
38 fprintf(stdout, "%s\n", table);
39 XFREE(MTYPE_TMP, table);
40
41 /* add empty row, assert that it is added */
42 assert(ttable_add_row(tt, "||"));
43 assert(tt->ncols == 3);
44 assert(tt->nrows == 3);
45 table = ttable_dump(tt, "\n");
46 fprintf(stdout, "%s\n", table);
47 XFREE(MTYPE_TMP, table);
48
49 /* delete 1st row, assert that it is removed */
50 ttable_del_row(tt, 0);
51 assert(tt->ncols == 3);
52 assert(tt->nrows == 2);
53 table = ttable_dump(tt, "\n");
54 fprintf(stdout, "%s\n", table);
55 XFREE(MTYPE_TMP, table);
56
57 /* delete last row, assert that it is removed */
58 ttable_del_row(tt, 0);
59 assert(tt->ncols == 3);
60 assert(tt->nrows == 1);
61 table = ttable_dump(tt, "\n");
62 fprintf(stdout, "%s\n", table);
63 XFREE(MTYPE_TMP, table);
64
65 /* delete the remaining row, check dumping an empty table */
66 ttable_del_row(tt, 0);
67 assert(tt->ncols == 0);
68 assert(tt->nrows == 0);
69 table = ttable_dump(tt, "\n");
70 fprintf(stdout, "%s\n", table);
71 XFREE(MTYPE_TMP, table);
72
73 /* add new row */
74 ttable_add_row(tt, "%s|%s||%s|%9d", "slick", "black", "triple", 1337);
75 assert(tt->ncols == 5);
76 assert(tt->nrows == 1);
77 table = ttable_dump(tt, "\n");
78 fprintf(stdout, "%s\n", table);
79 XFREE(MTYPE_TMP, table);
80
81 /* add bigger row */
82 ttable_add_row(tt, "%s|%s||%s|%s",
83 "nebula dusk session streets twilight pioneer beats yeah",
84 "prarie dog", "cornmeal", ":O -*_-*");
85 assert(tt->ncols == 5);
86 assert(tt->nrows == 2);
87 table = ttable_dump(tt, "\n");
88 fprintf(stdout, "%s\n", table);
89 XFREE(MTYPE_TMP, table);
90
91 /* insert new row at beginning */
92 ttable_insert_row(tt, 0, "%s|%s||%d|%lf", "converting", "vegetarians",
93 2, 2015.0);
94 assert(tt->ncols == 5);
95 assert(tt->nrows == 3);
96 table = ttable_dump(tt, "\n");
97 fprintf(stdout, "%s\n", table);
98 XFREE(MTYPE_TMP, table);
99
100 /* insert new row at end */
101 ttable_insert_row(tt, tt->nrows - 1, "%s|%s||%d|%ld", "converting",
102 "vegetarians", 1, 2003L);
103 assert(tt->ncols == 5);
104 assert(tt->nrows == 4);
105 table = ttable_dump(tt, "\n");
106 fprintf(stdout, "%s\n", table);
107 XFREE(MTYPE_TMP, table);
108
109 /* insert new row at middle */
110 ttable_insert_row(tt, 1, "%s|%s||%s|%ld", "she", "pioneer", "aki", 1l);
111 assert(tt->ncols == 5);
112 assert(tt->nrows == 5);
113 table = ttable_dump(tt, "\n");
114 fprintf(stdout, "%s\n", table);
115 XFREE(MTYPE_TMP, table);
116
117 /* set alignment */
118 ttable_align(tt, 0, 1, 2, 2, LEFT);
119 assert(tt->ncols == 5);
120 assert(tt->nrows == 5);
121 table = ttable_dump(tt, "\n");
122 fprintf(stdout, "%s\n", table);
123 XFREE(MTYPE_TMP, table);
124
125 ttable_align(tt, 0, 1, 5, 1, RIGHT);
126 assert(tt->ncols == 5);
127 assert(tt->nrows == 5);
128 table = ttable_dump(tt, "\n");
129 fprintf(stdout, "%s\n", table);
130 XFREE(MTYPE_TMP, table);
131
132 /* set padding */
133 ttable_pad(tt, 0, 1, 1, 1, RIGHT, 2);
134 assert(tt->ncols == 5);
135 assert(tt->nrows == 5);
136 table = ttable_dump(tt, "\n");
137 fprintf(stdout, "%s\n", table);
138 XFREE(MTYPE_TMP, table);
139
140 ttable_pad(tt, 0, 0, 5, 4, LEFT, 2);
141 assert(tt->ncols == 5);
142 assert(tt->nrows == 5);
143 table = ttable_dump(tt, "\n");
144 fprintf(stdout, "%s\n", table);
145 XFREE(MTYPE_TMP, table);
146
147 /* restyle */
148 tt->style.cell.border.bottom_on = false;
149 tt->style.cell.border.top_on = false;
150 tt->style.cell.border.right_on = false;
151 tt->style.cell.border.left_on = false;
152 ttable_restyle(tt);
153
154 /* top & double bottom border for top row */
155 ttable_rowseps(tt, 0, BOTTOM, true, '-');
156 ttable_rowseps(tt, 1, TOP, true, '-');
157 table = ttable_dump(tt, "\n");
158 fprintf(stdout, "%s\n", table);
159 XFREE(MTYPE_TMP, table);
160
161 /* column separators for leftmost column */
162 ttable_colseps(tt, 0, RIGHT, true, '|');
163 table = ttable_dump(tt, "\n");
164 fprintf(stdout, "%s\n", table);
165 XFREE(MTYPE_TMP, table);
166
167 /* delete table */
168 ttable_del(tt);
169 }