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>
17 #define MAX_PIECE_SIZE 100
23 char buffer[MAX_PIECE_SIZE * 2];
26 symbols = g_test_rand_int_range (1, MAX_PIECE_SIZE + 1);
30 gint c = g_test_rand_int_range (0, 30);
47 buffer[i++] = c + 'a';
51 g_assert_cmpint (i, <=, sizeof buffer);
54 return g_strndup (buffer, i);
63 pieces = g_test_rand_int_range (0, MAX_PIECES + 1);
64 array = g_new (char *, pieces + 1);
68 array[pieces] = cook_piece ();
75 GInputStream parent_instance;
77 gboolean built_to_fail;
84 typedef GInputStreamClass SleepyStreamClass;
86 GType sleepy_stream_get_type (void);
88 G_DEFINE_TYPE (SleepyStream, sleepy_stream, G_TYPE_INPUT_STREAM)
91 sleepy_stream_read (GInputStream *stream,
94 GCancellable *cancellable,
97 SleepyStream *sleepy = (SleepyStream *) stream;
99 if (sleepy->pieces[sleepy->index] == NULL)
101 if (sleepy->built_to_fail)
103 g_set_error (error, 0, 0, "fail");
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;
126 sleepy_stream_init (SleepyStream *sleepy)
128 sleepy->pieces = cook_pieces ();
129 sleepy->built_to_fail = FALSE;
134 sleepy_stream_finalize (GObject *object)
136 SleepyStream *sleepy = (SleepyStream *) object;
138 g_strfreev (sleepy->pieces);
139 G_OBJECT_CLASS (sleepy_stream_parent_class)
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);
161 read_line (GDataInputStream *stream,
170 eol_len = 1 + (eol[1] != '\0');
172 str = g_data_input_stream_read_line (stream, &length, NULL, error);
177 g_assert (strstr (str, eol) == NULL);
178 g_assert (strlen (str) == length);
180 g_string_append (string, str);
181 g_string_append (string, eol);
188 build_comparison (GString *str,
189 SleepyStream *stream)
191 /* build this for comparison */
194 for (i = 0; stream->pieces[i]; i++)
195 g_string_append (str, stream->pieces[i]);
197 if (str->len && str->str[str->len - 1] != '\n')
198 g_string_append_c (str, '\n');
205 SleepyStream *stream = sleepy_stream_new ();
206 GDataInputStream *data;
207 GError *error = NULL;
211 one = g_string_new (NULL);
212 two = g_string_new (NULL);
214 data = g_data_input_stream_new (G_INPUT_STREAM (stream));
215 g_data_input_stream_set_newline_type (data, G_DATA_STREAM_NEWLINE_TYPE_LF);
216 build_comparison (one, stream);
218 while (read_line (data, two, "\n", &error));
220 g_assert_cmpstr (one->str, ==, two->str);
221 g_string_free (one, TRUE);
222 g_string_free (two, TRUE);
223 g_object_unref (stream);
224 g_object_unref (data);
227 static GDataInputStream *data;
228 static GString *one, *two;
229 static GMainLoop *loop;
230 static const gchar *eol;
233 asynch_ready (GObject *object,
234 GAsyncResult *result,
237 GError *error = NULL;
241 g_assert (data == G_DATA_INPUT_STREAM (object));
243 str = g_data_input_stream_read_line_finish (data, result, &length, &error);
247 g_main_loop_quit (loop);
249 g_error_free (error);
253 g_assert (length == strlen (str));
254 g_string_append (two, str);
255 g_string_append (two, eol);
259 g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
267 SleepyStream *sleepy = sleepy_stream_new ();
269 data = g_data_input_stream_new (G_INPUT_STREAM (sleepy));
270 one = g_string_new (NULL);
271 two = g_string_new (NULL);
274 build_comparison (one, sleepy);
275 g_data_input_stream_read_line_async (data, 0, NULL, asynch_ready, NULL);
276 g_main_loop_run (loop = g_main_loop_new (NULL, FALSE));
278 g_assert_cmpstr (one->str, ==, two->str);
279 g_string_free (one, TRUE);
280 g_string_free (two, TRUE);
281 g_object_unref (sleepy);
282 g_object_unref (data);
286 main (int argc, char **argv)
288 g_test_init (&argc, &argv, NULL);
289 g_test_bug_base ("http://bugzilla.gnome.org/");
292 g_test_add_func ("/filter-stream/input", test);
293 g_test_add_func ("/filter-stream/async", asynch);