]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
tests: add libsmartcols termreduce regression helper
authorAlessandro Ratti <alessandro@0x65c.net>
Sun, 28 Dec 2025 18:11:08 +0000 (19:11 +0100)
committerAlessandro Ratti <alessandro@0x65c.net>
Wed, 7 Jan 2026 13:52:22 +0000 (14:52 +0100)
Add a small test helper program that creates a libsmartcols table,
applies scols_table_reduce_termwidth(), and prints the table multiple
times within the same process.

The helper is used by regression tests to detect unintended cumulative
width reduction across repeated printing.

Signed-off-by: Alessandro Ratti <alessandro@0x65c.net>
tests/helpers/Makemodule.am
tests/helpers/test_scols_termreduce.c [new file with mode: 0644]

index f876753509565dfb006dbccc21a2d0bcf2b5402b..e1cac2f783f01d598d3732d85ad8387437323932 100644 (file)
@@ -1,3 +1,9 @@
+if !OSS_FUZZ
+check_PROGRAMS += test_scols_termreduce
+test_scols_termreduce_SOURCES = tests/helpers/test_scols_termreduce.c
+test_scols_termreduce_LDADD = $(LDADD) libsmartcols.la
+endif
+
 check_PROGRAMS += test_mbsencode
 test_mbsencode_SOURCES = tests/helpers/test_mbsencode.c
 test_mbsencode_LDADD = $(LDADD) libcommon.la
diff --git a/tests/helpers/test_scols_termreduce.c b/tests/helpers/test_scols_termreduce.c
new file mode 100644 (file)
index 0000000..c96ce6d
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * test_scols_termreduce.c
+ *
+ * Regression test helper for libsmartcols.
+ *
+ * Verify that calling scols_table_reduce_termwidth() does not cause
+ * cumulative width reduction when printing is repeated in the same process.
+ *
+ * This helper prints the same multiple times. All outputs must be identical:
+ *   - All prints:   width = 40 - 4 = 36 (constant)
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "libsmartcols/src/libsmartcols.h"
+
+#define TEST_TERMWIDTH 40
+#define TEST_TERMREDUCE 4
+#define NUM_ITERATIONS 3
+
+int main(void)
+{
+       struct libscols_table *tb;
+       struct libscols_column *cl;
+       struct libscols_line *ln;
+       int i;
+       const char *longstr =
+               "THIS-IS-A-VERY-LONG-STRING-THAT-WOULD-BE-TRUNCATED";
+
+       tb = scols_new_table();
+       if (!tb)
+               return EXIT_FAILURE;
+
+       /* Make output deterministic */
+       scols_table_set_termwidth(tb, TEST_TERMWIDTH);
+
+       /* Force terminal mode */
+       scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
+
+       /* Exercise termreduce handling */
+       scols_table_reduce_termwidth(tb, TEST_TERMREDUCE);
+
+       /* One truncated column */
+       cl = scols_table_new_column(tb, "DATA", 0, SCOLS_FL_TRUNC);
+       if (!cl)
+               goto err;
+
+       ln = scols_table_new_line(tb, NULL);
+       if (!ln)
+               goto err;
+
+       if (scols_line_set_data(ln, 0, longstr) != 0)
+               goto err;
+
+       /* Print multiple times - output must be identical */
+       for (i = 0; i < NUM_ITERATIONS; i++) {
+               scols_print_table(tb);
+       }
+
+       scols_unref_table(tb);
+       return EXIT_SUCCESS;
+
+err:
+       scols_unref_table(tb);
+       return EXIT_FAILURE;
+}