HALF ECHO LEARN / REFERENCE COLLECTION

v262.2

src/test/test-format-table.c

48,536 bytesGit mode 100644Download original fileLGPL-2.1-or-later

Git blob: 0b3ec62953b4197a5057fe199f486f3c5059e196

Lines 1–1000 of 1,128

/* SPDX-License-Identifier: LGPL-2.1-or-later */
 
#include <stdlib.h>
#include <unistd.h>
 
#include "alloc-util.h"
#include "ansi-color.h"
#include "env-util.h"
#include "format-table.h"
#include "json-util.h"
#include "string-util.h"
#include "strv.h"
#include "terminal-util.h"
#include "tests.h"
#include "time-util.h"
#include "utf8.h"
 
TEST(issue_9549) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((table = table_new("name", "type", "ro", "usage", "created", "modified")));
        ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100));
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "foooo",
                                 TABLE_STRING, "raw",
                                 TABLE_BOOLEAN, false,
                                 TABLE_SIZE, (uint64_t) (673.7*1024*1024),
                                 TABLE_STRING, "Wed 2018-07-11 00:10:33 JST",
                                 TABLE_STRING, "Wed 2018-07-11 00:16:00 JST"));
 
        table_set_width(table, 75);
        ASSERT_OK(table_format(table, &formatted));
 
        printf("%s\n", formatted);
        ASSERT_STREQ(formatted,
                     "NAME  TYPE RO  USAGE CREATED                    MODIFIED\n"
                     "foooo raw  no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n");
}
 
TEST(invalid_utf8_cell) {
        /* A cell that is not valid UTF-8 must be refused the same way whatever the column width is.
         * The width was previously determined with a more permissive decoder than the one ellipsize()
         * uses, so a surrogate, overlong, noncharacter or obsolete five byte sequence was measured as
         * if it were fine, and then failed inside ellipsize() as a bogus -ENOMEM, but only once the
         * column happened to be narrow enough to require truncation. The widths below deliberately go
         * past the width of the whole table, so that the last iterations are ones where no truncation
         * is needed at all: those are the ones where the old code did not fail but printed the invalid
         * sequence to the terminal instead. */
 
        FOREACH_STRING(x,
                       "\xed\xa0\x80",          /* UTF-16 surrogate */
                       "\xc0\xaf",              /* overlong '/' */
                       "\xef\xb7\x90",          /* noncharacter U+FDD0 */
                       "\xf8\x88\x80\x80\x80")  /* obsolete five byte form */
                for (size_t w = 8; w <= 64; w += 8) {
                        _cleanup_(table_unrefp) Table *table = NULL;
                        _cleanup_free_ char *formatted = NULL, *cell = NULL;
 
                        ASSERT_NOT_NULL((table = table_new("name", "value")));
                        ASSERT_NOT_NULL((cell = strjoin("aaaaaaaaaaaaaaaa", x, "aaaaaaaaaaaaaaaa")));
                        ASSERT_OK(table_add_many(table,
                                                 TABLE_STRING, cell,
                                                 TABLE_STRING, "second"));
                        table_set_width(table, w);
 
                        ASSERT_ERROR(table_format(table, &formatted), EINVAL);
                }
}
 
TEST(tab_in_cell) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        /* A tab takes up eight character cells, so the padding align_string_mem() adds has to be
         * computed with unichar_console_width() too, or the column comes out too wide.
         *
         * Note that unichar_console_width() charges a tab a flat eight cells, while a terminal
         * advances to the next multiple of eight instead. The two only agree if the tab itself starts
         * on a multiple of eight, hence the eight characters in front of it here: that way the
         * expected output below is what a terminal really renders, too. */
 
        ASSERT_NOT_NULL((table = table_new("name", "value")));
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "aaaaaaaa\tb",
                                 TABLE_STRING, "1",
                                 TABLE_STRING, "wide enough to pad the tab cell",
                                 TABLE_STRING, "2"));
 
        table_set_width(table, 40);
        ASSERT_OK(table_format(table, &formatted));
 
        printf("%s\n", formatted);
        ASSERT_STREQ(formatted,
                     "NAME                             VALUE\n"
                     "aaaaaaaa\tb                1\n"
                     "wide enough to pad the tab cell  2\n");
 
        /* And the case the two passes have to agree on: a cell whose tab makes it the widest one in
         * its column, in a table pinned narrower than that. The cell drives the requested column width
         * and is then handed to ellipsize(), which used to believe the tab was a single cell and so
         * returned something that still overflowed the column it had just been asked to fit into. */
 
        _cleanup_(table_unrefp) Table *narrow = NULL;
        _cleanup_free_ char *narrow_formatted = NULL;
        _cleanup_strv_free_ char **lines = NULL;
 
        ASSERT_NOT_NULL((narrow = table_new("name", "value")));
        ASSERT_OK(table_add_many(narrow,
                                 TABLE_STRING, "aaaaaaaa\tbbbbbbbb",
                                 TABLE_STRING, "1",
                                 TABLE_STRING, "short",
                                 TABLE_STRING, "2"));
 
        table_set_width(narrow, 20);
        ASSERT_OK(table_format(narrow, &narrow_formatted));
 
        printf("%s\n", narrow_formatted);
        ASSERT_NOT_NULL((lines = strv_split_newlines(narrow_formatted)));
        STRV_FOREACH(l, lines)
                ASSERT_LE(utf8_console_width(*l), (size_t) 20);
}
 
