1 | /* $Id: tstRTList.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - List interface.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*******************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *******************************************************************************/
|
---|
31 | #include <iprt/list.h>
|
---|
32 |
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/mem.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/test.h>
|
---|
37 |
|
---|
38 |
|
---|
39 | /*******************************************************************************
|
---|
40 | * Structures and Typedefs *
|
---|
41 | *******************************************************************************/
|
---|
42 | typedef struct LISTELEM
|
---|
43 | {
|
---|
44 | /** Test data */
|
---|
45 | unsigned idx;
|
---|
46 | /** Node */
|
---|
47 | RTLISTNODE Node;
|
---|
48 | } LISTELEM, *PLISTELEM;
|
---|
49 |
|
---|
50 |
|
---|
51 | static void tstRTListOrder(RTTEST hTest, PRTLISTNODE pList, unsigned cElements,
|
---|
52 | unsigned idxFirst, unsigned idxLast, unsigned idxStep)
|
---|
53 | {
|
---|
54 | RTTEST_CHECK(hTest, RTListIsEmpty(pList) == false);
|
---|
55 | RTTEST_CHECK(hTest, RTListNodeGetFirst(pList, LISTELEM, Node) != NULL);
|
---|
56 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) != NULL);
|
---|
57 | if (cElements > 1)
|
---|
58 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) != RTListNodeGetFirst(pList, LISTELEM, Node));
|
---|
59 | else
|
---|
60 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) == RTListNodeGetFirst(pList, LISTELEM, Node));
|
---|
61 |
|
---|
62 | /* Check that the order is right. */
|
---|
63 | PLISTELEM pNode = RTListNodeGetFirst(pList, LISTELEM, Node);
|
---|
64 | for (unsigned i = idxFirst; i < idxLast; i += idxStep)
|
---|
65 | {
|
---|
66 | RTTEST_CHECK(hTest, pNode->idx == i);
|
---|
67 | pNode = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
|
---|
68 | }
|
---|
69 |
|
---|
70 | RTTEST_CHECK(hTest, pNode->idx == idxLast);
|
---|
71 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) == pNode);
|
---|
72 | RTTEST_CHECK(hTest, RTListNodeIsLast(pList, &pNode->Node) == true);
|
---|
73 |
|
---|
74 | /* Check reverse order */
|
---|
75 | pNode = RTListNodeGetLast(pList, LISTELEM, Node);
|
---|
76 | for (unsigned i = idxLast; i > idxFirst; i -= idxStep)
|
---|
77 | {
|
---|
78 | RTTEST_CHECK(hTest, pNode->idx == i);
|
---|
79 | pNode = RTListNodeGetPrev(&pNode->Node, LISTELEM, Node);
|
---|
80 | }
|
---|
81 |
|
---|
82 | RTTEST_CHECK(hTest, pNode->idx == idxFirst);
|
---|
83 | RTTEST_CHECK(hTest, RTListNodeGetFirst(pList, LISTELEM, Node) == pNode);
|
---|
84 | RTTEST_CHECK(hTest, RTListNodeIsFirst(pList, &pNode->Node) == true);
|
---|
85 |
|
---|
86 | /* The list enumeration. */
|
---|
87 | unsigned idx = idxFirst;
|
---|
88 | RTListForEach(pList, pNode, LISTELEM, Node)
|
---|
89 | {
|
---|
90 | RTTEST_CHECK_RETV(hTest, idx == pNode->idx);
|
---|
91 | idx += idxStep;
|
---|
92 | }
|
---|
93 | RTTEST_CHECK_MSG_RETV(hTest, idx == idxLast + idxStep || (idx == idxFirst && idxFirst == idxLast),
|
---|
94 | (hTest, "idx=%u idxFirst=%u idxLast=%u idxStep=%u\n", idx, idxFirst, idxLast, idxStep));
|
---|
95 |
|
---|
96 | idx = idxLast;
|
---|
97 | RTListForEachReverse(pList, pNode, LISTELEM, Node)
|
---|
98 | {
|
---|
99 | RTTEST_CHECK_RETV(hTest, idx == pNode->idx);
|
---|
100 | idx -= idxStep;
|
---|
101 | }
|
---|
102 | RTTEST_CHECK_MSG_RETV(hTest, idx == idxFirst - idxStep || (idx == idxLast && idxFirst == idxLast),
|
---|
103 | (hTest, "idx=%u idxFirst=%u idxLast idxStep=%u\n", idx, idxFirst, idxLast, idxStep));
|
---|
104 | }
|
---|
105 |
|
---|
106 | static void tstRTListCreate(RTTEST hTest, unsigned cElements)
|
---|
107 | {
|
---|
108 | RTTestISubF("RTList - Test with %u elements", cElements);
|
---|
109 |
|
---|
110 | RTLISTNODE ListHead;
|
---|
111 |
|
---|
112 | RTListInit(&ListHead);
|
---|
113 | RTTEST_CHECK(hTest, RTListIsEmpty(&ListHead) == true);
|
---|
114 | RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHead, LISTELEM, Node) == NULL);
|
---|
115 | RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHead, LISTELEM, Node) == NULL);
|
---|
116 |
|
---|
117 | /* Create the list */
|
---|
118 | for (unsigned i = 0; i< cElements; i++)
|
---|
119 | {
|
---|
120 | PLISTELEM pNode = (PLISTELEM)RTMemAlloc(sizeof(LISTELEM));
|
---|
121 |
|
---|
122 | pNode->idx = i;
|
---|
123 | pNode->Node.pPrev = NULL;
|
---|
124 | pNode->Node.pNext = NULL;
|
---|
125 | RTListAppend(&ListHead, &pNode->Node);
|
---|
126 | }
|
---|
127 |
|
---|
128 | tstRTListOrder(hTest, &ListHead, cElements, 0, cElements-1, 1);
|
---|
129 |
|
---|
130 | /* Move the list to a new one. */
|
---|
131 | RTLISTNODE ListHeadNew;
|
---|
132 |
|
---|
133 | RTListInit(&ListHeadNew);
|
---|
134 | RTListMove(&ListHeadNew, &ListHead);
|
---|
135 |
|
---|
136 | RTTEST_CHECK(hTest, RTListIsEmpty(&ListHead) == true);
|
---|
137 | RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHead, LISTELEM, Node) == NULL);
|
---|
138 | RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHead, LISTELEM, Node) == NULL);
|
---|
139 |
|
---|
140 | tstRTListOrder(hTest, &ListHeadNew, cElements, 0, cElements-1, 1);
|
---|
141 |
|
---|
142 | /* Remove elements now */
|
---|
143 | if (cElements > 1)
|
---|
144 | {
|
---|
145 | /* Remove every second */
|
---|
146 | RTTestISub("Remove every second node");
|
---|
147 |
|
---|
148 | PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
|
---|
149 | for (unsigned i = 0; i < cElements; i++)
|
---|
150 | {
|
---|
151 | PLISTELEM pNext = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
|
---|
152 |
|
---|
153 | if (!(pNode->idx % 2))
|
---|
154 | {
|
---|
155 | RTListNodeRemove(&pNode->Node);
|
---|
156 | RTMemFree(pNode);
|
---|
157 | }
|
---|
158 |
|
---|
159 | pNode = pNext;
|
---|
160 | }
|
---|
161 |
|
---|
162 | bool fElementsEven = (cElements % 2) == 0;
|
---|
163 | unsigned idxEnd = fElementsEven ? cElements - 1 : cElements - 2;
|
---|
164 |
|
---|
165 | cElements /= 2;
|
---|
166 | tstRTListOrder(hTest, &ListHeadNew, cElements, 1, idxEnd, 2);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /* Remove the rest now. */
|
---|
170 | RTTestISub("Remove all nodes");
|
---|
171 | PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
|
---|
172 | for (unsigned i = 0; i < cElements; i++)
|
---|
173 | {
|
---|
174 | PLISTELEM pNext = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
|
---|
175 |
|
---|
176 | RTListNodeRemove(&pNode->Node);
|
---|
177 | RTMemFree(pNode);
|
---|
178 | pNode = pNext;
|
---|
179 | }
|
---|
180 |
|
---|
181 | /* List should be empty again */
|
---|
182 | RTTEST_CHECK(hTest, RTListIsEmpty(&ListHeadNew) == true);
|
---|
183 | RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node) == NULL);
|
---|
184 | RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHeadNew, LISTELEM, Node) == NULL);
|
---|
185 | }
|
---|
186 |
|
---|
187 | int main()
|
---|
188 | {
|
---|
189 | RTTEST hTest;
|
---|
190 | int rc = RTTestInitAndCreate("tstRTList", &hTest);
|
---|
191 | if (rc)
|
---|
192 | return rc;
|
---|
193 | RTTestBanner(hTest);
|
---|
194 |
|
---|
195 | tstRTListCreate(hTest, 1);
|
---|
196 | tstRTListCreate(hTest, 2);
|
---|
197 | tstRTListCreate(hTest, 3);
|
---|
198 | tstRTListCreate(hTest, 99);
|
---|
199 | tstRTListCreate(hTest, 100);
|
---|
200 | tstRTListCreate(hTest, 101);
|
---|
201 |
|
---|
202 | /*
|
---|
203 | * Summary.
|
---|
204 | */
|
---|
205 | return RTTestSummaryAndDestroy(hTest);
|
---|
206 | }
|
---|
207 |
|
---|