LCOV - code coverage report
Current view: top level - glib/gobject/tests - boxed.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 413 417 99.0 %
Date: 2024-04-30 05:17:35 Functions: 33 35 94.3 %
Branches: 5 8 62.5 %

           Branch data     Line data    Source code
       1                 :            : #ifndef GLIB_DISABLE_DEPRECATION_WARNINGS
       2                 :            : #define GLIB_DISABLE_DEPRECATION_WARNINGS
       3                 :            : #endif
       4                 :            : 
       5                 :            : #include <glib-object.h>
       6                 :            : 
       7                 :            : typedef struct _MyBoxed MyBoxed;
       8                 :            : 
       9                 :            : struct _MyBoxed
      10                 :            : {
      11                 :            :   gint ivalue;
      12                 :            :   gchar *bla;
      13                 :            : };
      14                 :            : 
      15                 :            : static gpointer
      16                 :          4 : my_boxed_copy (gpointer orig)
      17                 :            : {
      18                 :          4 :   MyBoxed *a = orig;
      19                 :            :   MyBoxed *b;
      20                 :            : 
      21                 :          4 :   b = g_slice_new (MyBoxed);
      22                 :          4 :   b->ivalue = a->ivalue;
      23                 :          4 :   b->bla = g_strdup (a->bla);
      24                 :            : 
      25                 :          4 :   return b;
      26                 :            : }
      27                 :            : 
      28                 :            : static gint my_boxed_free_count;
      29                 :            : 
      30                 :            : static void
      31                 :          4 : my_boxed_free (gpointer orig)
      32                 :            : {
      33                 :          4 :   MyBoxed *a = orig;
      34                 :            : 
      35                 :          4 :   g_free (a->bla);
      36                 :          4 :   g_slice_free (MyBoxed, a);
      37                 :            : 
      38                 :          4 :   my_boxed_free_count++;
      39                 :          4 : }
      40                 :            : 
      41                 :            : static GType my_boxed_get_type (void);
      42                 :            : #define MY_TYPE_BOXED (my_boxed_get_type ())
      43                 :            : 
      44   [ +  +  +  -  :          6 : G_DEFINE_BOXED_TYPE (MyBoxed, my_boxed, my_boxed_copy, my_boxed_free)
                   +  + ]
      45                 :            : 
      46                 :            : static void
      47                 :          1 : test_define_boxed (void)
      48                 :            : {
      49                 :            :   MyBoxed a;
      50                 :            :   MyBoxed *b;
      51                 :            : 
      52                 :          1 :   a.ivalue = 20;
      53                 :          1 :   a.bla = g_strdup ("bla");
      54                 :            : 
      55                 :          1 :   b = g_boxed_copy (MY_TYPE_BOXED, &a);
      56                 :            : 
      57                 :          1 :   g_assert_cmpint (b->ivalue, ==, 20);
      58                 :          1 :   g_assert_cmpstr (b->bla, ==, "bla");
      59                 :            : 
      60                 :          1 :   g_boxed_free (MY_TYPE_BOXED, b);
      61                 :            : 
      62                 :          1 :   g_free (a.bla);
      63                 :          1 : }
      64                 :            : 
      65                 :            : static void
      66                 :          1 : test_boxed_ownership (void)
      67                 :            : {
      68                 :          1 :   GValue value = G_VALUE_INIT;
      69                 :            :   static MyBoxed boxed = { 10, "bla" };
      70                 :            : 
      71                 :          1 :   g_value_init (&value, MY_TYPE_BOXED);
      72                 :            : 
      73                 :          1 :   my_boxed_free_count = 0;
      74                 :            : 
      75                 :          1 :   g_value_set_static_boxed (&value, &boxed);
      76                 :          1 :   g_value_reset (&value);
      77                 :            : 
      78                 :          1 :   g_assert_cmpint (my_boxed_free_count, ==, 0);
      79                 :            : 
      80                 :          1 :   g_value_set_boxed_take_ownership (&value, g_boxed_copy (MY_TYPE_BOXED, &boxed));
      81                 :          1 :   g_value_reset (&value);
      82                 :          1 :   g_assert_cmpint (my_boxed_free_count, ==, 1);
      83                 :            : 
      84                 :          1 :   g_value_take_boxed (&value, g_boxed_copy (MY_TYPE_BOXED, &boxed));
      85                 :          1 :   g_value_reset (&value);
      86                 :          1 :   g_assert_cmpint (my_boxed_free_count, ==, 2);
      87                 :            : 
      88                 :          1 :   g_value_set_boxed (&value, &boxed);
      89                 :          1 :   g_value_reset (&value);
      90                 :          1 :   g_assert_cmpint (my_boxed_free_count, ==, 3);
      91                 :          1 : }
      92                 :            : 
      93                 :            : static void
      94                 :          0 : my_callback (gpointer user_data)
      95                 :            : {
      96                 :          0 : }
      97                 :            : 
      98                 :            : static gint destroy_count;
      99                 :            : 
     100                 :            : static void
     101                 :          1 : my_closure_notify (gpointer user_data, GClosure *closure)
     102                 :            : {
     103                 :          1 :   destroy_count++;
     104                 :          1 : }
     105                 :            : 
     106                 :            : static void
     107                 :          1 : test_boxed_closure (void)
     108                 :            : {
     109                 :            :   GClosure *closure;
     110                 :            :   GClosure *closure2;
     111                 :          1 :   GValue value = G_VALUE_INIT;
     112                 :            : 
     113                 :          1 :   g_value_init (&value, G_TYPE_CLOSURE);
     114                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     115                 :            : 
     116                 :          1 :   closure = g_cclosure_new (G_CALLBACK (my_callback), "bla", my_closure_notify);
     117                 :          1 :   g_value_take_boxed (&value, closure);
     118                 :            : 
     119                 :          1 :   closure2 = g_value_get_boxed (&value);
     120                 :          1 :   g_assert (closure2 == closure);
     121                 :            : 
     122                 :          1 :   closure2 = g_value_dup_boxed (&value);
     123                 :          1 :   g_assert (closure2 == closure); /* closures use ref/unref for copy/free */
     124                 :          1 :   g_closure_unref (closure2);
     125                 :            : 
     126                 :          1 :   g_value_unset (&value);
     127                 :          1 :   g_assert_cmpint (destroy_count, ==, 1);
     128                 :          1 : }
     129                 :            : 
     130                 :            : static void
     131                 :          1 : test_boxed_date (void)
     132                 :            : {
     133                 :            :   GDate *date;
     134                 :            :   GDate *date2;
     135                 :          1 :   GValue value = G_VALUE_INIT;
     136                 :            : 
     137                 :          1 :   g_value_init (&value, G_TYPE_DATE);
     138                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     139                 :            : 
     140                 :          1 :   date = g_date_new_dmy (1, 3, 1970);
     141                 :          1 :   g_value_take_boxed (&value, date);
     142                 :            : 
     143                 :          1 :   date2 = g_value_get_boxed (&value);
     144                 :          1 :   g_assert (date2 == date);
     145                 :            : 
     146                 :          1 :   date2 = g_value_dup_boxed (&value);
     147                 :          1 :   g_assert (date2 != date);
     148                 :          1 :   g_assert (g_date_compare (date, date2) == 0);
     149                 :          1 :   g_date_free (date2);
     150                 :            : 
     151                 :          1 :   g_value_unset (&value);
     152                 :          1 : }
     153                 :            : 
     154                 :            : static void
     155                 :          1 : test_boxed_value (void)
     156                 :            : {
     157                 :          1 :   GValue value1 = G_VALUE_INIT;
     158                 :            :   GValue *value2;
     159                 :          1 :   GValue value = G_VALUE_INIT;
     160                 :            : 
     161                 :          1 :   g_value_init (&value, G_TYPE_VALUE);
     162                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     163                 :            : 
     164                 :          1 :   g_value_init (&value1, G_TYPE_INT);
     165                 :          1 :   g_value_set_int (&value1, 26);
     166                 :            : 
     167                 :          1 :   g_value_set_static_boxed (&value, &value1);
     168                 :            : 
     169                 :          1 :   value2 = g_value_get_boxed (&value);
     170                 :          1 :   g_assert (value2 == &value1);
     171                 :            : 
     172                 :          1 :   value2 = g_value_dup_boxed (&value);
     173                 :          1 :   g_assert (value2 != &value1);
     174                 :          1 :   g_assert (G_VALUE_HOLDS_INT (value2));
     175                 :          1 :   g_assert_cmpint (g_value_get_int (value2), ==, 26);
     176                 :          1 :   g_boxed_free (G_TYPE_VALUE, value2);
     177                 :            : 
     178                 :          1 :   g_value_unset (&value);
     179                 :          1 : }
     180                 :            : 
     181                 :            : static void
     182                 :          1 : test_boxed_string (void)
     183                 :            : {
     184                 :            :   GString *v;
     185                 :            :   GString *v2;
     186                 :          1 :   GValue value = G_VALUE_INIT;
     187                 :            : 
     188                 :          1 :   g_value_init (&value, G_TYPE_GSTRING);
     189                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     190                 :            : 
     191                 :          1 :   v = g_string_new ("bla");
     192                 :          1 :   g_value_take_boxed (&value, v);
     193                 :            : 
     194                 :          1 :   v2 = g_value_get_boxed (&value);
     195                 :          1 :   g_assert (v2 == v);
     196                 :            : 
     197                 :          1 :   v2 = g_value_dup_boxed (&value);
     198                 :          1 :   g_assert (v2 != v);
     199                 :          1 :   g_assert (g_string_equal (v, v2));
     200                 :          1 :   g_string_free (v2, TRUE);
     201                 :            : 
     202                 :          1 :   g_value_unset (&value);
     203                 :          1 : }
     204                 :            : 
     205                 :            : static void
     206                 :          1 : test_boxed_hashtable (void)
     207                 :            : {
     208                 :            :   GHashTable *v;
     209                 :            :   GHashTable *v2;
     210                 :          1 :   GValue value = G_VALUE_INIT;
     211                 :            : 
     212                 :          1 :   g_value_init (&value, G_TYPE_HASH_TABLE);
     213                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     214                 :            : 
     215                 :          1 :   v = g_hash_table_new (g_str_hash, g_str_equal);
     216                 :          1 :   g_value_take_boxed (&value, v);
     217                 :            : 
     218                 :          1 :   v2 = g_value_get_boxed (&value);
     219                 :          1 :   g_assert (v2 == v);
     220                 :            : 
     221                 :          1 :   v2 = g_value_dup_boxed (&value);
     222                 :          1 :   g_assert (v2 == v);  /* hash tables use ref/unref for copy/free */
     223                 :          1 :   g_hash_table_unref (v2);
     224                 :            : 
     225                 :          1 :   g_value_unset (&value);
     226                 :          1 : }
     227                 :            : 
     228                 :            : static void
     229                 :          1 : test_boxed_array (void)
     230                 :            : {
     231                 :            :   GArray *v;
     232                 :            :   GArray *v2;
     233                 :          1 :   GValue value = G_VALUE_INIT;
     234                 :            : 
     235                 :          1 :   g_value_init (&value, G_TYPE_ARRAY);
     236                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     237                 :            : 
     238                 :          1 :   v = g_array_new (TRUE, FALSE, 1);
     239                 :          1 :   g_value_take_boxed (&value, v);
     240                 :            : 
     241                 :          1 :   v2 = g_value_get_boxed (&value);
     242                 :          1 :   g_assert (v2 == v);
     243                 :            : 
     244                 :          1 :   v2 = g_value_dup_boxed (&value);
     245                 :          1 :   g_assert (v2 == v);  /* arrays use ref/unref for copy/free */
     246                 :          1 :   g_array_unref (v2);
     247                 :            : 
     248                 :          1 :   g_value_unset (&value);
     249                 :          1 : }
     250                 :            : 
     251                 :            : static void
     252                 :          1 : test_boxed_ptrarray (void)
     253                 :            : {
     254                 :            :   GPtrArray *v;
     255                 :            :   GPtrArray *v2;
     256                 :          1 :   GValue value = G_VALUE_INIT;
     257                 :            : 
     258                 :          1 :   g_value_init (&value, G_TYPE_PTR_ARRAY);
     259                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     260                 :            : 
     261                 :          1 :   v = g_ptr_array_new ();
     262                 :          1 :   g_value_take_boxed (&value, v);
     263                 :            : 
     264                 :          1 :   v2 = g_value_get_boxed (&value);
     265                 :          1 :   g_assert (v2 == v);
     266                 :            : 
     267                 :          1 :   v2 = g_value_dup_boxed (&value);
     268                 :          1 :   g_assert (v2 == v);  /* ptr arrays use ref/unref for copy/free */
     269                 :          1 :   g_ptr_array_unref (v2);
     270                 :            : 
     271                 :          1 :   g_value_unset (&value);
     272                 :          1 : }
     273                 :            : 
     274                 :            : static void
     275                 :          1 : test_boxed_regex (void)
     276                 :            : {
     277                 :            :   GRegex *v;
     278                 :            :   GRegex *v2;
     279                 :          1 :   GValue value = G_VALUE_INIT;
     280                 :            : 
     281                 :          1 :   g_value_init (&value, G_TYPE_REGEX);
     282                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     283                 :            : 
     284                 :          1 :   v = g_regex_new ("a+b+", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
     285                 :          1 :   g_value_take_boxed (&value, v);
     286                 :            : 
     287                 :          1 :   v2 = g_value_get_boxed (&value);
     288                 :          1 :   g_assert (v2 == v);
     289                 :            : 
     290                 :          1 :   v2 = g_value_dup_boxed (&value);
     291                 :          1 :   g_assert (v2 == v);  /* regexes use ref/unref for copy/free */
     292                 :          1 :   g_regex_unref (v2);
     293                 :            : 
     294                 :          1 :   g_value_unset (&value);
     295                 :          1 : }
     296                 :            : 
     297                 :            : static void
     298                 :          1 : test_boxed_matchinfo (void)
     299                 :            : {
     300                 :            :   GRegex *r;
     301                 :            :   GMatchInfo *info, *info2;
     302                 :            :   gboolean ret;
     303                 :          1 :   GValue value = G_VALUE_INIT;
     304                 :            : 
     305                 :          1 :   g_value_init (&value, G_TYPE_MATCH_INFO);
     306                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     307                 :            : 
     308                 :          1 :   r = g_regex_new ("ab", G_REGEX_DEFAULT, G_REGEX_MATCH_DEFAULT, NULL);
     309                 :          1 :   ret = g_regex_match (r, "blabla abab bla", 0, &info);
     310                 :          1 :   g_assert (ret);
     311                 :          1 :   g_value_take_boxed (&value, info);
     312                 :            : 
     313                 :          1 :   info2 = g_value_get_boxed (&value);
     314                 :          1 :   g_assert (info == info2);
     315                 :            : 
     316                 :          1 :   info2 = g_value_dup_boxed (&value);
     317                 :          1 :   g_assert (info == info2);  /* matchinfo uses ref/unref for copy/free */
     318                 :          1 :   g_match_info_unref (info2);
     319                 :            : 
     320                 :          1 :   g_value_unset (&value);
     321                 :          1 :   g_regex_unref (r);
     322                 :          1 : }
     323                 :            : 
     324                 :            : static void
     325                 :          1 : test_boxed_varianttype (void)
     326                 :            : {
     327                 :            :   GVariantType *v;
     328                 :            :   GVariantType *v2;
     329                 :          1 :   GValue value = G_VALUE_INIT;
     330                 :            : 
     331                 :          1 :   g_value_init (&value, G_TYPE_VARIANT_TYPE);
     332                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     333                 :            : 
     334                 :          1 :   v = g_variant_type_new ("mas");
     335                 :          1 :   g_value_take_boxed (&value, v);
     336                 :            : 
     337                 :          1 :   v2 = g_value_get_boxed (&value);
     338                 :          1 :   g_assert (v2 == v);
     339                 :            : 
     340                 :          1 :   v2 = g_value_dup_boxed (&value);
     341                 :          1 :   g_assert (v2 != v);
     342                 :          1 :   g_assert_cmpstr (g_variant_type_peek_string (v), ==, g_variant_type_peek_string (v2));
     343                 :          1 :   g_variant_type_free (v2);
     344                 :            : 
     345                 :          1 :   g_value_unset (&value);
     346                 :          1 : }
     347                 :            : 
     348                 :            : static void
     349                 :          1 : test_boxed_datetime (void)
     350                 :            : {
     351                 :            :   GDateTime *v;
     352                 :            :   GDateTime *v2;
     353                 :          1 :   GValue value = G_VALUE_INIT;
     354                 :            : 
     355                 :          1 :   g_value_init (&value, G_TYPE_DATE_TIME);
     356                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     357                 :            : 
     358                 :          1 :   v = g_date_time_new_now_local ();
     359                 :          1 :   g_value_take_boxed (&value, v);
     360                 :            : 
     361                 :          1 :   v2 = g_value_get_boxed (&value);
     362                 :          1 :   g_assert (v2 == v);
     363                 :            : 
     364                 :          1 :   v2 = g_value_dup_boxed (&value);
     365                 :          1 :   g_assert (v2 == v); /* datetime uses ref/unref for copy/free */
     366                 :          1 :   g_date_time_unref (v2);
     367                 :            : 
     368                 :          1 :   g_value_unset (&value);
     369                 :          1 : }
     370                 :            : 
     371                 :            : static void
     372                 :          1 : test_boxed_error (void)
     373                 :            : {
     374                 :            :   GError *v;
     375                 :            :   GError *v2;
     376                 :          1 :   GValue value = G_VALUE_INIT;
     377                 :            : 
     378                 :          1 :   g_value_init (&value, G_TYPE_ERROR);
     379                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     380                 :            : 
     381                 :          1 :   v = g_error_new_literal (G_VARIANT_PARSE_ERROR,
     382                 :            :                            G_VARIANT_PARSE_ERROR_NUMBER_TOO_BIG,
     383                 :            :                            "Too damn big");
     384                 :          1 :   g_value_take_boxed (&value, v);
     385                 :            : 
     386                 :          1 :   v2 = g_value_get_boxed (&value);
     387                 :          1 :   g_assert (v2 == v);
     388                 :            : 
     389                 :          1 :   v2 = g_value_dup_boxed (&value);
     390                 :          1 :   g_assert (v2 != v);
     391                 :          1 :   g_assert_cmpint (v->domain, ==, v2->domain);
     392                 :          1 :   g_assert_cmpint (v->code, ==, v2->code);
     393                 :          1 :   g_assert_cmpstr (v->message, ==, v2->message);
     394                 :          1 :   g_error_free (v2);
     395                 :            : 
     396                 :          1 :   g_value_unset (&value);
     397                 :          1 : }
     398                 :            : 
     399                 :            : static void
     400                 :          1 : test_boxed_keyfile (void)
     401                 :            : {
     402                 :            :   GKeyFile *k, *k2;
     403                 :          1 :   GValue value = G_VALUE_INIT;
     404                 :            : 
     405                 :          1 :   g_value_init (&value, G_TYPE_KEY_FILE);
     406                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     407                 :            : 
     408                 :          1 :   k = g_key_file_new ();
     409                 :          1 :   g_value_take_boxed (&value, k);
     410                 :            : 
     411                 :          1 :   k2 = g_value_get_boxed (&value);
     412                 :          1 :   g_assert (k == k2);
     413                 :            : 
     414                 :          1 :   k2 = g_value_dup_boxed (&value);
     415                 :          1 :   g_assert (k == k2);  /* keyfile uses ref/unref for copy/free */
     416                 :          1 :   g_key_file_unref (k2);
     417                 :            : 
     418                 :          1 :   g_value_unset (&value);
     419                 :          1 : }
     420                 :            : 
     421                 :            : static void
     422                 :          1 : test_boxed_mainloop (void)
     423                 :            : {
     424                 :            :   GMainLoop *l, *l2;
     425                 :          1 :   GValue value = G_VALUE_INIT;
     426                 :            : 
     427                 :          1 :   g_value_init (&value, G_TYPE_MAIN_LOOP);
     428                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     429                 :            : 
     430                 :          1 :   l = g_main_loop_new (NULL, FALSE);
     431                 :          1 :   g_value_take_boxed (&value, l);
     432                 :            : 
     433                 :          1 :   l2 = g_value_get_boxed (&value);
     434                 :          1 :   g_assert (l == l2);
     435                 :            : 
     436                 :          1 :   l2 = g_value_dup_boxed (&value);
     437                 :          1 :   g_assert (l == l2);  /* mainloop uses ref/unref for copy/free */
     438                 :          1 :   g_main_loop_unref (l2);
     439                 :            : 
     440                 :          1 :   g_value_unset (&value);
     441                 :          1 : }
     442                 :            : 
     443                 :            : static void
     444                 :          1 : test_boxed_maincontext (void)
     445                 :            : {
     446                 :            :   GMainContext *c, *c2;
     447                 :          1 :   GValue value = G_VALUE_INIT;
     448                 :            : 
     449                 :          1 :   g_value_init (&value, G_TYPE_MAIN_CONTEXT);
     450                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     451                 :            : 
     452                 :          1 :   c = g_main_context_new ();
     453                 :          1 :   g_value_take_boxed (&value, c);
     454                 :            : 
     455                 :          1 :   c2 = g_value_get_boxed (&value);
     456                 :          1 :   g_assert (c == c2);
     457                 :            : 
     458                 :          1 :   c2 = g_value_dup_boxed (&value);
     459                 :          1 :   g_assert (c == c2);  /* maincontext uses ref/unref for copy/free */
     460                 :          1 :   g_main_context_unref (c2);
     461                 :            : 
     462                 :          1 :   g_value_unset (&value);
     463                 :          1 : }
     464                 :            : 
     465                 :            : static void
     466                 :          1 : test_boxed_source (void)
     467                 :            : {
     468                 :            :   GSource *s, *s2;
     469                 :          1 :   GValue value = G_VALUE_INIT;
     470                 :            : 
     471                 :          1 :   g_value_init (&value, G_TYPE_SOURCE);
     472                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     473                 :            : 
     474                 :          1 :   s = g_idle_source_new ();
     475                 :          1 :   g_value_take_boxed (&value, s);
     476                 :            : 
     477                 :          1 :   s2 = g_value_get_boxed (&value);
     478                 :          1 :   g_assert (s == s2);
     479                 :            : 
     480                 :          1 :   s2 = g_value_dup_boxed (&value);
     481                 :          1 :   g_assert (s == s2);  /* source uses ref/unref for copy/free */
     482                 :          1 :   g_source_unref (s2);
     483                 :            : 
     484                 :          1 :   g_value_unset (&value);
     485                 :          1 : }
     486                 :            : 
     487                 :            : static void
     488                 :          1 : test_boxed_variantbuilder (void)
     489                 :            : {
     490                 :            :   GVariantBuilder *v, *v2;
     491                 :          1 :   GValue value = G_VALUE_INIT;
     492                 :            : 
     493                 :          1 :   g_value_init (&value, G_TYPE_VARIANT_BUILDER);
     494                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     495                 :            : 
     496                 :          1 :   v = g_variant_builder_new (G_VARIANT_TYPE_OBJECT_PATH_ARRAY);
     497                 :          1 :   g_value_take_boxed (&value, v);
     498                 :            : 
     499                 :          1 :   v2 = g_value_get_boxed (&value);
     500                 :          1 :   g_assert (v == v2);
     501                 :            : 
     502                 :          1 :   v2 = g_value_dup_boxed (&value);
     503                 :          1 :   g_assert (v == v2);  /* variantbuilder uses ref/unref for copy/free */
     504                 :          1 :   g_variant_builder_unref (v2);
     505                 :            : 
     506                 :          1 :   g_value_unset (&value);
     507                 :          1 : }
     508                 :            : 
     509                 :            : static void
     510                 :          1 : test_boxed_timezone (void)
     511                 :            : {
     512                 :            :   GTimeZone *z, *z2;
     513                 :          1 :   GValue value = G_VALUE_INIT;
     514                 :            : 
     515                 :          1 :   g_value_init (&value, G_TYPE_TIME_ZONE);
     516                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     517                 :            : 
     518                 :          1 :   z = g_time_zone_new_utc ();
     519                 :          1 :   g_value_take_boxed (&value, z);
     520                 :            : 
     521                 :          1 :   z2 = g_value_get_boxed (&value);
     522                 :          1 :   g_assert (z == z2);
     523                 :            : 
     524                 :          1 :   z2 = g_value_dup_boxed (&value);
     525                 :          1 :   g_assert (z == z2);  /* timezone uses ref/unref for copy/free */
     526                 :          1 :   g_time_zone_unref (z2);
     527                 :            : 
     528                 :          1 :   g_value_unset (&value);
     529                 :          1 : }
     530                 :            : 
     531                 :            : static void
     532                 :          1 : test_boxed_pollfd (void)
     533                 :            : {
     534                 :            :   GPollFD *p, *p2;
     535                 :          1 :   GValue value = G_VALUE_INIT;
     536                 :            : 
     537                 :          1 :   g_value_init (&value, G_TYPE_POLLFD);
     538                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     539                 :            : 
     540                 :          1 :   p = g_new (GPollFD, 1);
     541                 :          1 :   g_value_take_boxed (&value, p);
     542                 :            : 
     543                 :          1 :   p2 = g_value_get_boxed (&value);
     544                 :          1 :   g_assert (p == p2);
     545                 :            : 
     546                 :          1 :   p2 = g_value_dup_boxed (&value);
     547                 :          1 :   g_assert (p != p2);
     548                 :          1 :   g_free (p2);
     549                 :            : 
     550                 :          1 :   g_value_unset (&value);
     551                 :          1 : }
     552                 :            : 
     553                 :            : static void
     554                 :          1 : test_boxed_markup (void)
     555                 :            : {
     556                 :            :   GMarkupParseContext *c, *c2;
     557                 :          1 :   const GMarkupParser parser = { 0 };
     558                 :          1 :   GValue value = G_VALUE_INIT;
     559                 :            : 
     560                 :          1 :   g_value_init (&value, G_TYPE_MARKUP_PARSE_CONTEXT);
     561                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     562                 :            : 
     563                 :          1 :   c = g_markup_parse_context_new (&parser, G_MARKUP_DEFAULT_FLAGS, NULL, NULL);
     564                 :          1 :   g_value_take_boxed (&value, c);
     565                 :            : 
     566                 :          1 :   c2 = g_value_get_boxed (&value);
     567                 :          1 :   g_assert (c == c2);
     568                 :            : 
     569                 :          1 :   c2 = g_value_dup_boxed (&value);
     570                 :          1 :   g_assert (c == c2);
     571                 :          1 :   g_markup_parse_context_unref (c2);
     572                 :            : 
     573                 :          1 :   g_value_unset (&value);
     574                 :          1 : }
     575                 :            : 
     576                 :            : static void
     577                 :          1 : test_boxed_thread (void)
     578                 :            : {
     579                 :            :   GThread *t, *t2;
     580                 :          1 :   GValue value = G_VALUE_INIT;
     581                 :            : 
     582                 :          1 :   g_value_init (&value, G_TYPE_THREAD);
     583                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     584                 :            : 
     585                 :          1 :   t = g_thread_self ();
     586                 :          1 :   g_value_set_boxed (&value, t);
     587                 :            : 
     588                 :          1 :   t2 = g_value_get_boxed (&value);
     589                 :          1 :   g_assert (t == t2);
     590                 :            : 
     591                 :          1 :   t2 = g_value_dup_boxed (&value);
     592                 :          1 :   g_assert (t == t2);
     593                 :          1 :   g_thread_unref (t2);
     594                 :            : 
     595                 :          1 :   g_value_unset (&value);
     596                 :          1 : }
     597                 :            : 
     598                 :            : static void
     599                 :          1 : test_boxed_checksum (void)
     600                 :            : {
     601                 :            :   GChecksum *c, *c2;
     602                 :          1 :   GValue value = G_VALUE_INIT;
     603                 :            : 
     604                 :          1 :   g_value_init (&value, G_TYPE_CHECKSUM);
     605                 :          1 :   g_assert (G_VALUE_HOLDS_BOXED (&value));
     606                 :            : 
     607                 :          1 :   c = g_checksum_new (G_CHECKSUM_SHA512);
     608                 :          1 :   g_value_take_boxed (&value, c);
     609                 :            : 
     610                 :          1 :   c2 = g_value_get_boxed (&value);
     611                 :          1 :   g_assert (c == c2);
     612                 :            : 
     613                 :          1 :   c2 = g_value_dup_boxed (&value);
     614                 :          1 :   g_assert (c != c2);
     615                 :          1 :   g_checksum_free (c2);
     616                 :            : 
     617                 :          1 :   g_value_unset (&value);
     618                 :          1 : }
     619                 :            : 
     620                 :            : static gint
     621                 :          0 : treecmp (gconstpointer a, gconstpointer b)
     622                 :            : {
     623         [ #  # ]:          0 :   return (a < b) ? -1 : (a > b);
     624                 :            : }
     625                 :            : 
     626                 :            : static void
     627                 :          1 : test_boxed_tree (void)
     628                 :            : {
     629                 :            :   GTree *t, *t2;
     630                 :          1 :   GValue value = G_VALUE_INIT;
     631                 :            : 
     632                 :          1 :   g_value_init (&value, G_TYPE_TREE);
     633                 :          1 :   g_assert_true (G_VALUE_HOLDS_BOXED (&value));
     634                 :            : 
     635                 :          1 :   t = g_tree_new (treecmp);
     636                 :          1 :   g_value_take_boxed (&value, t);
     637                 :            : 
     638                 :          1 :   t2 = g_value_get_boxed (&value);
     639                 :          1 :   g_assert_true (t == t2);
     640                 :            : 
     641                 :          1 :   t2 = g_value_dup_boxed (&value);
     642                 :          1 :   g_assert_true (t == t2); /* trees use ref/unref for copy/free */
     643                 :          1 :   g_tree_unref (t2);
     644                 :            : 
     645                 :          1 :   g_value_unset (&value);
     646                 :          1 : }
     647                 :            : 
     648                 :            : static void
     649                 :          1 : test_boxed_pattern_spec (void)
     650                 :            : {
     651                 :            :   GPatternSpec *ps, *ps2;
     652                 :          1 :   GValue value = G_VALUE_INIT;
     653                 :            : 
     654                 :          1 :   g_value_init (&value, G_TYPE_PATTERN_SPEC);
     655                 :          1 :   g_assert_true (G_VALUE_HOLDS_BOXED (&value));
     656                 :            : 
     657                 :          1 :   ps = g_pattern_spec_new ("*abc*?cde");
     658                 :          1 :   g_value_take_boxed (&value, ps);
     659                 :            : 
     660                 :          1 :   ps2 = g_value_get_boxed (&value);
     661                 :          1 :   g_assert_true (ps == ps2);
     662                 :            : 
     663                 :          1 :   ps2 = g_value_dup_boxed (&value);
     664                 :          1 :   g_assert_true (ps != ps2);
     665                 :          1 :   g_assert_true (g_pattern_spec_equal (ps, ps2));
     666                 :          1 :   g_pattern_spec_free (ps2);
     667                 :            : 
     668                 :          1 :   g_value_unset (&value);
     669                 :          1 : }
     670                 :            : 
     671                 :            : static void
     672                 :          1 : test_boxed_rand (void)
     673                 :            : {
     674                 :            :   GRand *r, *r2;
     675                 :          1 :   GValue value = G_VALUE_INIT;
     676                 :            : 
     677                 :          1 :   g_value_init (&value, G_TYPE_RAND);
     678                 :          1 :   g_assert_true (G_VALUE_HOLDS_BOXED (&value));
     679                 :            : 
     680                 :          1 :   r = g_rand_new ();
     681                 :          1 :   g_value_take_boxed (&value, r);
     682                 :            : 
     683                 :          1 :   r2 = g_value_get_boxed (&value);
     684                 :          1 :   g_assert_true (r == r2);
     685                 :            : 
     686                 :          1 :   r2 = g_value_dup_boxed (&value);
     687                 :          1 :   g_assert_true (r != r2);
     688                 :          1 :   g_rand_free (r2);
     689                 :            : 
     690                 :          1 :   g_value_unset (&value);
     691                 :          1 : }
     692                 :            : 
     693                 :            : int
     694                 :          1 : main (int argc, char *argv[])
     695                 :            : {
     696                 :          1 :   g_test_init (&argc, &argv, NULL);
     697                 :            : 
     698                 :          1 :   g_test_add_func ("/boxed/define", test_define_boxed);
     699                 :          1 :   g_test_add_func ("/boxed/ownership", test_boxed_ownership);
     700                 :          1 :   g_test_add_func ("/boxed/closure", test_boxed_closure);
     701                 :          1 :   g_test_add_func ("/boxed/date", test_boxed_date);
     702                 :          1 :   g_test_add_func ("/boxed/value", test_boxed_value);
     703                 :          1 :   g_test_add_func ("/boxed/string", test_boxed_string);
     704                 :          1 :   g_test_add_func ("/boxed/hashtable", test_boxed_hashtable);
     705                 :          1 :   g_test_add_func ("/boxed/array", test_boxed_array);
     706                 :          1 :   g_test_add_func ("/boxed/ptrarray", test_boxed_ptrarray);
     707                 :          1 :   g_test_add_func ("/boxed/regex", test_boxed_regex);
     708                 :          1 :   g_test_add_func ("/boxed/varianttype", test_boxed_varianttype);
     709                 :          1 :   g_test_add_func ("/boxed/error", test_boxed_error);
     710                 :          1 :   g_test_add_func ("/boxed/datetime", test_boxed_datetime);
     711                 :          1 :   g_test_add_func ("/boxed/matchinfo", test_boxed_matchinfo);
     712                 :          1 :   g_test_add_func ("/boxed/keyfile", test_boxed_keyfile);
     713                 :          1 :   g_test_add_func ("/boxed/mainloop", test_boxed_mainloop);
     714                 :          1 :   g_test_add_func ("/boxed/maincontext", test_boxed_maincontext);
     715                 :          1 :   g_test_add_func ("/boxed/source", test_boxed_source);
     716                 :          1 :   g_test_add_func ("/boxed/variantbuilder", test_boxed_variantbuilder);
     717                 :          1 :   g_test_add_func ("/boxed/timezone", test_boxed_timezone);
     718                 :          1 :   g_test_add_func ("/boxed/pollfd", test_boxed_pollfd);
     719                 :          1 :   g_test_add_func ("/boxed/markup", test_boxed_markup);
     720                 :          1 :   g_test_add_func ("/boxed/thread", test_boxed_thread);
     721                 :          1 :   g_test_add_func ("/boxed/checksum", test_boxed_checksum);
     722                 :          1 :   g_test_add_func ("/boxed/tree", test_boxed_tree);
     723                 :          1 :   g_test_add_func ("/boxed/patternspec", test_boxed_pattern_spec);
     724                 :          1 :   g_test_add_func ("/boxed/rand", test_boxed_rand);
     725                 :            : 
     726                 :          1 :   return g_test_run ();
     727                 :            : }

Generated by: LCOV version 1.14