librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/id10ts.git] / lib / util / tests / dlinklist.c
blob8db0a02b88efa22f82bcf00818c2b0fde0f9ee1e
1 /*
2 Unix SMB/CIFS implementation.
4 local testing of DLIST_*() macros
6 Copyright (C) Andrew Tridgell 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "torture/torture.h"
24 #include "lib/util/dlinklist.h"
26 struct listel {
27 struct listel *next, *prev;
30 static bool torture_local_dlinklist_simple(struct torture_context *tctx)
32 TALLOC_CTX *mem_ctx = talloc_new(tctx);
33 struct listel *l1 = NULL, *l2 = NULL, *el, *el2;
34 int i;
36 torture_comment(tctx, "add 5 elements at front\n");
37 for (i=0; i<5; i++) {
38 el = talloc(mem_ctx, struct listel);
39 DLIST_ADD(l1, el);
42 torture_comment(tctx, "add 5 elements at end\n");
43 for (i=0; i<5; i++) {
44 el = talloc(mem_ctx, struct listel);
45 DLIST_ADD_END(l1, el, NULL);
48 torture_comment(tctx, "delete 3 from front\n");
49 for (i=0; i < 3; i++) {
50 el = l1;
51 DLIST_REMOVE(l1, l1);
52 DLIST_ADD(l2, el);
55 torture_comment(tctx, "delete 3 from back\n");
56 for (i=0; i < 3; i++) {
57 el = DLIST_TAIL(l1);
58 DLIST_REMOVE(l1, el);
59 DLIST_ADD_END(l2, el, NULL);
62 torture_comment(tctx, "count forward\n");
63 for (i=0,el=l1; el; el=el->next) i++;
64 torture_assert_int_equal(tctx, i, 4, "should have 4 elements");
66 torture_comment(tctx, "count backwards\n");
67 for (i=0,el=DLIST_TAIL(l1); el; el=DLIST_PREV(el)) i++;
68 torture_assert_int_equal(tctx, i, 4, "should have 4 elements");
70 torture_comment(tctx, "check DLIST_HEAD\n");
71 el = DLIST_TAIL(l1);
72 DLIST_HEAD(el, el2);
73 torture_assert(tctx, el2 == l1, "should find head");
75 torture_comment(tctx, "check DLIST_ADD_AFTER\n");
76 el = talloc(mem_ctx, struct listel);
77 el2 = talloc(mem_ctx, struct listel);
78 DLIST_ADD_AFTER(l1, el, l1);
79 DLIST_ADD_AFTER(l1, el2, el);
80 torture_assert(tctx, l1->next == el, "2nd in list");
81 torture_assert(tctx, el->next == el2, "3rd in list");
83 torture_comment(tctx, "check DLIST_PROMOTE\n");
84 DLIST_PROMOTE(l1, el2);
85 torture_assert(tctx, el2==l1, "1st in list");
86 torture_assert(tctx, el2->next->next == el, "3rd in list");
88 torture_comment(tctx, "check DLIST_DEMOTE\n");
89 DLIST_DEMOTE(l1, el, NULL);
90 torture_assert(tctx, el->next == NULL, "last in list");
91 torture_assert(tctx, el2->prev == el, "backlink from head");
93 torture_comment(tctx, "count forward\n");
94 for (i=0,el=l1; el; el=el->next) i++;
95 torture_assert_int_equal(tctx, i, 6, "should have 6 elements");
97 torture_comment(tctx, "count backwards\n");
98 for (i=0,el=DLIST_TAIL(l1); el; el=DLIST_PREV(el)) i++;
99 torture_assert_int_equal(tctx, i, 6, "should have 6 elements");
101 torture_comment(tctx, "check DLIST_CONCATENATE\n");
102 DLIST_CONCATENATE(l1, l2, NULL);
103 torture_comment(tctx, "count forward\n");
104 for (i=0,el=l1; el; el=el->next) i++;
105 torture_assert_int_equal(tctx, i, 12, "should have 12 elements");
107 torture_comment(tctx, "count backwards\n");
108 for (i=0,el=DLIST_TAIL(l1); el; el=DLIST_PREV(el)) i++;
109 torture_assert_int_equal(tctx, i, 12, "should have 12 elements");
111 torture_comment(tctx, "free forwards\n");
112 for (el=l1; el; el=el2) {
113 el2 = el->next;
114 DLIST_REMOVE(l1, el);
115 talloc_free(el);
118 torture_assert(tctx, l1 == NULL, "list empty");
119 torture_assert_int_equal(tctx, talloc_total_blocks(mem_ctx), 1, "1 block");
121 talloc_free(mem_ctx);
122 return true;
125 struct torture_suite *torture_local_dlinklist(TALLOC_CTX *mem_ctx)
127 struct torture_suite *suite = torture_suite_create(mem_ctx, "dlinklist");
128 torture_suite_add_simple_test(suite, "dlinklist", torture_local_dlinklist_simple);
129 return suite;