xref: /netbsd-src/external/gpl2/gettext/dist/gettext-tools/tests/gettext-7-prg.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /* Test program, used by the gettext-7 test.
2    Copyright (C) 2005-2006 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
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) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3))
30 
31 #include <pthread.h>
32 #include "setenv.h"
33 
34 /* Make sure we use the included libintl, not the system's one. */
35 #undef _LIBINTL_H
36 #include "libgnuintl.h"
37 
38 /* Set to 1 if the program is not behaving correctly.  */
39 int result;
40 
41 /* Denotes which thread should run next.  */
42 int flipflop;
43 /* Lock and wait queue used to switch between the threads.  */
44 pthread_mutex_t lock;
45 pthread_cond_t waitqueue;
46 
47 /* Waits until the flipflop has a given value.
48    Before the call, the lock is unlocked.  After the call, it is locked.  */
49 static void
waitfor(int value)50 waitfor (int value)
51 {
52   if (pthread_mutex_lock (&lock))
53     exit (10);
54   while (flipflop != value)
55     if (pthread_cond_wait (&waitqueue, &lock))
56       exit (11);
57 }
58 
59 /* Sets the flipflop to a given value.
60    Before the call, the lock is locked.  After the call, it is unlocked.  */
61 static void
setto(int value)62 setto (int value)
63 {
64   flipflop = value;
65   if (pthread_cond_signal (&waitqueue))
66     exit (20);
67   if (pthread_mutex_unlock (&lock))
68     exit (21);
69 }
70 
71 void *
thread1_execution(void * arg)72 thread1_execution (void *arg)
73 {
74   char *s;
75 
76   waitfor (1);
77   uselocale (newlocale (LC_ALL_MASK, "de_DE.ISO-8859-1", NULL));
78   setto (2);
79 
80   /* Here we expect output in ISO-8859-1.  */
81 
82   waitfor (1);
83   s = gettext ("cheese");
84   puts (s);
85   if (strcmp (s, "K\344se"))
86     {
87       fprintf (stderr, "thread 1 call 1 returned: %s\n", s);
88       result = 1;
89     }
90   setto (2);
91 
92   waitfor (1);
93   s = gettext ("cheese");
94   puts (s);
95   if (strcmp (s, "K\344se"))
96     {
97       fprintf (stderr, "thread 1 call 2 returned: %s\n", s);
98       result = 1;
99     }
100   setto (2);
101 
102   return NULL;
103 }
104 
105 void *
thread2_execution(void * arg)106 thread2_execution (void *arg)
107 {
108   char *s;
109 
110   waitfor (2);
111   uselocale (newlocale (LC_ALL_MASK, "de_DE.UTF-8", NULL));
112   setto (1);
113 
114   /* Here we expect output in UTF-8.  */
115 
116   waitfor (2);
117   s = gettext ("cheese");
118   puts (s);
119   if (strcmp (s, "K\303\244se"))
120     {
121       fprintf (stderr, "thread 2 call 1 returned: %s\n", s);
122       result = 1;
123     }
124   setto (1);
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 2 returned: %s\n", s);
132       result = 1;
133     }
134   setto (1);
135 
136   return NULL;
137 }
138 
139 int
main(void)140 main (void)
141 {
142   pthread_t thread1;
143   pthread_t thread2;
144 
145   unsetenv ("LANGUAGE");
146   unsetenv ("OUTPUT_CHARSET");
147   textdomain ("tstthread");
148   bindtextdomain ("tstthread", ".");
149   result = 0;
150 
151   flipflop = 1;
152   if (pthread_mutex_init (&lock, NULL))
153     exit (2);
154   if (pthread_cond_init (&waitqueue, NULL))
155     exit (2);
156   if (pthread_create (&thread1, NULL, &thread1_execution, NULL))
157     exit (2);
158   if (pthread_create (&thread2, NULL, &thread2_execution, NULL))
159     exit (2);
160   if (pthread_join (thread2, NULL))
161     exit (3);
162 
163   return result;
164 }
165 
166 #else
167 
168 /* This test is not executed.  */
169 
170 int
main(void)171 main (void)
172 {
173   return 77;
174 }
175 
176 #endif
177