TEST(multiline) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((table = table_new("foo", "bar")));
 
        ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100));
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "three\ndifferent\nlines",
                                 TABLE_STRING, "two\nlines\n"));
 
        table_set_cell_height_max(table, 1);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO     BAR\n"
                     "three… two…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 2);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO          BAR\n"
                     "three        two\n"
                     "different… lines\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 3);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, SIZE_MAX);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n");
        formatted = mfree(formatted);
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "short",
                                 TABLE_STRING, "a\npair"));
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "short2\n",
                                 TABLE_STRING, "a\nfour\nline\ncell"));
 
        table_set_cell_height_max(table, 1);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO     BAR\n"
                     "three… two…\n"
                     "short    a…\n"
                     "short2   a…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 2);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO          BAR\n"
                     "three        two\n"
                     "different… lines\n"
                     "short          a\n"
                     "            pair\n"
                     "short2         a\n"
                     "           four…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 3);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n"
                     "short         a\n"
                     "           pair\n"
                     "short2        a\n"
                     "           four\n"
                     "          line…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, SIZE_MAX);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n"
                     "short         a\n"
                     "           pair\n"
                     "short2        a\n"
                     "           four\n"
                     "           line\n"
                     "           cell\n");
        formatted = mfree(formatted);
}
 
TEST(strv) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((table = table_new("foo", "bar")));
 
        ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100));
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRV, STRV_MAKE("three", "different", "lines"),
                                 TABLE_STRV, STRV_MAKE("two", "lines")));
 
        table_set_cell_height_max(table, 1);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO     BAR\n"
                     "three… two…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 2);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO          BAR\n"
                     "three        two\n"
                     "different… lines\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 3);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, SIZE_MAX);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n");
        formatted = mfree(formatted);
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "short",
                                 TABLE_STRV, STRV_MAKE("a", "pair")));
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRV, STRV_MAKE("short2"),
                                 TABLE_STRV, STRV_MAKE("a", "four", "line", "cell")));
 
        table_set_cell_height_max(table, 1);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                        "FOO     BAR\n"
                        "three… two…\n"
                        "short    a…\n"
                        "short2   a…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 2);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO          BAR\n"
                     "three        two\n"
                     "different… lines\n"
                     "short          a\n"
                     "            pair\n"
                     "short2         a\n"
                     "           four…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 3);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n"
                     "short         a\n"
                     "           pair\n"
                     "short2        a\n"
                     "           four\n"
                     "          line…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, SIZE_MAX);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO         BAR\n"
                     "three       two\n"
                     "different lines\n"
                     "lines     \n"
                     "short         a\n"
                     "           pair\n"
                     "short2        a\n"
                     "           four\n"
                     "           line\n"
                     "           cell\n");
        formatted = mfree(formatted);
}
 
