# HG changeset patch # User Bruno Haible # Date 1261571450 -3600 # Node ID 4756a0883f46ce05eb3eed2063407104a4857ded # Parent 3607500d45237eeefeca500a54d15fa0ee22a9db More localename tests. diff --git a/ChangeLog b/ChangeLog --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2009-12-23 Bruno Haible + + * tests/test-localename.c (test_locale_name): New function, extracted + from main. Also test mixed situations. + (test_locale_name_posix, test_locale_name_environ, + test_locale_name_default): New functions. + (main): Invoke them all. + * modules/localename-tests (configure.ac): Test for newlocale. + 2009-12-23 Bruno Haible unistd: Ensure getcwd gets declared before being overridden. diff --git a/modules/localename-tests b/modules/localename-tests --- a/modules/localename-tests +++ b/modules/localename-tests @@ -7,6 +7,7 @@ unsetenv configure.ac: +AC_CHECK_FUNCS_ONCE([newlocale]) Makefile.am: TESTS += test-localename diff --git a/tests/test-localename.c b/tests/test-localename.c --- a/tests/test-localename.c +++ b/tests/test-localename.c @@ -1,5 +1,5 @@ -/* Test of gl_locale_name function. - Copyright (C) 2007, 2008 Free Software Foundation, Inc. +/* Test of gl_locale_name function and its variants. + Copyright (C) 2007-2009 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 @@ -37,14 +37,23 @@ } \ while (0) -int -main () +/* Test the gl_locale_name() function. */ +static void +test_locale_name (void) { + const char *name; + /* Check that gl_locale_name returns non-NULL. */ ASSERT (gl_locale_name (LC_MESSAGES, "LC_MESSAGES") != NULL); + /* Get into a defined state, */ + setlocale (LC_ALL, "en_US.UTF-8"); +#if HAVE_NEWLOCALE + uselocale (LC_GLOBAL_LOCALE); +#endif + /* Check that when all environment variables are unset, - gl_locale_name_posix returns NULL. */ + gl_locale_name returns the default locale. */ unsetenv ("LC_ALL"); unsetenv ("LC_CTYPE"); unsetenv ("LC_MESSAGES"); @@ -99,7 +108,6 @@ unsetenv ("LC_MESSAGES"); unsetenv ("LANG"); setlocale (LC_ALL, ""); - ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0); ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0); unsetenv ("LC_ALL"); @@ -107,7 +115,6 @@ setenv ("LC_MESSAGES", "C", 1); unsetenv ("LANG"); setlocale (LC_ALL, ""); - ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0); ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0); unsetenv ("LC_ALL"); @@ -115,8 +122,265 @@ unsetenv ("LC_MESSAGES"); setenv ("LANG", "C", 1); setlocale (LC_ALL, ""); - ASSERT (strcmp (gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"), "C") == 0); ASSERT (strcmp (gl_locale_name (LC_MESSAGES, "LC_MESSAGES"), "C") == 0); + /* Check mixed situations. */ + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1); + setenv ("LANG", "de_DE.UTF-8", 1); + if (setlocale (LC_ALL, "") != NULL) + { + name = gl_locale_name (LC_CTYPE, "LC_CTYPE"); + ASSERT (strcmp (name, "de_DE.UTF-8") == 0); + name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "fr_FR.UTF-8") == 0); + } + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1); + unsetenv ("LANG"); + if (setlocale (LC_ALL, "") != NULL) + { + name = gl_locale_name (LC_CTYPE, "LC_CTYPE"); + ASSERT (strcmp (name, gl_locale_name_default ()) == 0); + name = gl_locale_name (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "fr_FR.UTF-8") == 0); + } +} + +/* Test the gl_locale_name_posix() function. */ +static void +test_locale_name_posix (void) +{ + const char *name; + + /* Get into a defined state, */ + setlocale (LC_ALL, "en_US.UTF-8"); +#if HAVE_NEWLOCALE + uselocale (LC_GLOBAL_LOCALE); +#endif + + /* Check that when all environment variables are unset, + gl_locale_name_posix returns either NULL or the default locale. */ + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + unsetenv ("LC_NUMERIC"); + unsetenv ("LANG"); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + name = gl_locale_name_posix (LC_NUMERIC, "LC_NUMERIC"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + + /* Check that an empty environment variable is treated like an unset + environment variable. */ + + setenv ("LC_ALL", "", 1); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + unsetenv ("LANG"); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + + unsetenv ("LC_ALL"); + setenv ("LC_CTYPE", "", 1); + unsetenv ("LC_MESSAGES"); + unsetenv ("LANG"); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "", 1); + unsetenv ("LANG"); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + setenv ("LANG", "", 1); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + + /* Check that LC_ALL overrides the others, and LANG is overridden by the + others. */ + + setenv ("LC_ALL", "C", 1); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + unsetenv ("LANG"); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "C") == 0); + + unsetenv ("LC_ALL"); + setenv ("LC_CTYPE", "C", 1); + setenv ("LC_MESSAGES", "C", 1); + unsetenv ("LANG"); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "C") == 0); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + setenv ("LANG", "C", 1); + setlocale (LC_ALL, ""); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "C") == 0); + + /* Check mixed situations. */ + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1); + setenv ("LANG", "de_DE.UTF-8", 1); + if (setlocale (LC_ALL, "") != NULL) + { + name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE"); + ASSERT (strcmp (name, "de_DE.UTF-8") == 0); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "fr_FR.UTF-8") == 0); + } + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1); + unsetenv ("LANG"); + if (setlocale (LC_ALL, "") != NULL) + { + name = gl_locale_name_posix (LC_CTYPE, "LC_CTYPE"); + ASSERT (name == NULL || strcmp (name, gl_locale_name_default ()) == 0); + name = gl_locale_name_posix (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "fr_FR.UTF-8") == 0); + } +} + +/* Test the gl_locale_name_environ() function. */ +static void +test_locale_name_environ (void) +{ + const char *name; + + /* Get into a defined state, */ + setlocale (LC_ALL, "en_US.UTF-8"); +#if HAVE_NEWLOCALE + uselocale (LC_GLOBAL_LOCALE); +#endif + + /* Check that when all environment variables are unset, + gl_locale_name_environ returns NULL. */ + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + unsetenv ("LC_NUMERIC"); + unsetenv ("LANG"); + ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL); + ASSERT (gl_locale_name_environ (LC_NUMERIC, "LC_NUMERIC") == NULL); + + /* Check that an empty environment variable is treated like an unset + environment variable. */ + + setenv ("LC_ALL", "", 1); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + unsetenv ("LANG"); + ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL); + + unsetenv ("LC_ALL"); + setenv ("LC_CTYPE", "", 1); + unsetenv ("LC_MESSAGES"); + unsetenv ("LANG"); + ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "", 1); + unsetenv ("LANG"); + ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + setenv ("LANG", "", 1); + ASSERT (gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES") == NULL); + + /* Check that LC_ALL overrides the others, and LANG is overridden by the + others. */ + + setenv ("LC_ALL", "C", 1); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + unsetenv ("LANG"); + name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "C") == 0); + + unsetenv ("LC_ALL"); + setenv ("LC_CTYPE", "C", 1); + setenv ("LC_MESSAGES", "C", 1); + unsetenv ("LANG"); + name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "C") == 0); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + unsetenv ("LC_MESSAGES"); + setenv ("LANG", "C", 1); + name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "C") == 0); + + /* Check mixed situations. */ + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1); + setenv ("LANG", "de_DE.UTF-8", 1); + name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE"); + ASSERT (strcmp (name, "de_DE.UTF-8") == 0); + name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "fr_FR.UTF-8") == 0); + + unsetenv ("LC_ALL"); + unsetenv ("LC_CTYPE"); + setenv ("LC_MESSAGES", "fr_FR.UTF-8", 1); + unsetenv ("LANG"); + name = gl_locale_name_environ (LC_CTYPE, "LC_CTYPE"); + ASSERT (name == NULL); + name = gl_locale_name_environ (LC_MESSAGES, "LC_MESSAGES"); + ASSERT (strcmp (name, "fr_FR.UTF-8") == 0); +} + +/* Test the gl_locale_name_default() function. */ +static void +test_locale_name_default (void) +{ + const char *name = gl_locale_name_default (); + + ASSERT (name != NULL); + + /* Only MacOS X and Windows have a facility for the user to set the default + locale. */ +#if !((defined __APPLE__ && defined __MACH__) || (defined _WIN32 || defined __WIN32__ || defined __CYGWIN__)) + ASSERT (strcmp (name, "C") == 0); +#endif +} + +int +main () +{ + test_locale_name (); + test_locale_name_posix (); + test_locale_name_environ (); + test_locale_name_default (); + return 0; }