LCOV - code coverage report
Current view: top level - test - test-format-table.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 64 64 100.0 %
Date: 2019-08-22 15:41:25 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include "alloc-util.h"
       4             : #include "format-table.h"
       5             : #include "string-util.h"
       6             : #include "time-util.h"
       7             : 
       8           1 : static void test_issue_9549(void) {
       9           1 :         _cleanup_(table_unrefp) Table *table = NULL;
      10           1 :         _cleanup_free_ char *formatted = NULL;
      11             : 
      12           1 :         assert_se(table = table_new("name", "type", "ro", "usage", "created", "modified"));
      13           1 :         assert_se(table_set_align_percent(table, TABLE_HEADER_CELL(3), 100) >= 0);
      14           1 :         assert_se(table_add_many(table,
      15             :                                  TABLE_STRING, "foooo",
      16             :                                  TABLE_STRING, "raw",
      17             :                                  TABLE_BOOLEAN, false,
      18             :                                  TABLE_SIZE, (uint64_t) (673.7*1024*1024),
      19             :                                  TABLE_STRING, "Wed 2018-07-11 00:10:33 JST",
      20             :                                  TABLE_STRING, "Wed 2018-07-11 00:16:00 JST") >= 0);
      21             : 
      22           1 :         table_set_width(table, 75);
      23           1 :         assert_se(table_format(table, &formatted) >= 0);
      24             : 
      25           1 :         printf("%s\n", formatted);
      26           1 :         assert_se(streq(formatted,
      27             :                         "NAME  TYPE RO  USAGE CREATED                    MODIFIED                   \n"
      28             :                         "foooo raw  no 673.6M Wed 2018-07-11 00:10:33 J… Wed 2018-07-11 00:16:00 JST\n"
      29             :                         ));
      30           1 : }
      31             : 
      32           1 : int main(int argc, char *argv[]) {
      33             : 
      34           1 :         _cleanup_(table_unrefp) Table *t = NULL;
      35           2 :         _cleanup_free_ char *formatted = NULL;
      36             : 
      37           1 :         assert_se(setenv("SYSTEMD_COLORS", "0", 1) >= 0);
      38           1 :         assert_se(setenv("COLUMNS", "40", 1) >= 0);
      39             : 
      40           1 :         assert_se(t = table_new("one", "two", "three"));
      41             : 
      42           1 :         assert_se(table_set_align_percent(t, TABLE_HEADER_CELL(2), 100) >= 0);
      43             : 
      44           1 :         assert_se(table_add_many(t,
      45             :                                  TABLE_STRING, "xxx",
      46             :                                  TABLE_STRING, "yyy",
      47             :                                  TABLE_BOOLEAN, true) >= 0);
      48             : 
      49           1 :         assert_se(table_add_many(t,
      50             :                                  TABLE_STRING, "a long field",
      51             :                                  TABLE_STRING, "yyy",
      52             :                                  TABLE_SET_UPPERCASE, 1,
      53             :                                  TABLE_BOOLEAN, false) >= 0);
      54             : 
      55           1 :         assert_se(table_format(t, &formatted) >= 0);
      56           1 :         printf("%s\n", formatted);
      57             : 
      58           1 :         assert_se(streq(formatted,
      59             :                         "ONE          TWO THREE\n"
      60             :                         "xxx          yyy   yes\n"
      61             :                         "a long field YYY    no\n"));
      62             : 
      63           1 :         formatted = mfree(formatted);
      64             : 
      65           1 :         table_set_width(t, 40);
      66             : 
      67           1 :         assert_se(table_format(t, &formatted) >= 0);
      68           1 :         printf("%s\n", formatted);
      69             : 
      70           1 :         assert_se(streq(formatted,
      71             :                         "ONE                TWO             THREE\n"
      72             :                         "xxx                yyy               yes\n"
      73             :                         "a long field       YYY                no\n"));
      74             : 
      75           1 :         formatted = mfree(formatted);
      76             : 
      77           1 :         table_set_width(t, 12);
      78           1 :         assert_se(table_format(t, &formatted) >= 0);
      79           1 :         printf("%s\n", formatted);
      80             : 
      81           1 :         assert_se(streq(formatted,
      82             :                         "ONE TWO THR…\n"
      83             :                         "xxx yyy  yes\n"
      84             :                         "a … YYY   no\n"));
      85             : 
      86           1 :         formatted = mfree(formatted);
      87             : 
      88           1 :         table_set_width(t, 5);
      89           1 :         assert_se(table_format(t, &formatted) >= 0);
      90           1 :         printf("%s\n", formatted);
      91             : 
      92           1 :         assert_se(streq(formatted,
      93             :                         "… … …\n"
      94             :                         "… … …\n"
      95             :                         "… … …\n"));
      96             : 
      97           1 :         formatted = mfree(formatted);
      98             : 
      99           1 :         table_set_width(t, 3);
     100           1 :         assert_se(table_format(t, &formatted) >= 0);
     101           1 :         printf("%s\n", formatted);
     102             : 
     103           1 :         assert_se(streq(formatted,
     104             :                         "… … …\n"
     105             :                         "… … …\n"
     106             :                         "… … …\n"));
     107             : 
     108           1 :         formatted = mfree(formatted);
     109             : 
     110           1 :         table_set_width(t, (size_t) -1);
     111           1 :         assert_se(table_set_sort(t, (size_t) 0, (size_t) 2, (size_t) -1) >= 0);
     112             : 
     113           1 :         assert_se(table_format(t, &formatted) >= 0);
     114           1 :         printf("%s\n", formatted);
     115             : 
     116           1 :         assert_se(streq(formatted,
     117             :                         "ONE          TWO THREE\n"
     118             :                         "a long field YYY    no\n"
     119             :                         "xxx          yyy   yes\n"));
     120             : 
     121           1 :         formatted = mfree(formatted);
     122             : 
     123           1 :         table_set_header(t, false);
     124             : 
     125           1 :         assert_se(table_add_many(t,
     126             :                                  TABLE_STRING, "fäää",
     127             :                                  TABLE_STRING, "uuu",
     128             :                                  TABLE_BOOLEAN, true) >= 0);
     129             : 
     130           1 :         assert_se(table_add_many(t,
     131             :                                  TABLE_STRING, "fäää",
     132             :                                  TABLE_STRING, "zzz",
     133             :                                  TABLE_BOOLEAN, false) >= 0);
     134             : 
     135           1 :         assert_se(table_add_many(t,
     136             :                                  TABLE_EMPTY,
     137             :                                  TABLE_SIZE, (uint64_t) 4711,
     138             :                                  TABLE_TIMESPAN, (usec_t) 5*USEC_PER_MINUTE) >= 0);
     139             : 
     140           1 :         assert_se(table_format(t, &formatted) >= 0);
     141           1 :         printf("%s\n", formatted);
     142             : 
     143           1 :         assert_se(streq(formatted,
     144             :                         "a long field YYY    no\n"
     145             :                         "fäää         zzz    no\n"
     146             :                         "fäää         uuu   yes\n"
     147             :                         "xxx          yyy   yes\n"
     148             :                         "             4.6K 5min\n"));
     149             : 
     150           1 :         formatted = mfree(formatted);
     151             : 
     152           1 :         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);
     153             : 
     154           1 :         assert_se(table_format(t, &formatted) >= 0);
     155           1 :         printf("%s\n", formatted);
     156             : 
     157           1 :         assert_se(streq(formatted,
     158             :                         "  no a long f…   no a long f… a long fi…\n"
     159             :                         "  no fäää        no fäää      fäää      \n"
     160             :                         " yes fäää       yes fäää      fäää      \n"
     161             :                         " yes xxx        yes xxx       xxx       \n"
     162             :                         "5min           5min                     \n"));
     163             : 
     164           1 :         test_issue_9549();
     165             : 
     166           1 :         return 0;
     167             : }

Generated by: LCOV version 1.14