1 | /* $Id: tstSafeArray.cpp 106061 2024-09-16 14:03:52Z 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.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 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 |
|
---|
29 | /*********************************************************************************************************************************
|
---|
30 | * Header Files *
|
---|
31 | *********************************************************************************************************************************/
|
---|
32 | #include <VBox/com/array.h>
|
---|
33 | #include <VBox/com/string.h>
|
---|
34 |
|
---|
35 | #include <iprt/mem.h>
|
---|
36 | #include <iprt/rand.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/test.h>
|
---|
39 | #include <iprt/uni.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | int main()
|
---|
43 | {
|
---|
44 | RTTEST hTest;
|
---|
45 | RTEXITCODE rcExit = RTTestInitAndCreate("tstSafeArray", &hTest);
|
---|
46 | if (rcExit == RTEXITCODE_SUCCESS)
|
---|
47 | {
|
---|
48 | RTTestBanner(hTest);
|
---|
49 |
|
---|
50 | /* Sizes / Pre-allocations. */
|
---|
51 | com::SafeArray<int> aInt;
|
---|
52 | RTTESTI_CHECK(aInt.size() == 0);
|
---|
53 |
|
---|
54 | com::SafeArray<int> aInt2(42);
|
---|
55 | RTTESTI_CHECK(aInt2.size() == 42);
|
---|
56 | aInt2.setNull();
|
---|
57 | RTTESTI_CHECK(aInt2.size() == 0);
|
---|
58 | aInt2.resize(42);
|
---|
59 | RTTESTI_CHECK(aInt2.size() == 42);
|
---|
60 | aInt2.setNull();
|
---|
61 |
|
---|
62 | com::SafeArray<int> aInt3((size_t)0);
|
---|
63 | RTTESTI_CHECK(aInt3.size() == 0);
|
---|
64 | aInt3.setNull();
|
---|
65 | RTTESTI_CHECK(aInt3.size() == 0);
|
---|
66 |
|
---|
67 | /* Push to back. */
|
---|
68 | int aPushToBack[] = { 51, 52, 53 };
|
---|
69 | for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
|
---|
70 | {
|
---|
71 | RTTESTI_CHECK(aInt.push_back(aPushToBack[i]));
|
---|
72 | RTTESTI_CHECK(aInt.size() == i + 1);
|
---|
73 | RTTESTI_CHECK(aInt[i] == aPushToBack[i]);
|
---|
74 | }
|
---|
75 | for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
|
---|
76 | RTTESTI_CHECK_MSG(aInt[i] == aPushToBack[i], ("Got %d, expected %d\n", aInt[i], aPushToBack[i]));
|
---|
77 |
|
---|
78 | aInt.setNull();
|
---|
79 |
|
---|
80 | /* Push to front. */
|
---|
81 | int aPushToFront[] = { 41, 42, 43 };
|
---|
82 | for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
|
---|
83 | {
|
---|
84 | RTTESTI_CHECK(aInt.push_front(aPushToFront[i]));
|
---|
85 | RTTESTI_CHECK(aInt.size() == i + 1);
|
---|
86 | RTTESTI_CHECK(aInt[0] == aPushToFront[i]);
|
---|
87 | }
|
---|
88 | for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
|
---|
89 | RTTESTI_CHECK_MSG(aInt[i] == aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1],
|
---|
90 | ("Got %d, expected %d\n", aInt[i], aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1]));
|
---|
91 |
|
---|
92 | /* A bit more data. */
|
---|
93 | aInt.setNull();
|
---|
94 | for (size_t i = 0; i < RTRandU32Ex(_4K, _64M); i++)
|
---|
95 | {
|
---|
96 | RTTESTI_CHECK(aInt.push_front(42));
|
---|
97 | RTTESTI_CHECK(aInt.push_back(41));
|
---|
98 | RTTESTI_CHECK(aInt.size() == (i + 1) * 2);
|
---|
99 | }
|
---|
100 | aInt.setNull();
|
---|
101 |
|
---|
102 | rcExit = RTTestSummaryAndDestroy(hTest);
|
---|
103 | }
|
---|
104 | return rcExit;
|
---|
105 | }
|
---|
106 |
|
---|