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