1 | /* $Id: tstVector.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Vector container structure.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2022 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * The contents of this file may alternatively be used under the terms
|
---|
26 | * of the Common Development and Distribution License Version 1.0
|
---|
27 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
28 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
29 | * CDDL are applicable instead of those of the GPL.
|
---|
30 | *
|
---|
31 | * You may elect to license modified versions of this file under the
|
---|
32 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
33 | *
|
---|
34 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
35 | */
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include <iprt/test.h>
|
---|
42 | #include <iprt/vector.h>
|
---|
43 |
|
---|
44 | #include <stdlib.h> /* For realloc */
|
---|
45 |
|
---|
46 | /** Counter of the number of delete calls made so far */
|
---|
47 | static unsigned s_cDeleteCalls = 0;
|
---|
48 |
|
---|
49 | /** Record the argument of the delete function here. */
|
---|
50 | static void *s_apvDeleteArg[10];
|
---|
51 |
|
---|
52 | /** Dummy delete function for vector-of-void pointer elements */
|
---|
53 | static void deletePVoid(void **ppv)
|
---|
54 | {
|
---|
55 | if (s_cDeleteCalls < RT_ELEMENTS(s_apvDeleteArg))
|
---|
56 | s_apvDeleteArg[s_cDeleteCalls] = *ppv;
|
---|
57 | ++s_cDeleteCalls;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /** Dummy delete by value function for vector-of-void pointer elements */
|
---|
61 | static void deletePVoidValue(void *pv)
|
---|
62 | {
|
---|
63 | if (s_cDeleteCalls < RT_ELEMENTS(s_apvDeleteArg))
|
---|
64 | s_apvDeleteArg[s_cDeleteCalls] = pv;
|
---|
65 | ++s_cDeleteCalls;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /* Start by instantiating each function once for syntax checking */
|
---|
69 | #ifdef __clang__
|
---|
70 | # pragma GCC diagnostic ignored "-Wunused-function" /* https://llvm.org/bugs/show_bug.cgi?id=22712 */
|
---|
71 | #endif
|
---|
72 | RTVEC_DECL_STRUCT(tstInstance, void *)
|
---|
73 | RTVEC_DECL_STRUCT(tstInstance2, void *)
|
---|
74 |
|
---|
75 | RTVEC_DECLFN_DELETE_ADAPTER_ID(tstInstance, void *)
|
---|
76 | RTVEC_DECLFN_DELETE_ADAPTER_TO_VALUE(tstInstance, void *)
|
---|
77 |
|
---|
78 | RTVEC_DECLFN_SIZE(tstInstance, void *)
|
---|
79 | RTVEC_DECLFN_RESERVE(tstInstance, void *, rtvecReallocDefTag)
|
---|
80 | RTVEC_DECLFN_BEGIN(tstInstance, void *)
|
---|
81 | RTVEC_DECLFN_END(tstInstance, void *)
|
---|
82 | RTVEC_DECLFN_PUSHBACK(tstInstance, void *)
|
---|
83 | RTVEC_DECLFN_POPBACK(tstInstance)
|
---|
84 | RTVEC_DECLFN_POPBACK_DELETE(tstInstance2, void *, deletePVoid, tstInstanceDeleteAdapterId)
|
---|
85 | RTVEC_DECLFN_CLEAR(tstInstance)
|
---|
86 | RTVEC_DECLFN_CLEAR_DELETE(tstInstance2, deletePVoid, tstInstanceDeleteAdapterId)
|
---|
87 | RTVEC_DECLFN_DETACH(tstInstance, void *)
|
---|
88 |
|
---|
89 | RTVEC_DECL(tstSimple, void *)
|
---|
90 |
|
---|
91 | static void testVectorSimple(void)
|
---|
92 | {
|
---|
93 | RTTestISub("Vector structure, no cleanup callback");
|
---|
94 |
|
---|
95 | struct tstSimple myVec = RTVEC_INITIALIZER;
|
---|
96 | void **ppvVal;
|
---|
97 |
|
---|
98 | RTTESTI_CHECK(tstSimpleSize(&myVec) == 0);
|
---|
99 |
|
---|
100 | ppvVal = tstSimplePushBack(&myVec);
|
---|
101 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
102 | RTTESTI_CHECK(ppvVal == tstSimpleBegin(&myVec));
|
---|
103 | RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
|
---|
104 | RTTESTI_CHECK(tstSimpleSize(&myVec) == 1);
|
---|
105 | *ppvVal = (void *)1;
|
---|
106 |
|
---|
107 | ppvVal = tstSimplePushBack(&myVec);
|
---|
108 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
109 | RTTESTI_CHECK(ppvVal - 1 == tstSimpleBegin(&myVec));
|
---|
110 | RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
|
---|
111 | RTTESTI_CHECK(tstSimpleSize(&myVec) == 2);
|
---|
112 | RTTESTI_CHECK(ppvVal[-1] == (void *)1);
|
---|
113 |
|
---|
114 | *ppvVal = (void *)3;
|
---|
115 | ppvVal = tstSimplePushBack(&myVec);
|
---|
116 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
117 | RTTESTI_CHECK(ppvVal - 2 == tstSimpleBegin(&myVec));
|
---|
118 | RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
|
---|
119 | RTTESTI_CHECK(tstSimpleSize(&myVec) == 3);
|
---|
120 | RTTESTI_CHECK(ppvVal[-2] == (void *)1);
|
---|
121 | RTTESTI_CHECK(ppvVal[-1] == (void *)3);
|
---|
122 |
|
---|
123 | tstSimplePopBack(&myVec);
|
---|
124 | RTTESTI_CHECK(tstSimpleBegin(&myVec) + 2 == tstSimpleEnd(&myVec));
|
---|
125 | RTTESTI_CHECK(*tstSimpleBegin(&myVec) == (void *)1);
|
---|
126 | RTTESTI_CHECK(*(tstSimpleEnd(&myVec) - 1) == (void *)3);
|
---|
127 |
|
---|
128 | tstSimpleClear(&myVec);
|
---|
129 | RTTESTI_CHECK(tstSimpleBegin(&myVec) == tstSimpleEnd(&myVec));
|
---|
130 | ppvVal = tstSimplePushBack(&myVec);
|
---|
131 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
132 | RTTESTI_CHECK(ppvVal == tstSimpleBegin(&myVec));
|
---|
133 | RTTESTI_CHECK(ppvVal + 1 == tstSimpleEnd(&myVec));
|
---|
134 |
|
---|
135 | tstSimpleClear(&myVec);
|
---|
136 | ppvVal = tstSimplePushBack(&myVec);
|
---|
137 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
138 | *ppvVal = (void *)1;
|
---|
139 | ppvVal = tstSimplePushBack(&myVec);
|
---|
140 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
141 | *ppvVal = (void *)3;
|
---|
142 | ppvVal = tstSimplePushBack(&myVec);
|
---|
143 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
144 | *ppvVal = (void *)2;
|
---|
145 | ppvVal = tstSimpleDetach(&myVec);
|
---|
146 | RTTESTI_CHECK(tstSimpleBegin(&myVec) == NULL);
|
---|
147 | RTTESTI_CHECK(tstSimpleSize(&myVec) == 0);
|
---|
148 | RTTESTI_CHECK(ppvVal[0] == (void *)1);
|
---|
149 | RTTESTI_CHECK(ppvVal[1] == (void *)3);
|
---|
150 | RTTESTI_CHECK(ppvVal[2] == (void *)2);
|
---|
151 |
|
---|
152 | RTMemFree(ppvVal); /** @todo there is no delete vector thing. */
|
---|
153 | }
|
---|
154 |
|
---|
155 | RTVEC_DECL_DELETE(tstDelete, void *, deletePVoid)
|
---|
156 |
|
---|
157 | static void testVectorDelete(void)
|
---|
158 | {
|
---|
159 | RTTestISub("Vector structure with cleanup by pointer callback");
|
---|
160 |
|
---|
161 | struct tstDelete myVec = RTVEC_INITIALIZER;
|
---|
162 | void **ppvVal;
|
---|
163 |
|
---|
164 | ppvVal = tstDeletePushBack(&myVec);
|
---|
165 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
166 | *ppvVal = (void *)1;
|
---|
167 | ppvVal = tstDeletePushBack(&myVec);
|
---|
168 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
169 | *ppvVal = (void *)3;
|
---|
170 | ppvVal = tstDeletePushBack(&myVec);
|
---|
171 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
172 | *ppvVal = (void *)2;
|
---|
173 |
|
---|
174 | s_cDeleteCalls = 0;
|
---|
175 | tstDeletePopBack(&myVec);
|
---|
176 | RTTESTI_CHECK(s_cDeleteCalls == 1);
|
---|
177 | RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)2);
|
---|
178 | RTTESTI_CHECK(tstDeleteBegin(&myVec) + 2 == tstDeleteEnd(&myVec));
|
---|
179 | RTTESTI_CHECK(*tstDeleteBegin(&myVec) == (void *)1);
|
---|
180 | RTTESTI_CHECK(*(tstDeleteEnd(&myVec) - 1) == (void *)3);
|
---|
181 |
|
---|
182 | s_cDeleteCalls = 0;
|
---|
183 | tstDeleteClear(&myVec);
|
---|
184 | RTTESTI_CHECK(s_cDeleteCalls == 2);
|
---|
185 | RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)1);
|
---|
186 | RTTESTI_CHECK(s_apvDeleteArg[1] == (void *)3);
|
---|
187 | RTTESTI_CHECK(tstDeleteBegin(&myVec) == tstDeleteEnd(&myVec));
|
---|
188 | ppvVal = tstDeletePushBack(&myVec);
|
---|
189 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
190 | RTTESTI_CHECK(ppvVal == tstDeleteBegin(&myVec));
|
---|
191 | RTTESTI_CHECK(ppvVal + 1 == tstDeleteEnd(&myVec));
|
---|
192 |
|
---|
193 | ppvVal = tstDeleteDetach(&myVec); /** @todo no delete myVec */
|
---|
194 | RTMemFree(ppvVal);
|
---|
195 | }
|
---|
196 |
|
---|
197 | RTVEC_DECL_DELETE_BY_VALUE(tstDeleteValue, void *, deletePVoidValue)
|
---|
198 |
|
---|
199 | static void testVectorDeleteValue(void)
|
---|
200 | {
|
---|
201 | RTTestISub("Vector structure with cleanup by value callback");
|
---|
202 |
|
---|
203 | struct tstDeleteValue myVec = RTVEC_INITIALIZER;
|
---|
204 | void **ppvVal;
|
---|
205 |
|
---|
206 | ppvVal = tstDeleteValuePushBack(&myVec);
|
---|
207 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
208 | *ppvVal = (void *)1;
|
---|
209 | ppvVal = tstDeleteValuePushBack(&myVec);
|
---|
210 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
211 | *ppvVal = (void *)3;
|
---|
212 | ppvVal = tstDeleteValuePushBack(&myVec);
|
---|
213 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
214 | *ppvVal = (void *)2;
|
---|
215 |
|
---|
216 | s_cDeleteCalls = 0;
|
---|
217 | tstDeleteValuePopBack(&myVec);
|
---|
218 | RTTESTI_CHECK(s_cDeleteCalls == 1);
|
---|
219 | RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)2);
|
---|
220 | RTTESTI_CHECK( tstDeleteValueBegin(&myVec) + 2
|
---|
221 | == tstDeleteValueEnd(&myVec));
|
---|
222 | RTTESTI_CHECK(*tstDeleteValueBegin(&myVec) == (void *)1);
|
---|
223 | RTTESTI_CHECK(*(tstDeleteValueEnd(&myVec) - 1) == (void *)3);
|
---|
224 |
|
---|
225 | s_cDeleteCalls = 0;
|
---|
226 | tstDeleteValueClear(&myVec);
|
---|
227 | RTTESTI_CHECK(s_cDeleteCalls == 2);
|
---|
228 | RTTESTI_CHECK(s_apvDeleteArg[0] == (void *)1);
|
---|
229 | RTTESTI_CHECK(s_apvDeleteArg[1] == (void *)3);
|
---|
230 | RTTESTI_CHECK(tstDeleteValueBegin(&myVec) == tstDeleteValueEnd(&myVec));
|
---|
231 | ppvVal = tstDeleteValuePushBack(&myVec);
|
---|
232 | /* AssertPtrReturnVoid(ppvVal); */
|
---|
233 | RTTESTI_CHECK(ppvVal == tstDeleteValueBegin(&myVec));
|
---|
234 | RTTESTI_CHECK(ppvVal + 1 == tstDeleteValueEnd(&myVec));
|
---|
235 |
|
---|
236 | ppvVal = tstDeleteValueDetach(&myVec); /** @todo no delete myVec */
|
---|
237 | RTMemFree(ppvVal);
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 |
|
---|
242 | int main()
|
---|
243 | {
|
---|
244 | RTTEST hTest;
|
---|
245 | RTEXITCODE rcExit = RTTestInitAndCreate("tstVector", &hTest);
|
---|
246 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
247 | return rcExit;
|
---|
248 |
|
---|
249 | testVectorSimple();
|
---|
250 | testVectorDelete();
|
---|
251 | testVectorDeleteValue();
|
---|
252 |
|
---|
253 | return RTTestSummaryAndDestroy(hTest);
|
---|
254 | }
|
---|