LCOV - code coverage report
Current view: top level - glib/glib/tests - list.c (source / functions) Hit Total Coverage
Test: unnamed Lines: 368 378 97.4 %
Date: 2024-04-23 05:16:05 Functions: 25 25 100.0 %
Branches: 58 62 93.5 %

           Branch data     Line data    Source code
       1                 :            : #include <glib.h>
       2                 :            : #include <stdlib.h>
       3                 :            : 
       4                 :            : #define SIZE       50
       5                 :            : #define NUMBER_MIN 0000
       6                 :            : #define NUMBER_MAX 9999
       7                 :            : 
       8                 :            : 
       9                 :            : static guint32 array[SIZE];
      10                 :            : 
      11                 :            : 
      12                 :            : static gint
      13                 :       1802 : sort (gconstpointer p1, gconstpointer p2)
      14                 :            : {
      15                 :            :   gint32 a, b;
      16                 :            : 
      17                 :       1802 :   a = GPOINTER_TO_INT (p1);
      18                 :       1802 :   b = GPOINTER_TO_INT (p2);
      19                 :            : 
      20   [ +  +  -  + ]:       1802 :   return (a > b ? +1 : a == b ? 0 : -1);
      21                 :            : }
      22                 :            : 
      23                 :            : /*
      24                 :            :  * glist sort tests
      25                 :            :  */
      26                 :            : static void
      27                 :          1 : test_list_sort (void)
      28                 :            : {
      29                 :          1 :   GList *list = NULL;
      30                 :            :   gint   i;
      31                 :            : 
      32         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
      33                 :         50 :     list = g_list_append (list, GINT_TO_POINTER (array[i]));
      34                 :            : 
      35                 :          1 :   list = g_list_sort (list, sort);
      36         [ +  + ]:         50 :   for (i = 0; i < SIZE - 1; i++)
      37                 :            :     {
      38                 :            :       gpointer p1, p2;
      39                 :            : 
      40                 :         49 :       p1 = g_list_nth_data (list, i);
      41                 :         49 :       p2 = g_list_nth_data (list, i+1);
      42                 :            : 
      43                 :         49 :       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
      44                 :            :     }
      45                 :            : 
      46                 :          1 :   g_list_free (list);
      47                 :          1 : }
      48                 :            : 
      49                 :            : static void
      50                 :          1 : test_list_sort_with_data (void)
      51                 :            : {
      52                 :          1 :   GList *list = NULL;
      53                 :            :   gint   i;
      54                 :            : 
      55         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
      56                 :         50 :     list = g_list_append (list, GINT_TO_POINTER (array[i]));
      57                 :            : 
      58                 :          1 :   list = g_list_sort_with_data (list, (GCompareDataFunc)sort, NULL);
      59         [ +  + ]:         50 :   for (i = 0; i < SIZE - 1; i++)
      60                 :            :     {
      61                 :            :       gpointer p1, p2;
      62                 :            : 
      63                 :         49 :       p1 = g_list_nth_data (list, i);
      64                 :         49 :       p2 = g_list_nth_data (list, i+1);
      65                 :            : 
      66                 :         49 :       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
      67                 :            :     }
      68                 :            : 
      69                 :          1 :   g_list_free (list);
      70                 :          1 : }
      71                 :            : 
      72                 :            : /* Test that the sort is stable. */
      73                 :            : static void
      74                 :          1 : test_list_sort_stable (void)
      75                 :            : {
      76                 :          1 :   GList *list = NULL;  /* (element-type utf8) */
      77                 :          1 :   GList *copy = NULL;  /* (element-type utf8) */
      78                 :            :   gsize i;
      79                 :            : 
      80                 :            :   /* Build a test list, already ordered. */
      81         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
      82                 :         50 :     list = g_list_append (list, g_strdup_printf ("%" G_GSIZE_FORMAT, i / 5));
      83                 :            : 
      84                 :            :   /* Take a copy and sort it. */
      85                 :          1 :   copy = g_list_copy (list);
      86                 :          1 :   copy = g_list_sort (copy, (GCompareFunc) g_strcmp0);
      87                 :            : 
      88                 :            :   /* Compare the two lists, checking pointers are equal to ensure the elements
      89                 :            :    * have been kept stable. */
      90         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
      91                 :            :     {
      92                 :            :       gpointer p1, p2;
      93                 :            : 
      94                 :         50 :       p1 = g_list_nth_data (list, i);
      95                 :         50 :       p2 = g_list_nth_data (list, i);
      96                 :            : 
      97                 :         50 :       g_assert (p1 == p2);
      98                 :            :     }
      99                 :            : 
     100                 :          1 :   g_list_free (copy);
     101                 :          1 :   g_list_free_full (list, g_free);
     102                 :          1 : }
     103                 :            : 
     104                 :            : static void
     105                 :          1 : test_list_insert_sorted (void)
     106                 :            : {
     107                 :          1 :   GList *list = NULL;
     108                 :            :   gint   i;
     109                 :            : 
     110         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
     111                 :         50 :     list = g_list_insert_sorted (list, GINT_TO_POINTER (array[i]), sort);
     112                 :            : 
     113         [ +  + ]:         50 :   for (i = 0; i < SIZE - 1; i++)
     114                 :            :     {
     115                 :            :       gpointer p1, p2;
     116                 :            : 
     117                 :         49 :       p1 = g_list_nth_data (list, i);
     118                 :         49 :       p2 = g_list_nth_data (list, i+1);
     119                 :            : 
     120                 :         49 :       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
     121                 :            :     }
     122                 :            : 
     123                 :          1 :   g_list_free (list);
     124                 :          1 : }
     125                 :            : 
     126                 :            : static void
     127                 :          1 : test_list_insert_sorted_with_data (void)
     128                 :            : {
     129                 :          1 :   GList *list = NULL;
     130                 :            :   gint   i;
     131                 :            : 
     132         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
     133                 :         50 :     list = g_list_insert_sorted_with_data (list,
     134                 :         50 :                                            GINT_TO_POINTER (array[i]),
     135                 :            :                                            (GCompareDataFunc)sort,
     136                 :            :                                            NULL);
     137                 :            : 
     138         [ +  + ]:         50 :   for (i = 0; i < SIZE - 1; i++)
     139                 :            :     {
     140                 :            :       gpointer p1, p2;
     141                 :            : 
     142                 :         49 :       p1 = g_list_nth_data (list, i);
     143                 :         49 :       p2 = g_list_nth_data (list, i+1);
     144                 :            : 
     145                 :         49 :       g_assert (GPOINTER_TO_INT (p1) <= GPOINTER_TO_INT (p2));
     146                 :            :     }
     147                 :            : 
     148                 :          1 :   g_list_free (list);
     149                 :          1 : }
     150                 :            : 
     151                 :            : static void
     152                 :          1 : test_list_reverse (void)
     153                 :            : {
     154                 :          1 :   GList *list = NULL;
     155                 :            :   GList *st;
     156                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     157                 :            :   gint   i;
     158                 :            : 
     159         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     160                 :         10 :     list = g_list_append (list, &nums[i]);
     161                 :            : 
     162                 :          1 :   list = g_list_reverse (list);
     163                 :            : 
     164         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     165                 :            :     {
     166                 :         10 :       st = g_list_nth (list, i);
     167                 :         10 :       g_assert (*((gint*) st->data) == (9 - i));
     168                 :            :     }
     169                 :            : 
     170                 :          1 :   g_list_free (list);
     171                 :          1 : }
     172                 :            : 
     173                 :            : static void
     174                 :          1 : test_list_nth (void)
     175                 :            : {
     176                 :          1 :   GList *list = NULL;
     177                 :            :   GList *st;
     178                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     179                 :            :   gint   i;
     180                 :            : 
     181         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     182                 :         10 :     list = g_list_append (list, &nums[i]);
     183                 :            : 
     184         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     185                 :            :     {
     186                 :         10 :       st = g_list_nth (list, i);
     187                 :         10 :       g_assert (*((gint*) st->data) == i);
     188                 :            :     }
     189                 :            : 
     190                 :          1 :   g_list_free (list);
     191                 :          1 : }
     192                 :            : 
     193                 :            : static void
     194                 :          1 : test_list_concat (void)
     195                 :            : {
     196                 :          1 :   GList *list1 = NULL;
     197                 :          1 :   GList *list2 = NULL;
     198                 :            :   GList *st;
     199                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     200                 :            :   gint i;
     201                 :            : 
     202         [ +  + ]:          6 :   for (i = 0; i < 5; i++)
     203                 :            :     {
     204                 :          5 :       list1 = g_list_append (list1, &nums[i]);
     205                 :          5 :       list2 = g_list_append (list2, &nums[i+5]);
     206                 :            :     }
     207                 :            : 
     208                 :          1 :   g_assert_cmpint (g_list_length (list1), ==, 5);
     209                 :          1 :   g_assert_cmpint (g_list_length (list2), ==, 5);
     210                 :            : 
     211                 :          1 :   list1 = g_list_concat (list1, list2);
     212                 :            : 
     213                 :          1 :   g_assert_cmpint (g_list_length (list1), ==, 10);
     214                 :            : 
     215         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     216                 :            :     {
     217                 :         10 :       st = g_list_nth (list1, i);
     218                 :         10 :       g_assert (*((gint*) st->data) == i);
     219                 :            :     }
     220                 :            : 
     221                 :          1 :   list2 = g_list_concat (NULL, list1);
     222                 :          1 :   g_assert_cmpint (g_list_length (list2), ==, 10);
     223                 :            : 
     224                 :          1 :   list2 = g_list_concat (list1, NULL);
     225                 :          1 :   g_assert_cmpint (g_list_length (list2), ==, 10);
     226                 :            : 
     227                 :          1 :   list2 = g_list_concat (NULL, NULL);
     228                 :          1 :   g_assert (list2 == NULL);
     229                 :            : 
     230                 :          1 :   g_list_free (list1);
     231                 :          1 : }
     232                 :            : 
     233                 :            : static void
     234                 :          1 : test_list_remove (void)
     235                 :            : {
     236                 :          1 :   GList *list = NULL;
     237                 :            :   GList *st;
     238                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     239                 :            :   gint   i;
     240                 :            : 
     241         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     242                 :            :     {
     243                 :         10 :       list = g_list_append (list, &nums[i]);
     244                 :         10 :       list = g_list_append (list, &nums[i]);
     245                 :            :     }
     246                 :            : 
     247                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 20);
     248                 :            : 
     249         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     250                 :            :     {
     251                 :         10 :       list = g_list_remove (list, &nums[i]);
     252                 :            :     }
     253                 :            : 
     254                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 10);
     255                 :            : 
     256         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     257                 :            :     {
     258                 :         10 :       st = g_list_nth (list, i);
     259                 :         10 :       g_assert (*((gint*) st->data) == i);
     260                 :            :     }
     261                 :            : 
     262                 :          1 :   g_list_free (list);
     263                 :          1 : }
     264                 :            : 
     265                 :            : static void
     266                 :          1 : test_list_remove_all (void)
     267                 :            : {
     268                 :          1 :   GList *list = NULL;
     269                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     270                 :            :   gint   i;
     271                 :            : 
     272         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     273                 :            :     {
     274                 :         10 :       list = g_list_append (list, &nums[i]);
     275                 :         10 :       list = g_list_append (list, &nums[i]);
     276                 :            :     }
     277                 :            : 
     278                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 20);
     279                 :            : 
     280         [ +  + ]:          6 :   for (i = 0; i < 5; i++)
     281                 :            :     {
     282                 :          5 :       list = g_list_remove_all (list, &nums[2 * i + 1]);
     283                 :          5 :       list = g_list_remove_all (list, &nums[8 - 2 * i]);
     284                 :            :     }
     285                 :            : 
     286                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 0);
     287                 :          1 :   g_assert (list == NULL);
     288                 :          1 : }
     289                 :            : 
     290                 :            : static void
     291                 :          1 : test_list_first_last (void)
     292                 :            : {
     293                 :          1 :   GList *list = NULL;
     294                 :            :   GList *st;
     295                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     296                 :            :   gint   i;
     297                 :            : 
     298         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     299                 :         10 :     list = g_list_append (list, &nums[i]);
     300                 :            : 
     301                 :          1 :   st = g_list_last (list);
     302                 :          1 :   g_assert (*((gint*) st->data) == 9);
     303                 :          1 :   st = g_list_nth_prev (st, 3);
     304                 :          1 :   g_assert (*((gint*) st->data) == 6);
     305                 :          1 :   st = g_list_first (st);
     306                 :          1 :   g_assert (*((gint*) st->data) == 0);
     307                 :            : 
     308                 :          1 :   g_list_free (list);
     309                 :          1 : }
     310                 :            : 
     311                 :            : static void
     312                 :          1 : test_list_insert (void)
     313                 :            : {
     314                 :          1 :   GList *list = NULL;
     315                 :            :   GList *st;
     316                 :          1 :   gint   nums[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
     317                 :            :   gint   i;
     318                 :            : 
     319                 :          1 :   list = g_list_insert_before (NULL, NULL, &nums[1]);
     320                 :          1 :   list = g_list_insert (list, &nums[3], 1);
     321                 :          1 :   list = g_list_insert (list, &nums[4], -1);
     322                 :          1 :   list = g_list_insert (list, &nums[0], 0);
     323                 :          1 :   list = g_list_insert (list, &nums[5], 100);
     324                 :          1 :   list = g_list_insert_before (list, NULL, &nums[6]);
     325                 :          1 :   list = g_list_insert_before (list, list->next->next, &nums[2]);
     326                 :            : 
     327                 :          1 :   list = g_list_insert (list, &nums[9], 7);
     328                 :          1 :   list = g_list_insert (list, &nums[8], 7);
     329                 :          1 :   list = g_list_insert (list, &nums[7], 7);
     330                 :            : 
     331         [ +  + ]:         11 :   for (i = 0; i < 10; i++)
     332                 :            :     {
     333                 :         10 :       st = g_list_nth (list, i);
     334                 :         10 :       g_assert (*((gint*) st->data) == i);
     335                 :            :     }
     336                 :            : 
     337                 :          1 :   g_list_free (list);
     338                 :          1 : }
     339                 :            : 
     340                 :            : typedef struct
     341                 :            : {
     342                 :            :   gboolean freed;
     343                 :            :   int x;
     344                 :            : } ListItem;
     345                 :            : 
     346                 :            : static void
     347                 :          6 : free_func (gpointer data)
     348                 :            : {
     349                 :          6 :   ListItem *item = data;
     350                 :            : 
     351                 :          6 :   item->freed = TRUE;
     352                 :          6 : }
     353                 :            : 
     354                 :            : static ListItem *
     355                 :          6 : new_item (int x)
     356                 :            : {
     357                 :            :   ListItem *item;
     358                 :            : 
     359                 :          6 :   item = g_slice_new (ListItem);
     360                 :          6 :   item->freed = FALSE;
     361                 :          6 :   item->x = x;
     362                 :            : 
     363                 :          6 :   return item;
     364                 :            : }
     365                 :            : 
     366                 :            : static void
     367                 :          1 : test_free_full (void)
     368                 :            : {
     369                 :            :   ListItem *one, *two, *three;
     370                 :          1 :   GSList *slist = NULL;
     371                 :          1 :   GList *list = NULL;
     372                 :            : 
     373                 :          1 :   slist = g_slist_prepend (slist, one = new_item (1));
     374                 :          1 :   slist = g_slist_prepend (slist, two = new_item (2));
     375                 :          1 :   slist = g_slist_prepend (slist, three = new_item (3));
     376                 :          1 :   g_assert (!one->freed);
     377                 :          1 :   g_assert (!two->freed);
     378                 :          1 :   g_assert (!three->freed);
     379                 :          1 :   g_slist_free_full (slist, free_func);
     380                 :          1 :   g_assert (one->freed);
     381                 :          1 :   g_assert (two->freed);
     382                 :          1 :   g_assert (three->freed);
     383                 :          1 :   g_slice_free (ListItem, one);
     384                 :          1 :   g_slice_free (ListItem, two);
     385                 :          1 :   g_slice_free (ListItem, three);
     386                 :            : 
     387                 :          1 :   list = g_list_prepend (list, one = new_item (1));
     388                 :          1 :   list = g_list_prepend (list, two = new_item (2));
     389                 :          1 :   list = g_list_prepend (list, three = new_item (3));
     390                 :          1 :   g_assert (!one->freed);
     391                 :          1 :   g_assert (!two->freed);
     392                 :          1 :   g_assert (!three->freed);
     393                 :          1 :   g_list_free_full (list, free_func);
     394                 :          1 :   g_assert (one->freed);
     395                 :          1 :   g_assert (two->freed);
     396                 :          1 :   g_assert (three->freed);
     397                 :          1 :   g_slice_free (ListItem, one);
     398                 :          1 :   g_slice_free (ListItem, two);
     399                 :          1 :   g_slice_free (ListItem, three);
     400                 :          1 : }
     401                 :            : 
     402                 :            : static void
     403                 :          1 : test_list_copy (void)
     404                 :            : {
     405                 :            :   GList *l, *l2;
     406                 :            :   GList *u, *v;
     407                 :            : 
     408                 :          1 :   l = NULL;
     409                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (1));
     410                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (2));
     411                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (3));
     412                 :            : 
     413                 :          1 :   l2 = g_list_copy (l);
     414                 :            : 
     415   [ +  +  +  - ]:          4 :   for (u = l, v = l2; u && v; u = u->next, v = v->next)
     416                 :            :     {
     417                 :          3 :       g_assert (u->data == v->data);
     418                 :            :     }
     419                 :            : 
     420                 :          1 :   g_list_free (l);
     421                 :          1 :   g_list_free (l2);
     422                 :          1 : }
     423                 :            : 
     424                 :            : static gpointer
     425                 :          3 : multiply_value (gconstpointer value, gpointer data)
     426                 :            : {
     427                 :          3 :   return GINT_TO_POINTER (GPOINTER_TO_INT (value) * GPOINTER_TO_INT (data));
     428                 :            : }
     429                 :            : 
     430                 :            : static void
     431                 :          1 : test_list_copy_deep (void)
     432                 :            : {
     433                 :            :   GList *l, *l2;
     434                 :            :   GList *u, *v;
     435                 :            : 
     436                 :          1 :   l = NULL;
     437                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (1));
     438                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (2));
     439                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (3));
     440                 :            : 
     441                 :          1 :   l2 = g_list_copy_deep (l, multiply_value, GINT_TO_POINTER (2));
     442                 :            : 
     443   [ +  +  +  - ]:          4 :   for (u = l, v = l2; u && v; u = u->next, v = v->next)
     444                 :            :     {
     445                 :          3 :       g_assert_cmpint (GPOINTER_TO_INT (u->data) * 2, ==, GPOINTER_TO_INT (v->data));
     446                 :            :     }
     447                 :            : 
     448                 :          1 :   g_list_free (l);
     449                 :          1 :   g_list_free (l2);
     450                 :          1 : }
     451                 :            : 
     452                 :            : static void
     453                 :          1 : test_delete_link (void)
     454                 :            : {
     455                 :            :   GList *l, *l2;
     456                 :            : 
     457                 :          1 :   l = NULL;
     458                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (1));
     459                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (2));
     460                 :          1 :   l = g_list_append (l, GINT_TO_POINTER (3));
     461                 :            : 
     462                 :          1 :   l2 = l->next;
     463                 :            : 
     464                 :          1 :   l = g_list_delete_link (l, l2);
     465                 :          1 :   g_assert (l->data == GINT_TO_POINTER (1));
     466                 :          1 :   g_assert (l->next->data == GINT_TO_POINTER (3));
     467                 :            : 
     468                 :          1 :   g_list_free (l);
     469                 :          1 : }
     470                 :            : 
     471                 :            : static void
     472                 :          1 : test_prepend (void)
     473                 :            : {
     474                 :          1 :   gpointer a = "a";
     475                 :          1 :   gpointer b = "b";
     476                 :          1 :   gpointer c = "c";
     477                 :            :   GList *l, *l2;
     478                 :            : 
     479                 :          1 :   l = NULL;
     480                 :          1 :   l = g_list_prepend (l, c);
     481                 :          1 :   l = g_list_prepend (l, a);
     482                 :            : 
     483                 :          1 :   g_assert (l->data == a);
     484                 :          1 :   g_assert (l->next->data == c);
     485                 :          1 :   g_assert (l->next->next == NULL);
     486                 :            : 
     487                 :          1 :   l2 = l->next;
     488                 :          1 :   l2 = g_list_prepend (l2, b);
     489                 :          1 :   g_assert (l2->prev == l);
     490                 :            : 
     491                 :          1 :   g_assert (l->data == a);
     492                 :          1 :   g_assert (l->next->data == b);
     493                 :          1 :   g_assert (l->next->next->data == c);
     494                 :          1 :   g_assert (l->next->next->next == NULL);
     495                 :            : 
     496                 :          1 :   g_list_free (l);
     497                 :          1 : }
     498                 :            : 
     499                 :            : static void
     500                 :          1 : test_position (void)
     501                 :            : {
     502                 :            :   GList *l, *ll;
     503                 :          1 :   char *a = "a";
     504                 :          1 :   char *b = "b";
     505                 :          1 :   char *c = "c";
     506                 :          1 :   char *d = "d";
     507                 :            : 
     508                 :          1 :   l = NULL;
     509                 :          1 :   l = g_list_append (l, a);
     510                 :          1 :   l = g_list_append (l, b);
     511                 :          1 :   l = g_list_append (l, c);
     512                 :            : 
     513                 :          1 :   ll = g_list_find (l, a);
     514                 :          1 :   g_assert_cmpint (g_list_position (l, ll), ==, 0);
     515                 :          1 :   g_assert_cmpint (g_list_index (l, a), ==, 0);
     516                 :          1 :   ll = g_list_find (l, b);
     517                 :          1 :   g_assert_cmpint (g_list_position (l, ll), ==, 1);
     518                 :          1 :   g_assert_cmpint (g_list_index (l, b), ==, 1);
     519                 :          1 :   ll = g_list_find (l, c);
     520                 :          1 :   g_assert_cmpint (g_list_position (l, ll), ==, 2);
     521                 :          1 :   g_assert_cmpint (g_list_index (l, c), ==, 2);
     522                 :            : 
     523                 :          1 :   ll = g_list_append (NULL, d);
     524                 :          1 :   g_assert_cmpint (g_list_position (l, ll), ==, -1);
     525                 :          1 :   g_assert_cmpint (g_list_index (l, d), ==, -1);
     526                 :            : 
     527                 :          1 :   g_list_free (l);
     528                 :          1 :   g_list_free (ll);
     529                 :          1 : }
     530                 :            : 
     531                 :            : static void
     532                 :          1 : test_double_free (void)
     533                 :            : {
     534                 :            :   GList *list, *link;
     535                 :            :   // Casts to size_t first ensure compilers won't warn about pointer casts that change size
     536                 :            :   // MSVC's C4312 warning with /Wp64
     537                 :          1 :   GList  intruder = { NULL, (gpointer)(size_t)0xDEADBEEF, (gpointer)(size_t)0xDEADBEEF };
     538                 :            : 
     539         [ -  + ]:          1 :   if (g_test_subprocess ())
     540                 :            :     {
     541                 :          0 :       list = NULL;
     542                 :          0 :       list = g_list_append (list, "a");
     543                 :          0 :       link = list = g_list_append (list, "b");
     544                 :          0 :       list = g_list_append (list, "c");
     545                 :            : 
     546                 :          0 :       list = g_list_remove_link (list, link);
     547                 :          0 :       link->prev = list;
     548                 :          0 :       link->next = &intruder;
     549                 :          0 :       list = g_list_remove_link (list, link);
     550                 :            : 
     551                 :          0 :       g_list_free (list);
     552                 :          0 :       return;
     553                 :            :     }
     554                 :            : 
     555                 :          1 :   g_test_trap_subprocess (NULL, 0, G_TEST_SUBPROCESS_DEFAULT);
     556                 :          1 :   g_test_trap_assert_failed ();
     557                 :          1 :   g_test_trap_assert_stderr ("*corrupted double-linked list detected*");
     558                 :            : }
     559                 :            : 
     560                 :            : static void
     561                 :          1 : test_list_insert_before_link (void)
     562                 :            : {
     563                 :          1 :   GList a = {0};
     564                 :          1 :   GList b = {0};
     565                 :          1 :   GList c = {0};
     566                 :          1 :   GList d = {0};
     567                 :          1 :   GList e = {0};
     568                 :            :   GList *list;
     569                 :            : 
     570                 :          1 :   list = g_list_insert_before_link (NULL, NULL, &a);
     571                 :          1 :   g_assert_nonnull (list);
     572                 :          1 :   g_assert_true (list == &a);
     573                 :          1 :   g_assert_null (a.prev);
     574                 :          1 :   g_assert_null (a.next);
     575                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 1);
     576                 :            : 
     577                 :          1 :   list = g_list_insert_before_link (list, &a, &b);
     578                 :          1 :   g_assert_nonnull (list);
     579                 :          1 :   g_assert_true (list == &b);
     580                 :          1 :   g_assert_null (b.prev);
     581                 :          1 :   g_assert_true (b.next == &a);
     582                 :          1 :   g_assert_true (a.prev == &b);
     583                 :          1 :   g_assert_null (a.next);
     584                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 2);
     585                 :            : 
     586                 :          1 :   list = g_list_insert_before_link (list, &a, &c);
     587                 :          1 :   g_assert_nonnull (list);
     588                 :          1 :   g_assert_true (list == &b);
     589                 :          1 :   g_assert_null (b.prev);
     590                 :          1 :   g_assert_true (b.next == &c);
     591                 :          1 :   g_assert_true (c.next == &a);
     592                 :          1 :   g_assert_true (c.prev == &b);
     593                 :          1 :   g_assert_true (a.prev == &c);
     594                 :          1 :   g_assert_null (a.next);
     595                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 3);
     596                 :            : 
     597                 :          1 :   list = g_list_insert_before_link (list, &b, &d);
     598                 :          1 :   g_assert_nonnull (list);
     599                 :          1 :   g_assert_true (list == &d);
     600                 :          1 :   g_assert_null (d.prev);
     601                 :          1 :   g_assert_true (b.prev == &d);
     602                 :          1 :   g_assert_true (c.prev == &b);
     603                 :          1 :   g_assert_true (a.prev == &c);
     604                 :          1 :   g_assert_true (d.next == &b);
     605                 :          1 :   g_assert_true (b.next == &c);
     606                 :          1 :   g_assert_true (c.next == &a);
     607                 :          1 :   g_assert_null (a.next);
     608                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 4);
     609                 :            : 
     610                 :          1 :   list = g_list_insert_before_link (list, NULL, &e);
     611                 :          1 :   g_assert_nonnull (list);
     612                 :          1 :   g_assert_true (list == &d);
     613                 :          1 :   g_assert_null (d.prev);
     614                 :          1 :   g_assert_true (b.prev == &d);
     615                 :          1 :   g_assert_true (c.prev == &b);
     616                 :          1 :   g_assert_true (a.prev == &c);
     617                 :          1 :   g_assert_true (d.next == &b);
     618                 :          1 :   g_assert_true (b.next == &c);
     619                 :          1 :   g_assert_true (c.next == &a);
     620                 :          1 :   g_assert_true (a.next == &e);
     621                 :          1 :   g_assert_true (e.prev == &a);
     622                 :          1 :   g_assert_null (e.next);
     623                 :          1 :   g_assert_cmpint (g_list_length (list), ==, 5);
     624                 :          1 : }
     625                 :            : 
     626                 :            : int
     627                 :          1 : main (int argc, char *argv[])
     628                 :            : {
     629                 :            :   gint i;
     630                 :            : 
     631                 :          1 :   g_test_init (&argc, &argv, NULL);
     632                 :            : 
     633                 :            :   /* Create an array of random numbers. */
     634         [ +  + ]:         51 :   for (i = 0; i < SIZE; i++)
     635                 :         50 :     array[i] = g_test_rand_int_range (NUMBER_MIN, NUMBER_MAX);
     636                 :            : 
     637                 :          1 :   g_test_add_func ("/list/sort", test_list_sort);
     638                 :          1 :   g_test_add_func ("/list/sort-with-data", test_list_sort_with_data);
     639                 :          1 :   g_test_add_func ("/list/sort/stable", test_list_sort_stable);
     640                 :          1 :   g_test_add_func ("/list/insert-before-link", test_list_insert_before_link);
     641                 :          1 :   g_test_add_func ("/list/insert-sorted", test_list_insert_sorted);
     642                 :          1 :   g_test_add_func ("/list/insert-sorted-with-data", test_list_insert_sorted_with_data);
     643                 :          1 :   g_test_add_func ("/list/reverse", test_list_reverse);
     644                 :          1 :   g_test_add_func ("/list/nth", test_list_nth);
     645                 :          1 :   g_test_add_func ("/list/concat", test_list_concat);
     646                 :          1 :   g_test_add_func ("/list/remove", test_list_remove);
     647                 :          1 :   g_test_add_func ("/list/remove-all", test_list_remove_all);
     648                 :          1 :   g_test_add_func ("/list/first-last", test_list_first_last);
     649                 :          1 :   g_test_add_func ("/list/insert", test_list_insert);
     650                 :          1 :   g_test_add_func ("/list/free-full", test_free_full);
     651                 :          1 :   g_test_add_func ("/list/copy", test_list_copy);
     652                 :          1 :   g_test_add_func ("/list/copy-deep", test_list_copy_deep);
     653                 :          1 :   g_test_add_func ("/list/delete-link", test_delete_link);
     654                 :          1 :   g_test_add_func ("/list/prepend", test_prepend);
     655                 :          1 :   g_test_add_func ("/list/position", test_position);
     656                 :          1 :   g_test_add_func ("/list/double-free", test_double_free);
     657                 :            : 
     658                 :          1 :   return g_test_run ();
     659                 :            : }

Generated by: LCOV version 1.14