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/>.
23 #include "torture/torture.h"
24 #include "torture/local/proto.h"
25 #include "lib/util/dlinklist.h"
28 struct listel
*next
, *prev
;
31 static bool torture_local_dlinklist_simple(struct torture_context
*tctx
)
33 TALLOC_CTX
*mem_ctx
= talloc_new(tctx
);
34 struct listel
*l1
= NULL
, *l2
= NULL
, *el
, *el2
;
37 torture_comment(tctx
, "add 5 elements at front\n");
39 el
= talloc(mem_ctx
, struct listel
);
43 torture_comment(tctx
, "add 5 elements at end\n");
45 el
= talloc(mem_ctx
, struct listel
);
46 DLIST_ADD_END(l1
, el
);
49 torture_comment(tctx
, "delete 3 from front\n");
50 for (i
=0; i
< 3; i
++) {
56 torture_comment(tctx
, "delete 3 from back\n");
57 for (i
=0; i
< 3; i
++) {
60 DLIST_ADD_END(l2
, el
);
63 torture_comment(tctx
, "count forward\n");
64 for (i
=0,el
=l1
; el
; el
=el
->next
) i
++;
65 torture_assert_int_equal(tctx
, i
, 4, "should have 4 elements");
67 torture_comment(tctx
, "count backwards\n");
68 for (i
=0,el
=DLIST_TAIL(l1
); el
; el
=DLIST_PREV(el
)) i
++;
69 torture_assert_int_equal(tctx
, i
, 4, "should have 4 elements");
71 torture_comment(tctx
, "check DLIST_HEAD\n");
74 torture_assert(tctx
, el2
== l1
, "should find head");
76 torture_comment(tctx
, "check DLIST_ADD_AFTER\n");
77 el
= talloc(mem_ctx
, struct listel
);
78 el2
= talloc(mem_ctx
, struct listel
);
79 DLIST_ADD_AFTER(l1
, el
, l1
);
80 DLIST_ADD_AFTER(l1
, el2
, el
);
81 torture_assert(tctx
, l1
->next
== el
, "2nd in list");
82 torture_assert(tctx
, el
->next
== el2
, "3rd in list");
84 torture_comment(tctx
, "check DLIST_PROMOTE\n");
85 DLIST_PROMOTE(l1
, el2
);
86 torture_assert(tctx
, el2
==l1
, "1st in list");
87 torture_assert(tctx
, el2
->next
->next
== el
, "3rd in list");
89 torture_comment(tctx
, "check DLIST_DEMOTE\n");
91 torture_assert(tctx
, el
->next
== NULL
, "last in list");
92 torture_assert(tctx
, el2
->prev
== el
, "backlink from head");
94 torture_comment(tctx
, "count forward\n");
95 for (i
=0,el
=l1
; el
; el
=el
->next
) i
++;
96 torture_assert_int_equal(tctx
, i
, 6, "should have 6 elements");
98 torture_comment(tctx
, "count backwards\n");
99 for (i
=0,el
=DLIST_TAIL(l1
); el
; el
=DLIST_PREV(el
)) i
++;
100 torture_assert_int_equal(tctx
, i
, 6, "should have 6 elements");
102 torture_comment(tctx
, "check DLIST_CONCATENATE\n");
103 DLIST_CONCATENATE(l1
, l2
);
104 torture_comment(tctx
, "count forward\n");
105 for (i
=0,el
=l1
; el
; el
=el
->next
) i
++;
106 torture_assert_int_equal(tctx
, i
, 12, "should have 12 elements");
108 torture_comment(tctx
, "count backwards\n");
109 for (i
=0,el
=DLIST_TAIL(l1
); el
; el
=DLIST_PREV(el
)) i
++;
110 torture_assert_int_equal(tctx
, i
, 12, "should have 12 elements");
112 torture_comment(tctx
, "free forwards\n");
113 for (el
=l1
; el
; el
=el2
) {
115 DLIST_REMOVE(l1
, el
);
119 torture_assert(tctx
, l1
== NULL
, "list empty");
120 torture_assert_int_equal(tctx
, talloc_total_blocks(mem_ctx
), 1, "1 block");
122 talloc_free(mem_ctx
);
126 struct torture_suite
*torture_local_dlinklist(TALLOC_CTX
*mem_ctx
)
128 struct torture_suite
*suite
= torture_suite_create(mem_ctx
, "dlinklist");
129 torture_suite_add_simple_test(suite
, "dlinklist", torture_local_dlinklist_simple
);