LCOV - code coverage report
Current view: top level - glib/gobject/tests - private.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 85 97 87.6 %
Date: 2024-04-30 05:17:35 Functions: 33 33 100.0 %
Branches: 20 32 62.5 %

           Branch data     Line data    Source code
       1                 :            : /* We are testing some deprecated APIs here */
       2                 :            : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
       3                 :            : #define GLIB_DISABLE_DEPRECATION_WARNINGS
       4                 :            : #endif
       5                 :            : 
       6                 :            : #include <glib-object.h>
       7                 :            : 
       8                 :            : typedef struct {
       9                 :            :   GObject parent_instance;
      10                 :            : } TestObject;
      11                 :            : 
      12                 :            : typedef struct {
      13                 :            :   int dummy_0;
      14                 :            :   float dummy_1;
      15                 :            : } TestObjectPrivate;
      16                 :            : 
      17                 :            : typedef struct {
      18                 :            :   GObjectClass parent_class;
      19                 :            : } TestObjectClass;
      20                 :            : 
      21                 :            : GType test_object_get_type (void);
      22                 :            : 
      23   [ +  +  +  -  :         13 : G_DEFINE_TYPE_WITH_CODE (TestObject, test_object, G_TYPE_OBJECT,
                   +  + ]
      24                 :            :                          G_ADD_PRIVATE (TestObject))
      25                 :            : 
      26                 :            : static void
      27                 :          1 : test_object_class_init (TestObjectClass *klass)
      28                 :            : {
      29                 :          1 : }
      30                 :            : 
      31                 :            : static void
      32                 :          4 : test_object_init (TestObject *self)
      33                 :            : {
      34                 :          4 :   TestObjectPrivate *priv = test_object_get_instance_private (self);
      35                 :            : 
      36         [ -  + ]:          4 :   if (g_test_verbose ())
      37                 :          0 :     g_printerr ("Offset of %sPrivate for type '%s': %d\n",
      38                 :          0 :              G_OBJECT_TYPE_NAME (self),
      39                 :          0 :              G_OBJECT_TYPE_NAME (self),
      40                 :            :              TestObject_private_offset);
      41                 :            : 
      42                 :          4 :   priv->dummy_0 = 42;
      43                 :          4 :   priv->dummy_1 = 3.14159f;
      44                 :          4 : }
      45                 :            : 
      46                 :            : static int
      47                 :          2 : test_object_get_dummy_0 (TestObject *self)
      48                 :            : {
      49                 :          2 :   TestObjectPrivate *priv = test_object_get_instance_private (self);
      50                 :            : 
      51                 :          2 :   return priv->dummy_0;
      52                 :            : }
      53                 :            : 
      54                 :            : static float
      55                 :          1 : test_object_get_dummy_1 (TestObject *self)
      56                 :            : {
      57                 :          1 :   TestObjectPrivate *priv = test_object_get_instance_private (self);
      58                 :            : 
      59                 :          1 :   return priv->dummy_1;
      60                 :            : }
      61                 :            : 
      62                 :            : typedef struct {
      63                 :            :   TestObject parent_instance;
      64                 :            : } TestDerived;
      65                 :            : 
      66                 :            : typedef struct {
      67                 :            :   char *dummy_2;
      68                 :            : } TestDerivedPrivate;
      69                 :            : 
      70                 :            : typedef struct {
      71                 :            :   TestObjectClass parent_class;
      72                 :            : } TestDerivedClass;
      73                 :            : 
      74                 :            : GType test_derived_get_type (void);
      75                 :            : 
      76   [ +  -  +  -  :          6 : G_DEFINE_TYPE_WITH_CODE (TestDerived, test_derived, test_object_get_type (),
                   +  - ]
      77                 :            :                          G_ADD_PRIVATE (TestDerived))
      78                 :            : 
      79                 :            : static void
      80                 :          1 : test_derived_finalize (GObject *obj)
      81                 :            : {
      82                 :          1 :   TestDerivedPrivate *priv = test_derived_get_instance_private ((TestDerived *) obj);
      83                 :            : 
      84                 :          1 :   g_free (priv->dummy_2);
      85                 :            : 
      86                 :          1 :   G_OBJECT_CLASS (test_derived_parent_class)->finalize (obj);
      87                 :          1 : }
      88                 :            : 
      89                 :            : static void
      90                 :          1 : test_derived_class_init (TestDerivedClass *klass)
      91                 :            : {
      92                 :          1 :   G_OBJECT_CLASS (klass)->finalize = test_derived_finalize;
      93                 :          1 : }
      94                 :            : 
      95                 :            : static void
      96                 :          1 : test_derived_init (TestDerived *self)
      97                 :            : {
      98                 :          1 :   TestDerivedPrivate *priv = test_derived_get_instance_private (self);
      99                 :            : 
     100         [ -  + ]:          1 :   if (g_test_verbose ())
     101                 :          0 :     g_printerr ("Offset of %sPrivate for type '%s': %d\n",
     102                 :          0 :              G_OBJECT_TYPE_NAME (self),
     103                 :          0 :              G_OBJECT_TYPE_NAME (self),
     104                 :            :              TestDerived_private_offset);
     105                 :            : 
     106                 :          1 :   priv->dummy_2 = g_strdup ("Hello");
     107                 :          1 : }
     108                 :            : 
     109                 :            : static const char *
     110                 :          1 : test_derived_get_dummy_2 (TestDerived *self)
     111                 :            : {
     112                 :          1 :   TestDerivedPrivate *priv = test_derived_get_instance_private (self);
     113                 :            : 
     114                 :          1 :   return priv->dummy_2;
     115                 :            : }
     116                 :            : 
     117                 :            : typedef struct {
     118                 :            :   TestObject parent_instance;
     119                 :            : } TestMixed;
     120                 :            : 
     121                 :            : typedef struct {
     122                 :            :   gint dummy_3;
     123                 :            : } TestMixedPrivate;
     124                 :            : 
     125                 :            : typedef struct {
     126                 :            :   TestObjectClass parent_class;
     127                 :            : } TestMixedClass;
     128                 :            : 
     129                 :            : GType test_mixed_get_type (void);
     130                 :            : 
     131   [ +  +  +  -  :          7 : G_DEFINE_TYPE (TestMixed, test_mixed, test_object_get_type ())
                   +  + ]
     132                 :            : 
     133                 :            : static void
     134                 :          1 : test_mixed_class_init (TestMixedClass *klass)
     135                 :            : {
     136                 :            : G_GNUC_BEGIN_IGNORE_DEPRECATIONS
     137                 :          1 :   g_type_class_add_private (klass, sizeof (TestMixedPrivate));
     138                 :            : G_GNUC_END_IGNORE_DEPRECATIONS
     139                 :          1 : }
     140                 :            : 
     141                 :            : static void
     142                 :          2 : test_mixed_init (TestMixed *self)
     143                 :            : {
     144                 :          2 :   TestMixedPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, test_mixed_get_type (), TestMixedPrivate);
     145                 :            : 
     146         [ -  + ]:          2 :   if (g_test_verbose ())
     147                 :          0 :     g_printerr ("Offset of %sPrivate for type '%s': %d\n",
     148                 :          0 :              G_OBJECT_TYPE_NAME (self),
     149                 :          0 :              G_OBJECT_TYPE_NAME (self),
     150                 :            :              TestMixed_private_offset);
     151                 :            : 
     152                 :          2 :   priv->dummy_3 = 47;
     153                 :          2 : }
     154                 :            : 
     155                 :            : static gint
     156                 :          1 : test_mixed_get_dummy_3 (TestMixed *self)
     157                 :            : {
     158                 :          1 :   TestMixedPrivate *priv = G_TYPE_INSTANCE_GET_PRIVATE (self, test_mixed_get_type (), TestMixedPrivate);
     159                 :            : 
     160                 :          1 :   return priv->dummy_3;
     161                 :            : }
     162                 :            : 
     163                 :            : typedef struct {
     164                 :            :   TestMixed parent_instance;
     165                 :            : } TestMixedDerived;
     166                 :            : 
     167                 :            : typedef struct {
     168                 :            :   gint64 dummy_4;
     169                 :            : } TestMixedDerivedPrivate;
     170                 :            : 
     171                 :            : typedef struct {
     172                 :            :   TestMixedClass parent_class;
     173                 :            : } TestMixedDerivedClass;
     174                 :            : 
     175                 :            : GType test_mixed_derived_get_type (void);
     176                 :            : 
     177   [ +  -  +  -  :          5 : G_DEFINE_TYPE_WITH_CODE (TestMixedDerived, test_mixed_derived, test_mixed_get_type (),
                   +  - ]
     178                 :            :                          G_ADD_PRIVATE (TestMixedDerived))
     179                 :            : 
     180                 :            : static void
     181                 :          1 : test_mixed_derived_class_init (TestMixedDerivedClass *klass)
     182                 :            : {
     183                 :          1 : }
     184                 :            : 
     185                 :            : static void
     186                 :          1 : test_mixed_derived_init (TestMixedDerived *self)
     187                 :            : {
     188                 :          1 :   TestMixedDerivedPrivate *priv = test_mixed_derived_get_instance_private (self);
     189                 :            : 
     190         [ -  + ]:          1 :   if (g_test_verbose ())
     191                 :          0 :     g_printerr ("Offset of %sPrivate for type '%s': %d\n",
     192                 :          0 :              G_OBJECT_TYPE_NAME (self),
     193                 :          0 :              G_OBJECT_TYPE_NAME (self),
     194                 :            :              TestMixedDerived_private_offset);
     195                 :            : 
     196                 :          1 :   priv->dummy_4 = g_get_monotonic_time ();
     197                 :          1 : }
     198                 :            : 
     199                 :            : static gint64
     200                 :          1 : test_mixed_derived_get_dummy_4 (TestMixedDerived *self)
     201                 :            : {
     202                 :          1 :   TestMixedDerivedPrivate *priv = test_mixed_derived_get_instance_private (self);
     203                 :            : 
     204                 :          1 :   return priv->dummy_4;
     205                 :            : }
     206                 :            : 
     207                 :            : static void
     208                 :          1 : private_instance (void)
     209                 :            : {
     210                 :          1 :   TestObject *obj = g_object_new (test_object_get_type (), NULL);
     211                 :            :   gpointer class;
     212                 :            :   gint offset;
     213                 :            : 
     214                 :          1 :   g_assert_cmpint (test_object_get_dummy_0 (obj), ==, 42);
     215                 :          1 :   g_assert_cmpfloat (test_object_get_dummy_1 (obj), ==, 3.14159f);
     216                 :            : 
     217                 :          1 :   class = g_type_class_ref (test_object_get_type ());
     218                 :          1 :   offset = g_type_class_get_instance_private_offset (class);
     219                 :          1 :   g_type_class_unref (class);
     220                 :            : 
     221                 :          1 :   g_assert (offset == TestObject_private_offset);
     222                 :            : 
     223                 :          1 :   g_object_unref (obj);
     224                 :          1 : }
     225                 :            : 
     226                 :            : static void
     227                 :          1 : private_derived_instance (void)
     228                 :            : {
     229                 :          1 :   TestDerived *obj = g_object_new (test_derived_get_type (), NULL);
     230                 :            : 
     231                 :          1 :   g_assert_cmpstr (test_derived_get_dummy_2 (obj), ==, "Hello");
     232                 :          1 :   g_assert_cmpint (test_object_get_dummy_0 ((TestObject *) obj), ==, 42);
     233                 :            : 
     234                 :          1 :   g_object_unref (obj);
     235                 :          1 : }
     236                 :            : 
     237                 :            : static void
     238                 :          1 : private_mixed_derived_instance (void)
     239                 :            : {
     240                 :          1 :   TestMixedDerived *derived = g_object_new (test_mixed_derived_get_type (), NULL);
     241                 :          1 :   TestMixed *mixed = g_object_new (test_mixed_get_type (), NULL);
     242                 :            : 
     243                 :          1 :   g_assert_cmpint (test_mixed_get_dummy_3 (mixed), ==, 47);
     244                 :          1 :   g_assert (test_mixed_derived_get_dummy_4 (derived) <= g_get_monotonic_time ());
     245                 :            : 
     246                 :          1 :   g_object_unref (derived);
     247                 :          1 :   g_object_unref (mixed);
     248                 :          1 : }
     249                 :            : 
     250                 :            : int
     251                 :          1 : main (int argc,
     252                 :            :       char *argv[])
     253                 :            : {
     254                 :          1 :   g_test_init (&argc, &argv, NULL);
     255                 :            : 
     256                 :          1 :   g_test_add_func ("/private/instance", private_instance);
     257                 :          1 :   g_test_add_func ("/private/derived-instance", private_derived_instance);
     258                 :          1 :   g_test_add_func ("/private/mixed-derived-instance", private_mixed_derived_instance);
     259                 :            : 
     260                 :          1 :   return g_test_run ();
     261                 :            : }

Generated by: LCOV version 1.14