1 | /* $Id: tstRTSort.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Sorting.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-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/sort.h>
|
---|
42 |
|
---|
43 | #include <iprt/errcore.h>
|
---|
44 | #include <iprt/rand.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 | #include <iprt/test.h>
|
---|
47 | #include <iprt/time.h>
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Structures and Typedefs *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | typedef struct TSTRTSORTAPV
|
---|
54 | {
|
---|
55 | uint32_t aValues[8192];
|
---|
56 | void *apv[8192];
|
---|
57 | size_t cElements;
|
---|
58 | } TSTRTSORTAPV;
|
---|
59 |
|
---|
60 |
|
---|
61 | static DECLCALLBACK(int) testApvCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
62 | {
|
---|
63 | TSTRTSORTAPV *pData = (TSTRTSORTAPV *)pvUser;
|
---|
64 | uint32_t const *pu32Element1 = (uint32_t const *)pvElement1;
|
---|
65 | uint32_t const *pu32Element2 = (uint32_t const *)pvElement2;
|
---|
66 | RTTESTI_CHECK(RT_VALID_PTR(pData) && pData->cElements <= RT_ELEMENTS(pData->aValues));
|
---|
67 | RTTESTI_CHECK((uintptr_t)(pu32Element1 - &pData->aValues[0]) < pData->cElements);
|
---|
68 | RTTESTI_CHECK((uintptr_t)(pu32Element2 - &pData->aValues[0]) < pData->cElements);
|
---|
69 |
|
---|
70 | if (*pu32Element1 < *pu32Element2)
|
---|
71 | return -1;
|
---|
72 | if (*pu32Element1 > *pu32Element2)
|
---|
73 | return 1;
|
---|
74 | return 0;
|
---|
75 | }
|
---|
76 |
|
---|
77 | static void testApvSorter(FNRTSORTAPV pfnSorter, const char *pszName)
|
---|
78 | {
|
---|
79 | RTTestISub(pszName);
|
---|
80 |
|
---|
81 | RTRAND hRand;
|
---|
82 | RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
|
---|
83 |
|
---|
84 | TSTRTSORTAPV Data;
|
---|
85 | for (size_t cElements = 0; cElements < RT_ELEMENTS(Data.apv); cElements++)
|
---|
86 | {
|
---|
87 | RT_ZERO(Data);
|
---|
88 | Data.cElements = cElements;
|
---|
89 |
|
---|
90 | /* popuplate the array */
|
---|
91 | for (size_t i = 0; i < cElements; i++)
|
---|
92 | {
|
---|
93 | Data.aValues[i] = RTRandAdvU32(hRand);
|
---|
94 | Data.apv[i] = &Data.aValues[i];
|
---|
95 | }
|
---|
96 |
|
---|
97 | /* sort it */
|
---|
98 | pfnSorter(&Data.apv[0], cElements, testApvCompare, &Data);
|
---|
99 |
|
---|
100 | /* verify it */
|
---|
101 | if (!RTSortApvIsSorted(&Data.apv[0], cElements, testApvCompare, &Data))
|
---|
102 | RTTestIFailed("failed sorting %u elements", cElements);
|
---|
103 | }
|
---|
104 |
|
---|
105 | RTRandAdvDestroy(hRand);
|
---|
106 | }
|
---|
107 |
|
---|
108 |
|
---|
109 | static DECLCALLBACK(int) testCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
110 | {
|
---|
111 | return memcmp(pvElement1, pvElement2, (size_t)pvUser);
|
---|
112 | }
|
---|
113 |
|
---|
114 | static void testSorter(RTTEST hTest, FNRTSORT pfnSorter, const char *pszName)
|
---|
115 | {
|
---|
116 | RTTestISub(pszName);
|
---|
117 |
|
---|
118 | /* Use pseudo random config and data. */
|
---|
119 | RTRAND hRand;
|
---|
120 | RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
|
---|
121 | RTTIMESPEC Now;
|
---|
122 | uint64_t uSeed = RTTimeSpecGetSeconds(RTTimeNow(&Now));
|
---|
123 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Seed %#RX64\n", uSeed);
|
---|
124 | RTRandAdvSeed(hRand, uSeed);
|
---|
125 |
|
---|
126 | for (uint32_t cArrays = 0; cArrays < 512; cArrays++)
|
---|
127 | {
|
---|
128 | /* Create a random array with random data bytes. */
|
---|
129 | uint32_t const cElements = RTRandAdvU32Ex(hRand, 2, 8192);
|
---|
130 | uint32_t const cbElement = RTRandAdvU32Ex(hRand, 1, 32);
|
---|
131 | uint8_t *pbArray;
|
---|
132 | RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(hTest, cElements * cbElement, 1 /*cbAlign*/,
|
---|
133 | RT_BOOL(RTRandAdvU32Ex(hRand, 0, 1)) /*fHead*/, (void **)&pbArray));
|
---|
134 | RTTESTI_CHECK_RETV(pbArray);
|
---|
135 | RTRandAdvBytes(hRand, pbArray, cElements * cbElement);
|
---|
136 |
|
---|
137 | /* sort it */
|
---|
138 | pfnSorter(pbArray, cElements, cbElement, testCompare, (void *)(uintptr_t)cbElement);
|
---|
139 |
|
---|
140 | /* verify it */
|
---|
141 | if (!RTSortIsSorted(pbArray, cElements, cbElement, testCompare, (void *)(uintptr_t)cbElement))
|
---|
142 | RTTestIFailed("failed sorting %u elements of %u size", cElements, cbElement);
|
---|
143 |
|
---|
144 | RTTestGuardedFree(hTest, pbArray);
|
---|
145 | }
|
---|
146 |
|
---|
147 | RTRandAdvDestroy(hRand);
|
---|
148 | }
|
---|
149 |
|
---|
150 |
|
---|
151 | int main()
|
---|
152 | {
|
---|
153 | RTTEST hTest;
|
---|
154 | int rc = RTTestInitAndCreate("tstRTTemp", &hTest);
|
---|
155 | if (rc)
|
---|
156 | return rc;
|
---|
157 | RTTestBanner(hTest);
|
---|
158 |
|
---|
159 | /*
|
---|
160 | * Test the different algorithms.
|
---|
161 | */
|
---|
162 | testSorter(hTest, RTSortShell, "RTSortShell - shell sort, variable sized element array");
|
---|
163 | testApvSorter(RTSortApvShell, "RTSortApvShell - shell sort, pointer array");
|
---|
164 |
|
---|
165 | /*
|
---|
166 | * Summary.
|
---|
167 | */
|
---|
168 | return RTTestSummaryAndDestroy(hTest);
|
---|
169 | }
|
---|
170 |
|
---|