LCOV - code coverage report
Current view: top level - glib/glib/tests - refstring.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 57 57 100.0 %
Date: 2024-04-16 05:15:53 Functions: 6 6 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /* refstring.c: Reference counted strings
       2                 :            :  *
       3                 :            :  * Copyright 2018  Emmanuele Bassi
       4                 :            :  *
       5                 :            :  * SPDX-License-Identifier: LGPL-2.1-or-later
       6                 :            :  *
       7                 :            :  * This library is free software; you can redistribute it and/or
       8                 :            :  * modify it under the terms of the GNU Lesser General Public
       9                 :            :  * License as published by the Free Software Foundation; either
      10                 :            :  * version 2.1 of the License, or (at your option) any later version.
      11                 :            :  *
      12                 :            :  * This library is distributed in the hope that it will be useful,
      13                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      15                 :            :  * Lesser General Public License for more details.
      16                 :            :  *
      17                 :            :  * You should have received a copy of the GNU Lesser General Public
      18                 :            :  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
      19                 :            :  */
      20                 :            : 
      21                 :            : #include <glib.h>
      22                 :            : #include <string.h>
      23                 :            : 
      24                 :            : /* test_refstring_base: Test the base API of GRefString */
      25                 :            : static void
      26                 :          1 : test_refstring_base (void)
      27                 :            : {
      28                 :          1 :   char *s = g_ref_string_new ("hello, world");
      29                 :            : 
      30                 :          1 :   g_test_message ("s = '%s' (%p)", s, s);
      31                 :          1 :   g_assert_cmpint (strcmp (s, "hello, world"), ==, 0);
      32                 :          1 :   g_assert_cmpint (strlen (s), ==, strlen ("hello, world"));
      33                 :          1 :   g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello, world"));
      34                 :            : 
      35                 :          1 :   g_assert_true (g_ref_string_acquire (s) == s);
      36                 :          1 :   g_ref_string_release (s);
      37                 :            : 
      38                 :          1 :   g_ref_string_release (s);
      39                 :          1 : }
      40                 :            : 
      41                 :            : /* test_refstring_length: Test the _len variant */
      42                 :            : static void
      43                 :          1 : test_refstring_length (void)
      44                 :            : {
      45                 :          1 :   char buf[] = {'h', 'e', 'l', 'l', 'o'}; /* no NUL */
      46                 :          1 :   char *s = g_ref_string_new_len (buf, 5);
      47                 :            : 
      48                 :          1 :   g_assert_cmpstr (s, ==, "hello");
      49                 :          1 :   g_assert_cmpint (strlen (s), ==, strlen ("hello"));
      50                 :          1 :   g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello"));
      51                 :          1 :   g_ref_string_release (s);
      52                 :          1 : }
      53                 :            : 
      54                 :            : /* test_refstring_length: Test the _len variant with no size set */
      55                 :            : static void
      56                 :          1 : test_refstring_length_auto (void)
      57                 :            : {
      58                 :          1 :   char *s = g_ref_string_new_len ("hello", -1);
      59                 :          1 :   g_assert_cmpstr (s, ==, "hello");
      60                 :          1 :   g_assert_cmpuint (g_ref_string_length (s), ==, strlen ("hello"));
      61                 :          1 :   g_ref_string_release (s);
      62                 :          1 : }
      63                 :            : 
      64                 :            : /* test_refstring_length_nuls: Test the _len variant */
      65                 :            : static void
      66                 :          1 : test_refstring_length_nuls (void)
      67                 :            : {
      68                 :          1 :   char buf[] = {'h', 'e', '\0', 'l', 'o'}; /* no NUL */
      69                 :          1 :   char *s = g_ref_string_new_len (buf, 5);
      70                 :            : 
      71                 :          1 :   g_assert_cmpstr (s, ==, "he");
      72                 :          1 :   g_assert_cmpint (memcmp (s, "he\0lo", 5), ==, 0);
      73                 :          1 :   g_assert_cmpuint (g_ref_string_length (s), ==, 5);
      74                 :          1 :   g_ref_string_release (s);
      75                 :          1 : }
      76                 :            : 
      77                 :            : /* test_refstring_intern: Test the interning API of GRefString */
      78                 :            : static void
      79                 :          1 : test_refstring_intern (void)
      80                 :            : {
      81                 :          1 :   char *s = g_ref_string_new_intern ("hello, world");
      82                 :            :   char *p;
      83                 :            : 
      84                 :          1 :   g_test_message ("s = '%s' (%p)", s, s);
      85                 :          1 :   g_assert_cmpstr (s, ==, "hello, world");
      86                 :            : 
      87                 :          1 :   p = g_ref_string_new_intern ("hello, world");
      88                 :          1 :   g_test_message ("p = s = '%s' (%p)", p, p);
      89                 :          1 :   g_assert_true (s == p);
      90                 :            : 
      91                 :          1 :   g_test_message ("releasing p[%p] ('%s')", p, p);
      92                 :          1 :   g_ref_string_release (p);
      93                 :            : 
      94                 :          1 :   p = g_ref_string_new_intern ("goodbye, world");
      95                 :          1 :   g_test_message ("p = '%s' (%p)", p, p);
      96                 :          1 :   g_assert_false (s == p);
      97                 :            : 
      98                 :          1 :   g_test_message ("releasing p[%p] ('%s')", p, p);
      99                 :          1 :   g_ref_string_release (p);
     100                 :            : 
     101                 :          1 :   g_test_message ("releasing s[%p] ('%s')", s, s);
     102                 :          1 :   g_ref_string_release (s);
     103                 :          1 : }
     104                 :            : 
     105                 :            : int
     106                 :          1 : main (int   argc,
     107                 :            :       char *argv[])
     108                 :            : {
     109                 :          1 :   g_test_init (&argc, &argv, NULL);
     110                 :            : 
     111                 :          1 :   g_test_add_func ("/refstring/base", test_refstring_base);
     112                 :          1 :   g_test_add_func ("/refstring/length", test_refstring_length);
     113                 :          1 :   g_test_add_func ("/refstring/length-auto", test_refstring_length_auto);
     114                 :          1 :   g_test_add_func ("/refstring/length-nuls", test_refstring_length_nuls);
     115                 :          1 :   g_test_add_func ("/refstring/intern", test_refstring_intern);
     116                 :            : 
     117                 :          1 :   return g_test_run ();
     118                 :            : }

Generated by: LCOV version 1.14