TEST(strv_wrapped) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((table = table_new("foo", "bar")));
 
        ASSERT_OK(table_set_align_percent(table, TABLE_HEADER_CELL(1), 100));
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRV_WRAPPED, STRV_MAKE("three", "different", "lines"),
                                 TABLE_STRV_WRAPPED, STRV_MAKE("two", "lines")));
 
        table_set_cell_height_max(table, 1);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                         BAR\n"
                     "three different lines two lines\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 2);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                         BAR\n"
                     "three different lines two lines\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 3);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                         BAR\n"
                     "three different lines two lines\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, SIZE_MAX);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                         BAR\n"
                     "three different lines two lines\n");
        formatted = mfree(formatted);
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRING, "short",
                                 TABLE_STRV_WRAPPED, STRV_MAKE("a", "pair")));
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_STRV_WRAPPED, STRV_MAKE("short2"),
                                 TABLE_STRV_WRAPPED, STRV_MAKE("a", "eight", "line", "ćęłł",
                                                               "___5___", "___6___", "___7___", "___8___")));
 
        table_set_cell_height_max(table, 1);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                             BAR\n"
                     "three different…          two lines\n"
                     "short                        a pair\n"
                     "short2           a eight line ćęłł…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 2);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                           BAR\n"
                     "three different         two lines\n"
                     "lines           \n"
                     "short                      a pair\n"
                     "short2          a eight line ćęłł\n"
                     "                 ___5___ ___6___…\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, 3);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                           BAR\n"
                     "three different         two lines\n"
                     "lines           \n"
                     "short                      a pair\n"
                     "short2          a eight line ćęłł\n"
                     "                  ___5___ ___6___\n"
                     "                  ___7___ ___8___\n");
        formatted = mfree(formatted);
 
        table_set_cell_height_max(table, SIZE_MAX);
        ASSERT_OK(table_format(table, &formatted));
        fputs(formatted, stdout);
        ASSERT_STREQ(formatted,
                     "FOO                           BAR\n"
                     "three different         two lines\n"
                     "lines           \n"
                     "short                      a pair\n"
                     "short2          a eight line ćęłł\n"
                     "                  ___5___ ___6___\n"
                     "                  ___7___ ___8___\n");
        formatted = mfree(formatted);
}
 
TEST(json) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL;
        _cleanup_(table_unrefp) Table *t = NULL;
 
        ASSERT_NOT_NULL((t = table_new_raw(4)));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_HEADER, "foo bar",
                                 TABLE_HEADER, "quux",
                                 TABLE_HEADER, "piep miau",
                                 TABLE_HEADER, "asdf",
                                 TABLE_SET_JSON_FIELD_NAME, "asdf-custom"));
        ASSERT_OK(table_set_json_field_name(t, 2, "ZZZ"));
        ASSERT_OK(table_set_json_field_name(t, 2, NULL));
        ASSERT_OK(table_set_json_field_name(t, 2, "zzz"));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "v1",
                                 TABLE_UINT64, UINT64_C(4711),
                                 TABLE_BOOLEAN, true,
                                 TABLE_EMPTY));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRV, STRV_MAKE("a", "b", "c"),
                                 TABLE_EMPTY,
                                 TABLE_MODE, 0755,
                                 TABLE_EMPTY));
 
        ASSERT_OK(table_to_json(t, &v));
 
        ASSERT_OK(sd_json_build(&w,
                             SD_JSON_BUILD_ARRAY(
                                             SD_JSON_BUILD_OBJECT(
                                                             SD_JSON_BUILD_PAIR("foo_bar", JSON_BUILD_CONST_STRING("v1")),
                                                             SD_JSON_BUILD_PAIR_UNSIGNED("quux", 4711),
                                                             SD_JSON_BUILD_PAIR_BOOLEAN("zzz", true),
                                                             SD_JSON_BUILD_PAIR("asdf-custom", SD_JSON_BUILD_NULL)),
                                             SD_JSON_BUILD_OBJECT(
                                                             SD_JSON_BUILD_PAIR("foo_bar", SD_JSON_BUILD_STRV(STRV_MAKE("a", "b", "c"))),
                                                             SD_JSON_BUILD_PAIR("quux", SD_JSON_BUILD_NULL),
                                                             SD_JSON_BUILD_PAIR_UNSIGNED("zzz", 0755),
                                                             SD_JSON_BUILD_PAIR("asdf-custom", SD_JSON_BUILD_NULL)))));
 
        ASSERT_TRUE(sd_json_variant_equal(v, w));
}
 
