VirtualBox

source: vbox/trunk/src/VBox/Main/testcase/tstSafeArray.cpp

Last change on this file was 109191, checked in by vboxsync, 6 days ago

VBox/Main: Fixed an error in SafeArray::push_front() for the Windows (COM) implementation. See comments for details. Added some more testcases for this function. ticketref:22175

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 6.2 KB
Line 
1/* $Id: tstSafeArray.cpp 109191 2025-05-07 13:32:53Z vboxsync $ */
2/** @file
3 * API Glue Testcase - SafeArray.
4 */
5
6/*
7 * Copyright (C) 2023-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
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 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32#include <VBox/com/com.h>
33#include <VBox/com/array.h>
34#include <VBox/com/string.h>
35
36#include <iprt/mem.h>
37#include <iprt/rand.h>
38#include <iprt/stream.h>
39#include <iprt/string.h>
40#include <iprt/test.h>
41#include <iprt/uni.h>
42
43
44int main()
45{
46 RTTEST hTest;
47 RTEXITCODE rcExit = RTTestInitAndCreate("tstSafeArray", &hTest);
48 if (rcExit == RTEXITCODE_SUCCESS)
49 {
50 RTTestBanner(hTest);
51
52 HRESULT hrc = com::Initialize();
53 if (FAILED(hrc))
54 {
55 RTPrintf("ERROR: failed to initialize COM, hrc=%Rhrc\n", hrc);
56 return RTEXITCODE_FAILURE;
57 }
58
59 /* Some simple push-to-front test to catch some off-by-one errors. */
60 com::SafeArray<int> aInt;
61 aInt.push_front(42);
62
63 /* Test NULL'ing. */
64 aInt.setNull();
65
66 /* Sizes / Pre-allocations. */
67 RTTESTI_CHECK(aInt.size() == 0);
68
69 com::SafeArray<int> aInt2(42);
70 RTTESTI_CHECK(aInt2.size() == 42);
71 aInt2.setNull();
72 RTTESTI_CHECK(aInt2.size() == 0);
73 aInt2.resize(42);
74 RTTESTI_CHECK(aInt2.size() == 42);
75 aInt2.setNull();
76
77 com::SafeArray<int> aInt3((size_t)0);
78 RTTESTI_CHECK(aInt3.size() == 0);
79 aInt3.setNull();
80 RTTESTI_CHECK(aInt3.size() == 0);
81
82 /* Push to back. */
83 int aPushToBack[] = { 51, 52, 53 };
84 for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
85 {
86 RTTESTI_CHECK(aInt.push_back(aPushToBack[i]));
87 RTTESTI_CHECK(aInt.size() == i + 1);
88 RTTESTI_CHECK(aInt[i] == aPushToBack[i]);
89 }
90 for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
91 RTTESTI_CHECK_MSG(aInt[i] == aPushToBack[i], ("Got %d, expected %d\n", aInt[i], aPushToBack[i]));
92
93 aInt.setNull();
94
95 /* Push to front. */
96 int aPushToFront[] = { 41, 42, 43 };
97 for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
98 {
99 RTTESTI_CHECK(aInt.push_front(aPushToFront[i]));
100 RTTESTI_CHECK(aInt.size() == i + 1);
101 RTTESTI_CHECK(aInt[0] == aPushToFront[i]);
102 }
103 for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
104 RTTESTI_CHECK_MSG(aInt[i] == aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1],
105 ("Got %d, expected %d\n", aInt[i], aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1]));
106
107 /* Push to back first, then push to front. */
108 com::SafeArray<int> aInt4;
109 for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
110 {
111 RTTESTI_CHECK(aInt4.push_back(aPushToBack[i]));
112 RTTESTI_CHECK(aInt4.size() == i + 1);
113 RTTESTI_CHECK(aInt4[i] == aPushToBack[i]);
114 }
115 for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
116 RTTESTI_CHECK_MSG(aInt4[i] == aPushToBack[i], ("Got %d, expected %d\n", aInt4[i], aPushToBack[i]));
117
118 for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
119 {
120 RTTESTI_CHECK(aInt4.push_front(aPushToFront[i]));
121 RTTESTI_CHECK(aInt4.size() == i + 1 + RT_ELEMENTS(aPushToBack));
122 RTTESTI_CHECK(aInt4[0] == aPushToFront[i]);
123 }
124 for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
125 RTTESTI_CHECK_MSG(aInt4[i] == aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1],
126 ("Got %d, expected %d\n", aInt4[i], aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1]));
127
128 aInt4.setNull();
129
130 /* Push to front first, then push to back. */
131 com::SafeArray<int> aInt5;
132 for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
133 {
134 RTTESTI_CHECK(aInt5.push_front(aPushToFront[i]));
135 RTTESTI_CHECK(aInt5.size() == i + 1);
136 RTTESTI_CHECK(aInt5[0] == aPushToFront[i]);
137 }
138 for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
139 RTTESTI_CHECK_MSG(aInt5[i] == aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1],
140 ("Got %d, expected %d\n", aInt5[i], aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1]));
141
142 /* Push to back. */
143 for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
144 {
145 RTTESTI_CHECK(aInt5.push_back(aPushToBack[i]));
146 RTTESTI_CHECK(aInt5.size() == i + 1 + RT_ELEMENTS(aPushToFront));
147 RTTESTI_CHECK(aInt5[i + RT_ELEMENTS(aPushToFront)] == aPushToBack[i]);
148 }
149 for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
150 RTTESTI_CHECK_MSG(aInt5[i + RT_ELEMENTS(aPushToFront)] == aPushToBack[i],
151 ("Got %d, expected %d\n", aInt5[i], aPushToBack[i]));
152
153 aInt5.setNull();
154
155 /* A bit more data. */
156 aInt.setNull();
157 for (size_t i = 0; i < RTRandU32Ex(_4K, _64M); i++)
158 {
159 RTTESTI_CHECK(aInt.push_front(42));
160 RTTESTI_CHECK(aInt.push_back(41));
161 RTTESTI_CHECK(aInt.size() == (i + 1) * 2);
162 }
163 aInt.setNull();
164
165 rcExit = RTTestSummaryAndDestroy(hTest);
166 }
167 return rcExit;
168}
169
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