/* $Id$ */
/** @file
* IPRT Testcase - AVL trees.
*/
/*
* Copyright (C) 2006-2010 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/initterm.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
typedef struct TRACKER
{
/** The max key value (exclusive). */
/** The last allocated key. */
/** The number of set bits in the bitmap. */
/** The bitmap size. */
/** Bitmap containing the allocated nodes. */
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/**
* Creates a new tracker.
*
* @returns Pointer to the new tracker.
* @param MaxKey The max key value for the tracker. (exclusive)
*/
{
if (pTracker)
{
}
return pTracker;
}
/**
* Destroys a tracker.
*
* @param pTracker The tracker.
*/
{
}
/**
* Inserts a key range into the tracker.
*
* @returns success indicator.
* @param pTracker The tracker.
* @param Key The first key in the range.
* @param KeyLast The last key in the range. (inclusive)
*/
{
if (fRc)
{
else
fRc = false;
KeyLast--;
}
return fRc;
}
/**
* Removes a key range from the tracker.
*
* @returns success indicator.
* @param pTracker The tracker.
* @param Key The first key in the range.
* @param KeyLast The last key in the range. (inclusive)
*/
{
if (fRc)
{
else
fRc = false;
KeyLast--;
}
return fRc;
}
/**
* Random key range allocation.
*
* @returns success indicator.
* @param pTracker The tracker.
* @param pKey Where to store the first key in the allocated range.
* @param pKeyLast Where to store the first key in the allocated range.
* @param cMaxKey The max range length.
* @remark The caller has to call TrackerInsert.
*/
static bool TrackerNewRandomEx(PTRACKER pTracker, uint32_t *pKey, uint32_t *pKeyLast, uint32_t cMaxKeys)
{
/*
* Find a key.
*/
{
return false;
if (Key2 > 0)
else
{
/* we're missing a ASMBitPrevClear function, so just try another, lower, value.*/
for (;;)
{
break;
if (Key2 > 0)
{
break;
}
}
}
}
/*
* Determine the range.
*/
else
{
if ( Key2 > 0
}
/*
* Return.
*/
if (pKeyLast)
return true;
}
/**
* Random single key allocation.
*
* @returns success indicator.
* @param pTracker The tracker.
* @param pKey Where to store the allocated key.
* @remark The caller has to call TrackerInsert.
*/
{
}
/**
* Random single key 'lookup'.
*
* @returns success indicator.
* @param pTracker The tracker.
* @param pKey Where to store the allocated key.
* @remark The caller has to call TrackerRemove.
*/
{
{
return false;
if (Key2 > 0)
else
{
/* we're missing a ASMBitPrevSet function, so here's a quick replacement hack. */
{
if (*pu32Cur)
{
return true;
}
pu32Cur--;
}
if (Key2 == -1)
{
return false;
}
}
}
return true;
}
/*
bool TrackerAllocSeq(PTRACKER pTracker, uint32_t *pKey, uint32_t *pKeyLast, uint32_t cMaxKeys)
{
return false;
}*/
/**
* Prints an unbuffered char.
* @param ch The char.
*/
{
//RTTestIPrintf(RTTESTLVL_INFO, "%c", ch);
}
/**
* Prints a progress indicator label.
* @param cMax The max number of operations (exclusive).
* @param pszFormat The format string.
* @param ... The arguments to the format string.
*/
{
if (cMax < 10000)
return;
//RTTestIPrintfV(RTTESTLVL_INFO, pszFormat, va);
}
/**
* Prints a progress indicator dot.
* @param iCur The current operation. (can be descending too)
* @param cMax The max number of operations (exclusive).
*/
{
if (cMax < 10000)
return;
ProgressChar('.');
}
{
/*
* Simple linear insert and remove.
*/
if (cMax >= 10000)
unsigned i;
for (i = 0; i < cMax; i++)
{
{
RTTestIFailed("linear left insert i=%d\n", i);
return 1;
}
/* negative. */
{
RTTestIFailed("linear left negative insert i=%d\n", i);
return 1;
}
}
for (i = 0; i < cMax; i++)
{
if (!pNode)
{
RTTestIFailed("linear left remove i=%d\n", i);
return 1;
}
/* negative */
if (pNode)
{
RTTestIFailed("linear left negative remove i=%d\n", i);
return 1;
}
}
/*
* Simple linear insert and remove from the right.
*/
if (cMax >= 10000)
for (i = 0; i < cMax; i++)
{
{
RTTestIFailed("linear right insert i=%d\n", i);
return 1;
}
/* negative. */
{
RTTestIFailed("linear right negative insert i=%d\n", i);
return 1;
}
}
while (i-- > 0)
{
if (!pNode)
{
RTTestIFailed("linear right remove i=%d\n", i);
return 1;
}
/* negative */
if (pNode)
{
RTTestIFailed("linear right negative remove i=%d\n", i);
return 1;
}
}
/*
* Linear insert but root based removal.
*/
if (cMax >= 10000)
for (i = 0; i < cMax; i++)
{
{
RTTestIFailed("linear root insert i=%d\n", i);
return 1;
}
/* negative. */
{
RTTestIFailed("linear root negative insert i=%d\n", i);
return 1;
}
}
while (i-- > 0)
{
if (!pNode)
{
return 1;
}
/* negative */
if (pNode)
{
return 1;
}
}
if (*pTree)
{
RTTestIFailed("sparse remove didn't remove it all!\n");
return 1;
}
/*
* Make a sparsely populated tree and remove the nodes using best fit in 5 cycles.
*/
if (cMaxSparse >= 10000)
for (i = 0; i < cMaxSparse; i += 8)
{
Progress(i, cMaxSparse);
{
RTTestIFailed("sparse insert i=%d\n", i);
return 1;
}
/* negative. */
{
RTTestIFailed("sparse negative insert i=%d\n", i);
return 1;
}
}
/* Remove using best fit in 5 cycles. */
unsigned j;
for (j = 0; j < 4; j++)
{
{
if (!pNode)
{
RTTestIFailed("sparse remove i=%d j=%d\n", i, j);
return 1;
}
{
return 1;
}
}
}
if (*pTree)
{
RTTestIFailed("sparse remove didn't remove it all!\n");
return 1;
}
return 0;
}
{
unsigned i;
/*
* Random tree.
*/
if (cMax >= 10000)
if (!pTracker)
{
return 1;
}
/* Insert a number of nodes in random order. */
for (i = 0; i < cMax; i++)
{
{
RTTestIFailed("failed to allocate node no. %d\n", i);
return 1;
}
{
return 1;
}
/* negative. */
{
return 1;
}
}
/* delete the nodes in random order. */
while (i-- > 0)
{
{
RTTestIFailed("failed to find free node no. %d\n", i);
return 1;
}
if (!pNode)
{
return 1;
}
{
return 1;
}
}
if (*pTree)
{
RTTestIFailed("random remove didn't remove it all!\n");
return 1;
}
return 0;
}
int avlrogcphys(void)
{
unsigned i;
unsigned j;
unsigned k;
RTTestISubF("RTAvlroGCPhys");
/*
* Simple linear insert, get and remove.
*/
/* insert */
for (i = 0; i < 65536; i += 4)
{
{
RTTestIFailed("linear insert i=%d\n", (unsigned)i);
return 1;
}
/* negative. */
for (j = i + 3; j > i - 32; j--)
{
for (k = i; k < i + 32; k++)
{
{
RTTestIFailed("linear negative insert i=%d j=%d k=%d\n", i, j, k);
return 1;
}
}
}
}
/* do gets. */
for (i = 0; i < 65536; i += 4)
{
if (!pNode)
{
RTTestIFailed("linear get i=%d\n", i);
return 1;
}
{
RTTestIFailed("linear get i=%d Key=%d KeyLast=%d\n", i, (unsigned)pNode->Key, (unsigned)pNode->KeyLast);
return 1;
}
for (j = 0; j < 4; j++)
{
{
RTTestIFailed("linear range get i=%d j=%d\n", i, j);
return 1;
}
}
/* negative. */
{
RTTestIFailed("linear negative get i=%d + n\n", i);
return 1;
}
}
/* remove */
for (i = 0; i < 65536; i += 4)
{
if (!pNode)
{
RTTestIFailed("linear remove i=%d\n", i);
return 1;
}
/* negative */
if ( RTAvlroGCPhysRemove(pTree, i)
{
RTTestIFailed("linear negative remove i=%d + n\n", i);
return 1;
}
}
/*
* Make a sparsely populated tree.
*/
for (i = 0; i < 65536; i += 8)
{
{
RTTestIFailed("sparse insert i=%d\n", i);
return 1;
}
/* negative. */
{
{
{
RTTestIFailed("sparse negative insert i=%d j=%d k=%d\n", i, j, k);
return 1;
}
}
}
}
/*
* Get and Remove using range matching in 5 cycles.
*/
for (j = 0; j < 4; j++)
{
for (i = 0; i < 65536; i += 8 * 4)
{
/* gets */
if (!pNode)
{
return 1;
}
{
RTTestIFailed("sparse get i=%d j=%d KeyBase=%d pNode->Key=%d\n", i, j, (unsigned)KeyBase, (unsigned)pNode->Key);
return 1;
}
{
{
RTTestIFailed("sparse range get i=%d j=%d k=%d\n", i, j, k);
return 1;
}
}
/* negative gets */
for (k = i + j; k < KeyBase + 8; k++)
{
if ( k != KeyBase
&& RTAvlroGCPhysGet(pTree, k))
{
RTTestIFailed("sparse negative get i=%d j=%d k=%d\n", i, j, k);
return 1;
}
}
for (k = i + j; k < KeyBase; k++)
{
if (RTAvlroGCPhysRangeGet(pTree, k))
{
RTTestIFailed("sparse negative range get i=%d j=%d k=%d\n", i, j, k);
return 1;
}
}
{
if (RTAvlroGCPhysRangeGet(pTree, k))
{
RTTestIFailed("sparse negative range get i=%d j=%d k=%d\n", i, j, k);
return 1;
}
}
/* remove */
{
return 1;
}
}
}
if (*pTree)
{
RTTestIFailed("sparse remove didn't remove it all!\n");
return 1;
}
/*
* Realworld testcase.
*/
struct
{
{
{
RTTestIFailed("real insert i=%d\n", i);
return 1;
}
{
RTTestIFailed("real negative insert i=%d\n", i);
return 1;
}
{
RTTestIFailed("real get (1) i=%d\n", i);
return 1;
}
{
RTTestIFailed("real negative get (2) i=%d\n", i);
return 1;
}
{
RTTestIFailed("real range get (1) i=%d\n", i);
return 1;
}
{
RTTestIFailed("real range get (2) i=%d\n", i);
return 1;
}
{
RTTestIFailed("real range get (3) i=%d\n", i);
return 1;
}
}
{
{
RTTestIFailed("real get (10) i=%d\n", i);
return 1;
}
{
RTTestIFailed("real range get (10) i=%d\n", i);
return 1;
}
do
{
{
RTTestIFailed("real negative get (11) i=%d j=%#x\n", i, j);
return 1;
}
{
RTTestIFailed("real range get (11) i=%d j=%#x\n", i, j);
return 1;
}
}
return 0;
}
int avlul(void)
{
RTTestISubF("RTAvlUL");
/*
* Simple linear insert and remove.
*/
unsigned i;
/* insert */
for (i = 0; i < 65536; i++)
{
{
RTTestIFailed("linear insert i=%d\n", i);
return 1;
}
/* negative. */
{
RTTestIFailed("linear negative insert i=%d\n", i);
return 1;
}
}
for (i = 0; i < 65536; i++)
{
if (!pNode)
{
RTTestIFailed("linear remove i=%d\n", i);
return 1;
}
/* negative */
if (pNode)
{
RTTestIFailed("linear negative remove i=%d\n", i);
return 1;
}
}
/*
* Make a sparsely populated tree.
*/
for (i = 0; i < 65536; i += 8)
{
{
RTTestIFailed("linear insert i=%d\n", i);
return 1;
}
/* negative. */
{
RTTestIFailed("linear negative insert i=%d\n", i);
return 1;
}
}
/*
* Remove using best fit in 5 cycles.
*/
unsigned j;
for (j = 0; j < 4; j++)
{
for (i = 0; i < 65536; i += 8 * 4)
{
//PAVLULNODECORE pNode = RTAvlULRemove(&pTree, i + j * 8);
if (!pNode)
{
RTTestIFailed("sparse remove i=%d j=%d\n", i, j);
return 1;
}
}
}
return 0;
}
int main()
{
/*
* Init.
*/
if (rc)
return rc;
if (RT_FAILURE(rc))
{
return RTTestSummaryAndDestroy(hTest);
}
/*
* Testing.
*/
unsigned i;
for (i = 32; i < 2048; i++)
if (avlogcphys(i))
break;
RTTestISubF("oGCPhys(32..2048, *1K)");
for (i = 32; i < 4096; i++)
if (avlogcphysRand(i, i + _1K))
break;
for (; i <= _4M; i *= 2)
if (avlogcphysRand(i, i * 8))
break;
avlrogcphys();
avlul();
/*
* Done.
*/
return RTTestSummaryAndDestroy(hTest);
}