Tests for module 'duplocale'.
[gnulib.git] / tests / test-duplocale.c
1 /* Test of duplicating a locale object.
2    Copyright (C) 2009 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
16
17 /* Written by Bruno Haible <bruno@clisp.org>, 2007.  */
18
19 #include <config.h>
20
21 #include <locale.h>
22
23 #if HAVE_DUPLOCALE
24
25 #include <langinfo.h>
26 #include <monetary.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #define ASSERT(expr) \
32   do                                                                         \
33     {                                                                        \
34       if (!(expr))                                                           \
35         {                                                                    \
36           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
37           fflush (stderr);                                                   \
38           abort ();                                                          \
39         }                                                                    \
40     }                                                                        \
41   while (0)
42
43 struct locale_dependent_values
44 {
45   char monetary[100];
46   char numeric[100];
47   char time[100];
48 };
49
50 static void
51 get_locale_dependent_values (struct locale_dependent_values *result)
52 {
53   strfmon (result->monetary, sizeof (result->monetary),
54            "%n", 123.75);
55   /* result->monetary is usually "$123.75" */
56   snprintf (result->numeric, sizeof (result->numeric),
57             "%g", 3.5);
58   /* result->numeric is usually "3,5" */
59   strcpy (result->time, nl_langinfo (MON_1));
60   /* result->time is usually "janvier" */
61 }
62
63 int
64 main ()
65 {
66   struct locale_dependent_values expected_results;
67   locale_t mixed;
68
69   /* Set up a locale which is a mix between different system locales.  */
70   setlocale (LC_ALL, "en_US.UTF-8");
71   setlocale (LC_NUMERIC, "de_DE.UTF-8");
72   setlocale (LC_TIME, "fr_FR.UTF-8");
73   get_locale_dependent_values (&expected_results);
74
75   /* Save the locale in a locale_t object.  */
76   mixed = duplocale (LC_GLOBAL_LOCALE);
77   ASSERT (mixed != NULL);
78
79   /* Set up a default locale.  */
80   setlocale (LC_ALL, "C");
81   {
82     struct locale_dependent_values c_results;
83     get_locale_dependent_values (&c_results);
84   }
85
86   /* Now use the saved locale again.  */
87   uselocale (mixed);
88   {
89     struct locale_dependent_values results;
90     get_locale_dependent_values (&results);
91     ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
92     ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
93     ASSERT (strcmp (results.time, expected_results.time) == 0);
94   }
95
96   return 0;
97 }
98
99 #else
100
101 int
102 main ()
103 {
104   return 77;
105 }
106
107 #endif