i18n: Add simple tests.
authorBen Pfaff <blp@cs.stanford.edu>
Wed, 22 Sep 2010 05:00:13 +0000 (22:00 -0700)
committerBen Pfaff <blp@cs.stanford.edu>
Wed, 22 Sep 2010 05:18:28 +0000 (22:18 -0700)
src/libpspp/i18n.c
tests/atlocal.in
tests/automake.mk
tests/libpspp/i18n-test.c [new file with mode: 0644]
tests/libpspp/i18n.at [new file with mode: 0644]

index 60dc693db6585a967ddae3cc82c37f26455587eb..6cb721a993df60ecd9fc3d4707a9d7b2d19dbeb4 100644 (file)
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
 
 #include <config.h>
-#include <xalloc.h>
+
+#include "libpspp/i18n.h"
+
 #include <assert.h>
+#include <errno.h>
+#include <iconv.h>
+#include <langinfo.h>
+#include <libintl.h>
 #include <locale.h>
-#include <stdlib.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
-#include <libintl.h>
-#include <iconv.h>
-#include <errno.h>
-#include <relocatable.h>
-#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 <localcharset.h>
-#include "xstrndup.h"
-
-#if HAVE_NL_LANGINFO
-#include <langinfo.h>
-#endif
+#include "gl/localcharset.h"
+#include "gl/xalloc.h"
+#include "gl/relocatable.h"
+#include "gl/xstrndup.h"
 
 struct converter
  {
index ecfd2d769fb96bfb1b4423d647fb5ffacf2481d8..cd61f44babbfdb2c9f3fb21fbf8509fdf67fada7 100644 (file)
@@ -3,3 +3,4 @@ PERL='@PERL@'
 CHARSETALIASDIR="$abs_top_builddir/gl"
 export CHARSETALIASDIR
 WITH_PERL_MODULE='@WITH_PERL_MODULE@'
+host='@host@'
index f64a66913ec0ddd83039e2d8d373b9ccf45fc1a2..a27e4d34cc8faba5d723b2271768e7c0ab110c0a 100644 (file)
@@ -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 (file)
index 0000000..f2bead9
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>. */
+
+#include <config.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#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 (file)
index 0000000..c845519
--- /dev/null
@@ -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?])