437de6486db2affad256bb93e2b8239745208077
[platform/upstream/gettext.git] / gettext-tools / tests / gettext-7-prg.c
1 /* Test program, used by the gettext-7 test.
2    Copyright (C) 2005-2007, 2009-2010 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 <haible@clisp.cons.org>, 2005.  */
18
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
22
23 #include <locale.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #if USE_POSIX_THREADS && ((__GLIBC__ >= 2 && !defined __UCLIBC__) || (defined __APPLE__ && defined __MACH__)) && HAVE_USELOCALE
29
30 #include <pthread.h>
31
32 /* Make sure we use the included libintl, not the system's one. */
33 #undef _LIBINTL_H
34 #include "libgnuintl.h"
35
36 /* Name of German locale in ISO-8859-1 or ISO-8859-15 encoding.  */
37 #if __GLIBC__ >= 2
38 # define LOCALE_DE_ISO8859 "de_DE.ISO-8859-1"
39 #elif defined __APPLE__ && defined __MACH__ /* MacOS X */
40 # define LOCALE_DE_ISO8859 "de_DE.ISO8859-1"
41 #else
42 # define LOCALE_DE_ISO8859 "de_DE"
43 #endif
44
45 /* Name of German locale in UTF-8 encoding.  */
46 #define LOCALE_DE_UTF8 "de_DE.UTF-8"
47
48 /* Set to 1 if the program is not behaving correctly.  */
49 int result;
50
51 /* Denotes which thread should run next.  */
52 int flipflop;
53 /* Lock and wait queue used to switch between the threads.  */
54 pthread_mutex_t lock;
55 pthread_cond_t waitqueue;
56
57 /* Waits until the flipflop has a given value.
58    Before the call, the lock is unlocked.  After the call, it is locked.  */
59 static void
60 waitfor (int value)
61 {
62   if (pthread_mutex_lock (&lock))
63     exit (10);
64   while (flipflop != value)
65     if (pthread_cond_wait (&waitqueue, &lock))
66       exit (11);
67 }
68
69 /* Sets the flipflop to a given value.
70    Before the call, the lock is locked.  After the call, it is unlocked.  */
71 static void
72 setto (int value)
73 {
74   flipflop = value;
75   if (pthread_cond_signal (&waitqueue))
76     exit (20);
77   if (pthread_mutex_unlock (&lock))
78     exit (21);
79 }
80
81 void *
82 thread1_execution (void *arg)
83 {
84   char *s;
85
86   waitfor (1);
87   uselocale (newlocale (LC_ALL_MASK, LOCALE_DE_ISO8859, NULL));
88   setto (2);
89
90   /* Here we expect output in ISO-8859-1.  */
91
92   waitfor (1);
93   s = gettext ("cheese");
94   puts (s);
95   if (strcmp (s, "K\344se"))
96     {
97       fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
98       result = 1;
99     }
100   setto (2);
101
102   waitfor (1);
103   s = gettext ("cheese");
104   puts (s);
105   if (strcmp (s, "K\344se"))
106     {
107       fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
108       result = 1;
109     }
110   setto (2);
111
112   return NULL;
113 }
114
115 void *
116 thread2_execution (void *arg)
117 {
118   char *s;
119
120   waitfor (2);
121   uselocale (newlocale (LC_ALL_MASK, LOCALE_DE_UTF8, NULL));
122   setto (1);
123
124   /* Here we expect output in UTF-8.  */
125
126   waitfor (2);
127   s = gettext ("cheese");
128   puts (s);
129   if (strcmp (s, "K\303\244se"))
130     {
131       fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
132       result = 1;
133     }
134   setto (1);
135
136   waitfor (2);
137   s = gettext ("cheese");
138   puts (s);
139   if (strcmp (s, "K\303\244se"))
140     {
141       fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
142       result = 1;
143     }
144   setto (1);
145
146   return NULL;
147 }
148
149 static void
150 check_locale_exists (const char *name)
151 {
152   if (newlocale (LC_ALL_MASK, name, NULL) == NULL)
153     {
154       printf ("%s\n", name);
155       exit (1);
156     }
157 }
158
159 int
160 main (int argc, char *argv[])
161 {
162   int arg;
163   pthread_t thread1;
164   pthread_t thread2;
165
166   arg = (argc > 1 ? atoi (argv[1]) : 0);
167   switch (arg)
168     {
169     case 1:
170       /* Check for the existence of the first locale.  */
171       check_locale_exists (LOCALE_DE_ISO8859);
172       /* Check for the existence of the second locale.  */
173       check_locale_exists (LOCALE_DE_UTF8);
174       return 0;
175     default:
176       break;
177     }
178
179   unsetenv ("LANGUAGE");
180   unsetenv ("OUTPUT_CHARSET");
181   textdomain ("tstthread");
182   bindtextdomain ("tstthread", "gt-7");
183   result = 0;
184
185   flipflop = 1;
186   if (pthread_mutex_init (&lock, NULL))
187     exit (2);
188   if (pthread_cond_init (&waitqueue, NULL))
189     exit (2);
190   if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
191     exit (2);
192   if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
193     exit (2);
194   if (pthread_join (thread2, NULL))
195     exit (3);
196
197   return result;
198 }
199
200 #else
201
202 /* This test is not executed.  */
203
204 int
205 main (void)
206 {
207   return 77;
208 }
209
210 #endif