tests: add signature checks
[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 "signature.h"
26 SIGNATURE_CHECK (duplocale, locale_t, (locale_t));
27
28 #include <langinfo.h>
29 #include <monetary.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #define ASSERT(expr) \
35   do                                                                         \
36     {                                                                        \
37       if (!(expr))                                                           \
38         {                                                                    \
39           fprintf (stderr, "%s:%d: assertion failed\n", __FILE__, __LINE__); \
40           fflush (stderr);                                                   \
41           abort ();                                                          \
42         }                                                                    \
43     }                                                                        \
44   while (0)
45
46 struct locale_dependent_values
47 {
48   char monetary[100];
49   char numeric[100];
50   char time[100];
51 };
52
53 static void
54 get_locale_dependent_values (struct locale_dependent_values *result)
55 {
56   strfmon (result->monetary, sizeof (result->monetary),
57            "%n", 123.75);
58   /* result->monetary is usually "$123.75" */
59   snprintf (result->numeric, sizeof (result->numeric),
60             "%g", 3.5);
61   /* result->numeric is usually "3,5" */
62   strcpy (result->time, nl_langinfo (MON_1));
63   /* result->time is usually "janvier" */
64 }
65
66 int
67 main ()
68 {
69   struct locale_dependent_values expected_results;
70   locale_t mixed1;
71   locale_t mixed2;
72
73   /* Set up a locale which is a mix between different system locales.  */
74   setlocale (LC_ALL, "en_US.UTF-8");
75   setlocale (LC_NUMERIC, "de_DE.UTF-8");
76   setlocale (LC_TIME, "fr_FR.UTF-8");
77   get_locale_dependent_values (&expected_results);
78
79   /* Save the locale in a locale_t object.  */
80   mixed1 = duplocale (LC_GLOBAL_LOCALE);
81   ASSERT (mixed1 != NULL);
82
83   /* Use a per-thread locale.  */
84   uselocale (newlocale (LC_ALL_MASK, "es_ES.UTF-8", NULL));
85
86   /* Save the locale in a locale_t object again.  */
87   mixed2 = duplocale (LC_GLOBAL_LOCALE);
88   ASSERT (mixed2 != NULL);
89
90   /* Set up a default locale.  */
91   setlocale (LC_ALL, "C");
92   uselocale (LC_GLOBAL_LOCALE);
93   {
94     struct locale_dependent_values c_results;
95     get_locale_dependent_values (&c_results);
96   }
97
98   /* Now use the saved locale mixed1 again.  */
99   setlocale (LC_ALL, "C");
100   uselocale (LC_GLOBAL_LOCALE);
101   uselocale (mixed1);
102   {
103     struct locale_dependent_values results;
104     get_locale_dependent_values (&results);
105     ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
106     ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
107     ASSERT (strcmp (results.time, expected_results.time) == 0);
108   }
109
110   /* Now use the saved locale mixed2 again.  */
111   setlocale (LC_ALL, "C");
112   uselocale (LC_GLOBAL_LOCALE);
113   uselocale (mixed2);
114   {
115     struct locale_dependent_values results;
116     get_locale_dependent_values (&results);
117     ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
118     ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
119     ASSERT (strcmp (results.time, expected_results.time) == 0);
120   }
121
122   return 0;
123 }
124
125 #else
126
127 int
128 main ()
129 {
130   return 77;
131 }
132
133 #endif