Imported Upstream version 0.19.7
[platform/upstream/gettext.git] / gettext-tools / tests / gettext-6-prg.c
1 /* Test program, used by the gettext-6 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 and French 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 # define LOCALE_FR_ISO8859 "fr_FR.ISO-8859-1"
41 #elif defined __APPLE__ && defined __MACH__ /* MacOS X */
42 # define LOCALE_DE_ISO8859 "de_DE.ISO8859-1"
43 # define LOCALE_FR_ISO8859 "fr_FR.ISO8859-1"
44 #else
45 # define LOCALE_DE_ISO8859 "de_DE"
46 # define LOCALE_FR_ISO8859 "fr_FR"
47 #endif
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   waitfor (1);
92   s = gettext ("beauty");
93   puts (s);
94   if (strcmp (s, "Sch\366nheit"))
95     {
96       fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
97       result = 1;
98     }
99   setto (2);
100
101   waitfor (1);
102   s = gettext ("beauty");
103   puts (s);
104   if (strcmp (s, "Sch\366nheit"))
105     {
106       fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
107       result = 1;
108     }
109   setto (2);
110
111   return NULL;
112 }
113
114 void *
115 thread2_execution (void *arg)
116 {
117   char *s;
118
119   waitfor (2);
120   uselocale (newlocale (LC_ALL_MASK, LOCALE_FR_ISO8859, NULL));
121   setto (1);
122
123   waitfor (2);
124   s = gettext ("beauty");
125   puts (s);
126   if (strcmp (s, "beaut\351"))
127     {
128       fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
129       result = 1;
130     }
131   setto (1);
132
133   waitfor (2);
134   s = gettext ("beauty");
135   puts (s);
136   if (strcmp (s, "beaut\351"))
137     {
138       fprintf (stderr, "thread 2 call 2 returned: %s\n", s);
139       result = 1;
140     }
141   setto (1);
142
143   return NULL;
144 }
145
146 static void
147 check_locale_exists (const char *name)
148 {
149   if (newlocale (LC_ALL_MASK, name, NULL) == NULL)
150     {
151       printf ("%s\n", name);
152       exit (1);
153     }
154 }
155
156 int
157 main (int argc, char *argv[])
158 {
159   int arg;
160   pthread_t thread1;
161   pthread_t thread2;
162
163   arg = (argc > 1 ? atoi (argv[1]) : 0);
164   switch (arg)
165     {
166     case 1:
167       /* Check for the existence of the first locale.  */
168       check_locale_exists (LOCALE_DE_ISO8859);
169       /* Check for the existence of the second locale.  */
170       check_locale_exists (LOCALE_FR_ISO8859);
171       return 0;
172     default:
173       break;
174     }
175
176   unsetenv ("LANGUAGE");
177   unsetenv ("OUTPUT_CHARSET");
178   textdomain ("tstthread");
179   bindtextdomain ("tstthread", "gt-6");
180   result = 0;
181
182   flipflop = 1;
183   if (pthread_mutex_init (&lock, NULL))
184     exit (2);
185   if (pthread_cond_init (&waitqueue, NULL))
186     exit (2);
187   if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
188     exit (2);
189   if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
190     exit (2);
191   if (pthread_join (thread2, NULL))
192     exit (3);
193
194   return result;
195 }
196
197 #else
198
199 /* This test is not executed.  */
200
201 int
202 main (void)
203 {
204   return 77;
205 }
206
207 #endif