/* Add sample data */
for (i = 0; i < MAX_LINES; i++)
g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
- g_strconcat (lines[i], endl[newline_type], NULL), -1, NULL);
+ g_strconcat (lines[i], endl[newline_type], NULL), -1, g_free);
/* Seek to the start */
test_seek_to_start (base_stream);
if (data)
{
g_assert_cmpstr (data, ==, lines[line]);
+ g_free (data);
g_assert_no_error (error);
line++;
}
+ if (error)
+ g_error_free (error);
}
g_assert_cmpint (line, ==, MAX_LINES);
}
static void
+test_read_lines_LF_valid_utf8 (void)
+{
+ GInputStream *stream;
+ GInputStream *base_stream;
+ GError *error = NULL;
+ char *line;
+ guint n_lines = 0;
+
+ base_stream = g_memory_input_stream_new ();
+ stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
+
+ g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
+ "foo\nthis is valid UTF-8 ☺!\nbar\n", -1, NULL);
+
+ /* Test read line */
+ error = NULL;
+ while (TRUE)
+ {
+ gsize length = -1;
+ line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
+ g_assert_no_error (error);
+ if (line == NULL)
+ break;
+ n_lines++;
+ g_free (line);
+ }
+ g_assert_cmpint (n_lines, ==, 3);
+
+ g_object_unref (base_stream);
+ g_object_unref (stream);
+}
+
+static void
+test_read_lines_LF_invalid_utf8 (void)
+{
+ GInputStream *stream;
+ GInputStream *base_stream;
+ GError *error = NULL;
+ char *line;
+ guint n_lines = 0;
+
+ base_stream = g_memory_input_stream_new ();
+ stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
+
+ g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream),
+ "foo\nthis is not valid UTF-8 \xE5 =(\nbar\n", -1, NULL);
+
+ /* Test read line */
+ error = NULL;
+ while (TRUE)
+ {
+ gsize length = -1;
+ line = g_data_input_stream_read_line_utf8 (G_DATA_INPUT_STREAM (stream), &length, NULL, &error);
+ if (n_lines == 0)
+ g_assert_no_error (error);
+ else
+ {
+ g_assert (error != NULL);
+ g_clear_error (&error);
+ g_free (line);
+ break;
+ }
+ n_lines++;
+ g_free (line);
+ }
+ g_assert_cmpint (n_lines, ==, 1);
+
+ g_object_unref (base_stream);
+ g_object_unref (stream);
+}
+
+static void
test_read_until (void)
{
GInputStream *stream;
#define DATA_STRING " part1 # part2 $ part3 % part4 ^"
#define DATA_PART_LEN 7 /* number of characters between separators */
#define DATA_SEP "#$%^"
- const int DATA_PARTS_NUM = strlen (DATA_SEP) * REPEATS;
+#define DATA_SEP_LEN 4
+ const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
base_stream = g_memory_input_stream_new ();
stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
if (data)
{
g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
+ g_free (data);
g_assert_no_error (error);
line++;
}
}
g_assert_no_error (error);
g_assert_cmpint (line, ==, DATA_PARTS_NUM);
-
-
+
g_object_unref (base_stream);
g_object_unref (stream);
}
+static void
+test_read_upto (void)
+{
+ GInputStream *stream;
+ GInputStream *base_stream;
+ GError *error = NULL;
+ char *data;
+ int line;
+ int i;
+ guchar stop_char;
+
+#undef REPEATS
+#undef DATA_STRING
+#undef DATA_PART_LEN
+#undef DATA_SEP
+#undef DATA_SEP_LEN
+#define REPEATS 10 /* number of rounds */
+#define DATA_STRING " part1 # part2 $ part3 \0 part4 ^"
+#define DATA_PART_LEN 7 /* number of characters between separators */
+#define DATA_SEP "#$\0^"
+#define DATA_SEP_LEN 4
+ const int DATA_PARTS_NUM = DATA_SEP_LEN * REPEATS;
+
+ base_stream = g_memory_input_stream_new ();
+ stream = G_INPUT_STREAM (g_data_input_stream_new (base_stream));
+
+ for (i = 0; i < REPEATS; i++)
+ g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (base_stream), DATA_STRING, 32, NULL);
+
+ /* Test stop characters */
+ error = NULL;
+ data = (char*)1;
+ line = 0;
+ while (data)
+ {
+ gsize length = -1;
+ data = g_data_input_stream_read_upto (G_DATA_INPUT_STREAM (stream), DATA_SEP, DATA_SEP_LEN, &length, NULL, &error);
+ if (data)
+ {
+ g_assert_cmpint (strlen (data), ==, DATA_PART_LEN);
+ g_assert_no_error (error);
+ line++;
+
+ stop_char = g_data_input_stream_read_byte (G_DATA_INPUT_STREAM (stream), NULL, &error);
+ g_assert (memchr (DATA_SEP, stop_char, DATA_SEP_LEN) != NULL);
+ g_assert_no_error (error);
+ }
+ g_free (data);
+ }
+ g_assert_no_error (error);
+ g_assert_cmpint (line, ==, DATA_PARTS_NUM);
+
+ g_object_unref (base_stream);
+ g_object_unref (stream);
+}
enum TestDataType {
TEST_DATA_BYTE = 0,
TEST_DATA_INT16,
g_assert_not_reached ();
break;
}
- if ((data) && (! error))
+ if (!error)
g_assert_cmpint (data, ==, TEST_DATA_RETYPE_BUFF(data_type, gint64, ((guchar*)buffer + pos)));
pos += data_size;
}
if (pos < len + 1)
g_assert_no_error (error);
+ if (error)
+ g_error_free (error);
g_assert_cmpint (pos - data_size, ==, len);
}
main (int argc,
char *argv[])
{
- g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/data-input-stream/basic", test_basic);
g_test_add_func ("/data-input-stream/read-lines-LF", test_read_lines_LF);
+ g_test_add_func ("/data-input-stream/read-lines-LF-valid-utf8", test_read_lines_LF_valid_utf8);
+ g_test_add_func ("/data-input-stream/read-lines-LF-invalid-utf8", test_read_lines_LF_invalid_utf8);
g_test_add_func ("/data-input-stream/read-lines-CR", test_read_lines_CR);
g_test_add_func ("/data-input-stream/read-lines-CR-LF", test_read_lines_CR_LF);
g_test_add_func ("/data-input-stream/read-lines-any", test_read_lines_any);
g_test_add_func ("/data-input-stream/read-until", test_read_until);
+ g_test_add_func ("/data-input-stream/read-upto", test_read_upto);
g_test_add_func ("/data-input-stream/read-int", test_read_int);
return g_test_run();