LCOV - code coverage report
Current view: top level - glib/gio/tests - data-output-stream.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 301 304 99.0 %
Date: 2024-04-23 05:16:05 Functions: 10 10 100.0 %
Branches: 48 51 94.1 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  * Copyright (C) 2008 Red Hat, Inc.
       3                 :            :  * Authors: Tomas Bzatek <tbzatek@redhat.com>
       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                 :            : 
      25                 :            : #include <glib/glib.h>
      26                 :            : #include <gio/gio.h>
      27                 :            : #include <stdlib.h>
      28                 :            : #include <string.h>
      29                 :            : 
      30                 :            : #define MAX_LINES               0xFFF   
      31                 :            : #define MAX_LINES_BUFF          0xFFFFFF
      32                 :            : #define MAX_BYTES_BINARY        0x100   
      33                 :            : 
      34                 :            : static void
      35                 :          1 : test_basic (void)
      36                 :            : {
      37                 :            :   GOutputStream *stream;
      38                 :            :   GOutputStream *base_stream;
      39                 :            :   gpointer data;
      40                 :            :   gint val;
      41                 :            : 
      42                 :          1 :   data = g_malloc0 (MAX_LINES_BUFF);
      43                 :            :   
      44                 :            :   /* initialize objects */
      45                 :          1 :   base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
      46                 :          1 :   stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
      47                 :            : 
      48                 :          1 :   g_object_get (stream, "byte-order", &val, NULL);
      49                 :          1 :   g_assert_cmpint (val, ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
      50                 :          1 :   g_object_set (stream, "byte-order", G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN, NULL);
      51                 :          1 :   g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
      52                 :            : 
      53                 :          1 :   g_object_unref (stream);
      54                 :          1 :   g_object_unref (base_stream);
      55                 :          1 :   g_free (data);
      56                 :          1 : }
      57                 :            : 
      58                 :            : static void
      59                 :          3 : test_read_lines (GDataStreamNewlineType newline_type)
      60                 :            : {
      61                 :            :   GOutputStream *stream;
      62                 :            :   GOutputStream *base_stream;
      63                 :          3 :   GError *error = NULL;
      64                 :            :   gpointer data;
      65                 :            :   char *lines;
      66                 :            :   int size;
      67                 :            :   int i;
      68                 :            : 
      69                 :            : #define TEST_STRING     "some_text"
      70                 :            :   
      71                 :          3 :   const char* endl[4] = {"\n", "\r", "\r\n", "\n"};
      72                 :            :   
      73                 :            :   
      74                 :          3 :   data = g_malloc0 (MAX_LINES_BUFF);
      75                 :          3 :   lines = g_malloc0 ((strlen (TEST_STRING) + strlen (endl[newline_type])) * MAX_LINES + 1);
      76                 :            :   
      77                 :            :   /* initialize objects */
      78                 :          3 :   base_stream = g_memory_output_stream_new (data, MAX_LINES_BUFF, NULL, NULL);
      79                 :          3 :   stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
      80                 :            : 
      81                 :            :   
      82                 :            :   /*  fill data */
      83         [ +  + ]:      12288 :   for (i = 0; i < MAX_LINES; i++)
      84                 :            :     {
      85                 :            :       gboolean res;
      86                 :      12285 :       char *s = g_strconcat (TEST_STRING, endl[newline_type], NULL);
      87                 :      12285 :       res = g_data_output_stream_put_string (G_DATA_OUTPUT_STREAM (stream), s, NULL, &error);
      88                 :      12285 :       g_stpcpy ((char*)(lines + i*strlen(s)), s);
      89                 :      12285 :       g_assert_no_error (error);
      90                 :      12285 :       g_assert (res == TRUE);
      91                 :      12285 :       g_free (s);
      92                 :            :     }
      93                 :            : 
      94                 :            :   /*  Byte order testing */
      95                 :          3 :   g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
      96                 :          3 :   g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
      97                 :          3 :   g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
      98                 :          3 :   g_assert_cmpint (g_data_output_stream_get_byte_order (G_DATA_OUTPUT_STREAM (stream)), ==, G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN);
      99                 :            :   
     100                 :            :   /*  compare data */
     101                 :          3 :   size = strlen (data);
     102                 :          3 :   g_assert_cmpint (size, <, MAX_LINES_BUFF);
     103                 :          3 :   g_assert_cmpstr ((char*)data, ==, lines);
     104                 :            :   
     105                 :          3 :   g_object_unref (base_stream);
     106                 :          3 :   g_object_unref (stream);
     107                 :          3 :   g_free (data);
     108                 :          3 :   g_free (lines);
     109                 :          3 : }
     110                 :            : 
     111                 :            : static void
     112                 :          1 : test_read_lines_LF (void)
     113                 :            : {
     114                 :          1 :   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_LF);
     115                 :          1 : }
     116                 :            : 
     117                 :            : static void
     118                 :          1 : test_read_lines_CR (void)
     119                 :            : {
     120                 :          1 :   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR);
     121                 :          1 : }
     122                 :            : 
     123                 :            : static void
     124                 :          1 : test_read_lines_CR_LF (void)
     125                 :            : {
     126                 :          1 :   test_read_lines (G_DATA_STREAM_NEWLINE_TYPE_CR_LF);
     127                 :          1 : }
     128                 :            : 
     129                 :            : enum TestDataType {
     130                 :            :   TEST_DATA_BYTE = 0,
     131                 :            :   TEST_DATA_INT16,
     132                 :            :   TEST_DATA_UINT16,
     133                 :            :   TEST_DATA_INT32,
     134                 :            :   TEST_DATA_UINT32,
     135                 :            :   TEST_DATA_INT64,
     136                 :            :   TEST_DATA_UINT64
     137                 :            : };
     138                 :            : 
     139                 :            : static void
     140                 :         21 : test_data_array (guchar *buffer, gsize len,
     141                 :            :                  enum TestDataType data_type, GDataStreamByteOrder byte_order)
     142                 :            : {
     143                 :            :   GOutputStream *stream;
     144                 :            :   GOutputStream *base_stream;
     145                 :            :   guchar *stream_data;
     146                 :            :   
     147                 :         21 :   GError *error = NULL;
     148                 :            :   guint pos;
     149                 :            :   GDataStreamByteOrder native;
     150                 :            :   gboolean swap;
     151                 :            :   gboolean res;
     152                 :            :   
     153                 :            :   /*  create objects */
     154                 :         21 :   stream_data = g_malloc0 (len);
     155                 :         21 :   base_stream = g_memory_output_stream_new (stream_data, len, NULL, NULL);
     156                 :         21 :   stream = G_OUTPUT_STREAM (g_data_output_stream_new (base_stream));
     157                 :         21 :   g_data_output_stream_set_byte_order (G_DATA_OUTPUT_STREAM (stream), byte_order);
     158                 :            :   
     159                 :            :   /*  Set flag to swap bytes if needed */
     160                 :         21 :   native = (G_BYTE_ORDER == G_BIG_ENDIAN) ? G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN : G_DATA_STREAM_BYTE_ORDER_LITTLE_ENDIAN;
     161   [ +  +  +  + ]:         21 :   swap = (byte_order != G_DATA_STREAM_BYTE_ORDER_HOST_ENDIAN) && (byte_order != native);
     162                 :            : 
     163                 :            :   /* set len to length of buffer cast to actual type */
     164   [ +  +  +  +  :         21 :   switch (data_type)
                      - ]
     165                 :            :     {
     166                 :          3 :     case TEST_DATA_BYTE:
     167                 :          3 :       break;
     168                 :          6 :     case TEST_DATA_INT16:
     169                 :            :     case TEST_DATA_UINT16:
     170                 :          6 :       g_assert_cmpint (len % 2, ==, 0);
     171                 :            :       G_GNUC_FALLTHROUGH;
     172                 :            :     case TEST_DATA_INT32:
     173                 :            :     case TEST_DATA_UINT32:
     174                 :         12 :       g_assert_cmpint (len % 4, ==, 0);
     175                 :            :       G_GNUC_FALLTHROUGH;
     176                 :            :     case TEST_DATA_INT64:
     177                 :            :     case TEST_DATA_UINT64:
     178                 :         18 :       g_assert_cmpint (len % 8, ==, 0);
     179                 :         18 :       len /= 8;
     180                 :         18 :       break;
     181                 :          0 :     default:
     182                 :            :       g_assert_not_reached ();
     183                 :            :       break;
     184                 :            :     }
     185                 :            : 
     186                 :            :   /*  Write data to the file */
     187         [ +  + ]:       1365 :   for (pos = 0; pos < len; pos++)
     188                 :            :     {
     189   [ +  +  +  +  :       1344 :       switch (data_type)
             +  +  +  - ]
     190                 :            :         {
     191                 :        768 :         case TEST_DATA_BYTE:
     192                 :        768 :           res = g_data_output_stream_put_byte (G_DATA_OUTPUT_STREAM (stream), buffer[pos], NULL, &error);
     193                 :        768 :           break;
     194                 :         96 :         case TEST_DATA_INT16:
     195                 :         96 :           res = g_data_output_stream_put_int16 (G_DATA_OUTPUT_STREAM (stream), ((gint16 *) buffer)[pos], NULL, &error);
     196                 :         96 :           break;
     197                 :         96 :         case TEST_DATA_UINT16:
     198                 :         96 :           res = g_data_output_stream_put_uint16 (G_DATA_OUTPUT_STREAM (stream), ((guint16 *) buffer)[pos], NULL, &error);
     199                 :         96 :           break;
     200                 :         96 :         case TEST_DATA_INT32:
     201                 :         96 :           res = g_data_output_stream_put_int32 (G_DATA_OUTPUT_STREAM (stream), ((gint32 *) buffer)[pos], NULL, &error);
     202                 :         96 :           break;
     203                 :         96 :         case TEST_DATA_UINT32:
     204                 :         96 :           res = g_data_output_stream_put_uint32 (G_DATA_OUTPUT_STREAM (stream), ((guint32 *) buffer)[pos], NULL, &error);
     205                 :         96 :           break;
     206                 :         96 :         case TEST_DATA_INT64:
     207                 :         96 :           res = g_data_output_stream_put_int64 (G_DATA_OUTPUT_STREAM (stream), ((gint64 *) buffer)[pos], NULL, &error);
     208                 :         96 :           break;
     209                 :         96 :         case TEST_DATA_UINT64:
     210                 :         96 :           res = g_data_output_stream_put_uint64 (G_DATA_OUTPUT_STREAM (stream), ((guint64 *) buffer)[pos], NULL, &error);
     211                 :         96 :           break;
     212                 :          0 :         default:
     213                 :            :           g_assert_not_reached ();
     214                 :            :           break;
     215                 :            :         }
     216                 :       1344 :       g_assert_no_error (error);
     217                 :       1344 :       g_assert_cmpint (res, ==, TRUE);
     218                 :            :     }
     219                 :            :   
     220                 :            :   /*  Compare data back */
     221         [ +  + ]:       1365 :   for (pos = 0; pos < len; pos++)
     222                 :            :     {
     223   [ +  +  +  +  :       1344 :       switch (data_type)
             +  +  +  - ]
     224                 :            :         {
     225                 :        768 :         case TEST_DATA_BYTE:
     226                 :            :           /* swapping unnecessary */
     227                 :        768 :           g_assert_cmpint (buffer[pos], ==, stream_data[pos]);
     228                 :        768 :           break;
     229                 :         96 :         case TEST_DATA_UINT16:
     230         [ +  + ]:         96 :           if (swap)
     231                 :         32 :             g_assert_cmpint (GUINT16_SWAP_LE_BE (((guint16 *) buffer)[pos]), ==, ((guint16 *) stream_data)[pos]);
     232                 :            :           else
     233                 :         64 :             g_assert_cmpint (((guint16 *) buffer)[pos], ==, ((guint16 *) stream_data)[pos]);
     234                 :         96 :           break;
     235                 :         96 :         case TEST_DATA_INT16:
     236         [ +  + ]:         96 :           if (swap)
     237                 :         32 :             g_assert_cmpint ((gint16) GUINT16_SWAP_LE_BE (((gint16 *) buffer)[pos]), ==, ((gint16 *) stream_data)[pos]);
     238                 :            :           else
     239                 :         64 :             g_assert_cmpint (((gint16 *) buffer)[pos], ==, ((gint16 *) stream_data)[pos]);
     240                 :         96 :           break;
     241                 :         96 :         case TEST_DATA_UINT32:
     242         [ +  + ]:         96 :           if (swap)
     243                 :         32 :             g_assert_cmpint (GUINT32_SWAP_LE_BE (((guint32 *) buffer)[pos]), ==, ((guint32 *) stream_data)[pos]);
     244                 :            :           else
     245                 :         64 :             g_assert_cmpint (((guint32 *) buffer)[pos], ==, ((guint32 *) stream_data)[pos]);
     246                 :         96 :           break;
     247                 :         96 :         case TEST_DATA_INT32:
     248         [ +  + ]:         96 :           if (swap)
     249                 :         32 :             g_assert_cmpint ((gint32) GUINT32_SWAP_LE_BE (((gint32 *) buffer)[pos]), ==, ((gint32 *) stream_data)[pos]);
     250                 :            :           else
     251                 :         64 :             g_assert_cmpint (((gint32 *) buffer)[pos], ==, ((gint32 *) stream_data)[pos]);
     252                 :         96 :           break;
     253                 :         96 :         case TEST_DATA_UINT64:
     254         [ +  + ]:         96 :           if (swap)
     255                 :         32 :             g_assert_cmpint (GUINT64_SWAP_LE_BE (((guint64 *) buffer)[pos]), ==, ((guint64 *) stream_data)[pos]);
     256                 :            :           else
     257                 :         64 :             g_assert_cmpint (((guint64 *) buffer)[pos], ==, ((guint64 *) stream_data)[pos]);
     258                 :         96 :           break;
     259                 :         96 :         case TEST_DATA_INT64:
     260         [ +  + ]:         96 :           if (swap)
     261                 :         32 :             g_assert_cmpint ((gint64) GUINT64_SWAP_LE_BE (((gint64 *) buffer)[pos]), ==, ((gint64 *) stream_data)[pos]);
     262                 :            :           else
     263                 :         64 :             g_assert_cmpint (((gint64 *) buffer)[pos], ==, ((gint64 *) stream_data)[pos]);
     264                 :         96 :           break;
     265                 :          0 :         default:
     266                 :            :             g_assert_not_reached ();
     267                 :            :           break;
     268                 :            :         }
     269                 :            :     }
     270                 :            :   
     271                 :         21 :   g_object_unref (base_stream);
     272                 :         21 :   g_object_unref (stream);
     273                 :         21 :   g_free (stream_data);
     274                 :         21 : }
     275                 :            : 
     276                 :            : static void
     277                 :          1 : test_read_int (void)
     278                 :            : {
     279                 :            :   GRand *randomizer;
     280                 :            :   gpointer buffer;
     281                 :            :   int i;
     282                 :            :   
     283                 :          1 :   randomizer = g_rand_new ();
     284                 :          1 :   buffer = g_malloc0(MAX_BYTES_BINARY);
     285                 :            :   
     286                 :            :   /*  Fill in some random data */
     287         [ +  + ]:        257 :   for (i = 0; i < MAX_BYTES_BINARY; i++)
     288                 :            :     {
     289                 :        256 :       guchar x = 0;
     290         [ +  + ]:        512 :       while (! x)  x = (guchar)g_rand_int (randomizer);
     291                 :        256 :       *(guchar*)((guchar*)buffer + sizeof (guchar) * i) = x; 
     292                 :            :     }
     293                 :            : 
     294         [ +  + ]:          4 :   for (i = 0; i < 3; i++)
     295                 :            :     {
     296                 :            :       int j;
     297         [ +  + ]:         24 :       for (j = 0; j <= TEST_DATA_UINT64; j++)
     298                 :         21 :         test_data_array (buffer, MAX_BYTES_BINARY, j, i);
     299                 :            :     }
     300                 :            :   
     301                 :          1 :   g_rand_free (randomizer);
     302                 :          1 :   g_free (buffer);
     303                 :          1 : }
     304                 :            : 
     305                 :            : static void
     306                 :          1 : test_seek (void)
     307                 :            : {
     308                 :            :   GDataOutputStream *stream;
     309                 :            :   GMemoryOutputStream *base_stream;
     310                 :            :   GSeekable *seekable;
     311                 :            :   GError *error;
     312                 :            :   guchar *stream_data;
     313                 :            :   gsize len;
     314                 :            :   gboolean res;
     315                 :            : 
     316                 :          1 :   len = 8;
     317                 :            :   
     318                 :            :   /*  create objects */
     319                 :          1 :   stream_data = g_malloc0 (len);
     320                 :          1 :   base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, NULL, NULL));
     321                 :          1 :   stream = g_data_output_stream_new (G_OUTPUT_STREAM (base_stream));
     322                 :          1 :   g_data_output_stream_set_byte_order (stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
     323                 :          1 :   seekable = G_SEEKABLE (stream);
     324                 :          1 :   g_assert (!g_seekable_can_truncate (seekable));
     325                 :          1 :   error = NULL;
     326                 :            :   
     327                 :            :   /* Write */
     328                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 0);
     329                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0x0123, NULL, &error);
     330                 :          1 :   g_assert_no_error (error);
     331                 :          1 :   g_assert (res);
     332                 :          1 :   g_data_output_stream_put_uint16 (stream, 0x4567, NULL, NULL);
     333                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
     334                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     335                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     336                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     337                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     338                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
     339                 :            : 
     340                 :            :   /* Forward relative seek */
     341                 :          1 :   res = g_seekable_seek (seekable, 2, G_SEEK_CUR, NULL, &error);
     342                 :          1 :   g_assert_no_error (error);
     343                 :          1 :   g_assert (res);
     344                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
     345                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
     346                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0x89AB, NULL, &error);
     347                 :          1 :   g_assert (res);
     348                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 8);
     349                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     350                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     351                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     352                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     353                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     354                 :          1 :   g_assert_cmpint (stream_data[4], ==, 0x00);
     355                 :          1 :   g_assert_cmpint (stream_data[5], ==, 0x00);
     356                 :          1 :   g_assert_cmpint (stream_data[6], ==, 0x89);
     357                 :          1 :   g_assert_cmpint (stream_data[7], ==, 0xAB);
     358                 :            : 
     359                 :            :   /* Backward relative seek */
     360                 :          1 :   res = g_seekable_seek (seekable, -3, G_SEEK_CUR, NULL, &error);
     361                 :          1 :   g_assert_no_error (error);
     362                 :          1 :   g_assert (res);
     363                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 5);
     364                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     365                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0xCDEF, NULL, &error);
     366                 :          1 :   g_assert_no_error (error);
     367                 :          1 :   g_assert (res);
     368                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 7);
     369                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     370                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     371                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     372                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     373                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     374                 :          1 :   g_assert_cmpint (stream_data[4], ==, 0x00);
     375                 :          1 :   g_assert_cmpint (stream_data[5], ==, 0xCD);
     376                 :          1 :   g_assert_cmpint (stream_data[6], ==, 0xEF);
     377                 :          1 :   g_assert_cmpint (stream_data[7], ==, 0xAB);
     378                 :            : 
     379                 :            :   /* From start */
     380                 :          1 :   res = g_seekable_seek (seekable, 4, G_SEEK_SET, NULL, &error);
     381                 :          1 :   g_assert_no_error (error);
     382                 :          1 :   g_assert (res);
     383                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
     384                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     385                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0xFEDC, NULL, &error);
     386                 :          1 :   g_assert_no_error (error);
     387                 :          1 :   g_assert (res);
     388                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
     389                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     390                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     391                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     392                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     393                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     394                 :          1 :   g_assert_cmpint (stream_data[4], ==, 0xFE);
     395                 :          1 :   g_assert_cmpint (stream_data[5], ==, 0xDC);
     396                 :          1 :   g_assert_cmpint (stream_data[6], ==, 0xEF);
     397                 :          1 :   g_assert_cmpint (stream_data[7], ==, 0xAB);
     398                 :            : 
     399                 :            :   /* From end */
     400                 :          1 :   res = g_seekable_seek (seekable, -4, G_SEEK_END, NULL, &error);
     401                 :          1 :   g_assert_no_error (error);
     402                 :          1 :   g_assert (res);
     403                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 4);
     404                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     405                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0xBA87, NULL, &error);
     406                 :          1 :   g_assert_no_error (error);
     407                 :          1 :   g_assert (res);
     408                 :          1 :   g_assert_cmpint (g_seekable_tell (seekable), ==, 6);
     409                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 8);
     410                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     411                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     412                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     413                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     414                 :          1 :   g_assert_cmpint (stream_data[4], ==, 0xBA);
     415                 :          1 :   g_assert_cmpint (stream_data[5], ==, 0x87);
     416                 :          1 :   g_assert_cmpint (stream_data[6], ==, 0xEF);
     417                 :          1 :   g_assert_cmpint (stream_data[7], ==, 0xAB);
     418                 :            : 
     419                 :          1 :   g_object_unref (stream);
     420                 :          1 :   g_object_unref (base_stream);
     421                 :          1 :   g_free (stream_data);
     422                 :          1 : }
     423                 :            : 
     424                 :            : static void
     425                 :          1 : test_truncate (void)
     426                 :            : {
     427                 :            :   GDataOutputStream *stream;
     428                 :            :   GMemoryOutputStream *base_stream;
     429                 :            :   GSeekable *seekable;
     430                 :            :   GError *error;
     431                 :            :   guchar *stream_data;
     432                 :            :   gsize len;
     433                 :            :   gboolean res;
     434                 :            : 
     435                 :          1 :   len = 8;
     436                 :            : 
     437                 :            :   /* Create objects */
     438                 :          1 :   stream_data = g_malloc0 (len);
     439                 :          1 :   base_stream = G_MEMORY_OUTPUT_STREAM (g_memory_output_stream_new (stream_data, len, g_realloc, g_free));
     440                 :          1 :   stream = g_data_output_stream_new (G_OUTPUT_STREAM (base_stream));
     441                 :          1 :   g_data_output_stream_set_byte_order (stream, G_DATA_STREAM_BYTE_ORDER_BIG_ENDIAN);
     442                 :          1 :   seekable = G_SEEKABLE (stream);
     443                 :          1 :   error = NULL;
     444                 :          1 :   g_assert (g_seekable_can_truncate (seekable));
     445                 :            :   
     446                 :            :   /* Write */
     447                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
     448                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 0);
     449                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0x0123, NULL, &error);
     450                 :          1 :   g_assert_no_error (error);
     451                 :          1 :   g_assert (res);
     452                 :          1 :   res = g_data_output_stream_put_uint16 (stream, 0x4567, NULL, NULL);
     453                 :          1 :   g_assert_no_error (error);
     454                 :          1 :   g_assert (res);
     455                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, len);
     456                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
     457                 :          1 :   stream_data = g_memory_output_stream_get_data (base_stream);
     458                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     459                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     460                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     461                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     462                 :            : 
     463                 :            :   /* Truncate at position */
     464                 :          1 :   res = g_seekable_truncate (seekable, 4, NULL, &error);
     465                 :          1 :   g_assert_no_error (error);
     466                 :          1 :   g_assert (res);
     467                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 4);
     468                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 4);
     469                 :          1 :   stream_data = g_memory_output_stream_get_data (base_stream);
     470                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     471                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     472                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     473                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     474                 :            : 
     475                 :            :   /* Truncate beyond position */
     476                 :          1 :   res = g_seekable_truncate (seekable, 6, NULL, &error);
     477                 :          1 :   g_assert_no_error (error);
     478                 :          1 :   g_assert (res);
     479                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 6);
     480                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 6);
     481                 :          1 :   stream_data = g_memory_output_stream_get_data (base_stream);
     482                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     483                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     484                 :          1 :   g_assert_cmpint (stream_data[2], ==, 0x45);
     485                 :          1 :   g_assert_cmpint (stream_data[3], ==, 0x67);
     486                 :            : 
     487                 :            :   /* Truncate before position */
     488                 :          1 :   res = g_seekable_truncate (seekable, 2, NULL, &error);
     489                 :          1 :   g_assert_no_error (error);
     490                 :          1 :   g_assert (res);
     491                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_size (base_stream), ==, 2);
     492                 :          1 :   g_assert_cmpint (g_memory_output_stream_get_data_size (base_stream), ==, 2);
     493                 :          1 :   stream_data = g_memory_output_stream_get_data (base_stream);
     494                 :          1 :   g_assert_cmpint (stream_data[0], ==, 0x01);
     495                 :          1 :   g_assert_cmpint (stream_data[1], ==, 0x23);
     496                 :            : 
     497                 :          1 :   g_object_unref (stream);
     498                 :          1 :   g_object_unref (base_stream);
     499                 :          1 : }
     500                 :            : 
     501                 :            : int
     502                 :          1 : main (int   argc,
     503                 :            :       char *argv[])
     504                 :            : {
     505                 :          1 :   g_test_init (&argc, &argv, NULL);
     506                 :            : 
     507                 :          1 :   g_test_add_func ("/data-output-stream/basic", test_basic);
     508                 :          1 :   g_test_add_func ("/data-output-stream/write-lines-LF", test_read_lines_LF);
     509                 :          1 :   g_test_add_func ("/data-output-stream/write-lines-CR", test_read_lines_CR);
     510                 :          1 :   g_test_add_func ("/data-output-stream/write-lines-CR-LF", test_read_lines_CR_LF);
     511                 :          1 :   g_test_add_func ("/data-output-stream/write-int", test_read_int);
     512                 :          1 :   g_test_add_func ("/data-output-stream/seek", test_seek);
     513                 :          1 :   g_test_add_func ("/data-output-stream/truncate", test_truncate);
     514                 :            : 
     515                 :          1 :   return g_test_run();
     516                 :            : }

Generated by: LCOV version 1.14