buffered-output-stream.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
#include <gio/gio.h>

static void
test_write (void)
{
  GOutputStream *base;
  GOutputStream *out;
  GError *error;
  const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";

11
  base = g_memory_output_stream_new (g_malloc0 (20), 20, NULL, g_free);
12 13 14 15
  out = g_buffered_output_stream_new (base);

  g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 4096);
  g_assert (!g_buffered_output_stream_get_auto_grow (G_BUFFERED_OUTPUT_STREAM (out)));
16 17 18 19
  g_object_set (out, "auto-grow", TRUE, NULL);
  g_assert (g_buffered_output_stream_get_auto_grow (G_BUFFERED_OUTPUT_STREAM (out)));
  g_object_set (out, "auto-grow", FALSE, NULL);

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  g_buffered_output_stream_set_buffer_size (G_BUFFERED_OUTPUT_STREAM (out), 16);
  g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 16);

  error = NULL;
  g_assert_cmpint (g_output_stream_write (out, buffer, 10, NULL, &error), ==, 10);
  g_assert_no_error (error);

  g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);

  g_assert_cmpint (g_output_stream_write (out, buffer + 10, 10, NULL, &error), ==, 6);
  g_assert_no_error (error);

  g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);
  g_assert (g_output_stream_flush (out, NULL, &error));
  g_assert_no_error (error);
  g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 16);

  g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base)), ==, "abcdefghijklmnop");

  g_object_unref (out);
  g_object_unref (base);
}

static void
test_grow (void)
{
  GOutputStream *base;
  GOutputStream *out;
  GError *error;
  const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";
50 51
  gint size;
  gboolean grow;
52 53

  base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
54
  out = g_buffered_output_stream_new_sized (base, 16);
55 56 57

  g_buffered_output_stream_set_auto_grow (G_BUFFERED_OUTPUT_STREAM (out), TRUE);

58 59 60 61
  g_object_get (out, "buffer-size", &size, "auto-grow", &grow, NULL);
  g_assert_cmpint (size, ==, 16);
  g_assert (grow);

62 63
  g_assert (g_seekable_can_seek (G_SEEKABLE (out)));

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
  error = NULL;
  g_assert_cmpint (g_output_stream_write (out, buffer, 10, NULL, &error), ==, 10);
  g_assert_no_error (error);

  g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), ==, 16);
  g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);

  g_assert_cmpint (g_output_stream_write (out, buffer + 10, 10, NULL, &error), ==, 10);
  g_assert_no_error (error);

  g_assert_cmpint (g_buffered_output_stream_get_buffer_size (G_BUFFERED_OUTPUT_STREAM (out)), >=, 20);
  g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (base)), ==, 0);

  g_assert (g_output_stream_flush (out, NULL, &error));
  g_assert_no_error (error);

  g_assert_cmpstr (g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (base)), ==, "abcdefghijklmnopqrst");

  g_object_unref (out);
  g_object_unref (base);
}

Matthias Clasen's avatar
Matthias Clasen committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
static void
test_close (void)
{
  GOutputStream *base;
  GOutputStream *out;
  GError *error;

  base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
  out = g_buffered_output_stream_new (base);

  g_assert (g_filter_output_stream_get_close_base_stream (G_FILTER_OUTPUT_STREAM (out)));

  error = NULL;
  g_assert (g_output_stream_close (out, NULL, &error));
  g_assert_no_error (error);
  g_assert (g_output_stream_is_closed (base));

  g_object_unref (out);
  g_object_unref (base);

  base = g_memory_output_stream_new (g_malloc0 (30), 30, g_realloc, g_free);
  out = g_buffered_output_stream_new (base);

  g_filter_output_stream_set_close_base_stream (G_FILTER_OUTPUT_STREAM (out), FALSE);

  error = NULL;
  g_assert (g_output_stream_close (out, NULL, &error));
  g_assert_no_error (error);
  g_assert (!g_output_stream_is_closed (base));

  g_object_unref (out);
  g_object_unref (base);
}