TEST(json_mangling) {
        static const struct {
                const char *arg;
                const char *exp;
        } cases[] = {
                /* Not Mangled */
                { "foo", "foo" },
                { "foo_bar", "foo_bar" },
                { "fooBar", "fooBar" },
                { "fooBar123", "fooBar123" },
                { "foo_bar123", "foo_bar123" },
                { ALPHANUMERICAL, ALPHANUMERICAL },
                { "_123", "_123" },
 
                /* Mangled */
                { "Foo Bar", "foo_bar" },
                { "Foo-Bar", "foo_bar" },
                { "Foo@Bar", "foo_bar" },
                { "Foo (Bar)", "foo__bar_"},
                { "MixedCase ALLCAPS", "mixedCase_ALLCAPS" },
                { "_X", "_x" },
                { "_Foo", "_foo" },
        };
 
        FOREACH_ELEMENT(i, cases) {
                _cleanup_free_ char *ret = NULL;
                ASSERT_NOT_NULL((ret = table_mangle_to_json_field_name(i->arg)));
                printf("\"%s\" -> \"%s\"\n", i->arg, ret);
                ASSERT_STREQ(ret, i->exp);
        }
}
 
TEST(table) {
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new("one", "two", "three", "four")));
 
        ASSERT_OK(table_set_align_percent(t, TABLE_HEADER_CELL(3), 100));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "xxx",
                                 TABLE_STRING, "yyy",
                                 TABLE_BOOLEAN, true,
                                 TABLE_INT, -1));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "a long field",
                                 TABLE_STRING, "yyy",
                                 TABLE_SET_UPPERCASE, 1,
                                 TABLE_BOOLEAN, false,
                                 TABLE_INT, -999999));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "ONE          TWO THREE    FOUR\n"
                     "xxx          yyy yes        -1\n"
                     "a long field YYY no    -999999\n");
 
        formatted = mfree(formatted);
 
        table_set_width(t, 40);
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "ONE            TWO   THREE          FOUR\n"
                     "xxx            yyy   yes              -1\n"
                     "a long field   YYY   no          -999999\n");
 
        formatted = mfree(formatted);
 
        table_set_width(t, 15);
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "ONE TWO TH… FO…\n"
                     "xxx yyy yes  -1\n"
                     "a … YYY no  -9…\n");
 
        formatted = mfree(formatted);
 
        table_set_width(t, 5);
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "… … … …\n"
                     "… … … …\n"
                     "… … … …\n");
 
        formatted = mfree(formatted);
 
        table_set_width(t, 3);
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "… … … …\n"
                     "… … … …\n"
                     "… … … …\n");
 
        formatted = mfree(formatted);
 
        table_set_width(t, SIZE_MAX);
        ASSERT_OK(table_set_sort(t, (size_t) 0, (size_t) 2, SIZE_MAX));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "ONE          TWO THREE    FOUR\n"
                     "a long field YYY no    -999999\n"
                     "xxx          yyy yes        -1\n");
 
        formatted = mfree(formatted);
 
        table_set_header(t, false);
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "fäää",
                                 TABLE_STRING, "uuu",
                                 TABLE_BOOLEAN, true,
                                 TABLE_INT, 42));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "fäää",
                                 TABLE_STRING, "zzz",
                                 TABLE_BOOLEAN, false,
                                 TABLE_INT, 0));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_EMPTY,
                                 TABLE_SIZE, (uint64_t) 4711,
                                 TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE,
                                 TABLE_INT64, (uint64_t) -123456789));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "a long field YYY  no      -999999\n"
                     "fäää         zzz  no            0\n"
                     "fäää         uuu  yes          42\n"
                     "xxx          yyy  yes          -1\n"
                     "             4.6K 5min -123456789\n");
 
        formatted = mfree(formatted);
 
        ASSERT_OK(table_set_display(t, (size_t) 2, (size_t) 0, (size_t) 2, (size_t) 0, (size_t) 0, SIZE_MAX));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        if (isatty_safe(STDOUT_FILENO))
                ASSERT_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");
        else
                ASSERT_STREQ(formatted,
                             "no   a long field no   a long field a long field\n"
                             "no   fäää         no   fäää         fäää\n"
                             "yes  fäää         yes  fäää         fäää\n"
                             "yes  xxx          yes  xxx          xxx\n"
                             "5min              5min              \n");
}
 
