1 | /* $Id: tstRTSort.cpp 57358 2015-08-14 15:16:38Z 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 |
|
---|
38 |
|
---|
39 | /*********************************************************************************************************************************
|
---|
40 | * Structures and Typedefs *
|
---|
41 | *********************************************************************************************************************************/
|
---|
42 | typedef struct TSTRTSORTAPV
|
---|
43 | {
|
---|
44 | uint32_t aValues[8192];
|
---|
45 | void *apv[8192];
|
---|
46 | size_t cElements;
|
---|
47 | } TSTRTSORTAPV;
|
---|
48 |
|
---|
49 |
|
---|
50 | static DECLCALLBACK(int) testApvCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
|
---|
51 | {
|
---|
52 | TSTRTSORTAPV *pData = (TSTRTSORTAPV *)pvUser;
|
---|
53 | uint32_t const *pu32Element1 = (uint32_t const *)pvElement1;
|
---|
54 | uint32_t const *pu32Element2 = (uint32_t const *)pvElement2;
|
---|
55 | RTTESTI_CHECK(VALID_PTR(pData) && pData->cElements <= RT_ELEMENTS(pData->aValues));
|
---|
56 | RTTESTI_CHECK((uintptr_t)(pu32Element1 - &pData->aValues[0]) < pData->cElements);
|
---|
57 | RTTESTI_CHECK((uintptr_t)(pu32Element2 - &pData->aValues[0]) < pData->cElements);
|
---|
58 |
|
---|
59 | if (*pu32Element1 < *pu32Element2)
|
---|
60 | return -1;
|
---|
61 | if (*pu32Element1 > *pu32Element2)
|
---|
62 | return 1;
|
---|
63 | return 0;
|
---|
64 | }
|
---|
65 |
|
---|
66 | static void testApvSorter(FNRTSORTAPV pfnSorter, const char *pszName)
|
---|
67 | {
|
---|
68 | RTTestISub(pszName);
|
---|
69 |
|
---|
70 | RTRAND hRand;
|
---|
71 | RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
|
---|
72 |
|
---|
73 | TSTRTSORTAPV Data;
|
---|
74 | for (size_t cElements = 0; cElements < RT_ELEMENTS(Data.apv); cElements++)
|
---|
75 | {
|
---|
76 | RT_ZERO(Data);
|
---|
77 | Data.cElements = cElements;
|
---|
78 |
|
---|
79 | /* popuplate the array */
|
---|
80 | for (size_t i = 0; i < cElements; i++)
|
---|
81 | {
|
---|
82 | Data.aValues[i] = RTRandAdvU32(hRand);
|
---|
83 | Data.apv[i] = &Data.aValues[i];
|
---|
84 | }
|
---|
85 |
|
---|
86 | /* sort it */
|
---|
87 | pfnSorter(&Data.apv[0], cElements, testApvCompare, &Data);
|
---|
88 |
|
---|
89 | /* verify it */
|
---|
90 | if (!RTSortApvIsSorted(&Data.apv[0], cElements, testApvCompare, &Data))
|
---|
91 | RTTestIFailed("failed sorting %u elements", cElements);
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 |
|
---|
96 | int main()
|
---|
97 | {
|
---|
98 | RTTEST hTest;
|
---|
99 | int rc = RTTestInitAndCreate("tstRTTemp", &hTest);
|
---|
100 | if (rc)
|
---|
101 | return rc;
|
---|
102 | RTTestBanner(hTest);
|
---|
103 |
|
---|
104 | /*
|
---|
105 | * Test the different algorithms.
|
---|
106 | */
|
---|
107 | testApvSorter(RTSortApvShell, "RTSortApvShell - shell sort, pointer array");
|
---|
108 |
|
---|
109 | /*
|
---|
110 | * Summary.
|
---|
111 | */
|
---|
112 | return RTTestSummaryAndDestroy(hTest);
|
---|
113 | }
|
---|
114 |
|
---|