VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTList.cpp@ 28437

Last change on this file since 28437 was 28437, checked in by vboxsync, 15 years ago

RTList: Fixed RTListNodeIsDummy and added RTListForEachReverse. Added testcases for RTListForEach and RTListForEachReverse.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.2 KB
Line 
1/* $Id: tstRTList.cpp 28437 2010-04-17 21:58:07Z 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.215389.xyz. 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*******************************************************************************/
46typedef struct LISTELEM
47{
48 /** Test data */
49 unsigned idx;
50 /** Node */
51 RTLISTNODE Node;
52} LISTELEM, *PLISTELEM;
53
54
55static void tstRTListOrder(RTTEST hTest, PRTLISTNODE pList, unsigned cElements,
56 unsigned idxFirst, unsigned idxLast, 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 = idxFirst; i < idxLast; 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 == idxLast);
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 = idxLast; i > idxFirst; 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 == idxFirst);
87 RTTEST_CHECK(hTest, RTListNodeGetFirst(pList, LISTELEM, Node) == pNode);
88 RTTEST_CHECK(hTest, RTListNodeIsFirst(pList, &pNode->Node) == true);
89
90 /* The list enumeration. */
91 unsigned idx = idxFirst;
92 RTListForEach(pList, pNode, LISTELEM, Node)
93 {
94 RTTEST_CHECK_RETV(hTest, idx == pNode->idx);
95 idx += idxStep;
96 }
97 RTTEST_CHECK_MSG_RETV(hTest, idx == idxLast + idxStep || (idx == idxFirst && idxFirst == idxLast),
98 (hTest, "idx=%u idxFirst=%u idxLast=%u idxStep=%u\n", idx, idxFirst, idxLast, idxStep));
99
100 idx = idxLast;
101 RTListForEachReverse(pList, pNode, LISTELEM, Node)
102 {
103 RTTEST_CHECK_RETV(hTest, idx == pNode->idx);
104 idx -= idxStep;
105 }
106 RTTEST_CHECK_MSG_RETV(hTest, idx == idxFirst - idxStep || (idx == idxLast && idxFirst == idxLast),
107 (hTest, "idx=%u idxFirst=%u idxLast idxStep=%u\n", idx, idxFirst, idxLast, idxStep));
108}
109
110static void tstRTListCreate(RTTEST hTest, unsigned cElements)
111{
112 RTTestISubF("RTList - Test with %u elements", cElements);
113
114 RTLISTNODE ListHead;
115
116 RTListInit(&ListHead);
117 RTTEST_CHECK(hTest, RTListIsEmpty(&ListHead) == true);
118 RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHead, LISTELEM, Node) == NULL);
119 RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHead, LISTELEM, Node) == NULL);
120
121 /* Create the list */
122 for (unsigned i = 0; i< cElements; i++)
123 {
124 PLISTELEM pNode = (PLISTELEM)RTMemAlloc(sizeof(LISTELEM));
125
126 pNode->idx = i;
127 pNode->Node.pPrev = NULL;
128 pNode->Node.pNext = NULL;
129 RTListAppend(&ListHead, &pNode->Node);
130 }
131
132 tstRTListOrder(hTest, &ListHead, cElements, 0, cElements-1, 1);
133
134 /* Move the list to a new one. */
135 RTLISTNODE ListHeadNew;
136
137 RTListInit(&ListHeadNew);
138 RTListMove(&ListHeadNew, &ListHead);
139
140 RTTEST_CHECK(hTest, RTListIsEmpty(&ListHead) == true);
141 RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHead, LISTELEM, Node) == NULL);
142 RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHead, LISTELEM, Node) == NULL);
143
144 tstRTListOrder(hTest, &ListHeadNew, cElements, 0, cElements-1, 1);
145
146 /* Remove elements now */
147 if (cElements > 1)
148 {
149 /* Remove every second */
150 RTTestISub("Remove every second node");
151
152 PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
153 for (unsigned i = 0; i < cElements; i++)
154 {
155 PLISTELEM pNext = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
156
157 if (!(pNode->idx % 2))
158 {
159 RTListNodeRemove(&pNode->Node);
160 RTMemFree(pNode);
161 }
162
163 pNode = pNext;
164 }
165
166 bool fElementsEven = (cElements % 2) == 0;
167 unsigned idxEnd = fElementsEven ? cElements - 1 : cElements - 2;
168
169 cElements /= 2;
170 tstRTListOrder(hTest, &ListHeadNew, cElements, 1, idxEnd, 2);
171 }
172
173 /* Remove the rest now. */
174 RTTestISub("Remove all nodes");
175 PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
176 for (unsigned i = 0; i < cElements; i++)
177 {
178 PLISTELEM pNext = RTListNodeGetNext(&pNode->Node, LISTELEM, Node);
179
180 RTListNodeRemove(&pNode->Node);
181 RTMemFree(pNode);
182 pNode = pNext;
183 }
184
185 /* List should be empty again */
186 RTTEST_CHECK(hTest, RTListIsEmpty(&ListHeadNew) == true);
187 RTTEST_CHECK(hTest, RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node) == NULL);
188 RTTEST_CHECK(hTest, RTListNodeGetLast(&ListHeadNew, LISTELEM, Node) == NULL);
189}
190
191int main()
192{
193 RTTEST hTest;
194 int rc = RTTestInitAndCreate("tstRTList", &hTest);
195 if (rc)
196 return rc;
197 RTTestBanner(hTest);
198
199 tstRTListCreate(hTest, 1);
200 tstRTListCreate(hTest, 2);
201 tstRTListCreate(hTest, 3);
202 tstRTListCreate(hTest, 99);
203 tstRTListCreate(hTest, 100);
204 tstRTListCreate(hTest, 101);
205
206 /*
207 * Summary.
208 */
209 return RTTestSummaryAndDestroy(hTest);
210}
211
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette