LCOV - code coverage report
Current view: top level - glib/gio/tests - memory-output-stream.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 244 244 100.0 %
Date: 2024-04-23 05:16:05 Functions: 12 12 100.0 %
Branches: 14 14 100.0 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  * Copyright (C) 2008 Red Hat, Inc.
       3                 :            :  * Author: Matthias Clasen
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       6                 :            :  *
       7                 :            :  * This work is provided "as is"; redistribution and modification
       8                 :            :  * in whole or in part, in any medium, physical or electronic is
       9                 :            :  * permitted without restriction.
      10                 :            :  *
      11                 :            :  * This work is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      14                 :            :  *
      15                 :            :  * In no event shall the authors or contributors be liable for any
      16                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      17                 :            :  * damages (including, but not limited to, procurement of substitute
      18                 :            :  * goods or services; loss of use, data, or profits; or business
      19                 :            :  * interruption) however caused and on any theory of liability, whether
      20                 :            :  * in contract, strict liability, or tort (including negligence or
      21                 :            :  * otherwise) arising in any way out of the use of this software, even
      22                 :            :  * if advised of the possibility of such damage.
      23                 :            :  */
      24                 :            : #include <glib/glib.h>
      25                 :            : #include <gio/gio.h>
      26                 :            : #include <stdlib.h>
      27                 :            : #include <string.h>
      28                 :            : 
      29                 :            : static void
      30                 :          1 : test_truncate (void)
      31                 :            : {
      32                 :            :   GOutputStream *mo;
      33                 :            :   GDataOutputStream *o;
      34                 :            :   int i;
      35                 :          1 :   GError *error = NULL;
      36                 :            :   guint8 *data;
      37                 :            : 
      38                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540423");
      39                 :            : 
      40                 :          1 :   mo = g_memory_output_stream_new_resizable ();
      41                 :          1 :   g_assert (g_seekable_can_truncate (G_SEEKABLE (mo)));
      42                 :          1 :   o = g_data_output_stream_new (mo);
      43         [ +  + ]:       1001 :   for (i = 0; i < 1000; i++)
      44                 :            :     {
      45                 :       1000 :       g_data_output_stream_put_byte (o, 1, NULL, &error);
      46                 :       1000 :       g_assert_no_error (error);
      47                 :            :     }
      48                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 1000);
      49                 :          1 :   g_seekable_truncate (G_SEEKABLE (mo), 0, NULL, &error);
      50                 :          1 :   g_assert_cmpuint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1000);
      51                 :            :   
      52                 :          1 :   g_assert_no_error (error);
      53                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
      54         [ +  + ]:       2001 :   for (i = 0; i < 2000; i++)
      55                 :            :     {
      56                 :       2000 :       g_data_output_stream_put_byte (o, 2, NULL, &error);
      57                 :       2000 :       g_assert_no_error (error);
      58                 :            :     }
      59                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 3000);
      60                 :            : 
      61                 :          1 :   data = (guint8 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
      62                 :            : 
      63                 :            :   /* The 1's written initially were lost when we truncated to 0
      64                 :            :    * and then started writing at position 1000.
      65                 :            :    */
      66         [ +  + ]:       1001 :   for (i = 0; i < 1000; i++)
      67                 :       1000 :     g_assert_cmpuint (data[i], ==, 0);
      68         [ +  + ]:       2001 :   for (i = 1000; i < 3000; i++)
      69                 :       2000 :     g_assert_cmpuint (data[i], ==, 2);
      70                 :            : 
      71                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=720080");
      72                 :            : 
      73                 :          1 :   g_seekable_truncate (G_SEEKABLE (mo), 8192, NULL, &error);
      74                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 8192);
      75                 :            : 
      76                 :          1 :   data = (guint8 *)g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
      77         [ +  + ]:       5193 :   for (i = 3000; i < 8192; i++)
      78                 :       5192 :     g_assert_cmpuint (data[i], ==, 0);
      79                 :            : 
      80                 :          1 :   g_object_unref (o);
      81                 :          1 :   g_object_unref (mo);
      82                 :          1 : }
      83                 :            : 
      84                 :            : static void
      85                 :          1 : test_seek_fixed (void)
      86                 :            : {
      87                 :            :   GOutputStream *mo;
      88                 :            :   GError *error;
      89                 :            : 
      90                 :          1 :   mo = g_memory_output_stream_new (g_new (gchar, 100), 100, NULL, g_free);
      91                 :            : 
      92                 :          1 :   g_assert (G_IS_SEEKABLE (mo));
      93                 :          1 :   g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
      94                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
      95                 :            : 
      96                 :          1 :   error = NULL;
      97                 :          1 :   g_assert (!g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
      98                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
      99                 :          1 :   g_clear_error (&error);
     100                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     101                 :            : 
     102                 :          1 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
     103                 :          1 :   g_assert_no_error (error);
     104                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
     105                 :            : 
     106                 :          1 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
     107                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     108                 :          1 :   g_assert_no_error (error);
     109                 :            : 
     110                 :          1 :   g_assert (!g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
     111                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     112                 :          1 :   g_clear_error (&error);
     113                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     114                 :            : 
     115                 :          1 :   g_assert (!g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
     116                 :          1 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     117                 :          1 :   g_clear_error (&error);
     118                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     119                 :            : 
     120                 :          1 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
     121                 :          1 :   g_assert_no_error (error);
     122                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 100);
     123                 :            : 
     124                 :          1 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
     125                 :          1 :   g_assert_no_error (error);
     126                 :          1 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 99);
     127                 :            : 
     128                 :          1 :   g_object_unref (mo);
     129                 :          1 : }
     130                 :            : 
     131                 :            : static void
     132                 :       1024 : test_seek_resizable_stream (GOutputStream *mo)
     133                 :            : {
     134                 :            :   GError *error;
     135                 :            : 
     136                 :       1024 :   g_assert (G_IS_SEEKABLE (mo));
     137                 :       1024 :   g_assert (g_seekable_can_seek (G_SEEKABLE (mo)));
     138                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     139                 :            : 
     140                 :       1024 :   error = NULL;
     141                 :       1024 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 222, G_SEEK_CUR, NULL, &error));
     142                 :       1024 :   g_assert_no_error (error);
     143                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 222);
     144                 :            : 
     145                 :       1024 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 26, G_SEEK_SET, NULL, &error));
     146                 :       1024 :   g_assert_no_error (error);
     147                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 26);
     148                 :            : 
     149                 :       1024 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 20, G_SEEK_CUR, NULL, &error));
     150                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 46);
     151                 :       1024 :   g_assert_no_error (error);
     152                 :            : 
     153                 :       1024 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 200, G_SEEK_CUR, NULL, &error));
     154                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 246);
     155                 :       1024 :   g_assert_no_error (error);
     156                 :            : 
     157                 :       1024 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 1, G_SEEK_END, NULL, &error));
     158                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 1);
     159                 :       1024 :   g_assert_no_error (error);
     160                 :            : 
     161                 :       1024 :   g_assert (g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_END, NULL, &error));
     162                 :       1024 :   g_assert_no_error (error);
     163                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     164                 :            : 
     165                 :            :   /* The 'end' is still zero, so this should fail */
     166                 :       1024 :   g_assert (!g_seekable_seek (G_SEEKABLE (mo), -1, G_SEEK_END, NULL, &error));
     167                 :       1024 :   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
     168                 :       1024 :   g_clear_error (&error);
     169                 :       1024 :   g_assert_cmpint (g_seekable_tell (G_SEEKABLE (mo)), ==, 0);
     170                 :       1024 : }
     171                 :            : 
     172                 :            : static void
     173                 :          1 : test_seek_resizable (void)
     174                 :            : {
     175                 :            :   GOutputStream *mo;
     176                 :            :   gint i;
     177                 :            : 
     178                 :            :   /* For resizable streams, the initially allocated size is purely an
     179                 :            :    * implementation detail.  We should not be able to tell the
     180                 :            :    * difference based on the seek API, so make a bunch of streams with
     181                 :            :    * different sizes and subject them to the same test.
     182                 :            :    */
     183         [ +  + ]:       1025 :   for (i = 0; i < 1024; i++)
     184                 :            :     {
     185                 :       1024 :       mo = g_memory_output_stream_new (g_malloc (i), i, g_realloc, g_free);
     186                 :            : 
     187                 :       1024 :       test_seek_resizable_stream (mo);
     188                 :            : 
     189                 :       1024 :       g_assert_cmpint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 0);
     190                 :            :       /* No writes = no resizes */
     191                 :       1024 :       g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, i);
     192                 :            : 
     193                 :       1024 :       g_object_unref (mo);
     194                 :            :     }
     195                 :          1 : }
     196                 :            : 
     197                 :            : static void
     198                 :          1 : test_data_size (void)
     199                 :            : {
     200                 :            :   GOutputStream *mo;
     201                 :            :   GDataOutputStream *o;
     202                 :            :   int pos;
     203                 :            : 
     204                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540459");
     205                 :            : 
     206                 :          1 :   mo = g_memory_output_stream_new_resizable ();
     207                 :          1 :   o = g_data_output_stream_new (mo);
     208                 :          1 :   g_data_output_stream_put_byte (o, 1, NULL, NULL);
     209                 :          1 :   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
     210                 :          1 :   g_assert_cmpint (pos, ==, 1);
     211                 :            : 
     212                 :          1 :   g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_CUR, NULL, NULL);
     213                 :          1 :   pos = g_seekable_tell (G_SEEKABLE (mo));
     214                 :          1 :   g_assert_cmpint (pos, ==, 1);
     215                 :            : 
     216                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=540461");
     217                 :            :   
     218                 :          1 :   g_seekable_seek (G_SEEKABLE (mo), 0, G_SEEK_SET, NULL, NULL);
     219                 :          1 :   pos = g_seekable_tell (G_SEEKABLE (mo));
     220                 :          1 :   g_assert_cmpint (pos, ==, 0);
     221                 :            :   
     222                 :          1 :   pos = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
     223                 :          1 :   g_assert_cmpint (pos, ==, 1);
     224                 :            : 
     225                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, 16);
     226                 :            : 
     227                 :          1 :   g_object_unref (o);
     228                 :          1 :   g_object_unref (mo);
     229                 :          1 : }
     230                 :            : 
     231                 :            : static void
     232                 :          1 : test_properties (void)
     233                 :            : {
     234                 :            :   GOutputStream *mo;
     235                 :            :   GDataOutputStream *o;
     236                 :            :   int i;
     237                 :          1 :   GError *error = NULL;
     238                 :            :   gsize data_size_fun;
     239                 :          1 :   gsize data_size_prop = 0;
     240                 :            :   gpointer data_fun;
     241                 :            :   gpointer data_prop;
     242                 :            :   gpointer func;
     243                 :            : 
     244                 :          1 :   g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=605733");
     245                 :            : 
     246                 :          1 :   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     247                 :            :                                       "realloc-function", g_realloc,
     248                 :            :                                       "destroy-function", g_free,
     249                 :            :                                       NULL);
     250                 :          1 :   o = g_data_output_stream_new (mo);
     251                 :            : 
     252         [ +  + ]:       1001 :   for (i = 0; i < 1000; i++)
     253                 :            :     {
     254                 :       1000 :       g_data_output_stream_put_byte (o, 1, NULL, &error);
     255                 :       1000 :       g_assert_no_error (error);
     256                 :            :     }
     257                 :            : 
     258                 :          1 :   data_size_fun = g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo));
     259                 :          1 :   g_object_get (mo, "data-size", &data_size_prop, NULL);
     260                 :          1 :   g_assert_cmpint (data_size_fun, ==, data_size_prop);
     261                 :            : 
     262                 :          1 :   data_fun = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
     263                 :          1 :   g_object_get (mo, "data", &data_prop, NULL);
     264                 :          1 :   g_assert_cmphex (GPOINTER_TO_SIZE (data_fun), ==, GPOINTER_TO_SIZE (data_prop));
     265                 :            : 
     266                 :          1 :   g_object_get (mo, "realloc-function", &func, NULL);
     267                 :          1 :   g_assert (func == g_realloc);
     268                 :          1 :   g_object_get (mo, "destroy-function", &func, NULL);
     269                 :          1 :   g_assert (func == g_free);
     270                 :            : 
     271                 :          1 :   data_size_fun = g_memory_output_stream_get_size (G_MEMORY_OUTPUT_STREAM (mo));
     272                 :          1 :   g_object_get (mo, "size", &data_size_prop, NULL);
     273                 :          1 :   g_assert_cmpint (data_size_fun, ==, data_size_prop);
     274                 :            : 
     275                 :          1 :   g_object_unref (o);
     276                 :          1 :   g_object_unref (mo);
     277                 :          1 : }
     278                 :            : 
     279                 :            : static void
     280                 :          1 : test_write_bytes (void)
     281                 :            : {
     282                 :            :   GOutputStream *mo;
     283                 :            :   GBytes *bytes, *bytes2;
     284                 :          1 :   GError *error = NULL;
     285                 :            : 
     286                 :          1 :   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     287                 :            :                                       "realloc-function", g_realloc,
     288                 :            :                                       "destroy-function", g_free,
     289                 :            :                                       NULL);
     290                 :          1 :   bytes = g_bytes_new_static ("hello world!", strlen ("hello world!") + 1);
     291                 :          1 :   g_output_stream_write_bytes (mo, bytes, NULL, &error);
     292                 :          1 :   g_assert_no_error (error);
     293                 :            : 
     294                 :          1 :   g_output_stream_close (mo, NULL, &error);
     295                 :          1 :   g_assert_no_error (error);
     296                 :            : 
     297                 :          1 :   bytes2 = g_memory_output_stream_steal_as_bytes (G_MEMORY_OUTPUT_STREAM (mo));
     298                 :          1 :   g_object_unref (mo);
     299                 :          1 :   g_assert (g_bytes_equal (bytes, bytes2));
     300                 :            : 
     301                 :          1 :   g_bytes_unref (bytes);
     302                 :          1 :   g_bytes_unref (bytes2);
     303                 :          1 : }
     304                 :            : 
     305                 :            : static void
     306                 :          1 : test_write_null (void)
     307                 :            : {
     308                 :            :   GOutputStream *mo;
     309                 :          1 :   GError *error = NULL;
     310                 :            : 
     311                 :          1 :   g_test_bug ("https://gitlab.gnome.org/GNOME/glib/-/issues/2471");
     312                 :            : 
     313                 :          1 :   mo = g_memory_output_stream_new_resizable ();
     314                 :          1 :   g_output_stream_write_all (mo, NULL, 0, NULL, NULL, &error);
     315                 :          1 :   g_assert_no_error (error);
     316                 :            : 
     317                 :          1 :   g_assert_cmpint (0, ==, g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)));
     318                 :            : 
     319                 :          1 :   g_output_stream_close (mo, NULL, &error);
     320                 :          1 :   g_assert_no_error (error);
     321                 :          1 :   g_object_unref (mo);
     322                 :          1 : }
     323                 :            : 
     324                 :            : /* Test that writev() works on #GMemoryOutputStream with a non-empty set of vectors. This
     325                 :            :  * covers the default writev() implementation around write(). */
     326                 :            : static void
     327                 :          1 : test_writev (void)
     328                 :            : {
     329                 :            :   GOutputStream *mo;
     330                 :          1 :   GError *error = NULL;
     331                 :            :   gboolean res;
     332                 :            :   gsize bytes_written;
     333                 :            :   GOutputVector vectors[3];
     334                 :          1 :   const guint8 buffer[] = {1, 2, 3, 4, 5,
     335                 :            :                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
     336                 :            :                            1, 2, 3};
     337                 :            :   guint8 *output_buffer;
     338                 :            : 
     339                 :          1 :   vectors[0].buffer = buffer;
     340                 :          1 :   vectors[0].size = 5;
     341                 :            : 
     342                 :          1 :   vectors[1].buffer = buffer + vectors[0].size;
     343                 :          1 :   vectors[1].size = 12;
     344                 :            : 
     345                 :          1 :   vectors[2].buffer = buffer + vectors[0].size + vectors[1].size;
     346                 :          1 :   vectors[2].size = 3;
     347                 :            : 
     348                 :          1 :   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     349                 :            :                                       "realloc-function", g_realloc,
     350                 :            :                                       "destroy-function", g_free,
     351                 :            :                                       NULL);
     352                 :          1 :   res = g_output_stream_writev_all (mo, vectors, G_N_ELEMENTS (vectors), &bytes_written, NULL, &error);
     353                 :          1 :   g_assert_no_error (error);
     354                 :          1 :   g_assert_true (res);
     355                 :          1 :   g_assert_cmpuint (bytes_written, ==, sizeof buffer);
     356                 :            : 
     357                 :          1 :   g_output_stream_close (mo, NULL, &error);
     358                 :          1 :   g_assert_no_error (error);
     359                 :            : 
     360                 :          1 :   g_assert_cmpuint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, sizeof buffer);
     361                 :          1 :   output_buffer = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
     362                 :          1 :   g_assert_cmpmem (output_buffer, sizeof buffer, buffer, sizeof buffer);
     363                 :            : 
     364                 :          1 :   g_object_unref (mo);
     365                 :          1 : }
     366                 :            : 
     367                 :            : /* Test that writev_nonblocking() works on #GMemoryOutputStream with a non-empty set of vectors. This
     368                 :            :  * covers the default writev_nonblocking() implementation around write_nonblocking(). */
     369                 :            : static void
     370                 :          1 : test_writev_nonblocking (void)
     371                 :            : {
     372                 :            :   GOutputStream *mo;
     373                 :          1 :   GError *error = NULL;
     374                 :            :   gboolean res;
     375                 :            :   gsize bytes_written;
     376                 :            :   GOutputVector vectors[3];
     377                 :          1 :   const guint8 buffer[] = {1, 2, 3, 4, 5,
     378                 :            :                            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
     379                 :            :                            1, 2, 3};
     380                 :            :   guint8 *output_buffer;
     381                 :            : 
     382                 :          1 :   vectors[0].buffer = buffer;
     383                 :          1 :   vectors[0].size = 5;
     384                 :            : 
     385                 :          1 :   vectors[1].buffer = buffer + vectors[0].size;
     386                 :          1 :   vectors[1].size = 12;
     387                 :            : 
     388                 :          1 :   vectors[2].buffer = buffer + vectors[0].size + vectors[1].size;
     389                 :          1 :   vectors[2].size = 3;
     390                 :            : 
     391                 :          1 :   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     392                 :            :                                       "realloc-function", g_realloc,
     393                 :            :                                       "destroy-function", g_free,
     394                 :            :                                       NULL);
     395                 :          1 :   res = g_pollable_output_stream_writev_nonblocking (G_POLLABLE_OUTPUT_STREAM (mo),
     396                 :            :                                                      vectors, G_N_ELEMENTS (vectors),
     397                 :            :                                                      &bytes_written, NULL, &error);
     398                 :          1 :   g_assert_no_error (error);
     399                 :          1 :   g_assert_cmpint (res, ==, G_POLLABLE_RETURN_OK);
     400                 :          1 :   g_assert_cmpuint (bytes_written, ==, sizeof buffer);
     401                 :            : 
     402                 :          1 :   g_output_stream_close (mo, NULL, &error);
     403                 :          1 :   g_assert_no_error (error);
     404                 :            : 
     405                 :          1 :   g_assert_cmpuint (g_memory_output_stream_get_data_size (G_MEMORY_OUTPUT_STREAM (mo)), ==, sizeof buffer);
     406                 :          1 :   output_buffer = g_memory_output_stream_get_data (G_MEMORY_OUTPUT_STREAM (mo));
     407                 :          1 :   g_assert_cmpmem (output_buffer, sizeof buffer, buffer, sizeof buffer);
     408                 :            : 
     409                 :          1 :   g_object_unref (mo);
     410                 :          1 : }
     411                 :            : 
     412                 :            : static void
     413                 :          1 : test_steal_as_bytes (void)
     414                 :            : {
     415                 :            :   GOutputStream *mo;
     416                 :            :   GDataOutputStream *o;
     417                 :          1 :   GError *error = NULL;
     418                 :            :   GBytes *bytes;
     419                 :            :   gsize size;
     420                 :            : 
     421                 :          1 :   mo = (GOutputStream*) g_object_new (G_TYPE_MEMORY_OUTPUT_STREAM,
     422                 :            :                                       "realloc-function", g_realloc,
     423                 :            :                                       "destroy-function", g_free,
     424                 :            :                                       NULL);
     425                 :          1 :   o = g_data_output_stream_new (mo);
     426                 :            : 
     427                 :          1 :   g_data_output_stream_put_string (o, "hello ", NULL, &error);
     428                 :          1 :   g_assert_no_error (error);
     429                 :            : 
     430                 :          1 :   g_data_output_stream_put_string (o, "world!", NULL, &error);
     431                 :          1 :   g_assert_no_error (error);
     432                 :            : 
     433                 :          1 :   g_data_output_stream_put_byte (o, '\0', NULL, &error);
     434                 :          1 :   g_assert_no_error (error);
     435                 :            : 
     436                 :          1 :   g_output_stream_close ((GOutputStream*) o, NULL, &error);
     437                 :          1 :   g_assert_no_error (error);
     438                 :            : 
     439                 :          1 :   bytes = g_memory_output_stream_steal_as_bytes ((GMemoryOutputStream*)mo);
     440                 :          1 :   g_object_unref (mo);
     441                 :            : 
     442                 :          1 :   g_assert_cmpint (g_bytes_get_size (bytes), ==, strlen ("hello world!") + 1);
     443                 :          1 :   g_assert_cmpstr (g_bytes_get_data (bytes, &size), ==, "hello world!");
     444                 :            : 
     445                 :          1 :   g_bytes_unref (bytes);
     446                 :          1 :   g_object_unref (o);
     447                 :          1 : }
     448                 :            : 
     449                 :            : int
     450                 :          1 : main (int   argc,
     451                 :            :       char *argv[])
     452                 :            : {
     453                 :          1 :   g_test_init (&argc, &argv, NULL);
     454                 :            : 
     455                 :          1 :   g_test_add_func ("/memory-output-stream/truncate", test_truncate);
     456                 :          1 :   g_test_add_func ("/memory-output-stream/seek/fixed", test_seek_fixed);
     457                 :          1 :   g_test_add_func ("/memory-output-stream/seek/resizable", test_seek_resizable);
     458                 :          1 :   g_test_add_func ("/memory-output-stream/get-data-size", test_data_size);
     459                 :          1 :   g_test_add_func ("/memory-output-stream/properties", test_properties);
     460                 :          1 :   g_test_add_func ("/memory-output-stream/write-bytes", test_write_bytes);
     461                 :          1 :   g_test_add_func ("/memory-output-stream/write-null", test_write_null);
     462                 :          1 :   g_test_add_func ("/memory-output-stream/writev", test_writev);
     463                 :          1 :   g_test_add_func ("/memory-output-stream/writev_nonblocking", test_writev_nonblocking);
     464                 :          1 :   g_test_add_func ("/memory-output-stream/steal_as_bytes", test_steal_as_bytes);
     465                 :            : 
     466                 :          1 :   return g_test_run();
     467                 :            : }

Generated by: LCOV version 1.14