diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/meson.build | 17 | ||||
-rw-r--r-- | src/test/test-format-table.c | 139 |
2 files changed, 143 insertions, 13 deletions
diff --git a/src/test/meson.build b/src/test/meson.build index 2585bb3fa..833634ffa 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -1,19 +1,6 @@ # SPDX-License-Identifier: LGPL-2.1+ # # Copyright 2017 Zbigniew Jędrzejewski-Szmek -# -# systemd is free software; you can redistribute it and/or modify it -# under the terms of the GNU Lesser General Public License as published by -# the Free Software Foundation; either version 2.1 of the License, or -# (at your option) any later version. -# -# systemd is distributed in the hope that it will be useful, but -# WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU -# Lesser General Public License for more details. -# -# You should have received a copy of the GNU Lesser General Public License -# along with systemd; If not, see <http://www.gnu.org/licenses/>. awkscript = 'test-hashmap-ordered.awk' test_hashmap_ordered_c = custom_target( @@ -203,6 +190,10 @@ tests += [ [], []], + [['src/test/test-format-table.c'], + [], + []], + #if 0 /// UNNEEDED in elogind # [['src/test/test-ratelimit.c'], # [], diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c new file mode 100644 index 000000000..6c5ae9f87 --- /dev/null +++ b/src/test/test-format-table.c @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: LGPL-2.1+ */ + +//#include "alloc-util.h" +//#include "format-table.h" +//#include "string-util.h" +//#include "time-util.h" + +int main(int argc, char *argv[]) { + + _cleanup_(table_unrefp) Table *t = NULL; + _cleanup_free_ char *formatted = NULL; + + assert_se(setenv("COLUMNS", "40", 1) >= 0); + + assert_se(t = table_new("ONE", "TWO", "THREE")); + + assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0); + + assert_se(table_add_many(t, + TABLE_STRING, "xxx", + TABLE_STRING, "yyy", + TABLE_BOOLEAN, true) >= 0); + + assert_se(table_add_many(t, + TABLE_STRING, "a long field", + TABLE_STRING, "yyy", + TABLE_BOOLEAN, false) >= 0); + + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "ONE TWO THREE\n" + "xxx yyy yes\n" + "a long field yyy no\n")); + + formatted = mfree(formatted); + + table_set_width(t, 40); + + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "ONE TWO THREE\n" + "xxx yyy yes\n" + "a long field yyy no\n")); + + formatted = mfree(formatted); + + table_set_width(t, 12); + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "ONE TWO THR…\n" + "xxx yyy yes\n" + "a … yyy no\n")); + + formatted = mfree(formatted); + + table_set_width(t, 5); + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "… … …\n" + "… … …\n" + "… … …\n")); + + formatted = mfree(formatted); + + table_set_width(t, 3); + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "… … …\n" + "… … …\n" + "… … …\n")); + + formatted = mfree(formatted); + + table_set_width(t, (size_t) -1); + assert_se(table_set_sort(t, (size_t) 0, (size_t) 2, (size_t) -1) >= 0); + + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "ONE TWO THREE\n" + "a long field yyy no\n" + "xxx yyy yes\n")); + + formatted = mfree(formatted); + + table_set_header(t, false); + + assert_se(table_add_many(t, + TABLE_STRING, "fäää", + TABLE_STRING, "uuu", + TABLE_BOOLEAN, true) >= 0); + + assert_se(table_add_many(t, + TABLE_STRING, "fäää", + TABLE_STRING, "zzz", + TABLE_BOOLEAN, false) >= 0); + + assert_se(table_add_many(t, + TABLE_EMPTY, + TABLE_SIZE, (uint64_t) 4711, + TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0); + + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + "a long field yyy no\n" + "fäää zzz no\n" + "fäää uuu yes\n" + "xxx yyy yes\n" + " 4.6K 5min\n")); + + formatted = mfree(formatted); + + assert_se(table_set_display(t, (size_t) 2, (size_t) 0, (size_t) 2, (size_t) 0, (size_t) 0, (size_t) -1) >= 0); + + assert_se(table_format(t, &formatted) >= 0); + printf("%s\n", formatted); + + assert_se(streq(formatted, + " no a long f… no a long f… a long fi…\n" + " no fäää no fäää fäää \n" + " yes fäää yes fäää fäää \n" + " yes xxx yes xxx xxx \n" + "5min 5min \n")); + + return 0; +} |