NCBI C++ ToolKit
dlist.c
Go to the documentation of this file.

Go to the SVN repository for this file.

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 
28 #ifdef HAVE_STRING_H
29 #include <string.h>
30 #endif
31 
32 #include <freetds/test_assert.h>
33 
34 #include <freetds/utils/dlist.h>
35 
36 typedef struct
37 {
38  DLIST_FIELDS(list_item);
39  int n;
40 } test_item;
41 
42 #define DLIST_PREFIX list
43 #define DLIST_LIST_TYPE test_items
44 #define DLIST_ITEM_TYPE test_item
46 
47 int
48 main(void)
49 {
50  test_items list[1];
51  test_item items[6], *p;
52  int n;
53 
54  list_init(list);
55  memset(&items, 0, sizeof(items));
56 
57  assert(list_first(list) == NULL);
58  assert(list_last(list) == NULL);
59 
60  p = &items[0];
61  p->n = 2;
62  list_append(list, p++);
63  p->n = 3;
64  list_append(list, p++);
65  p->n = 1;
66  list_prepend(list, p++);
67 
68  assert(!list_in_list(list, p));
69  assert(list_first(list)->n == 1);
70  assert(list_last(list)->n == 3);
71 
72  /* check sequence is [1, 2, 3] */
73  n = 0;
74  DLIST_FOREACH(list, list, p) {
75  assert(p->n == ++n);
76  }
77  assert(n == 3);
78 
79  /* remove item with n == 2, sequence will be [1, 3] */
80  assert(list_in_list(list, &items[0]));
81  list_remove(list, &items[0]);
82  assert(!list_in_list(list, &items[0]));
83 
84  /* change sequence to [1, 2] */
85  items[1].n = 2;
86 
87  n = 0;
88  DLIST_FOREACH(list, list, p) {
89  assert(p->n == ++n);
90  }
91  assert(n == 2);
92 
93  return 0;
94 }
95 
#define DLIST_FOREACH(prefix, list, p)
Definition: dlist.h:35
int main(void)
Definition: dlist.c:48
#define NULL
Definition: ncbistd.hpp:225
yy_size_t n
#define assert(x)
Definition: srv_diag.hpp:58
DLIST_FIELDS(list_item)
int n
Definition: dlist.c:39
Modified on Wed May 15 15:03:38 2024 by modify_doxy.py rev. 669887