tstHandleTable.cpp revision 09034c53c75eab7ea04021a749b98e7f78fae85a
/* $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 fFlags, uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks)
{
int rc;
uint32_t cRetainerCalls = 0;
RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
if (RT_FAILURE(rc))
{
return 1;
}
/* fill it */
for (;; i++)
{
uint32_t h;
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++)
{
if (!pvObj)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx failed!\n", __LINE__, i);
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 = 1; i < c; i++)
{
if (!pvObj)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx failed!\n", __LINE__, i);
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx succeeded after free!\n", __LINE__, i);
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 = 1; i < c * 2; i++)
{
/* alloc */
uint32_t h;
if (RT_FAILURE(rc))
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAllocWithCtx: rc=%Rrc!\n", __LINE__, i, rc);
g_cErrors++;
}
else if (h != hExpect)
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAllocWithCtx: rc=%Rrc!\n", __LINE__, i, rc);
g_cErrors++;
}
if (i > cDelta)
{
/* lookup */
{
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx(,%u,): pvObj=%p expected %p!\n",
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
g_cErrors++;
}
}
/* free */
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFreeWithCtx: pvObj=%p expected %p!\n",
g_cErrors++;
}
{
RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/FreeWithCtx: succeeded after free\n",
__LINE__, i);
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.
*/
/* these two are for the default case. */
/* Test that the retain and delete functions work. */
/* For testing 1st level expansion / reallocation. */
/*
* Threaded tests.
*/
/*
* Summary.
*/
if (!g_cErrors)
RTPrintf("tstHandleTable: SUCCESS\n");
else
return !!g_cErrors;
}