1 | /* $Id: tstRTSort.cpp 59983 2016-03-10 15:38:31Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT Testcase - Sorting.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/sort.h>
|
---|
32 |
|
---|
33 | #include <iprt/err.h>
|
---|
34 | #include <iprt/rand.h>
|
---|
35 | #include <iprt/string.h>
|
---|
36 | #include <iprt/test.h>
|
---|
37 | #include <iprt/time.h>
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Structures and Typedefs *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | typedef struct TSTRTSORTAPV
|
---|
44 | {
|
---|
45 | uint32_t aValues[8192];
|
---|
46 | void *apv[8192];
|
---|
47 | size_t cElements;
|
---|
48 | } TSTRTSORTAPV;
|
---|
49 |
|
---|
50 |
|
---|
51 | static DECLCALLBACK(int) testApvCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
52 | {
|
---|
53 | TSTRTSORTAPV *pData = (TSTRTSORTAPV *)pvUser;
|
---|
54 | uint32_t const *pu32Element1 = (uint32_t const *)pvElement1;
|
---|
55 | uint32_t const *pu32Element2 = (uint32_t const *)pvElement2;
|
---|
56 | RTTESTI_CHECK(VALID_PTR(pData) && pData->cElements <= RT_ELEMENTS(pData->aValues));
|
---|
57 | RTTESTI_CHECK((uintptr_t)(pu32Element1 - &pData->aValues[0]) < pData->cElements);
|
---|
58 | RTTESTI_CHECK((uintptr_t)(pu32Element2 - &pData->aValues[0]) < pData->cElements);
|
---|
59 |
|
---|
60 | if (*pu32Element1 < *pu32Element2)
|
---|
61 | return -1;
|
---|
62 | if (*pu32Element1 > *pu32Element2)
|
---|
63 | return 1;
|
---|
64 | return 0;
|
---|
65 | }
|
---|
66 |
|
---|
67 | static void testApvSorter(FNRTSORTAPV pfnSorter, const char *pszName)
|
---|
68 | {
|
---|
69 | RTTestISub(pszName);
|
---|
70 |
|
---|
71 | RTRAND hRand;
|
---|
72 | RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
|
---|
73 |
|
---|
74 | TSTRTSORTAPV Data;
|
---|
75 | for (size_t cElements = 0; cElements < RT_ELEMENTS(Data.apv); cElements++)
|
---|
76 | {
|
---|
77 | RT_ZERO(Data);
|
---|
78 | Data.cElements = cElements;
|
---|
79 |
|
---|
80 | /* popuplate the array */
|
---|
81 | for (size_t i = 0; i < cElements; i++)
|
---|
82 | {
|
---|
83 | Data.aValues[i] = RTRandAdvU32(hRand);
|
---|
84 | Data.apv[i] = &Data.aValues[i];
|
---|
85 | }
|
---|
86 |
|
---|
87 | /* sort it */
|
---|
88 | pfnSorter(&Data.apv[0], cElements, testApvCompare, &Data);
|
---|
89 |
|
---|
90 | /* verify it */
|
---|
91 | if (!RTSortApvIsSorted(&Data.apv[0], cElements, testApvCompare, &Data))
|
---|
92 | RTTestIFailed("failed sorting %u elements", cElements);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 |
|
---|
97 | static DECLCALLBACK(int) testCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
98 | {
|
---|
99 | return memcmp(pvElement1, pvElement2, (size_t)pvUser);
|
---|
100 | }
|
---|
101 |
|
---|
102 | static void testSorter(RTTEST hTest, FNRTSORT pfnSorter, const char *pszName)
|
---|
103 | {
|
---|
104 | RTTestISub(pszName);
|
---|
105 |
|
---|
106 | /* Use pseudo random config and data. */
|
---|
107 | RTRAND hRand;
|
---|
108 | RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
|
---|
109 | RTTIMESPEC Now;
|
---|
110 | uint64_t uSeed = RTTimeSpecGetSeconds(RTTimeNow(&Now));
|
---|
111 | RTTestIPrintf(RTTESTLVL_ALWAYS, "Seed %#RX64\n", uSeed);
|
---|
112 | RTRandAdvSeed(hRand, uSeed);
|
---|
113 |
|
---|
114 | for (uint32_t cArrays = 0; cArrays < 512; cArrays++)
|
---|
115 | {
|
---|
116 | /* Create a random array with random data bytes. */
|
---|
117 | uint32_t const cElements = RTRandAdvU32Ex(hRand, 2, 8192);
|
---|
118 | uint32_t const cbElement = RTRandAdvU32Ex(hRand, 1, 32);
|
---|
119 | uint8_t *pbArray;
|
---|
120 | RTTESTI_CHECK_RC_OK_RETV(RTTestGuardedAlloc(hTest, cElements * cbElement, 1 /*cbAlign*/,
|
---|
121 | RT_BOOL(RTRandAdvU32Ex(hRand, 0, 1)) /*fHead*/, (void **)&pbArray));
|
---|
122 | RTTESTI_CHECK_RETV(pbArray);
|
---|
123 | RTRandAdvBytes(hRand, pbArray, cElements * cbElement);
|
---|
124 |
|
---|
125 | /* sort it */
|
---|
126 | pfnSorter(pbArray, cElements, cbElement, testCompare, (void *)(uintptr_t)cbElement);
|
---|
127 |
|
---|
128 | /* verify it */
|
---|
129 | if (!RTSortIsSorted(pbArray, cElements, cbElement, testCompare, (void *)(uintptr_t)cbElement))
|
---|
130 | RTTestIFailed("failed sorting %u elements of %u size", cElements, cbElement);
|
---|
131 |
|
---|
132 | RTTestGuardedFree(hTest, pbArray);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | int main()
|
---|
138 | {
|
---|
139 | RTTEST hTest;
|
---|
140 | int rc = RTTestInitAndCreate("tstRTTemp", &hTest);
|
---|
141 | if (rc)
|
---|
142 | return rc;
|
---|
143 | RTTestBanner(hTest);
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Test the different algorithms.
|
---|
147 | */
|
---|
148 | testSorter(hTest, RTSortShell, "RTSortShell - shell sort, variable sized element array");
|
---|
149 | testApvSorter(RTSortApvShell, "RTSortApvShell - shell sort, pointer array");
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Summary.
|
---|
153 | */
|
---|
154 | return RTTestSummaryAndDestroy(hTest);
|
---|
155 | }
|
---|
156 |
|
---|