LCOV - code coverage report
Current view: top level - src/utils/unittests - dlist.c (source / functions) Hit Total Coverage
Test: FreeTDS coverage Lines: 24 26 92.3 %
Date: 2025-01-18 12:13:41 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* FreeTDS - Library of routines accessing Sybase and Microsoft databases
       2             :  * Copyright (C) 2016  Frediano Ziglio
       3             :  *
       4             :  * This library is free software; you can redistribute it and/or
       5             :  * modify it under the terms of the GNU Library General Public
       6             :  * License as published by the Free Software Foundation; either
       7             :  * version 2 of the License, or (at your option) any later version.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      12             :  * Library General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU Library General Public
      15             :  * License along with this library; if not, write to the
      16             :  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
      17             :  * Boston, MA 02111-1307, USA.
      18             :  */
      19             : 
      20             : /*
      21             :  * Purpose: test dlist code.
      22             :  */
      23             : #undef NDEBUG
      24             : #include <config.h>
      25             : 
      26             : #include <stdio.h>
      27             : #include <assert.h>
      28             : 
      29             : #ifdef HAVE_STRING_H
      30             : #include <string.h>
      31             : #endif
      32             : 
      33             : #include <freetds/utils/dlist.h>
      34             : 
      35             : typedef struct
      36             : {
      37             :         DLIST_FIELDS(list_item);
      38             :         int n;
      39             : } test_item;
      40             : 
      41             : #define DLIST_PREFIX list
      42             : #define DLIST_LIST_TYPE test_items
      43             : #define DLIST_ITEM_TYPE test_item
      44             : #include <freetds/utils/dlist.tmpl.h>
      45             : 
      46             : int
      47           8 : main(void)
      48             : {
      49             :         test_items list[1];
      50             :         test_item items[6], *p;
      51             :         int n;
      52             : 
      53           8 :         list_init(list);
      54           8 :         memset(&items, 0, sizeof(items));
      55             : 
      56           0 :         assert(list_first(list) == NULL);
      57           0 :         assert(list_last(list) == NULL);
      58             : 
      59           8 :         p = &items[0];
      60           8 :         p->n = 2;
      61           8 :         list_append(list, p++);
      62           8 :         p->n = 3;
      63           8 :         list_append(list, p++);
      64           8 :         p->n = 1;
      65           8 :         list_prepend(list, p++);
      66             : 
      67          16 :         assert(!list_in_list(list, p));
      68           8 :         assert(list_first(list)->n == 1);
      69           8 :         assert(list_last(list)->n == 3);
      70             : 
      71             :         /* check sequence is [1, 2, 3] */
      72             :         n = 0;
      73          56 :         DLIST_FOREACH(list, list, p) {
      74          24 :                 assert(p->n == ++n);
      75             :         }
      76           8 :         assert(n == 3);
      77             : 
      78             :         /* remove item with n == 2, sequence will be [1, 3] */
      79          16 :         assert(list_in_list(list, &items[0]));
      80           8 :         list_remove(list, &items[0]);
      81          16 :         assert(!list_in_list(list, &items[0]));
      82             : 
      83             :         /* change sequence to [1, 2] */
      84           8 :         items[1].n = 2;
      85             : 
      86           8 :         n = 0;
      87          40 :         DLIST_FOREACH(list, list, p) {
      88          16 :                 assert(p->n == ++n);
      89             :         }
      90           8 :         assert(n == 2);
      91             : 
      92             :         return 0;
      93             : }
      94             : 

Generated by: LCOV version 1.13