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