LCOV - code coverage report
Current view: top level - glib/gobject/tests - autoptr.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 94 96 97.9 %
Date: 2024-04-30 05:17:35 Functions: 27 28 96.4 %
Branches: 12 16 75.0 %

           Branch data     Line data    Source code
       1                 :            : /* GLib testing framework examples and tests
       2                 :            :  * Copyright (C) 2018 Canonical Ltd
       3                 :            :  * Authors: Marco Trevisan <marco@ubuntu.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-object.h>
      26                 :            : #include <string.h>
      27                 :            : 
      28         [ +  - ]:         10 : G_DECLARE_DERIVABLE_TYPE (TestAutoCleanupBase, test_base_auto_cleanup, TEST, BASE_AUTO_CLEANUP, GObject)
      29                 :            : 
      30                 :            : struct _TestAutoCleanupBaseClass {
      31                 :            :   GObjectClass parent_class;
      32                 :            : };
      33                 :            : 
      34   [ +  +  +  -  :          3 : G_DEFINE_TYPE (TestAutoCleanupBase, test_base_auto_cleanup, G_TYPE_OBJECT)
                   +  - ]
      35                 :            : 
      36                 :            : static void
      37                 :          1 : test_base_auto_cleanup_class_init (TestAutoCleanupBaseClass *class)
      38                 :            : {
      39                 :          1 : }
      40                 :            : 
      41                 :            : static void
      42                 :          0 : test_base_auto_cleanup_init (TestAutoCleanupBase *tac)
      43                 :            : {
      44                 :          0 : }
      45                 :            : 
      46         [ +  + ]:         17 : G_DECLARE_FINAL_TYPE (TestAutoCleanup, test_auto_cleanup, TEST, AUTO_CLEANUP, TestAutoCleanupBase)
      47                 :            : 
      48                 :            : struct _TestAutoCleanup
      49                 :            : {
      50                 :            :   TestAutoCleanupBase parent_instance;
      51                 :            : };
      52                 :            : 
      53   [ +  +  +  -  :         14 : G_DEFINE_TYPE (TestAutoCleanup, test_auto_cleanup, G_TYPE_OBJECT)
                   +  + ]
      54                 :            : 
      55                 :            : static void
      56                 :          1 : test_auto_cleanup_class_init (TestAutoCleanupClass *class)
      57                 :            : {
      58                 :          1 : }
      59                 :            : 
      60                 :            : static void
      61                 :         11 : test_auto_cleanup_init (TestAutoCleanup *tac)
      62                 :            : {
      63                 :         11 : }
      64                 :            : 
      65                 :            : static TestAutoCleanup *
      66                 :         11 : test_auto_cleanup_new (void)
      67                 :            : {
      68                 :         11 :   return g_object_new (test_auto_cleanup_get_type (), NULL);
      69                 :            : }
      70                 :            : 
      71                 :            : /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
      72                 :            :  * autocleanup functions, defined using the ones of the base type (defined with
      73                 :            :  * G_DECLARE_DERIVABLE_TYPE) and so that it can be used with g_autoptr */
      74                 :            : static void
      75                 :          1 : test_autoptr (void)
      76                 :            : {
      77                 :          1 :   TestAutoCleanup *tac_ptr = test_auto_cleanup_new ();
      78                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac_ptr), (gpointer *) &tac_ptr);
      79                 :            : 
      80                 :            :   {
      81                 :          1 :     g_autoptr (TestAutoCleanup) tac = tac_ptr;
      82                 :          1 :     g_assert_nonnull (tac);
      83                 :            :   }
      84                 :            : #ifdef __GNUC__
      85                 :          1 :   g_assert_null (tac_ptr);
      86                 :            : #endif
      87                 :          1 : }
      88                 :            : 
      89                 :            : /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
      90                 :            :  * autocleanup functions, defined using the ones of the base type (defined with
      91                 :            :  * G_DECLARE_DERIVABLE_TYPE) and that stealing an autopointer works properly */
      92                 :            : static void
      93                 :          1 : test_autoptr_steal (void)
      94                 :            : {
      95                 :          2 :   g_autoptr (TestAutoCleanup) tac1 = test_auto_cleanup_new ();
      96                 :          1 :   TestAutoCleanup *tac_ptr = tac1;
      97                 :            : 
      98                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac_ptr), (gpointer *) &tac_ptr);
      99                 :            : 
     100                 :            :   {
     101                 :          2 :     g_autoptr (TestAutoCleanup) tac2 = g_steal_pointer (&tac1);
     102                 :          1 :     g_assert_nonnull (tac_ptr);
     103                 :          1 :     g_assert_null (tac1);
     104                 :          1 :     g_assert_true (tac2 == tac_ptr);
     105                 :            :   }
     106                 :            : #ifdef __GNUC__
     107                 :          1 :   g_assert_null (tac_ptr);
     108                 :            : #endif
     109                 :          1 : }
     110                 :            : 
     111                 :            : /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
     112                 :            :  * autolist cleanup functions defined using the ones of the parent type
     113                 :            :  * and so that can be used with g_autolist, and that freeing the list correctly
     114                 :            :  * unrefs the object too */
     115                 :            : static void
     116                 :          1 : test_autolist (void)
     117                 :            : {
     118                 :          1 :   TestAutoCleanup *tac1 = test_auto_cleanup_new ();
     119                 :          1 :   TestAutoCleanup *tac2 = test_auto_cleanup_new ();
     120                 :          2 :   g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
     121                 :            : 
     122                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
     123                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
     124                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
     125                 :            : 
     126                 :            :   {
     127                 :          2 :     g_autolist (TestAutoCleanup) l = NULL;
     128                 :            : 
     129                 :          1 :     l = g_list_prepend (l, tac1);
     130                 :          1 :     l = g_list_prepend (l, tac2);
     131                 :            : 
     132                 :            :     /* Squash warnings about dead stores */
     133                 :            :     (void) l;
     134                 :            :   }
     135                 :            : 
     136                 :            :   /* Only assert if autoptr works */
     137                 :            : #ifdef __GNUC__
     138                 :          1 :   g_assert_null (tac1);
     139                 :          1 :   g_assert_null (tac2);
     140                 :            : #endif
     141                 :          1 :   g_assert_nonnull (tac3);
     142                 :            : 
     143                 :          1 :   g_clear_object (&tac3);
     144                 :          1 :   g_assert_null (tac3);
     145                 :          1 : }
     146                 :            : 
     147                 :            : /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
     148                 :            :  * autoslist cleanup functions (defined using the ones of the base type declared
     149                 :            :  * with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoslist, and
     150                 :            :  * that freeing the slist correctly unrefs the object too */
     151                 :            : static void
     152                 :          1 : test_autoslist (void)
     153                 :            : {
     154                 :          1 :   TestAutoCleanup *tac1 = test_auto_cleanup_new ();
     155                 :          1 :   TestAutoCleanup *tac2 = test_auto_cleanup_new ();
     156                 :          2 :   g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
     157                 :            : 
     158                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
     159                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
     160                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
     161                 :            : 
     162                 :            :   {
     163                 :          2 :     g_autoslist (TestAutoCleanup) l = NULL;
     164                 :            : 
     165                 :          1 :     l = g_slist_prepend (l, tac1);
     166                 :          1 :     l = g_slist_prepend (l, tac2);
     167                 :            :   }
     168                 :            : 
     169                 :            :   /* Only assert if autoptr works */
     170                 :            : #ifdef __GNUC__
     171                 :          1 :   g_assert_null (tac1);
     172                 :          1 :   g_assert_null (tac2);
     173                 :            : #endif
     174                 :          1 :   g_assert_nonnull (tac3);
     175                 :            : 
     176                 :          1 :   g_clear_object (&tac3);
     177                 :          1 :   g_assert_null (tac3);
     178                 :          1 : }
     179                 :            : 
     180                 :            : /* Verify that an object declared with G_DECLARE_FINAL_TYPE provides by default
     181                 :            :  * autoqueue cleanup functions (defined using the ones of the base type declared
     182                 :            :  * with G_DECLARE_DERIVABLE_TYPE) and so that can be used with g_autoqueue, and
     183                 :            :  * that freeing the queue correctly unrefs the object too */
     184                 :            : static void
     185                 :          1 : test_autoqueue (void)
     186                 :            : {
     187                 :          1 :   TestAutoCleanup *tac1 = test_auto_cleanup_new ();
     188                 :          1 :   TestAutoCleanup *tac2 = test_auto_cleanup_new ();
     189                 :          2 :   g_autoptr (TestAutoCleanup) tac3 = test_auto_cleanup_new ();
     190                 :            : 
     191                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac1), (gpointer *) &tac1);
     192                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac2), (gpointer *) &tac2);
     193                 :          1 :   g_object_add_weak_pointer (G_OBJECT (tac3), (gpointer *) &tac3);
     194                 :            : 
     195                 :            :   {
     196                 :          2 :     g_autoqueue (TestAutoCleanup) q = g_queue_new ();
     197                 :            : 
     198                 :          1 :     g_queue_push_head (q, tac1);
     199                 :          1 :     g_queue_push_tail (q, tac2);
     200                 :            :   }
     201                 :            : 
     202                 :            :   /* Only assert if autoptr works */
     203                 :            : #ifdef __GNUC__
     204                 :          1 :   g_assert_null (tac1);
     205                 :          1 :   g_assert_null (tac2);
     206                 :            : #endif
     207                 :          1 :   g_assert_nonnull (tac3);
     208                 :            : 
     209                 :          1 :   g_clear_object (&tac3);
     210                 :          1 :   g_assert_null (tac3);
     211                 :          1 : }
     212                 :            : 
     213                 :            : static void
     214                 :          1 : test_autoclass (void)
     215                 :            : {
     216                 :          1 :   g_autoptr (TestAutoCleanupBaseClass) base_class_ptr = NULL;
     217                 :          1 :   g_autoptr (TestAutoCleanupClass) class_ptr = NULL;
     218                 :            : 
     219                 :          1 :   base_class_ptr = g_type_class_ref (test_base_auto_cleanup_get_type ());
     220                 :          1 :   class_ptr = g_type_class_ref (test_auto_cleanup_get_type ());
     221                 :            : 
     222                 :          1 :   g_assert_nonnull (base_class_ptr);
     223                 :          1 :   g_assert_nonnull (class_ptr);
     224                 :          1 : }
     225                 :            : 
     226                 :            : int
     227                 :          1 : main (int argc, gchar *argv[])
     228                 :            : {
     229                 :          1 :   g_test_init (&argc, &argv, NULL);
     230                 :            : 
     231                 :          1 :   g_test_add_func ("/autoptr/autoptr", test_autoptr);
     232                 :          1 :   g_test_add_func ("/autoptr/autoptr_steal", test_autoptr_steal);
     233                 :          1 :   g_test_add_func ("/autoptr/autolist", test_autolist);
     234                 :          1 :   g_test_add_func ("/autoptr/autoslist", test_autoslist);
     235                 :          1 :   g_test_add_func ("/autoptr/autoqueue", test_autoqueue);
     236                 :          1 :   g_test_add_func ("/autoptr/autoclass", test_autoclass);
     237                 :            : 
     238                 :          1 :   return g_test_run ();
     239                 :            : }

Generated by: LCOV version 1.14