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