VirtualBox

source: vbox/trunk/src/VBox/Runtime/testcase/tstRTSort.cpp@ 27955

Last change on this file since 27955 was 27345, checked in by vboxsync, 15 years ago

iprt: Added RTSort with one simple algorithm implemented.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.7 KB
Line 
1/* $Id: tstRTSort.cpp 27345 2010-03-14 18:33:46Z vboxsync $ */
2/** @file
3 * IPRT Testcase - Sorting.
4 */
5
6/*
7 * Copyright (C) 2010 Sun Microsystems, Inc.
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 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/sort.h>
35
36#include <iprt/err.h>
37#include <iprt/rand.h>
38#include <iprt/string.h>
39#include <iprt/test.h>
40
41
42/*******************************************************************************
43* Structures and Typedefs *
44*******************************************************************************/
45typedef struct TSTRTSORTAPV
46{
47 uint32_t aValues[8192];
48 void *apv[8192];
49 size_t cElements;
50} TSTRTSORTAPV;
51
52
53static DECLCALLBACK(int) testApvCompare(void const *pvElement1, void const *pvElement2, void *pvUser)
54{
55 TSTRTSORTAPV *pData = (TSTRTSORTAPV *)pvUser;
56 uint32_t const *pu32Element1 = (uint32_t const *)pvElement1;
57 uint32_t const *pu32Element2 = (uint32_t const *)pvElement2;
58 RTTESTI_CHECK(VALID_PTR(pData) && pData->cElements <= RT_ELEMENTS(pData->aValues));
59 RTTESTI_CHECK((uintptr_t)(pu32Element1 - &pData->aValues[0]) < pData->cElements);
60 RTTESTI_CHECK((uintptr_t)(pu32Element2 - &pData->aValues[0]) < pData->cElements);
61
62 if (*pu32Element1 < *pu32Element2)
63 return -1;
64 if (*pu32Element1 > *pu32Element2)
65 return 1;
66 return 0;
67}
68
69static void testApvSorter(FNRTSORTAPV pfnSorter, const char *pszName)
70{
71 RTTestISub(pszName);
72
73 RTRAND hRand;
74 RTTESTI_CHECK_RC_OK_RETV(RTRandAdvCreateParkMiller(&hRand));
75
76 TSTRTSORTAPV Data;
77 for (size_t cElements = 0; cElements < RT_ELEMENTS(Data.apv); cElements++)
78 {
79 RT_ZERO(Data);
80 Data.cElements = cElements;
81
82 /* popuplate the array */
83 for (size_t i = 0; i < cElements; i++)
84 {
85 Data.aValues[i] = RTRandAdvU32(hRand);
86 Data.apv[i] = &Data.aValues[i];
87 }
88
89 /* sort it */
90 pfnSorter(&Data.apv[0], cElements, testApvCompare, &Data);
91
92 /* verify it */
93 if (!RTSortApvIsSorted(&Data.apv[0], cElements, testApvCompare, &Data))
94 RTTestIFailed("failed sorting %u elements", cElements);
95 }
96}
97
98
99int main()
100{
101 RTTEST hTest;
102 int rc = RTTestInitAndCreate("tstRTTemp", &hTest);
103 if (rc)
104 return rc;
105 RTTestBanner(hTest);
106
107 /*
108 * Test the different algorithms.
109 */
110 testApvSorter(RTSortApvShell, "RTSortApvShell - shell sort, pointer array");
111
112 /*
113 * Summary.
114 */
115 return RTTestSummaryAndDestroy(hTest);
116}
117
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette