From c555358da9c0b1747b9b236ba28ec55efdc89852 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Tue, 21 Sep 2010 22:00:13 -0700 Subject: [PATCH] i18n: Add simple tests. --- src/libpspp/i18n.c | 36 +++++++++++++++--------------- tests/atlocal.in | 1 + tests/automake.mk | 7 +++++- tests/libpspp/i18n-test.c | 46 +++++++++++++++++++++++++++++++++++++++ tests/libpspp/i18n.at | 44 +++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 20 deletions(-) create mode 100644 tests/libpspp/i18n-test.c create mode 100644 tests/libpspp/i18n.at diff --git a/src/libpspp/i18n.c b/src/libpspp/i18n.c index 60dc693db6..6cb721a993 100644 --- a/src/libpspp/i18n.c +++ b/src/libpspp/i18n.c @@ -15,31 +15,29 @@ along with this program. If not, see . */ #include -#include + +#include "libpspp/i18n.h" + #include +#include +#include +#include +#include #include -#include #include +#include #include -#include -#include -#include -#include -#include "assertion.h" -#include "hmapx.h" -#include "hash-functions.h" -#include "pool.h" - -#include "i18n.h" -#include "version.h" +#include "libpspp/assertion.h" +#include "libpspp/hmapx.h" +#include "libpspp/hash-functions.h" +#include "libpspp/pool.h" +#include "libpspp/version.h" -#include -#include "xstrndup.h" - -#if HAVE_NL_LANGINFO -#include -#endif +#include "gl/localcharset.h" +#include "gl/xalloc.h" +#include "gl/relocatable.h" +#include "gl/xstrndup.h" struct converter { diff --git a/tests/atlocal.in b/tests/atlocal.in index ecfd2d769f..cd61f44bab 100644 --- a/tests/atlocal.in +++ b/tests/atlocal.in @@ -3,3 +3,4 @@ PERL='@PERL@' CHARSETALIASDIR="$abs_top_builddir/gl" export CHARSETALIASDIR WITH_PERL_MODULE='@WITH_PERL_MODULE@' +host='@host@' diff --git a/tests/automake.mk b/tests/automake.mk index f64a66913e..a27e4d34cc 100644 --- a/tests/automake.mk +++ b/tests/automake.mk @@ -169,6 +169,7 @@ check_PROGRAMS += \ $(nodist_TESTS) \ tests/data/datasheet-test \ tests/formats/inexactify \ + tests/libpspp/i18n-test \ tests/libpspp/sparse-xarray-test \ tests/output/render-test @@ -219,6 +220,9 @@ tests_libpspp_hmapx_test_SOURCES = \ tests_libpspp_hmapx_test_LDADD = gl/libgl.la $(LIBINTL) 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/libpspp.la gl/libgl.la $(LIBINTL) + tests_libpspp_abt_test_SOURCES = \ src/libpspp/abt.c \ src/libpspp/abt.h \ @@ -434,6 +438,7 @@ TESTSUITE_AT = \ tests/language/xforms/compute.at \ tests/language/xforms/count.at \ tests/language/xforms/recode.at \ + tests/libpspp/i18n.at \ tests/math/moments.at \ tests/output/render.at \ tests/output/charts.at \ @@ -451,7 +456,7 @@ $(srcdir)/tests/testsuite.at: tests/testsuite.in Makefile CHECK_LOCAL += tests_check tests_check: tests/atconfig tests/atlocal $(TESTSUITE) - $(SHELL) '$(TESTSUITE)' -C tests AUTOTEST_PATH=tests/output:src/ui/terminal $(TESTSUITEFLAGS) + $(SHELL) '$(TESTSUITE)' -C tests AUTOTEST_PATH=tests/libpspp:tests/output:src/ui/terminal $(TESTSUITEFLAGS) CLEAN_LOCAL += tests_clean tests_clean: diff --git a/tests/libpspp/i18n-test.c b/tests/libpspp/i18n-test.c new file mode 100644 index 0000000000..f2bead96f7 --- /dev/null +++ b/tests/libpspp/i18n-test.c @@ -0,0 +1,46 @@ +/* PSPP - a program for statistical analysis. + Copyright (C) 2010 Free Software Foundation, Inc. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +#include + +#include +#include + +#include "libpspp/i18n.h" + +int +main (int argc, char *argv[]) +{ + char *s; + + if (argc != 4) + { + fprintf (stderr, + "usage: %s FROM TO STRING\n" + "where FROM is the source encoding,\n" + " TO is the target encoding,\n" + " and STRING is the text to recode.\n", + argv[0]); + return EXIT_FAILURE; + } + + i18n_init (); + s = recode_string (argv[2], argv[1], argv[3], -1); + puts (s); + free (s); + + return 0; +} diff --git a/tests/libpspp/i18n.at b/tests/libpspp/i18n.at new file mode 100644 index 0000000000..c845519c23 --- /dev/null +++ b/tests/libpspp/i18n.at @@ -0,0 +1,44 @@ +AT_BANNER([i18n routines]) + +# CHECK_I18N([TITLE], [FROM-CODING], [TO-CODING], [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. (Be aware that hex escapes are not portable; +# use octal escapes instead.) +m4_define([CHECK_I18N], + [AT_SETUP([convert $1]) + AT_KEYWORDS([i18n]) + + dnl Skip the test if this host doesn't know the source and target encodings. + AT_CHECK( + [case "$host" in + *-*-linux* | *-*-*-gnu*) + dnl GNU/Linux always has the encodings we want. We can't ask + dnl config.charset about them because it has a special case here + dnl too and won't tell us. + ;; + *) + $SHELL $top_srcdir/gl/config.charset "$host" | grep '$2' || exit 77 + $SHELL $top_srcdir/gl/config.charset "$host" | grep '$3' || exit 77 + ;; + esac + ], [0], [ignore]) + AT_CHECK_UNQUOTED([i18n-test '$2' '$3' `printf '$4'`], [0], [`printf '$5'` +]) + AT_CLEANUP]) + +CHECK_I18N([reflexively], [ASCII], [ASCII], [abc], [abc]) +CHECK_I18N([without any change], [ASCII], [UTF-8], [abc], [abc]) + +CHECK_I18N([from ISO-8859-1 to UTF-8], [ISO-8859-1], [UTF-8], + [\242], [\302\242]) +CHECK_I18N([from UTF-8 to ISO-8859-1], [UTF-8], [ISO-8859-1], + [\302\242], [\242]) + +# 0xc0 == 0300 is invalid in UTF-8 +CHECK_I18N([invalid UTF-8 to ISO-8859-1], [UTF-8], [ISO-8859-1], + [xy\300z], [xy?z]) +# 0xc2 == 0302 is the first byte of a 2-byte UTF-8 sequence +CHECK_I18N([truncated UTF-8 to ISO-8559-1], [UTF-8], [ISO-8859-1], + [xy\302], [xy?]) -- 2.30.2