TEST(mixed_type_sort) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((table = table_new("key")));
        table_set_header(table, false);
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
 
        ASSERT_OK(table_add_many(table, TABLE_UINT, 1U));
        ASSERT_OK(table_add_cell(table, NULL, TABLE_STRING, "b"));
        ASSERT_OK(table_add_cell(table, NULL, TABLE_STRING, NULL));
        ASSERT_OK(table_add_cell(table, NULL, TABLE_STRING, "a"));
        ASSERT_OK(table_set_sort(table, (size_t) 0));
        ASSERT_OK(table_format(table, &formatted));
 
        ASSERT_STREQ(formatted,
                     "a\n"
                     "b\n"
                     "1\n"
                     "-\n");
 
        formatted = mfree(formatted);
        ASSERT_OK(table_set_reverse(table, 0, true));
        ASSERT_OK(table_format(table, &formatted));
 
        ASSERT_STREQ(formatted,
                     "-\n"
                     "1\n"
                     "b\n"
                     "a\n");
}
 
TEST(empty_primary_sort) {
        _cleanup_(table_unrefp) Table *table = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((table = table_new("primary", "secondary")));
        table_set_header(table, false);
        table_set_ersatz_string(table, TABLE_ERSATZ_DASH);
 
        ASSERT_OK(table_add_many(table,
                                 TABLE_EMPTY,
                                 TABLE_STRING, "b"));
        ASSERT_OK(table_add_many(table,
                                 TABLE_EMPTY,
                                 TABLE_STRING, "a"));
        ASSERT_OK(table_set_sort(table, (size_t) 0, (size_t) 1));
        ASSERT_OK(table_format(table, &formatted));
 
        ASSERT_STREQ(formatted,
                     "- a\n"
                     "- b\n");
}
 
TEST(tristate) {
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL, *w = NULL;
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new("name", "flag")));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "neg",
                                 TABLE_TRISTATE, -1));
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "zero",
                                 TABLE_TRISTATE, 0));
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "pos",
                                 TABLE_TRISTATE, 1));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
        ASSERT_STREQ(formatted,
                     "NAME FLAG\n"
                     "neg  \n"
                     "zero no\n"
                     "pos  yes\n");
        formatted = mfree(formatted);
 
        /* Try a non-default ersatz string. */
        table_set_ersatz_string(t, TABLE_ERSATZ_DASH);
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
        ASSERT_STREQ(formatted,
                     "NAME FLAG\n"
                     "neg  -\n"
                     "zero no\n"
                     "pos  yes\n");
        formatted = mfree(formatted);
 
        /* Sorting: -1 < 0 < 1 */
        ASSERT_OK(table_set_sort(t, (size_t) 1, SIZE_MAX));
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
        ASSERT_STREQ(formatted,
                     "NAME FLAG\n"
                     "neg  -\n"
                     "zero no\n"
                     "pos  yes\n");
        formatted = mfree(formatted);
 
        /* JSON: -1 → null, 0 → false, positive → true */
        ASSERT_OK(table_to_json(t, &v));
 
        ASSERT_OK(sd_json_build(&w,
                                SD_JSON_BUILD_ARRAY(
                                                SD_JSON_BUILD_OBJECT(
                                                                SD_JSON_BUILD_PAIR("name", JSON_BUILD_CONST_STRING("neg")),
                                                                SD_JSON_BUILD_PAIR("flag", SD_JSON_BUILD_NULL)),
                                                SD_JSON_BUILD_OBJECT(
                                                                SD_JSON_BUILD_PAIR("name", JSON_BUILD_CONST_STRING("zero")),
                                                                SD_JSON_BUILD_PAIR_BOOLEAN("flag", false)),
                                                SD_JSON_BUILD_OBJECT(
                                                                SD_JSON_BUILD_PAIR("name", JSON_BUILD_CONST_STRING("pos")),
                                                                SD_JSON_BUILD_PAIR_BOOLEAN("flag", true)))));
 
        ASSERT_TRUE(sd_json_variant_equal(v, w));
}
 
