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