pty: Activate the signature wrapper of forkpty.
[gnulib.git] / tests / test-duplocale.c
1 /* Test of duplicating a locale object.
2    Copyright (C) 2009-2013 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 <string.h>
32
33 #include "macros.h"
34
35 struct locale_dependent_values
36 {
37   char monetary[100];
38   char numeric[100];
39   char time[100];
40 };
41
42 static void
43 get_locale_dependent_values (struct locale_dependent_values *result)
44 {
45   strfmon (result->monetary, sizeof (result->monetary),
46            "%n", 123.75);
47   /* result->monetary is usually "$123.75" */
48   snprintf (result->numeric, sizeof (result->numeric),
49             "%g", 3.5);
50   /* result->numeric is usually "3,5" */
51   strcpy (result->time, nl_langinfo (MON_1));
52   /* result->time is usually "janvier" */
53 }
54
55 int
56 main ()
57 {
58   struct locale_dependent_values expected_results;
59   locale_t mixed1;
60   locale_t mixed2;
61
62   /* Set up a locale which is a mix between different system locales.  */
63   setlocale (LC_ALL, "en_US.UTF-8");
64   setlocale (LC_NUMERIC, "de_DE.UTF-8");
65   setlocale (LC_TIME, "fr_FR.UTF-8");
66   get_locale_dependent_values (&expected_results);
67
68   /* Save the locale in a locale_t object.  */
69   mixed1 = duplocale (LC_GLOBAL_LOCALE);
70   ASSERT (mixed1 != NULL);
71
72   /* Use a per-thread locale.  */
73   uselocale (newlocale (LC_ALL_MASK, "es_ES.UTF-8", NULL));
74
75   /* Save the locale in a locale_t object again.  */
76   mixed2 = duplocale (LC_GLOBAL_LOCALE);
77   ASSERT (mixed2 != NULL);
78
79   /* Set up a default locale.  */
80   setlocale (LC_ALL, "C");
81   uselocale (LC_GLOBAL_LOCALE);
82   {
83     struct locale_dependent_values c_results;
84     get_locale_dependent_values (&c_results);
85   }
86
87   /* Now use the saved locale mixed1 again.  */
88   setlocale (LC_ALL, "C");
89   uselocale (LC_GLOBAL_LOCALE);
90   uselocale (mixed1);
91   {
92     struct locale_dependent_values results;
93     get_locale_dependent_values (&results);
94     ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
95     ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
96     ASSERT (strcmp (results.time, expected_results.time) == 0);
97   }
98
99   /* Now use the saved locale mixed2 again.  */
100   setlocale (LC_ALL, "C");
101   uselocale (LC_GLOBAL_LOCALE);
102   uselocale (mixed2);
103   {
104     struct locale_dependent_values results;
105     get_locale_dependent_values (&results);
106     ASSERT (strcmp (results.monetary, expected_results.monetary) == 0);
107     ASSERT (strcmp (results.numeric, expected_results.numeric) == 0);
108     ASSERT (strcmp (results.time, expected_results.time) == 0);
109   }
110
111   return 0;
112 }
113
114 #else
115
116 #include <stdio.h>
117
118 int
119 main ()
120 {
121   fprintf (stderr, "Skipping test: function duplocale not available\n");
122   return 77;
123 }
124
125 #endif