1 /* GLib testing framework examples and tests
2 * Copyright (C) 2007 Imendio AB
5 * This work is provided "as is"; redistribution and modification
6 * in whole or in part, in any medium, physical or electronic is
7 * permitted without restriction.
9 * This work 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.
13 * In no event shall the authors or contributors be liable for any
14 * direct, indirect, incidental, special, exemplary, or consequential
15 * damages (including, but not limited to, procurement of substitute
16 * goods or services; loss of use, data, or profits; or business
17 * interruption) however caused and on any theory of liability, whether
18 * in contract, strict liability, or tort (including negligence or
19 * otherwise) arising in any way out of the use of this software, even
20 * if advised of the possibility of such damage.
23 #include <glib/glib.h>
29 test_read_chunks (void)
31 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
32 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
33 const char *result = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
35 gsize bytes_read, pos, len, chunk_size;
40 stream = g_memory_input_stream_new ();
42 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
44 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
46 len = strlen (data1) + strlen (data2);
48 for (chunk_size = 1; chunk_size < len - 1; chunk_size++)
53 bytes_read = g_input_stream_read (stream, buffer, chunk_size, NULL, &error);
54 g_assert_no_error (error);
55 g_assert_cmpint (bytes_read, ==, MIN (chunk_size, len - pos));
56 g_assert (strncmp (buffer, result + pos, bytes_read) == 0);
61 g_assert_cmpint (pos, ==, len);
62 res = g_seekable_seek (G_SEEKABLE (stream), 0, G_SEEK_SET, NULL, &error);
63 g_assert_cmpint (res, ==, TRUE);
64 g_assert_no_error (error);
67 g_object_unref (stream);
73 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
74 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
79 stream = g_memory_input_stream_new ();
81 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
83 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
86 g_assert (G_IS_SEEKABLE (stream));
87 g_assert (g_seekable_can_seek (G_SEEKABLE (stream)));
90 g_assert (g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_SET, NULL, &error));
91 g_assert_no_error (error);
92 g_assert_cmpint (g_seekable_tell (G_SEEKABLE (stream)), ==, 26);
94 g_assert (g_input_stream_read (stream, buffer, 1, NULL, &error) == 1);
95 g_assert_no_error (error);
97 g_assert (buffer[0] == 'A');
99 g_assert (!g_seekable_seek (G_SEEKABLE (stream), 26, G_SEEK_CUR, NULL, &error));
100 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
101 g_error_free (error);
103 g_object_unref (stream);
109 const char *data1 = "abcdefghijklmnopqrstuvwxyz";
110 const char *data2 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
111 GInputStream *stream;
114 stream = g_memory_input_stream_new ();
116 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
118 g_memory_input_stream_add_data (G_MEMORY_INPUT_STREAM (stream),
121 g_assert (G_IS_SEEKABLE (stream));
122 g_assert (!g_seekable_can_truncate (G_SEEKABLE (stream)));
125 g_assert (!g_seekable_truncate (G_SEEKABLE (stream), 26, NULL, &error));
126 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
127 g_error_free (error);
129 g_object_unref (stream);
137 g_test_init (&argc, &argv, NULL);
139 g_test_add_func ("/memory-input-stream/read-chunks", test_read_chunks);
140 g_test_add_func ("/memory-input-stream/seek", test_seek);
141 g_test_add_func ("/memory-input-stream/truncate", test_truncate);