VirtualBox

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

Last change on this file since 93468 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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