From 2485ec45d8a8a762cea1623fd46821e1a6d55630 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 2 Aug 2020 21:26:31 +0000 Subject: [PATCH] tests: Avoid dealing with character encoding issues on command line. Unix-like systems don't have a command-line encoding, so we can pass whatever bytes we want. mingw uses some particular encoding for command line arguments, so this screws up trying to pass UTF-8 in. This commit side-steps the problem by passing only ASCII characters on the command line and making the test program decode the bytes. This fixes several test failures under mingw. --- tests/automake.mk | 2 +- tests/libpspp/i18n-test.c | 35 ++++++++++++++++++++++++++++++++--- tests/libpspp/i18n.at | 9 +++------ 3 files changed, 36 insertions(+), 10 deletions(-) diff --git a/tests/automake.mk b/tests/automake.mk index f3e14e7e98..8897d9fbc2 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -100,7 +100,7 @@ tests_libpspp_hmapx_test_SOURCES = \ tests_libpspp_hmapx_test_CPPFLAGS = $(AM_CPPFLAGS) -DASSERT_LEVEL=10 tests_libpspp_i18n_test_SOURCES = tests/libpspp/i18n-test.c -tests_libpspp_i18n_test_LDADD = src/libpspp-core.la +tests_libpspp_i18n_test_LDADD = src/libpspp-core.la gl/libgl.la tests_libpspp_abt_test_SOURCES = \ src/libpspp/abt.c \ diff --git a/tests/libpspp/i18n-test.c b/tests/libpspp/i18n-test.c index b0d027090e..a4993a8d02 100644 --- a/tests/libpspp/i18n-test.c +++ b/tests/libpspp/i18n-test.c @@ -22,9 +22,35 @@ #include "libpspp/i18n.h" +#include "gl/xalloc.h" + #undef NDEBUG #include +/* Returns a malloc()'d copy of INPUT with \ooo octal escapes replaced by their + values. */ +static char * +backslash_decode (const char *input) +{ + char *output = xmalloc (strlen (input) + 1); + char *p = output; + for (; *input; input++) + { + if (*input == '\\' && input[1] >= '0' && input[1] <= '7') + { + uint8_t c = 0; + while (input[1] >= '0' && input[1] <= '7') + c = c * 8 + (*++input - '0'); + *p++ = c; + } + else + *p++ = *input; + } + *p = '\0'; + + return output; +} + int main (int argc, char *argv[]) { @@ -48,16 +74,17 @@ main (int argc, char *argv[]) { const char *from = argv[2]; const char *to = argv[3]; - const char *string = argv[4]; + char *string = backslash_decode (argv[4]); char *result = recode_string (to, from, string, -1); puts (result); assert (strlen (result) == recode_string_len (to, from, string, -1)); + free (string); free (result); } else if (argc == 6 && !strcmp (argv[1], "concat")) { - const char *head = argv[2]; - const char *tail = argv[3]; + char *head = backslash_decode (argv[2]); + char *tail = backslash_decode (argv[3]); const char *encoding = argv[4]; int max_len = atoi (argv[5]); char *result; @@ -78,6 +105,8 @@ main (int argc, char *argv[]) } free (result); + free (head); + free (tail); } else { diff --git a/tests/libpspp/i18n.at b/tests/libpspp/i18n.at index 77216aac20..d0e87bb3b9 100644 --- a/tests/libpspp/i18n.at +++ b/tests/libpspp/i18n.at @@ -20,16 +20,14 @@ AT_BANNER([i18n recoding]) # [FROM-TEXT], [TO-TEXT]) # # Converts FROM-TEXT from FROM-CODING to TO-CODING and checks that the result -# is TO-TEXT. The "printf" program is applied to both FROM-TEXT and TO-TEXT to -# allow for backslash-escapes. (Hex escapes are not portable; use octal -# escapes instead.) +# is TO-TEXT. Octal backslash-escapes are supported in FROM-TEXT and TO-TEXT. m4_define([CHECK_I18N_RECODE], [AT_SETUP([convert $1]) AT_KEYWORDS([i18n]) dnl Skip the test if this host doesn't know the source and target encodings. AT_CHECK([i18n-test supports_encodings '$2' '$3']) - AT_CHECK_UNQUOTED([i18n-test recode '$2' '$3' `printf '$4'`], [0], [`printf '$5'` + AT_CHECK_UNQUOTED([i18n-test recode '$2' '$3' '$4'], [0], [`printf '$5'` ]) AT_CLEANUP]) @@ -85,8 +83,7 @@ m4_define([CHECK_I18N_CONCAT], dnl Skip the test if this host doesn't know the encoding. AT_CHECK([i18n-test supports_encodings '$3']) AT_CHECK_UNQUOTED( - [i18n-test concat "`printf '$1'`" "`printf '$2'`" '$3' '$4'], [0], - [`printf '$5'` + [i18n-test concat '$1' '$2' '$3' '$4'], [0], [`printf '$5'` ]) AT_CLEANUP]) -- 2.30.2