120 121 122 123 124 125 126 127 128 129 130
static void
test_seek (void)
{
  GMemoryOutputStream *base;
  GOutputStream *out;
  GSeekable *seekable;
  GError *error;
  gsize bytes_written;
  gboolean ret;
  const gchar buffer[] = "abcdefghijklmnopqrstuvwxyz";

131
  base = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (g_malloc0 (30), 30, NULL, g_free));
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  out = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base), 8);
  seekable = G_SEEKABLE (out);
  error = NULL;

  /* Write data */
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 0);
  ret = g_output_stream_write_all (out, buffer, 4, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert_cmpint (bytes_written, ==, 4);
  g_assert (ret);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
  g_assert_cmpint (g_memory_output_stream_get_data_size (base), ==, 0);

  /* Forward relative seek */
  ret = g_seekable_seek (seekable, 2, G_SEEK_CUR, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
  g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
  g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
  ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (bytes_written, ==, 2);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);

  /* Backward relative seek */
  ret = g_seekable_seek (seekable, -4, G_SEEK_CUR, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
  g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
  g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
  ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (bytes_written, ==, 2);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);

  /* From start */
  ret = g_seekable_seek (seekable, 2, G_SEEK_SET, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 2);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
  g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
  g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
  ret = g_output_stream_write_all (out, buffer, 2, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (bytes_written, ==, 2);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 4);

  /* From end */
  ret = g_seekable_seek (seekable, 6 - 30, G_SEEK_END, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 6);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);
  ret = g_output_stream_write_all (out, buffer + 2, 2, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (bytes_written, ==, 2);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);

  /* Check flush */
  ret = g_output_stream_flush (out, NULL, &error);
  g_assert_no_error (error);
  g_assert (ret);
  g_assert_cmpint (g_seekable_tell (G_SEEKABLE (out)), ==, 8);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[0]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[1]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[2]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[3]);
  g_assert_cmpint ('a', ==, ((gchar *)g_memory_output_stream_get_data (base))[4]);
  g_assert_cmpint ('b', ==, ((gchar *)g_memory_output_stream_get_data (base))[5]);
  g_assert_cmpint ('c', ==, ((gchar *)g_memory_output_stream_get_data (base))[6]);
  g_assert_cmpint ('d', ==, ((gchar *)g_memory_output_stream_get_data (base))[7]);

  g_object_unref (out);
  g_object_unref (base);
}

static void
test_truncate(void)
{
  GMemoryOutputStream *base_stream;
  GOutputStream *stream;
  GSeekable *seekable;
  GError *error;
  gsize bytes_written;
  guchar *stream_data;
  gsize len;
  gboolean res;

  len = 8;

  /* Create objects */
  stream_data = g_malloc0 (len);
  base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, g_realloc, g_free));
  stream = g_buffered_output_stream_new_sized (G_OUTPUT_STREAM (base_stream), 8);
  seekable = G_SEEKABLE (stream);

  g_assert (g_seekable_can_truncate (seekable));

  /* Write */
  g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
  g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 0);

  error = NULL;
  res = g_output_stream_write_all (stream, "ab", 2, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert (res);
  res = g_output_stream_write_all (stream, "cd", 2, &bytes_written, NULL, &error);
  g_assert_no_error (error);
  g_assert (res);

  res = g_output_stream_flush (stream, NULL, &error);
  g_assert_no_error (error);
  g_assert (res);

  g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
  g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
273
  stream_data = g_memory_output_stream_get_data (base_stream);
274 275 276 277 278 279 280 281 282 283 284
  g_assert_cmpint (stream_data[0], ==, 'a');
  g_assert_cmpint (stream_data[1], ==, 'b');
  g_assert_cmpint (stream_data[2], ==, 'c');
  g_assert_cmpint (stream_data[3], ==, 'd');

  /* Truncate at position */
  res = g_seekable_truncate (seekable, 4, NULL, &error);
  g_assert_no_error (error);
  g_assert (res);
  g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 4);
  g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
285
  stream_data = g_memory_output_stream_get_data (base_stream);
286 287 288 289 290 291 292 293 294 295
  g_assert_cmpint (stream_data[0], ==, 'a');
  g_assert_cmpint (stream_data[1], ==, 'b');
  g_assert_cmpint (stream_data[2], ==, 'c');
  g_assert_cmpint (stream_data[3], ==, 'd');

  /* Truncate beyond position */
  res = g_seekable_truncate (seekable, 6, NULL, &error);
  g_assert_no_error (error);
  g_assert (res);
  g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 6);
296
  g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 6);
297
  stream_data = g_memory_output_stream_get_data (base_stream);
298 299 300 301 302 303 304 305 306 307 308
  g_assert_cmpint (stream_data[0], ==, 'a');
  g_assert_cmpint (stream_data[1], ==, 'b');
  g_assert_cmpint (stream_data[2], ==, 'c');
  g_assert_cmpint (stream_data[3], ==, 'd');

  /* Truncate before position */
  res = g_seekable_truncate (seekable, 2, NULL, &error);
  g_assert_no_error (error);
  g_assert (res);
  g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 2);
  g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 2);
309
  stream_data = g_memory_output_stream_get_data (base_stream);
310 311 312 313 314 315 316
  g_assert_cmpint (stream_data[0], ==, 'a');
  g_assert_cmpint (stream_data[1], ==, 'b');

  g_object_unref (stream);
  g_object_unref (base_stream);
}

317 318 319 320 321 322 323
int
main (int argc, char *argv[])
{
  g_test_init (&argc, &argv, NULL);

  g_test_add_func ("/buffered-output-stream/write", test_write);
  g_test_add_func ("/buffered-output-stream/grow", test_grow);
324 325
  g_test_add_func ("/buffered-output-stream/seek", test_seek);
  g_test_add_func ("/buffered-output-stream/truncate", test_truncate);
Matthias Clasen's avatar
Matthias Clasen committed
326
  g_test_add_func ("/filter-output-stream/close", test_close);
327 328 329

  return g_test_run ();
}