1 | /* $Id: tstRTList.cpp 26813 2010-02-25 22:32:48Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - List interface.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
27 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
28 | * additional information or have any questions.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include <iprt/list.h>
|
---|
36 |
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include <iprt/mem.h>
|
---|
39 | #include <iprt/string.h>
|
---|
40 | #include <iprt/test.h>
|
---|
41 |
|
---|
42 |
|
---|
43 | /*******************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *******************************************************************************/
|
---|
46 | typedef struct LISTELEM
|
---|
47 | {
|
---|
48 | /** Test data */
|
---|
49 | unsigned idx;
|
---|
50 | /** Node */
|
---|
51 | RTLISTNODE Node;
|
---|
52 | } LISTELEM, *PLISTELEM;
|
---|
53 |
|
---|
54 |
|
---|
55 | static void tstRTListOrder(RTTEST hTest, PRTLISTNODE pList, unsigned cElements,
|
---|
56 | unsigned idxStart, unsigned idxEnd, unsigned idxStep)
|
---|
57 | {
|
---|
58 | RTTEST_CHECK(hTest, RTListIsEmpty(pList) == false);
|
---|
59 | RTTEST_CHECK(hTest, RTListNodeGetFirst(pList, LISTELEM, Node) != NULL);
|
---|
60 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) != NULL);
|
---|
61 | if (cElements > 1)
|
---|
62 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) != RTListNodeGetFirst(pList, LISTELEM, Node));
|
---|
63 | else
|
---|
64 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) == RTListNodeGetFirst(pList, LISTELEM, Node));
|
---|
65 |
|
---|
66 | /* Check that the order is right. */
|
---|
67 | PLISTELEM pNode = RTListNodeGetFirst(pList, LISTELEM, Node);
|
---|
68 | for (unsigned i = idxStart; i < idxEnd; i += idxStep)
|
---|
69 | {
|
---|
70 | RTTEST_CHECK(hTest, pNode->idx == i);
|
---|
71 | pNode = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
|
---|
72 | }
|
---|
73 |
|
---|
74 | RTTEST_CHECK(hTest, pNode->idx == idxEnd);
|
---|
75 | RTTEST_CHECK(hTest, RTListNodeGetLast(pList, LISTELEM, Node) == pNode);
|
---|
76 | RTTEST_CHECK(hTest, RTListNodeIsLast(pList, &pNode->Node) == true);
|
---|
77 |
|
---|
78 | /* Check reverse order */
|
---|
79 | pNode = RTListNodeGetLast(pList, LISTELEM, Node);
|
---|
80 | for (unsigned i = idxEnd; i > idxStart; i -= idxStep)
|
---|
81 | {
|
---|
82 | RTTEST_CHECK(hTest, pNode->idx == i);
|
---|
83 | pNode = RTListNodeGetPrev(&pNode->Node, LISTELEM, Node);
|
---|
84 | }
|
---|
85 |
|
---|
86 | RTTEST_CHECK(hTest, pNode->idx == idxStart);
|
---|
87 | RTTEST_CHECK(hTest, RTListNodeGetFirst(pList, LISTELEM, Node) == pNode);
|
---|
88 | RTTEST_CHECK(hTest, RTListNodeIsFirst(pList, &pNode->Node) == true);
|
---|
89 | }
|
---|
90 |
|
---|
91 | static void tstRTListCreate(RTTEST hTest, unsigned cElements)
|
---|
92 | {
|
---|
93 | RTTestISubF("RTList - Test with %u elements", cElements);
|
---|
94 |
|
---|
95 | RTLISTNODE ListHead;
|
---|
96 |
|
---|
97 | RTListInit(&ListHead);
|
---|
98 | RTTEST_CHECK(hTest, RTListIsEmpty(&ListHead) == true);
|
---|
99 | RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHead, LISTELEM, Node) == NULL);
|
---|
100 | RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHead, LISTELEM, Node) == NULL);
|
---|
101 |
|
---|
102 | /* Create the list */
|
---|
103 | for (unsigned i = 0; i< cElements; i++)
|
---|
104 | {
|
---|
105 | PLISTELEM pNode = (PLISTELEM)RTMemAlloc(sizeof(LISTELEM));
|
---|
106 |
|
---|
107 | pNode->idx = i;
|
---|
108 | pNode->Node.pPrev = NULL;
|
---|
109 | pNode->Node.pNext = NULL;
|
---|
110 | RTListAppend(&ListHead, &pNode->Node);
|
---|
111 | }
|
---|
112 |
|
---|
113 | tstRTListOrder(hTest, &ListHead, cElements, 0, cElements-1, 1);
|
---|
114 |
|
---|
115 | /* Move the list to a new one. */
|
---|
116 | RTLISTNODE ListHeadNew;
|
---|
117 |
|
---|
118 | RTListInit(&ListHeadNew);
|
---|
119 | RTListMove(&ListHeadNew, &ListHead);
|
---|
120 |
|
---|
121 | RTTEST_CHECK(hTest, RTListIsEmpty(&ListHead) == true);
|
---|
122 | RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHead, LISTELEM, Node) == NULL);
|
---|
123 | RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHead, LISTELEM, Node) == NULL);
|
---|
124 |
|
---|
125 | tstRTListOrder(hTest, &ListHeadNew, cElements, 0, cElements-1, 1);
|
---|
126 |
|
---|
127 | /* Remove elements now */
|
---|
128 | if (cElements > 1)
|
---|
129 | {
|
---|
130 | /* Remove every second */
|
---|
131 | RTTestISub("Remove every second node");
|
---|
132 |
|
---|
133 | PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
|
---|
134 | for (unsigned i = 0; i < cElements; i++)
|
---|
135 | {
|
---|
136 | PLISTELEM pNext = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
|
---|
137 |
|
---|
138 | if (!(pNode->idx % 2))
|
---|
139 | {
|
---|
140 | RTListNodeRemove(&pNode->Node);
|
---|
141 | RTMemFree(pNode);
|
---|
142 | }
|
---|
143 |
|
---|
144 | pNode = pNext;
|
---|
145 | }
|
---|
146 |
|
---|
147 | bool fElementsEven = (cElements % 2) == 0;
|
---|
148 | unsigned idxEnd = fElementsEven ? cElements - 1 : cElements - 2;
|
---|
149 |
|
---|
150 | cElements /= 2;
|
---|
151 | tstRTListOrder(hTest, &ListHeadNew, cElements, 1, idxEnd, 2);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Remove the rest now. */
|
---|
155 | RTTestISub("Remove all nodes");
|
---|
156 | PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
|
---|
157 | for (unsigned i = 0; i < cElements; i++)
|
---|
158 | {
|
---|
159 | PLISTELEM pNext = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
|
---|
160 |
|
---|
161 | RTListNodeRemove(&pNode->Node);
|
---|
162 | RTMemFree(pNode);
|
---|
163 | pNode = pNext;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /* List should be empty again */
|
---|
167 | RTTEST_CHECK(hTest, RTListIsEmpty(&ListHeadNew) == true);
|
---|
168 | RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node) == NULL);
|
---|
169 | RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHeadNew, LISTELEM, Node) == NULL);
|
---|
170 | }
|
---|
171 |
|
---|
172 | int main()
|
---|
173 | {
|
---|
174 | RTTEST hTest;
|
---|
175 | int rc = RTTestInitAndCreate("tstRTList", &hTest);
|
---|
176 | if (rc)
|
---|
177 | return rc;
|
---|
178 | RTTestBanner(hTest);
|
---|
179 |
|
---|
180 | tstRTListCreate(hTest, 1);
|
---|
181 | tstRTListCreate(hTest, 2);
|
---|
182 | tstRTListCreate(hTest, 3);
|
---|
183 | tstRTListCreate(hTest, 99);
|
---|
184 | tstRTListCreate(hTest, 100);
|
---|
185 | tstRTListCreate(hTest, 101);
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * Summary.
|
---|
189 | */
|
---|
190 | return RTTestSummaryAndDestroy(hTest);
|
---|
191 | }
|
---|
192 |
|
---|