TEST(signed_integers) {
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new("int", "int8", "int16", "int32", "int64")));
        table_set_width(t, 0);
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_INT, -1,
                                 TABLE_INT8, INT8_C(-1),
                                 TABLE_INT16, INT16_C(-1),
                                 TABLE_INT32, INT32_C(-1),
                                 TABLE_INT64, INT64_C(-1)));
        ASSERT_OK(table_add_many(t,
                                 TABLE_INT, INT_MAX,
                                 TABLE_INT8, INT8_MAX,
                                 TABLE_INT16, INT16_MAX,
                                 TABLE_INT32, INT32_MAX,
                                 TABLE_INT64, INT64_MAX));
        ASSERT_OK(table_add_many(t,
                                 TABLE_INT, INT_MIN,
                                 TABLE_INT8, INT8_MIN,
                                 TABLE_INT16, INT16_MIN,
                                 TABLE_INT32, INT32_MIN,
                                 TABLE_INT64, INT64_MIN));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "INT         INT8 INT16  INT32       INT64\n"
                     "-1          -1   -1     -1          -1\n"
                     "2147483647  127  32767  2147483647  9223372036854775807\n"
                     "-2147483648 -128 -32768 -2147483648 -9223372036854775808\n");
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL;
        ASSERT_OK(table_to_json(t, &a));
 
        table_print_json(t, /* f= */ NULL, SD_JSON_FORMAT_NEWLINE);
 
        ASSERT_OK(sd_json_build(&b,
                                SD_JSON_BUILD_ARRAY(
                                  SD_JSON_BUILD_OBJECT(
                                    SD_JSON_BUILD_PAIR_INTEGER("int", -1),
                                    SD_JSON_BUILD_PAIR_INTEGER("int8", -1),
                                    SD_JSON_BUILD_PAIR_INTEGER("int16", -1),
                                    SD_JSON_BUILD_PAIR_INTEGER("int32", -1),
                                    SD_JSON_BUILD_PAIR_INTEGER("int64", -1)),
                                  SD_JSON_BUILD_OBJECT(
                                    SD_JSON_BUILD_PAIR_INTEGER("int", INT_MAX),
                                    SD_JSON_BUILD_PAIR_INTEGER("int8", INT8_MAX),
                                    SD_JSON_BUILD_PAIR_INTEGER("int16", INT16_MAX),
                                    SD_JSON_BUILD_PAIR_INTEGER("int32", INT32_MAX),
                                    SD_JSON_BUILD_PAIR_INTEGER("int64", INT64_MAX)),
                                  SD_JSON_BUILD_OBJECT(
                                    SD_JSON_BUILD_PAIR_INTEGER("int", INT_MIN),
                                    SD_JSON_BUILD_PAIR_INTEGER("int8", INT8_MIN),
                                    SD_JSON_BUILD_PAIR_INTEGER("int16", INT16_MIN),
                                    SD_JSON_BUILD_PAIR_INTEGER("int32", INT32_MIN),
                                    SD_JSON_BUILD_PAIR_INTEGER("int64", INT64_MIN)))));
        sd_json_variant_dump(b, SD_JSON_FORMAT_NEWLINE, stdout, NULL);
 
        ASSERT_TRUE(sd_json_variant_equal(a, b));
}
 
TEST(unsigned_integers) {
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new("uint", "uint8", "uint16", "uint32", "uhex32", "uint64", "uhex64")));
        table_set_width(t, 0);
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_UINT, 0,
                                 TABLE_UINT8, UINT8_C(0),
                                 TABLE_UINT16, UINT16_C(0),
                                 TABLE_UINT32, UINT32_C(0),
                                 TABLE_UINT32_HEX, UINT32_C(0),
                                 TABLE_UINT64, UINT64_C(0),
                                 TABLE_UINT64_HEX, UINT64_C(0)));
        ASSERT_OK(table_add_many(t,
                                 TABLE_UINT, UINT_MAX,
                                 TABLE_UINT8, UINT8_MAX,
                                 TABLE_UINT16, UINT16_MAX,
                                 TABLE_UINT32, UINT32_MAX,
                                 TABLE_UINT32_HEX, UINT32_MAX,
                                 TABLE_UINT64, UINT64_MAX,
                                 TABLE_UINT64_HEX, UINT64_MAX));
 
        ASSERT_OK(table_format(t, &formatted));
        printf("%s\n", formatted);
 
        ASSERT_STREQ(formatted,
                     "UINT       UINT8 UINT16 UINT32     UHEX32   UINT64               UHEX64\n"
                     "0          0     0      0          0        0                    0\n"
                     "4294967295 255   65535  4294967295 ffffffff 18446744073709551615 ffffffffffffffff\n");
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL;
        ASSERT_OK(table_to_json(t, &a));
 
        table_print_json(t, /* f= */ NULL, SD_JSON_FORMAT_NEWLINE);
 
        ASSERT_OK(sd_json_build(&b,
                                SD_JSON_BUILD_ARRAY(
                                  SD_JSON_BUILD_OBJECT(
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint", 0),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint8", 0),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint16", 0),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint32", 0),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uhex32", 0),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint64", 0),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uhex64", 0)),
                                  SD_JSON_BUILD_OBJECT(
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint", UINT_MAX),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint8", UINT8_MAX),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint16", UINT16_MAX),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint32", UINT32_MAX),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uhex32", UINT32_MAX),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uint64", UINT64_MAX),
                                    SD_JSON_BUILD_PAIR_UNSIGNED("uhex64", UINT64_MAX)))));
        sd_json_variant_dump(b, SD_JSON_FORMAT_NEWLINE, stdout, NULL);
 
        ASSERT_TRUE(sd_json_variant_equal(a, b));
}
 
