Add some more cases to the app-id unit tests
[glib.git] / gio / tests / sleepy-stream.c
blob4d3ca1d27745ca5742b4f935ca8dc27da1cb1cc5
1 /*
2 * Copyright © 2009 Codethink Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * See the included COPYING file for more information.
11 * Author: Ryan Lortie <desrt@desrt.ca>
14 #include <gio/gio.h>
15 #include <string.h>
17 #define MAX_PIECE_SIZE 100
18 #define MAX_PIECES 60
20 static gchar *
21 cook_piece (void)
23 char buffer[MAX_PIECE_SIZE * 2];
24 gint symbols, i = 0;
26 symbols = g_test_rand_int_range (1, MAX_PIECE_SIZE + 1);
28 while (symbols--)
30 gint c = g_test_rand_int_range (0, 30);
32 switch (c)
34 case 26:
35 buffer[i++] = '\n';
36 case 27:
37 buffer[i++] = '\r';
38 break;
40 case 28:
41 buffer[i++] = '\r';
42 case 29:
43 buffer[i++] = '\n';
44 break;
46 default:
47 buffer[i++] = c + 'a';
48 break;
51 g_assert_cmpint (i, <=, sizeof buffer);
54 return g_strndup (buffer, i);
57 static gchar **
58 cook_pieces (void)
60 gchar **array;
61 gint pieces;
63 pieces = g_test_rand_int_range (0, MAX_PIECES + 1);
64 array = g_new (char *, pieces + 1);
65 array[pieces] = NULL;
67 while (pieces--)
68 array[pieces] = cook_piece ();
70 return array;
73 typedef struct
75 GInputStream parent_instance;
77 gboolean built_to_fail;
78 gchar **pieces;
79 gint index;
81 const gchar *current;
82 } SleepyStream;
84 typedef GInputStreamClass SleepyStreamClass;
86 GType sleepy_stream_get_type (void);
88 G_DEFINE_TYPE (SleepyStream, sleepy_stream, G_TYPE_INPUT_STREAM)
90 static gssize
91 sleepy_stream_read (GInputStream *stream,
92 void *buffer,
93 gsize length,
94 GCancellable *cancellable,
95 GError **error)
97 SleepyStream *sleepy = (SleepyStream *) stream;
99 if (sleepy->pieces[sleepy->index] == NULL)
101 if (sleepy->built_to_fail)
103 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, "fail");
104 return -1;
106 else
107 return 0;
109 else
111 if (!sleepy->current)
112 sleepy->current = sleepy->pieces[sleepy->index++];
114 length = MIN (strlen (sleepy->current), length);
115 memcpy (buffer, sleepy->current, length);
117 sleepy->current += length;
118 if (*sleepy->current == '\0')
119 sleepy->current = NULL;
121 return length;
125 static void
126 sleepy_stream_init (SleepyStream *sleepy)
128 sleepy->pieces = cook_pieces ();
129 sleepy->built_to_fail = FALSE;
130 sleepy->index = 0;
133 static void
134 sleepy_stream_finalize (GObject *object)
136 SleepyStream *sleepy = (SleepyStream *) object;
138 g_strfreev (sleepy->pieces);
139 G_OBJECT_CLASS (sleepy_stream_parent_class)
140 ->finalize (object);
143 static void
144 sleepy_stream_class_init (SleepyStreamClass *class)
146 G_OBJECT_CLASS (class)->finalize = sleepy_stream_finalize;
147 class->read_fn = sleepy_stream_read;
149 /* no read_async implementation.
150 * main thread will sleep while read runs in a worker.
154 static SleepyStream *
155 sleepy_stream_new (void)
157 return g_object_new (sleepy_stream_get_type (), NULL);
160 static gboolean
161 read_line (GDataInputStream *stream,
162 GString *string,
163 const gchar *eol,
164 GError **error)
166 gsize length;
167 char *str;
169 str = g_data_input_stream_read_line (stream, &length, NULL, error);
171 if (str == NULL)
172 return FALSE;
174 g_assert (strstr (str, eol) == NULL);
175 g_assert (strlen (str) == length);
177 g_string_append (string, str);
178 g_string_append (string, eol);
179 g_free (str);
181 return TRUE;
184 static void
185 build_comparison (GString *str,
186 SleepyStream *stream)
188 /* build this for comparison */
189 gint i;
191 for (i = 0; stream->pieces[i]; i++)
192 g_string_append (str, stream->pieces[i]);
194 if (str->len && str->str[str->len - 1] != '\n')
195 g_string_append_c (str, '\n');
199 static void
200 test (void)
202 SleepyStream *stream = sleepy_stream_new ();
203 GDataInputStream *data;
204 GError *error = NULL;
205 GString *one;
206 GString *two;
208 one = g_string_new (NULL);
209 two = g_string_new (NULL);
211 data = g_data_input_stream_new (G_INPUT_STREAM (stream));
212 g_data_input_stream_set_newline_type (data, G_DATA_STREAM_NEWLINE_TYPE_LF);
213 build_comparison (one, stream);
215 while (read_line (data, two, "\n", &error));
217 g_assert_cmpstr (one->str, ==, two->str);
218 g_string_free (one, TRUE);
219 g_string_free (two, TRUE);
220 g_object_unref (stream);
221 g_object_unref (data);
224 static GDataInputStream *data;
225 static GString *one, *two;
226 static GMainLoop *loop;
227 static const gchar *eol;
229 static void
230 asynch_ready (GObject *object,
231 GAsyncResult *result,
232 gpointer user_data)
234 GError *error = NULL;
235 gsize length;
236 gchar *str;
238 g_assert (data == G_DATA_INPUT_STREAM (object));
240 str = g_data_input_stream_read_line_finish (data, result, &length, &error);
242 if (str == NULL)
244 g_main_loop_quit (loop);
245 if (error)
246 g_error_free (error);
248 else
250 g_assert (length == strlen (str));
251 g_string_append (two, str);
252 g_string_append (two, eol);
253 g_free (str);
255 /* MOAR!! */
256 g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
261 static void
262 asynch (void)
264 SleepyStream *sleepy = sleepy_stream_new ();
266 data = g_data_input_stream_new (G_INPUT_STREAM (sleepy));
267 one = g_string_new (NULL);
268 two = g_string_new (NULL);
269 eol = "\n";
271 build_comparison (one, sleepy);
272 g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
273 g_main_loop_run (loop = g_main_loop_new (NULL, FALSE));
275 g_assert_cmpstr (one->str, ==, two->str);
276 g_string_free (one, TRUE);
277 g_string_free (two, TRUE);
278 g_object_unref (sleepy);
279 g_object_unref (data);
283 main (int argc, char **argv)
285 g_test_init (&argc, &argv, NULL);
286 g_test_bug_base ("http://bugzilla.gnome.org/");
288 g_test_add_func ("/filter-stream/input", test);
289 g_test_add_func ("/filter-stream/async", asynch);
291 return g_test_run();