]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test-format-table: add test for TABLE_BPS formatting
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 29 May 2024 15:06:55 +0000 (17:06 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 29 May 2024 15:36:01 +0000 (17:36 +0200)
src/test/test-format-table.c

index c16a5b793b89d40ce7904a1c09df5a4b9c21e2c2..4425f4097a2eca7fc52f72164cf6384ae0a812b9 100644 (file)
@@ -626,6 +626,41 @@ TEST(dup_cell) {
                         "aaa     0     65535   4294967295   100%   ../    hello    hello    hello\n"));
 }
 
+TEST(table_bps) {
+        _cleanup_(table_unrefp) Table *table = NULL;
+        _cleanup_free_ char *formatted = NULL;
+
+        assert_se(table = table_new("uint64", "size", "bps"));
+        uint64_t v;
+        FOREACH_ARGUMENT(v,
+                         2500,
+                         10000000,
+                         20000000,
+                         25000000,
+                         1000000000,
+                         2000000000,
+                         2500000000)
+                assert_se(table_add_many(table,
+                                         TABLE_UINT64, v,
+                                         TABLE_SIZE, v,
+                                         TABLE_BPS, v) >= 0);
+
+        table_set_width(table, 50);
+        assert_se(table_format(table, &formatted) >= 0);
+
+        printf("%s", formatted);
+        assert_se(streq(formatted,
+                        "UINT64             SIZE            BPS\n"
+                        "2500               2.4K            2Kbps\n"
+                        "10000000           9.5M            10Mbps\n"
+                        "20000000           19.0M           20Mbps\n"
+                        "25000000           23.8M           25Mbps\n"
+                        "1000000000         953.6M          1Gbps\n"
+                        "2000000000         1.8G            2Gbps\n"
+                        "2500000000         2.3G            2Gbps\n"
+                  ));
+}
+
 static int intro(void) {
         assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
         assert_se(setenv("COLUMNS", "40", 1) >= 0);