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