1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2009 Paolo Borelli
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General
16 * Public License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18 * Boston, MA 02111-1307, USA.
20 * Author: Paolo Borelli <pborelli@gnome.org>
23 #include <glib/glib.h>
28 do_test_read (const char *str, gssize expected_nread, glong expected_nchar)
38 base = g_memory_input_stream_new_from_data (str, -1, NULL);
39 in = g_utf8_input_stream_new (base);
40 g_object_unref (base);
42 buf = g_new0 (char, strlen(str));
44 n = g_input_stream_read (in, buf, len, NULL, &err);
45 g_assert_cmpint (n, ==, expected_nread);
46 if (expected_nread < 0)
48 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_INVALID_DATA);
52 g_assert_cmpstr (str, ==, buf);
53 g_assert_cmpint (g_utf8_strlen (buf, -1), ==, expected_nchar);
54 g_assert (err == NULL);
62 do_test_read_partial (const char *str,
64 gssize expected_nread1,
65 gssize expected_nread2,
76 base = g_memory_input_stream_new_from_data (str, -1, NULL);
77 in = g_utf8_input_stream_new (base);
78 g_object_unref (base);
80 buf = g_new0 (char, strlen(str));
82 n1 = g_input_stream_read (in, buf, chunk_len, NULL, &err);
83 g_assert_cmpint (n1, ==, expected_nread1);
84 g_assert (err == NULL);
86 n2 = g_input_stream_read (in, buf + n1, len - n1, NULL, &err);
87 g_assert_cmpint (n2, ==, expected_nread2);
88 if (expected_nread2 < 0)
90 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_INVALID_DATA);
94 g_assert_cmpstr (str, ==, buf);
95 g_assert_cmpint (g_utf8_strlen (buf, -1), ==, expected_nchar);
96 g_assert (err == NULL);
104 test_read_ascii (void)
106 do_test_read ("foobar", 6, 6);
110 test_read_utf8 (void)
112 do_test_read ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", 18, 15);
116 test_read_utf8_partial (void)
118 do_test_read_partial ("foobar\xc3\xa8\xc3\xa8\xc3\xa8zzzzzz", 7, 6, 12, 15);
122 test_read_invalid_start (void)
124 do_test_read ("\xef\xbf\xbezzzzzz", -1, -1);
128 test_read_invalid_middle (void)
130 do_test_read ("foobar\xef\xbf\xbezzzzzz", -1, -1);
134 test_read_invalid_end (void)
136 do_test_read ("foobar\xef\xbf\xbe", -1, -1);
140 test_read_invalid_partial (void)
142 do_test_read_partial ("foobar\xef\xbf\xbezzzzzz", 7, 6, -1, -1);
146 test_read_small_valid (void)
154 base = g_memory_input_stream_new_from_data ("\xc3\xa8\xc3\xa8", -1, NULL);
155 in = g_utf8_input_stream_new (base);
156 g_object_unref (base);
158 len = strlen("\xc3\xa8\xc3\xa8");
159 buf = g_new0 (char, len);
162 /* read a single byte */
163 n = g_input_stream_read (in, buf, 1, NULL, &err);
164 g_assert_cmpint (n, ==, 1);
165 g_assert_cmpstr ("\xc3", ==, buf);
166 g_assert (err == NULL);
169 n = g_input_stream_read (in, buf + n, len - n, NULL, &err);
170 g_assert_cmpint (n, ==, len - 1);
171 g_assert_cmpstr ("\xc3\xa8\xc3\xa8", ==, buf);
172 g_assert (err == NULL);
178 test_read_small_invalid (void)
186 base = g_memory_input_stream_new_from_data ("\xbf\xbe", -1, NULL);
187 in = g_utf8_input_stream_new (base);
188 g_object_unref (base);
190 buf = g_new0 (char, 2);
192 n = g_input_stream_read (in, buf, 1, NULL, &err);
193 g_assert_cmpint (n, ==, -1);
194 g_assert_error (err, G_IO_ERROR, G_IO_ERROR_INVALID_DATA);
200 test_read_small_consecutive (void)
208 base = g_memory_input_stream_new_from_data ("\xc3\xa8\xc3\xa8", -1, NULL);
209 in = g_utf8_input_stream_new (base);
210 g_object_unref (base);
212 len = strlen("\xc3\xa8\xc3\xa8");
213 buf = g_new0 (char, len);
217 /* read a single byte at a time */
222 r = g_input_stream_read (in, buf + n, 1, NULL, &err);
223 g_assert_cmpint (r, ==, 1);
224 g_assert (err == NULL);
229 g_assert_cmpstr ("\xc3\xa8\xc3\xa8", ==, buf);
239 g_test_init (&argc, &argv, NULL);
240 g_test_add_func ("/utf8-input-stream/read-ascii", test_read_ascii);
241 g_test_add_func ("/utf8-input-stream/read-utf8", test_read_utf8);
242 g_test_add_func ("/utf8-input-stream/read-utf8-partial", test_read_utf8_partial);
243 g_test_add_func ("/utf8-input-stream/read-invalid-start", test_read_invalid_start);
244 g_test_add_func ("/utf8-input-stream/read-invalid-middle", test_read_invalid_middle);
245 g_test_add_func ("/utf8-input-stream/read-invalid-end", test_read_invalid_end);
246 g_test_add_func ("/utf8-input-stream/read-invalid-partial", test_read_invalid_partial);
247 g_test_add_func ("/utf8-input-stream/read-small-valid", test_read_small_valid);
248 g_test_add_func ("/utf8-input-stream/read-small-invalid", test_read_small_invalid);
249 g_test_add_func ("/utf8-input-stream/read-small-consecutive", test_read_small_consecutive);