TEST(vertical) {
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new_vertical()));
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_FIELD, "pfft aa", TABLE_STRING, "foo",
                                 TABLE_FIELD, "uuu o", TABLE_SIZE, UINT64_C(1024),
                                 TABLE_FIELD, "quux", TABLE_STRING, "asdf", TABLE_SET_JSON_FIELD_NAME, "custom-quux",
                                 TABLE_FIELD, "lllllllllllo", TABLE_STRING, "jjjjjjjjjjjjjjjjj"));
 
        ASSERT_OK(table_set_json_field_name(t, 1, "DIMPFELMOSER"));
        ASSERT_OK(table_set_json_field_name(t, 1, NULL));
        ASSERT_OK(table_set_json_field_name(t, 1, "dimpfelmoser"));
 
        ASSERT_OK(table_format(t, &formatted));
 
        ASSERT_STREQ(formatted,
                     "     pfft aa: foo\n"
                     "       uuu o: 1K\n"
                     "        quux: asdf\n"
                     "lllllllllllo: jjjjjjjjjjjjjjjjj\n");
 
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *a = NULL, *b = NULL;
        ASSERT_OK(table_to_json(t, &a));
 
        ASSERT_OK(sd_json_build(&b, SD_JSON_BUILD_OBJECT(
                                             SD_JSON_BUILD_PAIR_STRING("pfft_aa", "foo"),
                                             SD_JSON_BUILD_PAIR_UNSIGNED("dimpfelmoser", 1024),
                                             SD_JSON_BUILD_PAIR_STRING("custom-quux", "asdf"),
                                             SD_JSON_BUILD_PAIR_STRING("lllllllllllo", "jjjjjjjjjjjjjjjjj"))));
 
        ASSERT_TRUE(sd_json_variant_equal(a, b));
}
 
TEST(path_basename) {
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new("x")));
 
        table_set_header(t, false);
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_PATH_BASENAME, "/foo/bar",
                                 TABLE_PATH_BASENAME, "/quux/bar",
                                 TABLE_PATH_BASENAME, "/foo/baz"));
 
        ASSERT_OK(table_format(t, &formatted));
 
        ASSERT_STREQ(formatted, "bar\nbar\nbaz\n");
}
 
TEST(dup_cell) {
        _cleanup_(table_unrefp) Table *t = NULL;
        _cleanup_free_ char *formatted = NULL;
 
        ASSERT_NOT_NULL((t = table_new("foo", "bar", "x", "baz", ".", "%", "!", "~", "+")));
        table_set_width(t, 75);
 
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "hello",
                                 TABLE_UINT8, UINT8_C(42),
                                 TABLE_UINT16, UINT16_C(666),
                                 TABLE_UINT32, UINT32_C(253),
                                 TABLE_PERCENT, 0,
                                 TABLE_PATH_BASENAME, "/foo/bar",
                                 TABLE_STRING, "aaa",
                                 TABLE_STRING, "bbb",
                                 TABLE_STRING, "ccc"));
 
        /* Add the second row by duping cells */
        for (size_t i = 0; i < table_get_columns(t); i++)
                ASSERT_OK(table_dup_cell(t, table_get_cell(t, 1, i)));
 
        /* Another row, but dupe the last three strings from the same cell */
        ASSERT_OK(table_add_many(t,
                                 TABLE_STRING, "aaa",
                                 TABLE_UINT8, UINT8_C(0),
                                 TABLE_UINT16, UINT16_C(65535),
                                 TABLE_UINT32, UINT32_C(4294967295),
                                 TABLE_PERCENT, 100,
                                 TABLE_PATH_BASENAME, "../"));
 
        for (size_t i = 6; i < table_get_columns(t); i++)