VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstVector.cpp@ 51230

Last change on this file since 51230 was 37829, checked in by vboxsync, 13 years ago

header fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.0 KB
Line 
1/* $Id: tstVector.cpp 37829 2011-07-08 08:35:47Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Vector container structure.
4 */
5
6/*
7 * Copyright (C) 2011 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* Header Files *
29*******************************************************************************/
30#include <iprt/test.h>
31#include <iprt/vector.h>
32
33#include <stdlib.h> /* For realloc */
34
35/** Counter of the number of delete calls made so far */
36static unsigned s_cDeleteCalls = 0;
37
38/** Record the argument of the delete function here. */
39static void *s_apvDeleteArg[10];
40
41/** Dummy delete function for vector-of-void pointer elements */
42static void deletePVoid(void **ppv)
43{
44 if (s_cDeleteCalls < RT_ELEMENTS(s_apvDeleteArg))
45 s_apvDeleteArg[s_cDeleteCalls] = *ppv;
46 ++s_cDeleteCalls;
47}
48
49/** Dummy delete by value function for vector-of-void pointer elements */
50static void deletePVoidValue(void *pv)
51{
52 if (s_cDeleteCalls < RT_ELEMENTS(s_apvDeleteArg))
53 s_apvDeleteArg[s_cDeleteCalls] = pv;
54 ++s_cDeleteCalls;
55}
56
57/* Start by instantiating each function once for syntax checking */
58RTVEC_DECL_STRUCT(tstInstance, void *)
59RTVEC_DECL_STRUCT(tstInstance2, void *)
60
61RTVEC_DECLFN_DELETE_ADAPTER_ID(tstInstance, void *)
62RTVEC_DECLFN_DELETE_ADAPTER_TO_VALUE(tstInstance, void *)
63
64RTVEC_DECLFN_SIZE(tstInstance, void *)
65RTVEC_DECLFN_RESERVE(tstInstance, void *, rtvecReallocDefTag)
66RTVEC_DECLFN_BEGIN(tstInstance, void *)
67RTVEC_DECLFN_END(tstInstance, void *)
68RTVEC_DECLFN_PUSHBACK(tstInstance, void *)
69RTVEC_DECLFN_POPBACK(tstInstance)
70RTVEC_DECLFN_POPBACK_DELETE(tstInstance2, void *, deletePVoid,
71 tstInstanceDeleteAdapterId)
72RTVEC_DECLFN_CLEAR(tstInstance)
73RTVEC_DECLFN_CLEAR_DELETE(tstInstance2, deletePVoid,
74 tstInstanceDeleteAdapterId)
75RTVEC_DECLFN_DETACH(tstInstance, void *)
76
77RTVEC_DECL(tstSimple, void *)
78
79static void testVectorSimple(RTTEST hTest)
80{
81 RTTestISub("Vector structure, no cleanup callback");
82
83 struct tstSimple myVec = RTVEC_INITIALIZER;
84 void **ppvVal;
85
86 RTTESTI_CHECK(tstSimpleSize(&myVec) == 0);
87
88 ppvVal = tstSimplePushBack(&myVec);
89 /* AssertPtrReturnVoid(ppvVal); */
90 RTTESTI_CHECK(ppvVal == tstSimpleBegin(&myVec));
91 RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
92 RTTESTI_CHECK(tstSimpleSize(&myVec) == 1);
93 *ppvVal = (void *)1;
94
95 ppvVal = tstSimplePushBack(&myVec);
96 /* AssertPtrReturnVoid(ppvVal); */
97 RTTESTI_CHECK(ppvVal - 1 == tstSimpleBegin(&myVec));
98 RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
99 RTTESTI_CHECK(tstSimpleSize(&myVec) == 2);
100 RTTESTI_CHECK(ppvVal[-1] == (void *)1);
101
102 *ppvVal = (void *)3;
103 ppvVal = tstSimplePushBack(&myVec);
104 /* AssertPtrReturnVoid(ppvVal); */
105 RTTESTI_CHECK(ppvVal - 2 == tstSimpleBegin(&myVec));
106 RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
107 RTTESTI_CHECK(tstSimpleSize(&myVec) == 3);
108 RTTESTI_CHECK(ppvVal[-2] == (void *)1);
109 RTTESTI_CHECK(ppvVal[-1] == (void *)3);
110
111 tstSimplePopBack(&myVec);
112 RTTESTI_CHECK(tstSimpleBegin(&myVec) + 2 == tstSimpleEnd(&myVec));
113 RTTESTI_CHECK(*tstSimpleBegin(&myVec) == (void *)1);
114 RTTESTI_CHECK(*(tstSimpleEnd(&myVec) - 1) == (void *)3);
115
116 tstSimpleClear(&myVec);
117 RTTESTI_CHECK(tstSimpleBegin(&myVec) == tstSimpleEnd(&myVec));
118 ppvVal = tstSimplePushBack(&myVec);
119 /* AssertPtrReturnVoid(ppvVal); */
120 RTTESTI_CHECK(ppvVal == tstSimpleBegin(&myVec));
121 RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
122
123 tstSimpleClear(&myVec);
124 ppvVal = tstSimplePushBack(&myVec);
125 /* AssertPtrReturnVoid(ppvVal); */
126 *ppvVal = (void *)1;
127 ppvVal = tstSimplePushBack(&myVec);
128 /* AssertPtrReturnVoid(ppvVal); */
129 *ppvVal = (void *)3;
130 ppvVal = tstSimplePushBack(&myVec);
131 /* AssertPtrReturnVoid(ppvVal); */
132 *ppvVal = (void *)2;
133 ppvVal = tstSimpleDetach(&myVec);
134 RTTESTI_CHECK(tstSimpleBegin(&myVec) == NULL);
135 RTTESTI_CHECK(tstSimpleSize(&myVec) == 0);
136 RTTESTI_CHECK(ppvVal[0] == (void *)1);
137 RTTESTI_CHECK(ppvVal[1] == (void *)3);
138 RTTESTI_CHECK(ppvVal[2] == (void *)2);
139}
140
141RTVEC_DECL_DELETE(tstDelete, void *, deletePVoid)
142
143static void testVectorDelete(RTTEST hTest)
144{
145 RTTestISub("Vector structure with cleanup by pointer callback");
146
147 struct tstDelete myVec = RTVEC_INITIALIZER;
148 void **ppvVal;
149
150 ppvVal = tstDeletePushBack(&myVec);
151 /* AssertPtrReturnVoid(ppvVal); */
152 *ppvVal = (void *)1;
153 ppvVal = tstDeletePushBack(&myVec);
154 /* AssertPtrReturnVoid(ppvVal); */
155 *ppvVal = (void *)3;
156 ppvVal = tstDeletePushBack(&myVec);
157 /* AssertPtrReturnVoid(ppvVal); */
158 *ppvVal = (void *)2;
159
160 s_cDeleteCalls = 0;
161 tstDeletePopBack(&myVec);
162 RTTESTI_CHECK(s_cDeleteCalls == 1);
163 RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)2);
164 RTTESTI_CHECK(tstDeleteBegin(&myVec) + 2 == tstDeleteEnd(&myVec));
165 RTTESTI_CHECK(*tstDeleteBegin(&myVec) == (void *)1);
166 RTTESTI_CHECK(*(tstDeleteEnd(&myVec) - 1) == (void *)3);
167
168 s_cDeleteCalls = 0;
169 tstDeleteClear(&myVec);
170 RTTESTI_CHECK(s_cDeleteCalls == 2);
171 RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)1);
172 RTTESTI_CHECK(s_apvDeleteArg[1] == (void *)3);
173 RTTESTI_CHECK(tstDeleteBegin(&myVec) == tstDeleteEnd(&myVec));
174 ppvVal = tstDeletePushBack(&myVec);
175 /* AssertPtrReturnVoid(ppvVal); */
176 RTTESTI_CHECK(ppvVal == tstDeleteBegin(&myVec));
177 RTTESTI_CHECK(ppvVal + 1 == tstDeleteEnd(&myVec));
178}
179
180RTVEC_DECL_DELETE_BY_VALUE(tstDeleteValue, void *, deletePVoidValue)
181
182static void testVectorDeleteValue(RTTEST hTest)
183{
184 RTTestISub("Vector structure with cleanup by value callback");
185
186 struct tstDeleteValue myVec = RTVEC_INITIALIZER;
187 void **ppvVal;
188
189 ppvVal = tstDeleteValuePushBack(&myVec);
190 /* AssertPtrReturnVoid(ppvVal); */
191 *ppvVal = (void *)1;
192 ppvVal = tstDeleteValuePushBack(&myVec);
193 /* AssertPtrReturnVoid(ppvVal); */
194 *ppvVal = (void *)3;
195 ppvVal = tstDeleteValuePushBack(&myVec);
196 /* AssertPtrReturnVoid(ppvVal); */
197 *ppvVal = (void *)2;
198
199 s_cDeleteCalls = 0;
200 tstDeleteValuePopBack(&myVec);
201 RTTESTI_CHECK(s_cDeleteCalls == 1);
202 RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)2);
203 RTTESTI_CHECK( tstDeleteValueBegin(&myVec) + 2
204 == tstDeleteValueEnd(&myVec));
205 RTTESTI_CHECK(*tstDeleteValueBegin(&myVec) == (void *)1);
206 RTTESTI_CHECK(*(tstDeleteValueEnd(&myVec) - 1) == (void *)3);
207
208 s_cDeleteCalls = 0;
209 tstDeleteValueClear(&myVec);
210 RTTESTI_CHECK(s_cDeleteCalls == 2);
211 RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)1);
212 RTTESTI_CHECK(s_apvDeleteArg[1] == (void *)3);
213 RTTESTI_CHECK(tstDeleteValueBegin(&myVec) == tstDeleteValueEnd(&myVec));
214 ppvVal = tstDeleteValuePushBack(&myVec);
215 /* AssertPtrReturnVoid(ppvVal); */
216 RTTESTI_CHECK(ppvVal == tstDeleteValueBegin(&myVec));
217 RTTESTI_CHECK(ppvVal + 1 == tstDeleteValueEnd(&myVec));
218}
219
220
221
222int main()
223{
224 RTTEST hTest;
225 RTEXITCODE rcExit = RTTestInitAndCreate("tstVector", &hTest);
226 if (rcExit != RTEXITCODE_SUCCESS)
227 return rcExit;
228
229 testVectorSimple(hTest);
230 testVectorDelete(hTest);
231 testVectorDeleteValue(hTest);
232
233 return RTTestSummaryAndDestroy(hTest);
234}
Note: See TracBrowser for help on using the repository browser.

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