tstHandleTable.cpp revision cc66247640b520463f925a5533fc9e5de06aa982
/* $Id$ */
/** @file
* IPRT Testcase - Handle Tables.
*/
/*
* Copyright (C) 2008 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/handletable.h>
#include <iprt/initterm.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static unsigned g_cErrors;
static DECLCALLBACK(void) tstHandleTableTest1Delete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
{
(*pcCalls)++;
}
static DECLCALLBACK(int) tstHandleTableTest1Retain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
{
(*pcCalls)++;
return VINF_SUCCESS;
}
static int tstHandleTableTest1(uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks, uint32_t fFlags)
{
uint32_t cRetainerCalls = 0;
int rc;
RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
if (RT_FAILURE(rc))
{
return 1;
}
/* fill it */
for (;; i++)
{
uint32_t h;
else
if (RT_SUCCESS(rc))
{
if (h != i)
{
g_cErrors++;
}
}
else if (rc == VERR_NO_MORE_HANDLES)
{
if (i < cMax)
{
g_cErrors++;
}
break;
}
else
{
g_cErrors++;
}
if (!(i % cUnitsPerDot))
{
RTPrintf(".");
}
}
uint32_t const c = i;
RTPrintf(" c=%#x\n", c);
if (fCallbacks && cRetainerCalls != 0)
{
RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected 0!\n", __LINE__, i, cRetainerCalls);
g_cErrors++;
}
/* look up all the entries */
cRetainerCalls = 0;
for (i = uBase; i < c; i++)
{
void *pvObj;
else
if (!pvObj)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
g_cErrors++;
}
if (!(i % cUnitsPerDot))
{
RTPrintf(".");
}
}
RTPrintf("\n");
{
RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
g_cErrors++;
}
/* remove all the entries (in order) */
cRetainerCalls = 0;
for (i = uBase; i < c; i++)
{
void *pvObj;
else
if (!pvObj)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s failed!\n", __LINE__, i, pszWithCtx);
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
g_cErrors++;
}
else if ( fFlags & RTHANDLETABLE_FLAGS_CONTEXT
: RTHandleTableLookup(hHT, i))
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup%s succeeded after free!\n", __LINE__, i, pszWithCtx);
g_cErrors++;
}
if (!(i % cUnitsPerDot))
{
RTPrintf(".");
}
}
RTPrintf("\n");
{
RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
g_cErrors++;
}
/* do a mix of alloc, lookup and free where there is a constant of cDelta handles in the table. */
RTPrintf("tstHandleTable: TESTING Alloc,Lookup,Free mix [cDelta=%#x]..", cDelta); RTStrmFlush(g_pStdOut);
for (i = uBase; i < c * 2; i++)
{
/* alloc */
uint32_t h;
else
if (RT_FAILURE(rc))
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: rc=%Rrc!\n", __LINE__, i, pszWithCtx, rc);
g_cErrors++;
}
else if (h != hExpect)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAlloc%s: h=%u hExpect=%u! - abort sub-test\n", __LINE__, i, pszWithCtx, h, hExpect);
g_cErrors++;
break;
}
{
/* lookup */
{
void *pvObj;
else
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookup%s(,%u,): pvObj=%p expected %p!\n",
g_cErrors++;
}
else if ( (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
g_cErrors++;
}
}
/* free */
void *pvObj;
else
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFree%s: pvObj=%p expected %p!\n",
g_cErrors++;
}
else if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/Free%s: succeeded after free\n",
__LINE__, i, pszWithCtx);
g_cErrors++;
}
}
if (!(i % (cUnitsPerDot * 2)))
{
RTPrintf(".");
}
}
RTPrintf("\n");
/* finally, destroy the table (note that there are 128 entries in it). */
cRetainerCalls = 0;
uint32_t cDeleteCalls = 0;
if (RT_FAILURE(rc))
{
g_cErrors++;
}
return 0;
}
{
/*
* Init the runtime and parse the arguments.
*/
RTR3Init(false, 0);
static RTOPTIONDEF const s_aOptions[] =
{
};
int ch;
int iArg = 1;
switch (ch)
{
case 'b':
break;
case 'm':
break;
case '?':
case 'h':
RTPrintf("syntax: tstIntNet-1 [-pSt] [-d <secs>] [-f <file>] [-r <size>] [-s <size>]\n");
return 1;
default:
if (RT_SUCCESS(ch))
else
return 1;
}
{
return 1;
}
/*
* Do a simple warmup / smoke test first.
*/
tstHandleTableTest1(1, 65534, 63, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
/* Test that the retain and delete functions work. */
tstHandleTableTest1(1, 1024, 256, 256, true, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
/* check that the base works. */
tstHandleTableTest1(0x7ffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
tstHandleTableTest1(0xeffff000, 65534, 4, 2048, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
tstHandleTableTest1(0, 4097, 4, 256, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
tstHandleTableTest1(0, 1024, 4, 128, false, RTHANDLETABLE_FLAGS_CONTEXT | RTHANDLETABLE_FLAGS_LOCKED);
/* For testing 1st level expansion / reallocation. */
/*
* Threaded tests.
*/
/** @todo threaded test for checking out the locking and expansion races. */
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstHandleTable: SUCCESS\n");
else
return !!g_cErrors;
}