2 * Copyright (C) 2018 Red Hat, Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library. If not, see
16 * <http://www.gnu.org/licenses/>.
25 #include "testutils.h"
27 #include "../tools/vsh-table.h"
30 testVshTableNew(const void *opaque ATTRIBUTE_UNUSED
)
32 if (vshTableNew(NULL
)) {
33 fprintf(stderr
, "expected failure when passing null to vshTableNew\n");
41 testVshTableHeader(const void *opaque ATTRIBUTE_UNUSED
)
46 " 1 fedora28 running\n"
47 " 2 rhel7.5 running\n";
50 "--------------------------\n"
51 " 1 fedora28 running\n"
52 " 2 rhel7.5 running\n";
54 vshTablePtr table
= vshTableNew("Id", "Name", "State",
55 NULL
); //to ask about return
59 vshTableRowAppend(table
, "1", "fedora28", "running", NULL
);
60 vshTableRowAppend(table
, "2", "rhel7.5", "running",
63 act
= vshTablePrintToString(table
, false);
64 if (virTestCompareToString(exp
, act
) < 0)
68 act
= vshTablePrintToString(table
, true);
69 if (virTestCompareToString(exp2
, act
) < 0)
79 testVshTableRowAppend(const void *opaque ATTRIBUTE_UNUSED
)
83 vshTablePtr table
= vshTableNew("Id", "Name", NULL
);
87 if (vshTableRowAppend(table
, NULL
) >= 0) {
88 fprintf(stderr
, "Appending NULL shouldn't work\n");
92 if (vshTableRowAppend(table
, "2", NULL
) >= 0) {
93 fprintf(stderr
, "Appending less items than in header\n");
97 if (vshTableRowAppend(table
, "2", "rhel7.5", "running",
99 fprintf(stderr
, "Appending more items than in header\n");
103 if (vshTableRowAppend(table
, "2", "rhel7.5", NULL
) < 0) {
104 fprintf(stderr
, "Appending same number of items as in header"
105 " should not return NULL\n");
115 testUnicode(const void *opaque ATTRIBUTE_UNUSED
)
121 " Id 名稱 государство\n"
122 "-----------------------------------------\n"
123 " 1 fedora28 running\n"
124 " 2 つへソrhel7.5つへソ running\n";
127 table
= vshTableNew("Id", "名稱", "государство", NULL
);
131 vshTableRowAppend(table
, "1", "fedora28", "running", NULL
);
132 vshTableRowAppend(table
, "2", "つへソrhel7.5つへソ", "running",
135 act
= vshTablePrintToString(table
, true);
136 if (virTestCompareToString(exp
, act
) < 0)
145 /* Point of this test is to see how table behaves with right to left writing*/
147 testUnicodeArabic(const void *opaque ATTRIBUTE_UNUSED
)
153 " ﻡﺍ ﻢﻣﺍ ﻕﺎﺌﻣﺓ ﺓ ﺎﻠﺼﻋ ﺍﻸﺜﻧﺎﻧ\n"
154 "-------------------------------------------------------------------------------------------\n"
155 " 1 ﻉﺪﻴﻟ ﺎﻠﺜﻘﻴﻟ ﻕﺎﻣ ﻊﻧ, ٣٠ ﻎﻴﻨﻳﺍ ﻮﺘﻧﺎﻤﺗ ﺎﻠﺛﺎﻠﺛ، ﺄﺳﺭ, ﺩﻮﻟ ﺩﻮﻟ. ﺄﻣﺎﻣ ﺍ ﺎﻧ ﻲﻜﻧ\n"
156 " ﺺﻔﺣﺓ ﺖﻜﺘﻴﻛﺍً ﻊﻟ, ﺎﻠﺠﻧﻭﺩ ﻭﺎﻠﻌﺗﺍﺩ ﺵﺭ\n";
160 /* If this char is not classed as printable, the actual
161 * output won't match what this test expects. The code
162 * is still operating correctly, but we have different
164 mbrtowc(&wc
, "،", MB_CUR_MAX
, NULL
);
168 table
= vshTableNew("ﻡﺍ ﻢﻣﺍ ﻕﺎﺌﻣﺓ", "ﺓ ﺎﻠﺼﻋ", "ﺍﻸﺜﻧﺎﻧ", NULL
);
171 vshTableRowAppend(table
,
173 "ﻉﺪﻴﻟ ﺎﻠﺜﻘﻴﻟ ﻕﺎﻣ ﻊﻧ, ٣٠ ﻎﻴﻨﻳﺍ ﻮﺘﻧﺎﻤﺗ ﺎﻠﺛﺎﻠﺛ، ﺄﺳﺭ, ﺩﻮﻟ",
174 "ﺩﻮﻟ. ﺄﻣﺎﻣ ﺍ ﺎﻧ ﻲﻜﻧ",
176 vshTableRowAppend(table
, "ﺺﻔﺣﺓ", "ﺖﻜﺘﻴﻛﺍً ﻊﻟ, ﺎﻠﺠﻧﻭﺩ ﻭﺎﻠﻌﺗﺍﺩ", "ﺵﺭ",
178 act
= vshTablePrintToString(table
, true);
179 if (virTestCompareToString(exp
, act
) < 0)
188 /* Testing zero-width characters by inserting few zero-width spaces */
190 testUnicodeZeroWidthChar(const void *opaque ATTRIBUTE_UNUSED
)
193 vshTablePtr table
= NULL
;
195 " I\u200Bd Name \u200BStatus\n"
196 "--------------------------\n"
197 " 1\u200B fedora28 run\u200Bning\n"
198 " 2 rhel7.5 running\n";
202 /* If this char is not classed as printable, the actual
203 * output won't match what this test expects. The code
204 * is still operating correctly, but we have different
206 mbrtowc(&wc
, "\u200B", MB_CUR_MAX
, NULL
);
210 table
= vshTableNew("I\u200Bd", "Name", "\u200BStatus", NULL
);
213 vshTableRowAppend(table
, "1\u200B", "fedora28", "run\u200Bning", NULL
);
214 vshTableRowAppend(table
, "2", "rhel7.5", "running", NULL
);
215 act
= vshTablePrintToString(table
, true);
217 if (virTestCompareToString(exp
, act
) < 0)
227 testUnicodeCombiningChar(const void *opaque ATTRIBUTE_UNUSED
)
230 vshTablePtr table
= NULL
;
233 "--------------------------\n"
234 " 1 fědora28 running\n"
238 table
= vshTableNew("Id", "Náme", "Ⓢtatus", NULL
);
241 vshTableRowAppend(table
, "1", "fědora28", "running", NULL
);
242 vshTableRowAppend(table
, "2", "rhel", "running", NULL
);
243 act
= vshTablePrintToString(table
, true);
245 if (virTestCompareToString(exp
, act
) < 0)
254 /* Testing zero-width characters by inserting few zero-width spaces */
256 testUnicodeNonPrintableChar(const void *opaque ATTRIBUTE_UNUSED
)
259 vshTablePtr table
= NULL
;
261 " I\\x09d Name Status\n"
262 "----------------------------------\n"
263 " 1 f\\x07edora28 running\n"
264 " 2 rhel7.5 running\n";
267 table
= vshTableNew("I\td", "Name", "Status", NULL
);
270 vshTableRowAppend(table
, "1", "f\aedora28", "running", NULL
);
271 vshTableRowAppend(table
, "2", "rhel7.5", "running", NULL
);
272 act
= vshTablePrintToString(table
, true);
274 if (virTestCompareToString(exp
, act
) < 0)
284 testNTables(const void *opaque ATTRIBUTE_UNUSED
)
287 vshTablePtr table1
= NULL
;
288 vshTablePtr table2
= NULL
;
289 vshTablePtr table3
= NULL
;
292 "--------------------------\n"
293 " 1 fedora28 running\n"
294 " 2 rhel7.5 running\n"
298 "---------------------\n";
310 table1
= vshTableNew("Id", "Name", "Status", NULL
);
313 vshTableRowAppend(table1
, "1", "fedora28", "running", NULL
);
314 vshTableRowAppend(table1
, "2", "rhel7.5", "running", NULL
);
315 vshTableRowAppend(table1
, "3", "gazpacho", "", NULL
);
316 act1
= vshTablePrintToString(table1
, true);
318 table2
= vshTableNew("Id", "Name", "Status", NULL
);
321 act2
= vshTablePrintToString(table2
, true);
323 table3
= vshTableNew("Id", NULL
);
326 vshTableRowAppend(table3
, "1", NULL
);
327 vshTableRowAppend(table3
, "2", NULL
);
328 vshTableRowAppend(table3
, "3", NULL
);
329 vshTableRowAppend(table3
, "4", NULL
);
330 act3
= vshTablePrintToString(table3
, true);
332 if (virTestCompareToString(exp1
, act1
) < 0)
334 if (virTestCompareToString(exp2
, act2
) < 0)
336 if (virTestCompareToString(exp3
, act3
) < 0)
343 vshTableFree(table1
);
344 vshTableFree(table2
);
345 vshTableFree(table3
);
354 if (!setlocale(LC_CTYPE
, "en_US.UTF-8"))
357 if (virTestRun("testVshTableNew", testVshTableNew
, NULL
) < 0)
360 if (virTestRun("testVshTableHeader", testVshTableHeader
, NULL
) < 0)
363 if (virTestRun("testVshTableRowAppend", testVshTableRowAppend
, NULL
) < 0)
366 if (virTestRun("testUnicode", testUnicode
, NULL
) < 0)
369 if (virTestRun("testUnicodeArabic", testUnicodeArabic
, NULL
) < 0)
372 if (virTestRun("testUnicodeZeroWidthChar",
373 testUnicodeZeroWidthChar
,
377 if (virTestRun("testUnicodeCombiningChar",
378 testUnicodeCombiningChar
,
382 if (virTestRun("testUnicodeNonPrintableChar",
383 testUnicodeNonPrintableChar
,
387 if (virTestRun("testNTables", testNTables
, NULL
) < 0)
390 return ret
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
393 VIR_TEST_MAIN(mymain
)