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 :
24 : #include <freetds/utils/test_base.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 10 : TEST_MAIN()
47 : {
48 10 : test_items list[1] = { DLIST_INITIALIZER(list[0]) };
49 : test_item items[6], *p;
50 : int n;
51 :
52 : /* check static initialization above */
53 10 : list_check(list);
54 :
55 : /* init again */
56 10 : memset(list, 0xcc, sizeof(list));
57 10 : list_init(list);
58 10 : memset(&items, 0, sizeof(items));
59 :
60 : /* nothing in the list */
61 0 : assert(list_first(list) == NULL);
62 0 : assert(list_last(list) == NULL);
63 :
64 10 : p = &items[0];
65 10 : p->n = 2;
66 10 : list_append(list, p++);
67 10 : p->n = 3;
68 10 : list_append(list, p++);
69 10 : p->n = 1;
70 10 : list_prepend(list, p++);
71 :
72 20 : assert(!list_in_list(list, p));
73 10 : assert(list_first(list)->n == 1);
74 10 : assert(list_last(list)->n == 3);
75 :
76 : /* check sequence is [1, 2, 3] */
77 : n = 0;
78 70 : DLIST_FOREACH(list, list, p) {
79 30 : assert(p->n == ++n);
80 : }
81 10 : assert(n == 3);
82 :
83 : /* remove item with n == 2, sequence will be [1, 3] */
84 20 : assert(list_in_list(list, &items[0]));
85 10 : list_remove(list, &items[0]);
86 20 : assert(!list_in_list(list, &items[0]));
87 :
88 : /* change sequence to [1, 2] */
89 10 : items[1].n = 2;
90 :
91 10 : n = 0;
92 50 : DLIST_FOREACH(list, list, p) {
93 20 : assert(p->n == ++n);
94 : }
95 10 : assert(n == 2);
96 :
97 10 : return 0;
98 : }
99 :
|