LCOV - code coverage report
Current view: top level - glib/gobject/tests - type.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 81 91 89.0 %
Date: 2024-04-30 05:17:35 Functions: 23 28 82.1 %
Branches: 35 50 70.0 %

           Branch data     Line data    Source code
       1                 :            : #include <glib-object.h>
       2                 :            : 
       3                 :            : static void
       4                 :          1 : test_registration_serial (void)
       5                 :            : {
       6                 :            :   gint serial1, serial2, serial3;
       7                 :            : 
       8                 :          1 :   serial1 = g_type_get_type_registration_serial ();
       9                 :          1 :   g_pointer_type_register_static ("my+pointer");
      10                 :          1 :   serial2 = g_type_get_type_registration_serial ();
      11                 :          1 :   g_assert (serial1 != serial2);
      12                 :          1 :   serial3 = g_type_get_type_registration_serial ();
      13                 :          1 :   g_assert (serial2 == serial3);
      14                 :          1 : }
      15                 :            : 
      16                 :            : typedef struct {
      17                 :            :   GTypeInterface g_iface;
      18                 :            : } BarInterface;
      19                 :            : 
      20                 :            : GType bar_get_type (void);
      21                 :            : 
      22   [ +  +  +  -  :          8 : G_DEFINE_INTERFACE (Bar, bar, G_TYPE_OBJECT)
                   +  + ]
      23                 :            : 
      24                 :            : static void
      25                 :          0 : bar_default_init (BarInterface *iface)
      26                 :            : {
      27                 :          0 : }
      28                 :            : 
      29                 :            : typedef struct {
      30                 :            :   GTypeInterface g_iface;
      31                 :            : } FooInterface;
      32                 :            : 
      33                 :            : GType foo_get_type (void);
      34                 :            : 
      35   [ +  +  +  -  :          4 : G_DEFINE_INTERFACE_WITH_CODE (Foo, foo, G_TYPE_OBJECT,
                   +  + ]
      36                 :            :                               g_type_interface_add_prerequisite (g_define_type_id, bar_get_type ()))
      37                 :            : 
      38                 :            : static void
      39                 :          1 : foo_default_init (FooInterface *iface)
      40                 :            : {
      41                 :          1 : }
      42                 :            : 
      43                 :            : typedef struct {
      44                 :            :   GTypeInterface g_iface;
      45                 :            : } BaaInterface;
      46                 :            : 
      47                 :            : GType baa_get_type (void);
      48                 :            : 
      49   [ +  +  +  -  :          2 : G_DEFINE_INTERFACE (Baa, baa, G_TYPE_INVALID)
                   +  + ]
      50                 :            : 
      51                 :            : static void
      52                 :          0 : baa_default_init (BaaInterface *iface)
      53                 :            : {
      54                 :          0 : }
      55                 :            : 
      56                 :            : typedef struct {
      57                 :            :   GTypeInterface g_iface;
      58                 :            : } BooInterface;
      59                 :            : 
      60                 :            : GType boo_get_type (void);
      61                 :            : 
      62   [ +  -  +  -  :          1 : G_DEFINE_INTERFACE_WITH_CODE (Boo, boo, G_TYPE_INVALID,
                   +  - ]
      63                 :            :                               g_type_interface_add_prerequisite (g_define_type_id, baa_get_type ()))
      64                 :            : 
      65                 :            : static void
      66                 :          0 : boo_default_init (BooInterface *iface)
      67                 :            : {
      68                 :          0 : }
      69                 :            : 
      70                 :            : typedef struct {
      71                 :            :   GTypeInterface g_iface;
      72                 :            : } BibiInterface;
      73                 :            : 
      74                 :            : GType bibi_get_type (void);
      75                 :            : 
      76   [ +  +  +  -  :          4 : G_DEFINE_INTERFACE (Bibi, bibi, G_TYPE_INITIALLY_UNOWNED)
             +  +  +  - ]
      77                 :            : 
      78                 :            : static void
      79                 :          0 : bibi_default_init (BibiInterface *iface)
      80                 :            : {
      81                 :          0 : }
      82                 :            : 
      83                 :            : typedef struct {
      84                 :            :   GTypeInterface g_iface;
      85                 :            : } BozoInterface;
      86                 :            : 
      87                 :            : GType bozo_get_type (void);
      88                 :            : 
      89   [ +  -  +  -  :          1 : G_DEFINE_INTERFACE_WITH_CODE (Bozo, bozo, G_TYPE_INVALID,
                   +  - ]
      90                 :            :                               g_type_interface_add_prerequisite (g_define_type_id, foo_get_type ());
      91                 :            :                               g_type_interface_add_prerequisite (g_define_type_id, bibi_get_type ()))
      92                 :            : 
      93                 :            : static void
      94                 :          0 : bozo_default_init (BozoInterface *iface)
      95                 :            : {
      96                 :          0 : }
      97                 :            : 
      98                 :            : 
      99                 :            : 
     100                 :            : static void
     101                 :          1 : test_interface_prerequisite (void)
     102                 :            : {
     103                 :            :   GType *prereqs;
     104                 :            :   guint n_prereqs;
     105                 :            :   gpointer iface;
     106                 :            :   gpointer parent;
     107                 :            : 
     108                 :          1 :   prereqs = g_type_interface_prerequisites (foo_get_type (), &n_prereqs);
     109                 :          1 :   g_assert_cmpint (n_prereqs, ==, 2);
     110                 :          1 :   g_assert (prereqs[0] == bar_get_type ());
     111                 :          1 :   g_assert (prereqs[1] == G_TYPE_OBJECT);
     112                 :          1 :   g_assert (g_type_interface_instantiatable_prerequisite (foo_get_type ()) == G_TYPE_OBJECT);
     113                 :            : 
     114                 :          1 :   iface = g_type_default_interface_ref (foo_get_type ());
     115                 :          1 :   parent = g_type_interface_peek_parent (iface);
     116                 :          1 :   g_assert (parent == NULL);
     117                 :          1 :   g_type_default_interface_unref (iface);
     118                 :            : 
     119                 :          1 :   g_free (prereqs);
     120                 :            : 
     121                 :          1 :   g_assert_cmpint (g_type_interface_instantiatable_prerequisite (baa_get_type ()), ==, G_TYPE_INVALID);
     122                 :          1 :   g_assert_cmpint (g_type_interface_instantiatable_prerequisite (boo_get_type ()), ==, G_TYPE_INVALID);
     123                 :            : 
     124                 :          1 :   g_assert_cmpint (g_type_interface_instantiatable_prerequisite (bozo_get_type ()), ==, G_TYPE_INITIALLY_UNOWNED);
     125                 :          1 : }
     126                 :            : 
     127                 :            : typedef struct {
     128                 :            :   GTypeInterface g_iface;
     129                 :            : } BazInterface;
     130                 :            : 
     131                 :            : GType baz_get_type (void);
     132                 :            : 
     133   [ +  -  +  -  :          1 : G_DEFINE_INTERFACE (Baz, baz, G_TYPE_OBJECT)
                   +  - ]
     134                 :            : 
     135                 :            : static void
     136                 :          1 : baz_default_init (BazInterface *iface)
     137                 :            : {
     138                 :          1 : }
     139                 :            : 
     140                 :            : typedef struct {
     141                 :            :   GObject parent;
     142                 :            : } Bazo;
     143                 :            : 
     144                 :            : typedef struct {
     145                 :            :   GObjectClass parent_class;
     146                 :            : } BazoClass;
     147                 :            : 
     148                 :            : GType bazo_get_type (void);
     149                 :            : static void bazo_iface_init (BazInterface *i);
     150                 :            : 
     151   [ +  +  +  -  :          4 : G_DEFINE_TYPE_WITH_CODE (Bazo, bazo, G_TYPE_INITIALLY_UNOWNED,
                   +  + ]
     152                 :            :                          G_IMPLEMENT_INTERFACE (baz_get_type (),
     153                 :            :                                                 bazo_iface_init);)
     154                 :            : 
     155                 :            : static void
     156                 :          1 : bazo_init (Bazo *b)
     157                 :            : {
     158                 :          1 : }
     159                 :            : 
     160                 :            : static void
     161                 :          1 : bazo_class_init (BazoClass *c)
     162                 :            : {
     163                 :          1 : }
     164                 :            : 
     165                 :            : static void
     166                 :          1 : bazo_iface_init (BazInterface *i)
     167                 :            : {
     168                 :          1 : }
     169                 :            : 
     170                 :            : static gint check_called;
     171                 :            : 
     172                 :            : static void
     173                 :          1 : check_func (gpointer check_data,
     174                 :            :             gpointer g_iface)
     175                 :            : {
     176                 :          1 :   g_assert (check_data == &check_called);
     177                 :            : 
     178                 :          1 :   check_called++;
     179                 :          1 : }
     180                 :            : 
     181                 :            : static void
     182                 :          1 : test_interface_check (void)
     183                 :            : {
     184                 :            :   GObject *o;
     185                 :            : 
     186                 :          1 :   check_called = 0;
     187                 :          1 :   g_type_add_interface_check (&check_called, check_func);
     188                 :          1 :   o = g_object_ref_sink (g_object_new (bazo_get_type (), NULL));
     189                 :          1 :   g_object_unref (o);
     190                 :          1 :   g_assert_cmpint (check_called, ==, 1);
     191                 :          1 :   g_type_remove_interface_check (&check_called, check_func);
     192                 :          1 : }
     193                 :            : 
     194                 :            : static void
     195                 :          1 : test_next_base (void)
     196                 :            : {
     197                 :            :   GType type;
     198                 :            : 
     199                 :          1 :   type = g_type_next_base (bazo_get_type (), G_TYPE_OBJECT);
     200                 :            : 
     201                 :          1 :   g_assert (type == G_TYPE_INITIALLY_UNOWNED);
     202                 :          1 : }
     203                 :            : 
     204                 :            : /* Test that the macro an function versions of g_type_is_a
     205                 :            :  * work the same
     206                 :            :  */
     207                 :            : static void
     208                 :          1 : test_is_a (void)
     209                 :            : {
     210                 :            :   g_assert_true (g_type_is_a (G_TYPE_OBJECT, G_TYPE_OBJECT));
     211                 :          1 :   g_assert_true ((g_type_is_a) (G_TYPE_OBJECT, G_TYPE_OBJECT));
     212                 :          1 :   g_assert_true (g_type_is_a (bar_get_type (), G_TYPE_OBJECT));
     213                 :          1 :   g_assert_true ((g_type_is_a) (bar_get_type (), G_TYPE_OBJECT));
     214                 :          1 :   g_assert_false (g_type_is_a (bar_get_type (), bibi_get_type ()));
     215                 :          1 :   g_assert_false ((g_type_is_a) (bar_get_type (), bibi_get_type ()));
     216                 :          1 : }
     217                 :            : 
     218                 :            : static void
     219                 :          1 : test_query (void)
     220                 :            : {
     221                 :            :   GTypeQuery results;
     222                 :            : 
     223                 :          1 :   g_test_message ("Invalid types can’t be queried.");
     224                 :          1 :   g_type_query (G_TYPE_INVALID, &results);
     225                 :          1 :   g_assert_cmpuint (results.type, ==, 0);
     226                 :            : 
     227                 :          1 :   g_test_message ("Unclassed types can’t be queried.");
     228                 :          1 :   g_type_query (G_TYPE_INT64, &results);
     229                 :          1 :   g_assert_cmpuint (results.type, ==, 0);
     230                 :          1 : }
     231                 :            : 
     232                 :            : int
     233                 :          1 : main (int argc, char *argv[])
     234                 :            : {
     235                 :          1 :   g_test_init (&argc, &argv, NULL);
     236                 :            : 
     237                 :          1 :   g_test_add_func ("/type/registration-serial", test_registration_serial);
     238                 :          1 :   g_test_add_func ("/type/interface-prerequisite", test_interface_prerequisite);
     239                 :          1 :   g_test_add_func ("/type/interface-check", test_interface_check);
     240                 :          1 :   g_test_add_func ("/type/next-base", test_next_base);
     241                 :          1 :   g_test_add_func ("/type/is-a", test_is_a);
     242                 :          1 :   g_test_add_func ("/type/query", test_query);
     243                 :            : 
     244                 :          1 :   return g_test_run ();
     245                 :            : }

Generated by: LCOV version 1.14