LCOV - code coverage report
Current view: top level - glib/glib/tests - string.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 404 404 100.0 %
Date: 2024-04-23 05:16:05 Functions: 26 26 100.0 %
Branches: 24 36 66.7 %

           Branch data     Line data    Source code
       1                 :            : /* Unit tests for gstring
       2                 :            :  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       3                 :            :  *
       4                 :            :  * SPDX-License-Identifier: LicenseRef-old-glib-tests
       5                 :            :  *
       6                 :            :  * This work is provided "as is"; redistribution and modification
       7                 :            :  * in whole or in part, in any medium, physical or electronic is
       8                 :            :  * permitted without restriction.
       9                 :            :  *
      10                 :            :  * This work is distributed in the hope that it will be useful,
      11                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      12                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
      13                 :            :  *
      14                 :            :  * In no event shall the authors or contributors be liable for any
      15                 :            :  * direct, indirect, incidental, special, exemplary, or consequential
      16                 :            :  * damages (including, but not limited to, procurement of substitute
      17                 :            :  * goods or services; loss of use, data, or profits; or business
      18                 :            :  * interruption) however caused and on any theory of liability, whether
      19                 :            :  * in contract, strict liability, or tort (including negligence or
      20                 :            :  * otherwise) arising in any way out of the use of this software, even
      21                 :            :  * if advised of the possibility of such damage.
      22                 :            :  */
      23                 :            : 
      24                 :            : /* We are testing some deprecated APIs here */
      25                 :            : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
      26                 :            : #define GLIB_DISABLE_DEPRECATION_WARNINGS
      27                 :            : #endif
      28                 :            : 
      29                 :            : #include <stdio.h>
      30                 :            : #include <string.h>
      31                 :            : #include "glib.h"
      32                 :            : 
      33                 :            : 
      34                 :            : static void
      35                 :          1 : test_string_chunks (void)
      36                 :            : {
      37                 :            :   GStringChunk *string_chunk;
      38                 :            :   gchar *tmp_string, *tmp_string_2;
      39                 :            :   gint i;
      40                 :            : 
      41                 :          1 :   string_chunk = g_string_chunk_new (1024);
      42                 :            : 
      43         [ +  + ]:     100001 :   for (i = 0; i < 100000; i ++)
      44                 :            :     {
      45                 :     100000 :       tmp_string = g_string_chunk_insert (string_chunk, "hi pete");
      46                 :     100000 :       g_assert_cmpstr ("hi pete", ==, tmp_string);
      47                 :            :     }
      48                 :            : 
      49                 :          1 :   tmp_string_2 = g_string_chunk_insert_const (string_chunk, tmp_string);
      50                 :          1 :   g_assert_true (tmp_string_2 != tmp_string);
      51                 :          1 :   g_assert_cmpstr (tmp_string_2, ==, tmp_string);
      52                 :            : 
      53                 :          1 :   tmp_string = g_string_chunk_insert_const (string_chunk, tmp_string);
      54                 :          1 :   g_assert_cmpstr (tmp_string_2, ==, tmp_string);
      55                 :            : 
      56                 :          1 :   g_string_chunk_clear (string_chunk);
      57                 :          1 :   g_string_chunk_free (string_chunk);
      58                 :          1 : }
      59                 :            : 
      60                 :            : static void
      61                 :          1 : test_string_chunk_insert (void)
      62                 :            : {
      63                 :          1 :   const gchar s0[] = "Testing GStringChunk";
      64                 :          1 :   const gchar s1[] = "a\0b\0c\0d\0";
      65                 :          1 :   const gchar s2[] = "Hello, world";
      66                 :            :   GStringChunk *chunk;
      67                 :            :   gchar *str[3];
      68                 :            : 
      69                 :          1 :   chunk = g_string_chunk_new (512);
      70                 :            : 
      71                 :          1 :   str[0] = g_string_chunk_insert (chunk, s0);
      72                 :          1 :   str[1] = g_string_chunk_insert_len (chunk, s1, 8);
      73                 :          1 :   str[2] = g_string_chunk_insert (chunk, s2);
      74                 :            : 
      75                 :          1 :   g_assert_cmpmem (s0, sizeof s0, str[0], sizeof s0);
      76                 :          1 :   g_assert_cmpmem (s1, sizeof s1, str[1], sizeof s1);
      77                 :          1 :   g_assert_cmpmem (s2, sizeof s2, str[2], sizeof s2);
      78                 :            : 
      79                 :          1 :   g_string_chunk_free (chunk);
      80                 :          1 : }
      81                 :            : 
      82                 :            : static void
      83                 :          1 : test_string_new (void)
      84                 :            : {
      85                 :            :   GString *string1, *string2;
      86                 :            : 
      87                 :          1 :   string1 = g_string_new ("hi pete!");
      88                 :          1 :   string2 = g_string_new (NULL);
      89                 :            : 
      90                 :          1 :   g_assert_nonnull (string1);
      91                 :          1 :   g_assert_nonnull (string2);
      92                 :          1 :   g_assert_cmpuint (strlen (string1->str), ==, string1->len);
      93                 :          1 :   g_assert_cmpuint (strlen (string2->str), ==, string2->len);
      94                 :          1 :   g_assert_cmpuint (string2->len, ==, 0);
      95                 :          1 :   g_assert_cmpstr ("hi pete!", ==, string1->str);
      96                 :          1 :   g_assert_cmpstr ("", ==, string2->str);
      97                 :            : 
      98                 :          1 :   g_string_free (string1, TRUE);
      99                 :          1 :   g_string_free (string2, TRUE);
     100                 :            : 
     101                 :          1 :   string1 = g_string_new_len ("foo", -1);
     102                 :          1 :   string2 = g_string_new_len ("foobar", 3);
     103                 :            : 
     104                 :          1 :   g_assert_cmpstr (string1->str, ==, "foo");
     105                 :          1 :   g_assert_cmpuint (string1->len, ==, 3);
     106                 :          1 :   g_assert_cmpstr (string2->str, ==, "foo");
     107                 :          1 :   g_assert_cmpuint (string2->len, ==, 3);
     108                 :            : 
     109                 :          1 :   g_string_free (string1, TRUE);
     110                 :          1 :   g_string_free (string2, TRUE);
     111                 :          1 : }
     112                 :            : 
     113                 :            : G_GNUC_PRINTF(2, 3)
     114                 :            : static void
     115                 :          1 : my_string_printf (GString     *string,
     116                 :            :                   const gchar *format,
     117                 :            :                   ...)
     118                 :            : {
     119                 :            :   va_list args;
     120                 :            : 
     121                 :          1 :   va_start (args, format);
     122                 :          1 :   g_string_vprintf (string, format, args);
     123                 :          1 :   va_end (args);
     124                 :          1 : }
     125                 :            : 
     126                 :            : static void
     127                 :          1 : test_string_printf (void)
     128                 :            : {
     129                 :            :   GString *string;
     130                 :            : 
     131                 :          1 :   string = g_string_new (NULL);
     132                 :            : 
     133                 :            : #ifndef G_OS_WIN32
     134                 :            :   /* MSVC and mingw32 use the same run-time C library, which doesn't like
     135                 :            :      the %10000.10000f format... */
     136                 :          1 :   g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%10000.10000f",
     137                 :            :                    "this pete guy sure is a wuss, like he's the number ",
     138                 :            :                    1,
     139                 :            :                    " wuss.  everyone agrees.\n",
     140                 :            :                    10, 666, 15, 15, 666.666666666, 666.666666666);
     141                 :            : #else
     142                 :            :   g_string_printf (string, "%s|%0100d|%s|%0*d|%*.*f|%100.100f",
     143                 :            :                    "this pete guy sure is a wuss, like he's the number ",
     144                 :            :                    1,
     145                 :            :                    " wuss.  everyone agrees.\n",
     146                 :            :                    10, 666, 15, 15, 666.666666666, 666.666666666);
     147                 :            : #endif
     148                 :            : 
     149                 :          1 :   g_string_free (string, TRUE);
     150                 :            : 
     151                 :          1 :   string = g_string_new (NULL);
     152                 :          1 :   g_string_printf (string, "bla %s %d", "foo", 99);
     153                 :          1 :   g_assert_cmpstr (string->str, ==, "bla foo 99");
     154                 :          1 :   my_string_printf (string, "%d,%s,%d", 1, "two", 3);
     155                 :          1 :   g_assert_cmpstr (string->str, ==, "1,two,3");
     156                 :            : 
     157                 :          1 :   g_string_free (string, TRUE);
     158                 :          1 : }
     159                 :            : 
     160                 :            : static void
     161                 :          1 : test_string_assign (void)
     162                 :            : {
     163                 :            :   GString *string;
     164                 :            : 
     165                 :          1 :   string = g_string_new (NULL);
     166                 :          1 :   g_string_assign (string, "boring text");
     167                 :          1 :   g_assert_cmpstr (string->str, ==, "boring text");
     168                 :          1 :   g_string_free (string, TRUE);
     169                 :            : 
     170                 :            :   /* assign with string overlap */
     171                 :          1 :   string = g_string_new ("textbeforetextafter");
     172                 :          1 :   g_string_assign (string, string->str + 10);
     173                 :          1 :   g_assert_cmpstr (string->str, ==, "textafter");
     174                 :          1 :   g_string_free (string, TRUE);
     175                 :            : 
     176                 :          1 :   string = g_string_new ("boring text");
     177                 :          1 :   g_string_assign (string, string->str);
     178                 :          1 :   g_assert_cmpstr (string->str, ==, "boring text");
     179                 :          1 :   g_string_free (string, TRUE);
     180                 :          1 : }
     181                 :            : 
     182                 :            : static void
     183                 :          1 : test_string_append_c (void)
     184                 :            : {
     185                 :            :   GString *string;
     186                 :            :   guint i;
     187                 :            : 
     188                 :          1 :   string = g_string_new ("hi pete!");
     189                 :            : 
     190         [ +  + ]:      10001 :   for (i = 0; i < 10000; i++)
     191         [ +  + ]:      10000 :     if (i % 2)
     192         [ +  - ]:       5000 :       g_string_append_c (string, 'a'+(i%26));
     193                 :            :     else
     194                 :       5000 :       (g_string_append_c) (string, 'a'+(i%26));
     195                 :            : 
     196                 :          1 :   g_assert_true ((strlen("hi pete!") + 10000) == string->len);
     197                 :          1 :   g_assert_true ((strlen("hi pete!") + 10000) == strlen(string->str));
     198                 :            : 
     199         [ +  + ]:      10001 :   for (i = 0; i < 10000; i++)
     200                 :      10000 :     g_assert_true (string->str[strlen ("Hi pete!") + i] == 'a' + (gchar) (i%26));
     201                 :            : 
     202                 :          1 :   g_string_free (string, TRUE);
     203                 :          1 : }
     204                 :            : 
     205                 :            : static void
     206                 :          1 : test_string_append (void)
     207                 :            : {
     208                 :            :   GString *string;
     209                 :            :   char *tmp;
     210                 :            :   int i;
     211                 :            : 
     212                 :          1 :   tmp = g_strdup ("more");
     213                 :            : 
     214                 :            :   /* append */
     215                 :          1 :   string = g_string_new ("firsthalf");
     216         [ +  - ]:          1 :   g_string_append (string, "last");
     217                 :          1 :   (g_string_append) (string, "half");
     218                 :            : 
     219                 :          1 :   g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
     220                 :            : 
     221                 :          1 :   i = 0;
     222         [ -  + ]:          1 :   g_string_append (string, &tmp[i++]);
     223                 :          1 :   (g_string_append) (string, &tmp[i++]);
     224                 :          1 :   g_assert_true (i == 2);
     225                 :            : 
     226                 :          1 :   g_assert_cmpstr (string->str, ==, "firsthalflasthalfmoreore");
     227                 :            : 
     228                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     229                 :            :                          "*assertion*string != NULL*failed*");
     230                 :          2 :   g_assert_null (g_string_append (NULL, NULL));
     231                 :          1 :   g_test_assert_expected_messages ();
     232                 :            : 
     233                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     234                 :            :                          "*assertion*string != NULL*failed*");
     235                 :          1 :   g_assert_null ((g_string_append) (NULL, NULL));
     236                 :          1 :   g_test_assert_expected_messages ();
     237                 :            : 
     238                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     239                 :            :                          "*assertion*val != NULL*failed*");
     240                 :          2 :   g_assert_true (g_string_append (string, NULL) == string);
     241                 :          1 :   g_test_assert_expected_messages ();
     242                 :            : 
     243                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     244                 :            :                          "*assertion*val != NULL*failed*");
     245                 :          1 :   g_assert_true ((g_string_append) (string, NULL) == string);
     246                 :          1 :   g_test_assert_expected_messages ();
     247                 :            : 
     248                 :          1 :   g_string_free (string, TRUE);
     249                 :          1 :   g_free (tmp);
     250                 :            : 
     251                 :            :   /* append_len */
     252                 :          1 :   string = g_string_new ("firsthalf");
     253                 :            :   g_string_append_len (string, "lasthalfjunkjunk", strlen ("last"));
     254                 :          1 :   (g_string_append_len) (string, "halfjunkjunk", strlen ("half"));
     255                 :            :   g_string_append_len (string, "more", -1);
     256                 :          1 :   (g_string_append_len) (string, "ore", -1);
     257                 :            : 
     258                 :          1 :   g_assert_true (g_string_append_len (string, NULL, 0) == string);
     259                 :          1 :   g_assert_true ((g_string_append_len) (string, NULL, 0) == string);
     260                 :            : 
     261                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     262                 :            :                          "*assertion*string != NULL*failed*");
     263                 :          1 :   g_assert_null (g_string_append_len (NULL, NULL, -1));
     264                 :          1 :   g_test_assert_expected_messages ();
     265                 :            : 
     266                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     267                 :            :                          "*assertion*string != NULL*failed*");
     268                 :          1 :   g_assert_null ((g_string_append_len) (NULL, NULL, -1));
     269                 :          1 :   g_test_assert_expected_messages ();
     270                 :            : 
     271                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     272                 :            :                          "*assertion*val != NULL*failed*");
     273                 :          1 :   g_assert_true (g_string_append_len (string, NULL, -1) == string);
     274                 :          1 :   g_test_assert_expected_messages ();
     275                 :            : 
     276                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     277                 :            :                          "*assertion*val != NULL*failed*");
     278                 :          1 :   g_assert_true ((g_string_append_len) (string, NULL, -1) == string);
     279                 :          1 :   g_test_assert_expected_messages ();
     280                 :            : 
     281                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     282                 :            :                          "*assertion*val != NULL*failed*");
     283                 :          1 :   g_assert_true (g_string_append_len (string, NULL, 1) == string);
     284                 :          1 :   g_test_assert_expected_messages ();
     285                 :            : 
     286                 :          1 :   g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     287                 :            :                          "*assertion*val != NULL*failed*");
     288                 :          1 :   g_assert_true ((g_string_append_len) (string, NULL, 1) == string);
     289                 :          1 :   g_test_assert_expected_messages ();
     290                 :            : 
     291                 :          1 :   g_assert_cmpstr (string->str, ==, "firsthalflasthalfmoreore");
     292                 :          1 :   g_string_free (string, TRUE);
     293                 :          1 : }
     294                 :            : 
     295                 :            : static void string_append_vprintf_va (GString     *string,
     296                 :            :                                       const gchar *format,
     297                 :            :                                       ...) G_GNUC_PRINTF (2, 3);
     298                 :            : 
     299                 :            : /* Wrapper around g_string_append_vprintf() which takes varargs */
     300                 :            : static void
     301                 :          2 : string_append_vprintf_va (GString     *string,
     302                 :            :                           const gchar *format,
     303                 :            :                           ...)
     304                 :            : {
     305                 :            :   va_list args;
     306                 :            : 
     307                 :          2 :   va_start (args, format);
     308                 :          2 :   g_string_append_vprintf (string, format, args);
     309                 :          2 :   va_end (args);
     310                 :          2 : }
     311                 :            : 
     312                 :            : static void
     313                 :          1 : test_string_append_vprintf (void)
     314                 :            : {
     315                 :            :   GString *string;
     316                 :            : 
     317                 :            :   /* append */
     318                 :          1 :   string = g_string_new ("firsthalf");
     319                 :            : 
     320                 :          1 :   string_append_vprintf_va (string, "some %s placeholders", "format");
     321                 :            : 
     322                 :            :   /* vasprintf() placeholder checks on BSDs are less strict, so skip these checks if so */
     323                 :            : #if !defined(__APPLE__) && !defined(__FreeBSD__)
     324         [ +  - ]:          1 :   if (g_test_undefined ())
     325                 :            :     {
     326                 :          1 :       g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
     327                 :            :                              "Failed to append to string*");
     328                 :            : #pragma GCC diagnostic push
     329                 :            : #pragma GCC diagnostic ignored "-Wformat"
     330                 :            : #pragma GCC diagnostic ignored "-Wformat-extra-args"
     331                 :          1 :   string_append_vprintf_va (string, "%l", "invalid");
     332                 :            : #pragma GCC diagnostic pop
     333                 :          1 :       g_test_assert_expected_messages ();
     334                 :            :     }
     335                 :            : #endif
     336                 :            : 
     337                 :          1 :   g_assert_cmpstr (string->str, ==, "firsthalfsome format placeholders");
     338                 :            : 
     339                 :          1 :   g_string_free (string, TRUE);
     340                 :          1 : }
     341                 :            : 
     342                 :            : static void
     343                 :          1 : test_string_prepend_c (void)
     344                 :            : {
     345                 :            :   GString *string;
     346                 :            :   gint i;
     347                 :            : 
     348                 :          1 :   string = g_string_new ("hi pete!");
     349                 :            : 
     350         [ +  + ]:      10001 :   for (i = 0; i < 10000; i++)
     351                 :      10000 :     g_string_prepend_c (string, 'a'+(i%26));
     352                 :            : 
     353                 :          1 :   g_assert((strlen("hi pete!") + 10000) == string->len);
     354                 :          1 :   g_assert((strlen("hi pete!") + 10000) == strlen(string->str));
     355                 :            : 
     356                 :          1 :   g_string_free (string, TRUE);
     357                 :          1 : }
     358                 :            : 
     359                 :            : static void
     360                 :          1 : test_string_prepend (void)
     361                 :            : {
     362                 :            :   GString *string;
     363                 :            : 
     364                 :            :   /* prepend */
     365                 :          1 :   string = g_string_new ("lasthalf");
     366                 :          1 :   g_string_prepend (string, "firsthalf");
     367                 :          1 :   g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
     368                 :          1 :   g_string_free (string, TRUE);
     369                 :            : 
     370                 :            :   /* prepend_len */
     371                 :          1 :   string = g_string_new ("lasthalf");
     372                 :          1 :   g_string_prepend_len (string, "firsthalfjunkjunk", strlen ("firsthalf"));
     373                 :          1 :   g_assert_cmpstr (string->str, ==, "firsthalflasthalf");
     374                 :          1 :   g_string_free (string, TRUE);
     375                 :          1 : }
     376                 :            : 
     377                 :            : static void
     378                 :          1 : test_string_insert (void)
     379                 :            : {
     380                 :            :   GString *string;
     381                 :            : 
     382                 :            :   /* insert */
     383                 :          1 :   string = g_string_new ("firstlast");
     384                 :          1 :   g_string_insert (string, 5, "middle");
     385                 :          1 :   g_assert_cmpstr (string->str, ==, "firstmiddlelast");
     386                 :          1 :   g_string_free (string, TRUE);
     387                 :            : 
     388                 :            :   /* insert with pos == end of the string */
     389                 :          1 :   string = g_string_new ("firstmiddle");
     390                 :          1 :   g_string_insert (string, strlen ("firstmiddle"), "last");
     391                 :          1 :   g_assert_cmpstr (string->str, ==, "firstmiddlelast");
     392                 :          1 :   g_string_free (string, TRUE);
     393                 :            :   
     394                 :            :   /* insert_len */
     395                 :          1 :   string = g_string_new ("firstlast");
     396                 :          1 :   g_string_insert_len (string, 5, "middlejunkjunk", strlen ("middle"));
     397                 :          1 :   g_assert_cmpstr (string->str, ==, "firstmiddlelast");
     398                 :          1 :   g_string_free (string, TRUE);
     399                 :            : 
     400                 :            :   /* insert_len with magic -1 pos for append */
     401                 :          1 :   string = g_string_new ("first");
     402                 :          1 :   g_string_insert_len (string, -1, "lastjunkjunk", strlen ("last"));
     403                 :          1 :   g_assert_cmpstr (string->str, ==, "firstlast");
     404                 :          1 :   g_string_free (string, TRUE);
     405                 :            :   
     406                 :            :   /* insert_len with magic -1 len for strlen-the-string */
     407                 :          1 :   string = g_string_new ("first");
     408                 :          1 :   g_string_insert_len (string, 5, "last", -1);
     409                 :          1 :   g_assert_cmpstr (string->str, ==, "firstlast");
     410                 :          1 :   g_string_free (string, TRUE);
     411                 :            : 
     412                 :            :   /* insert_len with string overlap */
     413                 :          1 :   string = g_string_new ("textbeforetextafter");
     414                 :          1 :   g_string_insert_len (string, 10, string->str + 8, 5);
     415                 :          1 :   g_assert_cmpstr (string->str, ==, "textbeforeretextextafter");
     416                 :          1 :   g_string_free (string, TRUE);
     417                 :          1 : }
     418                 :            : 
     419                 :            : static void
     420                 :          1 : test_string_insert_unichar (void)
     421                 :            : {
     422                 :            :   GString *string;
     423                 :            : 
     424                 :            :   /* insert_unichar with insertion in middle */
     425                 :          1 :   string = g_string_new ("firsthalf");
     426                 :          1 :   g_string_insert_unichar (string, 5, 0x0041);
     427                 :          1 :   g_assert_cmpstr (string->str, ==, "first\x41half");
     428                 :          1 :   g_string_free (string, TRUE);
     429                 :            : 
     430                 :          1 :   string = g_string_new ("firsthalf");
     431                 :          1 :   g_string_insert_unichar (string, 5, 0x0298);
     432                 :          1 :   g_assert_cmpstr (string->str, ==, "first\xCA\x98half");
     433                 :          1 :   g_string_free (string, TRUE);
     434                 :            : 
     435                 :          1 :   string = g_string_new ("firsthalf");
     436                 :          1 :   g_string_insert_unichar (string, 5, 0xFFFD);
     437                 :          1 :   g_assert_cmpstr (string->str, ==, "first\xEF\xBF\xBDhalf");
     438                 :          1 :   g_string_free (string, TRUE);
     439                 :            : 
     440                 :          1 :   string = g_string_new ("firsthalf");
     441                 :          1 :   g_string_insert_unichar (string, 5, 0x1D100);
     442                 :          1 :   g_assert_cmpstr (string->str, ==, "first\xF0\x9D\x84\x80half");
     443                 :          1 :   g_string_free (string, TRUE);
     444                 :            : 
     445                 :            :   /* insert_unichar with insertion at end */
     446                 :          1 :   string = g_string_new ("start");
     447                 :          1 :   g_string_insert_unichar (string, -1, 0x0041);
     448                 :          1 :   g_assert_cmpstr (string->str, ==, "start\x41");
     449                 :          1 :   g_string_free (string, TRUE);
     450                 :            : 
     451                 :          1 :   string = g_string_new ("start");
     452                 :          1 :   g_string_insert_unichar (string, -1, 0x0298);
     453                 :          1 :   g_assert_cmpstr (string->str, ==, "start\xCA\x98");
     454                 :          1 :   g_string_free (string, TRUE);
     455                 :            : 
     456                 :          1 :   string = g_string_new ("start");
     457                 :          1 :   g_string_insert_unichar (string, -1, 0xFFFD);
     458                 :          1 :   g_assert_cmpstr (string->str, ==, "start\xEF\xBF\xBD");
     459                 :          1 :   g_string_free (string, TRUE);
     460                 :            : 
     461                 :          1 :   string = g_string_new ("start");
     462                 :          1 :   g_string_insert_unichar (string, -1, 0x1D100);
     463                 :          1 :   g_assert_cmpstr (string->str, ==, "start\xF0\x9D\x84\x80");
     464                 :          1 :   g_string_free (string, TRUE);
     465                 :            : 
     466                 :          1 :   string = g_string_new ("start");
     467                 :          1 :   g_string_insert_unichar (string, -1, 0xFFD0);
     468                 :          1 :   g_assert_cmpstr (string->str, ==, "start\xEF\xBF\x90");
     469                 :          1 :   g_string_free (string, TRUE);
     470                 :            : 
     471                 :          1 :   string = g_string_new ("start");
     472                 :          1 :   g_string_insert_unichar (string, -1, 0xFDD0);
     473                 :          1 :   g_assert_cmpstr (string->str, ==, "start\xEF\xB7\x90");
     474                 :          1 :   g_string_free (string, TRUE);
     475                 :          1 : }
     476                 :            : 
     477                 :            : static void
     478                 :          1 : test_string_equal (void)
     479                 :            : {
     480                 :            :   GString *string1, *string2;
     481                 :            : 
     482                 :          1 :   string1 = g_string_new ("test");
     483                 :          1 :   string2 = g_string_new ("te");
     484                 :          1 :   g_assert_false (g_string_equal (string1, string2));
     485         [ +  - ]:          1 :   g_string_append (string2, "st");
     486                 :          1 :   g_assert_true (g_string_equal (string1, string2));
     487                 :          1 :   g_string_free (string1, TRUE);
     488                 :          1 :   g_string_free (string2, TRUE);
     489                 :          1 : }
     490                 :            : 
     491                 :            : static void
     492                 :          1 : test_string_truncate (void)
     493                 :            : {
     494                 :            :   GString *string;
     495                 :            : 
     496                 :          1 :   string = g_string_new ("testing");
     497                 :            : 
     498                 :            :   g_string_truncate (string, 1000);
     499                 :          1 :   g_assert_cmpuint (string->len, ==, strlen("testing"));
     500                 :          1 :   g_assert_cmpstr (string->str, ==, "testing");
     501                 :            : 
     502                 :          1 :   (g_string_truncate) (string, 4);
     503                 :          1 :   g_assert_cmpuint (string->len, ==, 4);
     504                 :          1 :   g_assert_cmpstr (string->str, ==, "test");
     505                 :            : 
     506                 :            :   g_string_truncate (string, 0);
     507                 :          1 :   g_assert_cmpuint (string->len, ==, 0);
     508                 :          1 :   g_assert_cmpstr (string->str, ==, "");
     509                 :            : 
     510                 :          1 :   g_string_free (string, TRUE);
     511                 :          1 : }
     512                 :            : 
     513                 :            : static void
     514                 :          1 : test_string_overwrite (void)
     515                 :            : {
     516                 :            :   GString *string;
     517                 :            : 
     518                 :            :   /* overwriting functions */
     519                 :          1 :   string = g_string_new ("testing");
     520                 :            : 
     521                 :          1 :   g_string_overwrite (string, 4, " and expand");
     522                 :          1 :   g_assert_cmpuint (15, ==, string->len);
     523                 :          1 :   g_assert_true ('\0' == string->str[15]);
     524                 :          1 :   g_assert_true (g_str_equal ("test and expand", string->str));
     525                 :            : 
     526                 :          1 :   g_string_overwrite (string, 5, "NOT-");
     527                 :          1 :   g_assert_cmpuint (15, ==, string->len);
     528                 :          1 :   g_assert_true ('\0' == string->str[15]);
     529                 :          1 :   g_assert_true (g_str_equal ("test NOT-expand", string->str));
     530                 :            : 
     531                 :          1 :   g_string_overwrite_len (string, 9, "blablabla", 6);
     532                 :          1 :   g_assert_cmpuint (15, ==, string->len);
     533                 :          1 :   g_assert_true ('\0' == string->str[15]);
     534                 :          1 :   g_assert_true (g_str_equal ("test NOT-blabla", string->str));
     535                 :            : 
     536                 :          1 :   g_string_overwrite_len (string, 4, "BLABL", 0);
     537                 :          1 :   g_assert_true (g_str_equal ("test NOT-blabla", string->str));
     538                 :          1 :   g_string_overwrite_len (string, 4, "BLABL", -1);
     539                 :          1 :   g_assert_true (g_str_equal ("testBLABLblabla", string->str));
     540                 :            : 
     541                 :          1 :   g_string_free (string, TRUE);
     542                 :          1 : }
     543                 :            : 
     544                 :            : static void
     545                 :          1 : test_string_nul_handling (void)
     546                 :            : {
     547                 :            :   GString *string1, *string2;
     548                 :            : 
     549                 :            :   /* Check handling of embedded ASCII 0 (NUL) characters in GString. */
     550                 :          1 :   string1 = g_string_new ("fiddle");
     551                 :          1 :   string2 = g_string_new ("fiddle");
     552                 :          1 :   g_assert_true (g_string_equal (string1, string2));
     553                 :            :   g_string_append_c (string1, '\0');
     554                 :          1 :   g_assert_false (g_string_equal (string1, string2));
     555                 :            :   g_string_append_c (string2, '\0');
     556                 :          1 :   g_assert_true (g_string_equal (string1, string2));
     557                 :            :   g_string_append_c (string1, 'x');
     558                 :            :   g_string_append_c (string2, 'y');
     559                 :          1 :   g_assert_false (g_string_equal (string1, string2));
     560                 :          1 :   g_assert_cmpuint (string1->len, ==, 8);
     561         [ +  - ]:          1 :   g_string_append (string1, "yzzy");
     562                 :          1 :   g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
     563                 :          1 :   g_string_insert (string1, 1, "QED");
     564                 :          1 :   g_assert_cmpmem (string1->str, string1->len + 1, "fQEDiddle\0xyzzy", 16);
     565                 :          1 :   g_string_printf (string1, "fiddle%cxyzzy", '\0');
     566                 :          1 :   g_assert_cmpmem (string1->str, string1->len + 1, "fiddle\0xyzzy", 13);
     567                 :            : 
     568                 :          1 :   g_string_free (string1, TRUE);
     569                 :          1 :   g_string_free (string2, TRUE);
     570                 :          1 : }
     571                 :            : 
     572                 :            : static void
     573                 :          1 : test_string_up_down (void)
     574                 :            : {
     575                 :            :   GString *s;
     576                 :            : 
     577                 :          1 :   s = g_string_new ("Mixed Case String !?");
     578                 :          1 :   g_string_ascii_down (s);
     579                 :          1 :   g_assert_cmpstr (s->str, ==, "mixed case string !?");
     580                 :            : 
     581                 :          1 :   g_string_assign (s, "Mixed Case String !?");
     582                 :          1 :   g_string_down (s);
     583                 :          1 :   g_assert_cmpstr (s->str, ==, "mixed case string !?");
     584                 :            : 
     585                 :          1 :   g_string_assign (s, "Mixed Case String !?");
     586                 :          1 :   g_string_ascii_up (s);
     587                 :          1 :   g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
     588                 :            : 
     589                 :          1 :   g_string_assign (s, "Mixed Case String !?");
     590                 :          1 :   g_string_up (s);
     591                 :          1 :   g_assert_cmpstr (s->str, ==, "MIXED CASE STRING !?");
     592                 :            : 
     593                 :          1 :   g_string_free (s, TRUE);
     594                 :          1 : }
     595                 :            : 
     596                 :            : static void
     597                 :          1 : test_string_set_size (void)
     598                 :            : {
     599                 :            :   GString *s;
     600                 :            : 
     601                 :          1 :   s = g_string_new ("foo");
     602                 :          1 :   g_string_set_size (s, 30);
     603                 :            : 
     604                 :          1 :   g_assert_cmpstr (s->str, ==, "foo");
     605                 :          1 :   g_assert_cmpuint (s->len, ==, 30);
     606                 :            : 
     607                 :          1 :   g_string_free (s, TRUE);
     608                 :          1 : }
     609                 :            : 
     610                 :            : static void
     611                 :          1 : test_string_to_bytes (void)
     612                 :            : {
     613                 :            :   GString *s;
     614                 :            :   GBytes *bytes;
     615                 :            :   gconstpointer byte_data;
     616                 :            :   gsize byte_len;
     617                 :            : 
     618                 :          1 :   s = g_string_new ("foo");
     619         [ +  - ]:          1 :   g_string_append (s, "-bar");
     620                 :            : 
     621                 :          1 :   bytes = g_string_free_to_bytes (s);
     622                 :            : 
     623                 :          1 :   byte_data = g_bytes_get_data (bytes, &byte_len);
     624                 :            : 
     625                 :          1 :   g_assert_cmpuint (byte_len, ==, 7);
     626                 :            : 
     627                 :          1 :   g_assert_cmpmem (byte_data, byte_len, "foo-bar", 7);
     628                 :            : 
     629                 :          1 :   g_bytes_unref (bytes);
     630                 :          1 : }
     631                 :            : 
     632                 :            : static void
     633                 :          1 : test_string_replace (void)
     634                 :            : {
     635                 :            :   static const struct
     636                 :            :   {
     637                 :            :     const char *string;
     638                 :            :     const char *original;
     639                 :            :     const char *replacement;
     640                 :            :     guint limit;
     641                 :            :     const char *expected;
     642                 :            :     guint expected_n;
     643                 :            :   }
     644                 :            :   tests[] =
     645                 :            :   {
     646                 :            :     { "foo bar foo baz foo bar foobarbaz", "bar", "baz", 0,
     647                 :            :       "foo baz foo baz foo baz foobazbaz", 3 },
     648                 :            :     { "foo baz foo baz foo baz foobazbaz", "baz", "bar", 3,
     649                 :            :       "foo bar foo bar foo bar foobazbaz", 3 },
     650                 :            :     { "foo bar foo bar foo bar foobazbaz", "foobar", "bar", 1,
     651                 :            :       "foo bar foo bar foo bar foobazbaz", 0 },
     652                 :            :     { "aaaaaaaa", "a", "abcdefghijkl", 0,
     653                 :            :       "abcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijklabcdefghijkl",
     654                 :            :       8 },
     655                 :            :     { "/usr/$LIB/libMangoHud.so", "$LIB", "lib32", 0,
     656                 :            :       "/usr/lib32/libMangoHud.so", 1 },
     657                 :            :     { "food for foals", "o", "", 0,
     658                 :            :       "fd fr fals", 4 },
     659                 :            :     { "aaa", "a", "aaa", 0,
     660                 :            :       "aaaaaaaaa", 3 },
     661                 :            :     { "aaa", "a", "", 0,
     662                 :            :       "", 3 },
     663                 :            :     { "aaa", "aa", "bb", 0,
     664                 :            :       "bba", 1 },
     665                 :            :     { "foo", "", "bar", 0,
     666                 :            :       "barfbarobarobar", 4 },
     667                 :            :     { "", "", "x", 0,
     668                 :            :       "x", 1 },
     669                 :            :     { "", "", "", 0,
     670                 :            :       "", 1 },
     671                 :            :   };
     672                 :            :   gsize i;
     673                 :            : 
     674         [ +  + ]:         13 :   for (i = 0; i < G_N_ELEMENTS (tests); i++)
     675                 :            :     {
     676                 :            :       GString *s;
     677                 :            :       guint n;
     678                 :            : 
     679                 :         12 :       s = g_string_new (tests[i].string);
     680                 :         12 :       g_test_message ("%" G_GSIZE_FORMAT ": Replacing \"%s\" with \"%s\" (limit %u) in \"%s\"",
     681                 :         12 :                       i, tests[i].original, tests[i].replacement,
     682                 :         12 :                       tests[i].limit, tests[i].string);
     683                 :         12 :       n = g_string_replace (s, tests[i].original, tests[i].replacement,
     684                 :         12 :                             tests[i].limit);
     685                 :         12 :       g_test_message ("-> %u replacements, \"%s\"",
     686                 :            :                       n, s->str);
     687                 :         12 :       g_assert_cmpstr (tests[i].expected, ==, s->str);
     688                 :         12 :       g_assert_cmpuint (strlen (tests[i].expected), ==, s->len);
     689                 :         12 :       g_assert_cmpuint (strlen (tests[i].expected) + 1, <=, s->allocated_len);
     690                 :         12 :       g_assert_cmpuint (tests[i].expected_n, ==, n);
     691                 :         12 :       g_string_free (s, TRUE);
     692                 :            :     }
     693                 :          1 : }
     694                 :            : 
     695                 :            : static void
     696                 :          1 : test_string_steal (void)
     697                 :            : {
     698                 :            :   GString *string;
     699                 :            :   char *str;
     700                 :            : 
     701                 :          1 :   string = g_string_new ("One");
     702         [ +  - ]:          1 :   g_string_append (string, ", two");
     703   [ +  -  +  - ]:          2 :   g_string_append (string, ", three");
     704                 :            :   g_string_append_c (string, '.');
     705                 :            : 
     706                 :          1 :   str = g_string_free (string, FALSE);
     707                 :            : 
     708                 :          1 :   g_assert_cmpstr (str, ==, "One, two, three.");
     709                 :          1 :   g_free (str);
     710                 :            : 
     711                 :          1 :   string = g_string_new ("1");
     712         [ +  - ]:          1 :   g_string_append (string, " 2");
     713         [ +  - ]:          1 :   g_string_append (string, " 3");
     714                 :            : 
     715                 :          1 :   str = g_string_free_and_steal (string);
     716                 :            : 
     717                 :          1 :   g_assert_cmpstr (str, ==, "1 2 3");
     718                 :          1 :   g_free (str);
     719                 :          1 : }
     720                 :            : 
     721                 :            : static void
     722                 :          1 : test_string_new_take (void)
     723                 :            : {
     724                 :          1 :   const char *test_str_const = "test_test";
     725                 :          1 :   const char *replaced_str_const = "test__test";
     726                 :          1 :   char *test_str = malloc (10 * sizeof (*test_str_const));
     727                 :            :   GString *string;
     728                 :            : 
     729                 :          1 :   strcpy (test_str, test_str_const);
     730                 :          1 :   g_assert_cmpstr (test_str, ==, test_str_const);
     731                 :            : 
     732                 :          1 :   string = g_string_new_take (g_steal_pointer (&test_str));
     733                 :          1 :   g_assert_null (test_str);
     734                 :          1 :   g_assert_nonnull (string);
     735                 :            : 
     736                 :          1 :   g_string_replace (string, "_", "__", 0);
     737                 :          1 :   g_assert_cmpstr (string->str, ==, replaced_str_const);
     738                 :            : 
     739                 :          1 :   test_str = g_string_free_and_steal (g_steal_pointer (&string));
     740                 :          1 :   g_assert_cmpstr (test_str, ==, replaced_str_const);
     741                 :            : 
     742                 :          1 :   g_free (test_str);
     743                 :          1 : }
     744                 :            : 
     745                 :            : static void
     746                 :          1 : test_string_new_take_null (void)
     747                 :            : {
     748                 :          1 :   GString *string = g_string_new_take (NULL);
     749                 :            : 
     750                 :          1 :   g_assert_cmpstr (string->str, ==, "");
     751                 :            : 
     752                 :          1 :   g_string_free (g_steal_pointer (&string), TRUE);
     753                 :          1 : }
     754                 :            : 
     755                 :            : int
     756                 :          1 : main (int   argc,
     757                 :            :       char *argv[])
     758                 :            : {
     759                 :          1 :   g_test_init (&argc, &argv, NULL);
     760                 :            : 
     761                 :          1 :   g_test_add_func ("/string/test-string-chunks", test_string_chunks);
     762                 :          1 :   g_test_add_func ("/string/test-string-chunk-insert", test_string_chunk_insert);
     763                 :          1 :   g_test_add_func ("/string/test-string-new", test_string_new);
     764                 :          1 :   g_test_add_func ("/string/test-string-printf", test_string_printf);
     765                 :          1 :   g_test_add_func ("/string/test-string-assign", test_string_assign);
     766                 :          1 :   g_test_add_func ("/string/test-string-append-c", test_string_append_c);
     767                 :          1 :   g_test_add_func ("/string/test-string-append", test_string_append);
     768                 :          1 :   g_test_add_func ("/string/test-string-append-vprintf", test_string_append_vprintf);
     769                 :          1 :   g_test_add_func ("/string/test-string-prepend-c", test_string_prepend_c);
     770                 :          1 :   g_test_add_func ("/string/test-string-prepend", test_string_prepend);
     771                 :          1 :   g_test_add_func ("/string/test-string-insert", test_string_insert);
     772                 :          1 :   g_test_add_func ("/string/test-string-insert-unichar", test_string_insert_unichar);
     773                 :          1 :   g_test_add_func ("/string/test-string-equal", test_string_equal);
     774                 :          1 :   g_test_add_func ("/string/test-string-truncate", test_string_truncate);
     775                 :          1 :   g_test_add_func ("/string/test-string-overwrite", test_string_overwrite);
     776                 :          1 :   g_test_add_func ("/string/test-string-nul-handling", test_string_nul_handling);
     777                 :          1 :   g_test_add_func ("/string/test-string-up-down", test_string_up_down);
     778                 :          1 :   g_test_add_func ("/string/test-string-set-size", test_string_set_size);
     779                 :          1 :   g_test_add_func ("/string/test-string-to-bytes", test_string_to_bytes);
     780                 :          1 :   g_test_add_func ("/string/test-string-replace", test_string_replace);
     781                 :          1 :   g_test_add_func ("/string/test-string-steal", test_string_steal);
     782                 :          1 :   g_test_add_func ("/string/test-string-new-take", test_string_new_take);
     783                 :          1 :   g_test_add_func ("/string/test-string-new-take/null", test_string_new_take_null);
     784                 :            : 
     785                 :          1 :   return g_test_run();
     786                 :            : }

Generated by: LCOV version 1.14