From: Alessandro Ratti Date: Sun, 28 Dec 2025 18:11:08 +0000 (+0100) Subject: tests: add libsmartcols termreduce regression helper X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=34ebd2a75992472d8569ea262c64a2fef45c255c;p=thirdparty%2Futil-linux.git tests: add libsmartcols termreduce regression helper 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 --- diff --git a/tests/helpers/Makemodule.am b/tests/helpers/Makemodule.am index f87675350..e1cac2f78 100644 --- a/tests/helpers/Makemodule.am +++ b/tests/helpers/Makemodule.am @@ -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 index 000000000..c96ce6d9b --- /dev/null +++ b/tests/helpers/test_scols_termreduce.c @@ -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 +#include +#include + +#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; +}