Compile in the debugging code all the time, but only output debug messages
[platform/upstream/glib.git] / tests / gio-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2000  Tor Lillqvist
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* A test program for the main loop and IO channel code.
21  * Just run it.
22  */
23
24 #include "config.h"
25
26 #include <glib.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <math.h>
31 #include <time.h>
32
33 #ifdef G_OS_WIN32
34   #include <io.h>
35   #include <fcntl.h>
36   #include <process.h>
37 #else
38   #ifdef HAVE_UNISTD_H
39     #include <unistd.h>
40   #endif
41 #endif
42
43 static int nrunning;
44 static GMainLoop *main_loop;
45
46 #define BUFSIZE 5000            /* Larger than the circular buffer in
47                                  * giowin32.c on purpose.
48                                  */
49
50 static int nkiddies;
51
52 static struct {
53   int fd;
54   int seq;
55 } *seqtab;
56
57 static GIOError
58 read_all (int         fd,
59           GIOChannel *channel,
60           char       *buffer,
61           guint       nbytes,
62           guint      *bytes_read)
63 {
64   guint left = nbytes;
65   guint nb;
66   GIOError error;
67   char *bufp = buffer;
68
69   /* g_io_channel_read() doesn't necessarily return all the
70    * data we want at once.
71    */
72   *bytes_read = 0;
73   while (left)
74     {
75       error = g_io_channel_read (channel, bufp, left, &nb);
76       
77       if (error != G_IO_ERROR_NONE)
78         {
79           g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
80                    (error == G_IO_ERROR_AGAIN ? "AGAIN" :
81                     (error == G_IO_ERROR_INVAL ? "INVAL" :
82                      (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
83           if (error == G_IO_ERROR_AGAIN)
84             continue;
85           break;
86         }
87       if (nb == 0)
88         return error;
89       left -= nb;
90       bufp += nb;
91       *bytes_read += nb;
92     }
93   return error;
94 }
95
96
97 static gboolean
98 recv_message (GIOChannel  *channel,
99               GIOCondition cond,
100               gpointer    data)
101 {
102   gint fd = g_io_channel_unix_get_fd (channel);
103
104   g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
105            (cond & G_IO_ERR) ? " ERR" : "",
106            (cond & G_IO_HUP) ? " HUP" : "",
107            (cond & G_IO_IN)  ? " IN"  : "",
108            (cond & G_IO_PRI) ? " PRI" : "");
109
110   if (cond & (G_IO_ERR | G_IO_HUP))
111     {
112       g_source_remove (*(guint *) data);
113       nrunning--;
114       if (nrunning == 0)
115         g_main_quit (main_loop);
116     }
117
118   if (cond & G_IO_IN)
119     {
120       char buf[BUFSIZE];
121       guint nbytes;
122       guint nb;
123       int i, j, seq;
124       GIOError error;
125       
126       error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
127       if (error == G_IO_ERROR_NONE)
128         {
129           if (nb == 0)
130             {
131               g_print ("gio-test: ...from %d: EOF\n", fd);
132               return FALSE;
133             }
134           
135           g_assert (nb == sizeof (nbytes));
136
137           for (i = 0; i < nkiddies; i++)
138             if (seqtab[i].fd == fd)
139               {
140                 if (seq != seqtab[i].seq)
141                   {
142                     g_print ("gio-test: ...from &d: invalid sequence number %d, expected %d\n",
143                              seq, seqtab[i].seq);
144                     g_assert_not_reached ();
145                   }
146                 seqtab[i].seq++;
147                 break;
148               }
149
150           error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
151         }
152
153       if (error != G_IO_ERROR_NONE)
154         return FALSE;
155       
156       if (nb == 0)
157         {
158           g_print ("gio-test: ...from %d: EOF\n", fd);
159           return FALSE;
160         }
161       
162       g_assert (nb == sizeof (nbytes));
163
164       if (nbytes >= BUFSIZE)
165         {
166           g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
167           g_assert_not_reached ();
168         }
169       g_assert (nbytes >= 0 && nbytes < BUFSIZE);
170       
171       g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
172       
173       if (nbytes > 0)
174         {
175           error = read_all (fd, channel, buf, nbytes, &nb);
176
177           if (error != G_IO_ERROR_NONE)
178             return FALSE;
179
180           if (nb == 0)
181             {
182               g_print ("gio-test: ...from %d: EOF\n", fd);
183               return FALSE;
184             }
185       
186           for (j = 0; j < nbytes; j++)
187             if (buf[j] != ' ' + ((nbytes + j) % 95))
188               {
189                 g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
190                          fd, j, buf[j], 'a' + ((nbytes + j) % 32));
191                 g_assert_not_reached ();
192               }
193           g_print ("gio-test: ...from %d: OK\n", fd);
194         }
195     }
196   return TRUE;
197 }
198
199 int
200 main (int    argc,
201       char **argv)
202 {
203   if (argc < 3)
204     {
205       /* Parent */
206       
207       GIOChannel *my_read_channel;
208       gchar *cmdline;
209       guint *id;
210       int i;
211 #ifdef G_OS_WIN32
212       GTimeVal start, end;
213       int pollresult;
214 #endif
215
216       nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
217       seqtab = g_malloc (nkiddies * 2 * sizeof (int));
218
219       for (i = 0; i < nkiddies; i++)
220         {
221           int pipe_to_sub[2], pipe_from_sub[2];
222           
223           if (pipe (pipe_to_sub) == -1 ||
224               pipe (pipe_from_sub) == -1)
225             perror ("pipe"), exit (1);
226           
227           
228           seqtab[i].fd = pipe_from_sub[0];
229           seqtab[i].seq = 0;
230
231           my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
232           
233           id = g_new (guint, 1);
234           *id =
235             g_io_add_watch (my_read_channel,
236                             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
237                             recv_message,
238                             id);
239           
240           cmdline = g_strdup_printf ("%s %d %d &", argv[0],
241                                      pipe_to_sub[0], pipe_from_sub[1]);
242           
243           nrunning++;
244           
245 #ifdef G_OS_WIN32
246           {
247             gchar *readfd = g_strdup_printf ("%d", pipe_to_sub[0]);
248             gchar *writefd = g_strdup_printf ("%d", pipe_from_sub[1]);
249             _spawnl (_P_NOWAIT, argv[0], argv[0], readfd, writefd, NULL);
250           }
251 #else
252           system (cmdline);
253 #endif
254           close (pipe_to_sub[0]);
255           close (pipe_from_sub [1]);
256
257 #ifdef G_OS_WIN32
258           g_get_current_time (&start);
259           pollresult = g_io_channel_win32_wait_for_condition (my_read_channel, G_IO_IN, 100);
260           g_get_current_time (&end);
261           if (end.tv_usec < start.tv_usec)
262             end.tv_sec--, end.tv_usec += 1000000;
263           g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
264                    end.tv_sec - start.tv_sec,
265                    (end.tv_usec - start.tv_usec) / 1000,
266                    pollresult);
267 #endif
268         }
269       
270       main_loop = g_main_new (FALSE);
271       
272       g_main_run (main_loop);
273     }
274   else if (argc == 3)
275     {
276       /* Child */
277       
278       int readfd, writefd;
279       int i, j;
280       char buf[BUFSIZE];
281       int buflen;
282       GTimeVal tv;
283   
284       g_get_current_time (&tv);
285       
286       readfd = atoi (argv[1]);
287       writefd = atoi (argv[2]);
288       
289       srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
290   
291       for (i = 0; i < 20 + rand() % 20; i++)
292         {
293           g_usleep (100 + (rand() % 10) * 5000);
294           buflen = rand() % BUFSIZE;
295           for (j = 0; j < buflen; j++)
296             buf[j] = ' ' + ((buflen + j) % 95);
297           g_print ("gio-test: child writing %d bytes to %d\n", buflen, writefd);
298           write (writefd, &i, sizeof (i));
299           write (writefd, &buflen, sizeof (buflen));
300           write (writefd, buf, buflen);
301         }
302       g_print ("gio-test: child exiting, closing %d\n", writefd);
303       close (writefd);
304     }
305   else
306     g_print ("Huh?\n");
307   
308   return 0;
309 }
310