summaryrefslogtreecommitdiffstats
path: root/src/VBox/Main/testcase/tstSafeArray.cpp
blob: 8a6713637e24f2df5db9303bab4b034ee889b01b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/* $Id: tstSafeArray.cpp $ */
/** @file
 * API Glue Testcase - SafeArray.
 */

/*
 * Copyright (C) 2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <VBox/com/array.h>
#include <VBox/com/string.h>

#include <iprt/mem.h>
#include <iprt/rand.h>
#include <iprt/string.h>
#include <iprt/test.h>
#include <iprt/uni.h>


int main()
{
    RTTEST      hTest;
    RTEXITCODE  rcExit = RTTestInitAndCreate("tstSafeArray", &hTest);
    if (rcExit == RTEXITCODE_SUCCESS)
    {
        RTTestBanner(hTest);

        /* Sizes / Pre-allocations. */
        com::SafeArray<int> aInt;
        RTTESTI_CHECK(aInt.size() == 0);

        com::SafeArray<int> aInt2(42);
        RTTESTI_CHECK(aInt2.size() == 42);
        aInt2.setNull();
        RTTESTI_CHECK(aInt2.size() == 0);
        aInt2.resize(42);
        RTTESTI_CHECK(aInt2.size() == 42);
        aInt2.setNull();

        com::SafeArray<int> aInt3((size_t)0);
        RTTESTI_CHECK(aInt3.size() == 0);
        aInt3.setNull();
        RTTESTI_CHECK(aInt3.size() == 0);

        /* Push to back. */
        int aPushToBack[] = { 51, 52, 53 };
        for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
        {
            RTTESTI_CHECK(aInt.push_back(aPushToBack[i]));
            RTTESTI_CHECK(aInt.size() == i + 1);
            RTTESTI_CHECK(aInt[i] == aPushToBack[i]);
        }
        for (size_t i = 0; i < RT_ELEMENTS(aPushToBack); i++)
            RTTESTI_CHECK_MSG(aInt[i] == aPushToBack[i], ("Got %d, expected %d\n", aInt[i], aPushToBack[i]));

        aInt.setNull();

        /* Push to front. */
        int aPushToFront[] = { 41, 42, 43 };
        for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
        {
            RTTESTI_CHECK(aInt.push_front(aPushToFront[i]));
            RTTESTI_CHECK(aInt.size() == i + 1);
            RTTESTI_CHECK(aInt[0] == aPushToFront[i]);
        }
        for (size_t i = 0; i < RT_ELEMENTS(aPushToFront); i++)
            RTTESTI_CHECK_MSG(aInt[i] == aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1],
                              ("Got %d, expected %d\n", aInt[i], aPushToFront[RT_ELEMENTS(aPushToFront) - i - 1]));

        /* A bit more data. */
        aInt.setNull();
        for (size_t i = 0; i < RTRandU32Ex(_4K, _64M); i++)
        {
            RTTESTI_CHECK(aInt.push_front(42));
            RTTESTI_CHECK(aInt.push_back(41));
            RTTESTI_CHECK(aInt.size() == (i + 1) * 2);
        }
        aInt.setNull();

        rcExit = RTTestSummaryAndDestroy(hTest);
    }
    return rcExit;
}