VirtualBox

Changeset 10788 in vbox for trunk/src/VBox/Runtime/testcase


Ignore:
Timestamp:
Jul 21, 2008 4:44:33 PM (17 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
33560
Message:

IPRT: Implemented the context variant of the handle table.

Location:
trunk/src/VBox/Runtime/testcase
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/Makefile.kmk

    r10419 r10788  
    6363        tstFileLock \
    6464        tstGetOpt \
     65        tstHandleTable \
    6566        tstHeapSimple \
    6667        tstInlineAsm \
     
    153154tstGetOpt_SOURCES = tstGetOpt.cpp
    154155
     156tstHandleTable_SOURCES = tstHandleTable.cpp
     157
    155158tstHeapSimple_SOURCES = tstHeapSimple.cpp
    156159
  • trunk/src/VBox/Runtime/testcase/tstHandleTable.cpp

    r10772 r10788  
    11/* $Id$ */
    22/** @file
    3  * IPRT Testcase - Native Loader.
     3 * IPRT Testcase - Handle Tables.
    44 */
    55
    66/*
    7  * Copyright (C) 2006-2007 Sun Microsystems, Inc.
     7 * Copyright (C) 2008 Sun Microsystems, Inc.
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2929 */
    3030
    31 
    32 #include <iprt/ldr.h>
     31/*******************************************************************************
     32*   Header Files                                                               *
     33*******************************************************************************/
     34#include <iprt/handletable.h>
    3335#include <iprt/stream.h>
    34 #include <iprt/runtime.h>
     36#include <iprt/initterm.h>
    3537#include <iprt/err.h>
    36 
    37 int main(int argc, const char * const *argv)
     38#include <iprt/getopt.h>
     39
     40
     41/*******************************************************************************
     42*   Global Variables                                                           *
     43*******************************************************************************/
     44static unsigned g_cErrors;
     45
     46static DECLCALLBACK(void) tstHandleTableTest1Delete(RTHANDLETABLE hHandleTable, uint32_t h, void *pvObj, void *pvCtx, void *pvUser)
    3847{
    39     int rcRet = 0;
    40     RTR3Init();
    41 
     48    uint32_t *pcCalls = (uint32_t *)pvUser;
     49    (*pcCalls)++;
     50}
     51
     52static DECLCALLBACK(int) tstHandleTableTest1Retain(RTHANDLETABLE hHandleTable, void *pvObj, void *pvCtx, void *pvUser)
     53{
     54    uint32_t *pcCalls = (uint32_t *)pvUser;
     55    (*pcCalls)++;
     56    return VINF_SUCCESS;
     57}
     58
     59static int tstHandleTableTest1(uint32_t fFlags, uint32_t uBase, uint32_t cMax, uint32_t cDelta, uint32_t cUnitsPerDot, bool fCallbacks)
     60{
     61    int rc;
     62    uint32_t cRetainerCalls = 0;
     63
     64    RTPrintf("tstHandleTable: TESTING RTHandleTableCreateEx(, 0");
     65    fFlags |= RTHANDLETABLE_FLAGS_CONTEXT;
     66    if (fFlags & RTHANDLETABLE_FLAGS_LOCKED)    RTPrintf(" | LOCKED");
     67    if (fFlags & RTHANDLETABLE_FLAGS_CONTEXT)   RTPrintf(" | CONTEXT");
     68    RTPrintf(", %#x, %#x,,)...\n", uBase, cMax);
     69
     70    RTHANDLETABLE hHT;
     71    rc = RTHandleTableCreateEx(&hHT, fFlags, uBase, cMax,
     72                               fCallbacks ? tstHandleTableTest1Retain : NULL,
     73                               fCallbacks ? &cRetainerCalls : NULL);
     74    if (RT_FAILURE(rc))
     75    {
     76        RTPrintf("\ntstHandleTable: FAILURE - RTHandleTableCreateEx failed, %Rrc!\n", rc);
     77        return 1;
     78    }
     79
     80    /* fill it */
     81    RTPrintf("tstHandleTable: TESTING   RTHandleTableAllocWithCtx.."); RTStrmFlush(g_pStdOut);
     82    uint32_t i = uBase;
     83    for (;; i++)
     84    {
     85        uint32_t h;
     86        rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)i * 4), NULL, &h);
     87        if (RT_SUCCESS(rc))
     88        {
     89            if (h != i)
     90            {
     91                RTPrintf("\ntstHandleTable: FAILURE (%d) - h=%d, expected %d!\n", __LINE__, h, i);
     92                g_cErrors++;
     93            }
     94        }
     95        else if (rc == VERR_NO_MORE_HANDLES)
     96        {
     97            if (i < cMax)
     98            {
     99                RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, expected > 65534!\n", __LINE__, i);
     100                g_cErrors++;
     101            }
     102            break;
     103        }
     104        else
     105        {
     106            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, rc=%Rrc!\n", __LINE__, i, rc);
     107            g_cErrors++;
     108        }
     109        if (!(i % cUnitsPerDot))
     110        {
     111            RTPrintf(".");
     112            RTStrmFlush(g_pStdOut);
     113        }
     114    }
     115    uint32_t const c = i;
     116    RTPrintf(" c=%#x\n", c);
     117    if (fCallbacks && cRetainerCalls != 0)
     118    {
     119        RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected 0!\n", __LINE__, i, cRetainerCalls);
     120        g_cErrors++;
     121    }
     122
     123    /* look up all the entries */
     124    RTPrintf("tstHandleTable: TESTING   RTHandleTableLookupWithCtx.."); RTStrmFlush(g_pStdOut);
     125    cRetainerCalls = 0;
     126    for (i = uBase; i < c; i++)
     127    {
     128        void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
     129        void *pvObj = RTHandleTableLookupWithCtx(hHT, i, NULL);
     130        if (!pvObj)
     131        {
     132            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx failed!\n", __LINE__, i);
     133            g_cErrors++;
     134        }
     135        else if (pvObj != pvExpect)
     136        {
     137            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
     138            g_cErrors++;
     139        }
     140        if (!(i % cUnitsPerDot))
     141        {
     142            RTPrintf(".");
     143            RTStrmFlush(g_pStdOut);
     144        }
     145    }
     146    RTPrintf("\n");
     147    if (fCallbacks && cRetainerCalls != c - uBase)
     148    {
     149        RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
     150        g_cErrors++;
     151    }
     152
     153    /* remove all the entries (in order) */
     154    RTPrintf("tstHandleTable: TESTING   RTHandleTableFreeWithCtx.."); RTStrmFlush(g_pStdOut);
     155    cRetainerCalls = 0;
     156    for (i = 1; i < c; i++)
     157    {
     158        void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)i * 4);
     159        void *pvObj = RTHandleTableFreeWithCtx(hHT, i, NULL);
     160        if (!pvObj)
     161        {
     162            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx failed!\n", __LINE__, i);
     163            g_cErrors++;
     164        }
     165        else if (pvObj != pvExpect)
     166        {
     167            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, pvObj=%p expected %p\n", __LINE__, i, pvObj, pvExpect);
     168            g_cErrors++;
     169        }
     170        else if (RTHandleTableLookupWithCtx(hHT, i, NULL))
     171        {
     172            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookupWithCtx succeeded after free!\n", __LINE__, i);
     173            g_cErrors++;
     174        }
     175        if (!(i % cUnitsPerDot))
     176        {
     177            RTPrintf(".");
     178            RTStrmFlush(g_pStdOut);
     179        }
     180    }
     181    RTPrintf("\n");
     182    if (fCallbacks && cRetainerCalls != c - uBase)
     183    {
     184        RTPrintf("tstHandleTable: FAILURE (%d) - cRetainerCalls=%#x expected %#x!\n", __LINE__, cRetainerCalls, c - uBase);
     185        g_cErrors++;
     186    }
     187
     188    /* do a mix of alloc, lookup and free where there is a constant of cDelta handles in the table. */
     189    RTPrintf("tstHandleTable: TESTING   Alloc,Lookup,Free mix [cDelta=%#x]..", cDelta); RTStrmFlush(g_pStdOut);
     190    for (i = 1; i < c * 2; i++)
     191    {
     192        /* alloc */
     193        uint32_t hExpect = ((i - 1) % (c - 1)) + 1;
     194        uint32_t h;
     195        rc = RTHandleTableAllocWithCtx(hHT, (void *)((uintptr_t)&i + (uintptr_t)hExpect * 4), NULL, &h);
     196        if (RT_FAILURE(rc))
     197        {
     198            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAllocWithCtx: rc=%Rrc!\n", __LINE__, i, rc);
     199            g_cErrors++;
     200        }
     201        else if (h != hExpect)
     202        {
     203            RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableAllocWithCtx: rc=%Rrc!\n", __LINE__, i, rc);
     204            g_cErrors++;
     205        }
     206
     207        if (i > cDelta)
     208        {
     209            /* lookup */
     210            for (uint32_t j = i - cDelta; j <= i; j++)
     211            {
     212                uint32_t hLookup = ((j - 1) % (c - 1)) + 1;
     213                void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hLookup * 4);
     214                void *pvObj = RTHandleTableLookupWithCtx(hHT, hLookup, NULL);
     215                if (pvObj != pvExpect)
     216                {
     217                    RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx(,%u,): pvObj=%p expected %p!\n",
     218                             __LINE__, i, j, hLookup, pvObj, pvExpect);
     219                    g_cErrors++;
     220                }
     221                else if (RTHandleTableLookupWithCtx(hHT, hLookup, &i))
     222                {
     223                    RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, j=%d, RTHandleTableLookupWithCtx: succeeded with bad context\n",
     224                             __LINE__, i, j, pvObj, pvExpect);
     225                    g_cErrors++;
     226                }
     227            }
     228
     229            /* free */
     230            uint32_t hFree = ((i - 1 - cDelta) % (c - 1)) + 1;
     231            void *pvExpect = (void *)((uintptr_t)&i + (uintptr_t)hFree * 4);
     232            void *pvObj = RTHandleTableFreeWithCtx(hHT, hFree, NULL);
     233            if (pvObj != pvExpect)
     234            {
     235                RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableFreeWithCtx: pvObj=%p expected %p!\n",
     236                         __LINE__, i, pvObj, pvExpect);
     237                g_cErrors++;
     238            }
     239            else if (   RTHandleTableLookupWithCtx(hHT, hFree, NULL)
     240                     || RTHandleTableFreeWithCtx(hHT, hFree, NULL))
     241            {
     242                RTPrintf("\ntstHandleTable: FAILURE (%d) - i=%d, RTHandleTableLookup/FreeWithCtx: succeeded after free\n",
     243                         __LINE__, i);
     244                g_cErrors++;
     245            }
     246        }
     247        if (!(i % (cUnitsPerDot * 2)))
     248        {
     249            RTPrintf(".");
     250            RTStrmFlush(g_pStdOut);
     251        }
     252    }
     253    RTPrintf("\n");
     254
     255    /* finally, destroy the table (note that there are 128 entries in it). */
     256    cRetainerCalls = 0;
     257    uint32_t cDeleteCalls = 0;
     258    rc = RTHandleTableDestroy(hHT,
     259                              fCallbacks ? tstHandleTableTest1Delete : NULL,
     260                              fCallbacks ? &cDeleteCalls : NULL);
     261    if (RT_FAILURE(rc))
     262    {
     263        RTPrintf("tstHandleTable: FAILURE (%d) - RTHandleTableDestroy failed, %Rrc!\n", __LINE__, rc);
     264        g_cErrors++;
     265    }
     266
     267    return 0;
     268}
     269
     270int main(int argc, char **argv)
     271{
    42272    /*
    43      * If no args, display usage.
     273     * Init the runtime and parse the arguments.
    44274     */
    45     if (argc <= 1)
    46     {
    47         RTPrintf("Syntax: %s [so/dll [so/dll [..]]\n", argv[0]);
     275    RTR3Init(false, 0);
     276
     277    static RTOPTIONDEF const s_aOptions[] =
     278    {
     279        { "--base",         'b', RTGETOPT_REQ_UINT32 },
     280        { "--max",          'm', RTGETOPT_REQ_UINT32 },
     281    };
     282
     283    uint32_t uBase = 0;
     284    uint32_t cMax = 0;
     285
     286    int ch;
     287    int iArg = 1;
     288    RTOPTIONUNION Value;
     289    while ((ch = RTGetOpt(argc,argv, &s_aOptions[0], RT_ELEMENTS(s_aOptions), &iArg, &Value)))
     290        switch (ch)
     291        {
     292            case 'b':
     293                uBase = Value.u32;
     294                break;
     295
     296            case 'm':
     297                cMax = Value.u32;
     298                break;
     299
     300            case '?':
     301            case 'h':
     302                RTPrintf("syntax: tstIntNet-1 [-pSt] [-d <secs>] [-f <file>] [-r <size>] [-s <size>]\n");
     303                return 1;
     304
     305            default:
     306                if (RT_SUCCESS(ch))
     307                    RTPrintf("tstHandleTable: invalid argument (%#x): %s\n", ch, Value.psz);
     308                else
     309                    RTPrintf("tstHandleTable: invalid argument: %Rrc - \n", ch, Value.pDef->pszLong);
     310                return 1;
     311        }
     312    if (iArg < argc)
     313    {
     314        RTPrintf("tstHandleTable: invalid argument: %s\n", argv[iArg]);
    48315        return 1;
    49316    }
    50317
    51318    /*
    52      * Iterate the arguments and treat all of them as so/dll paths.
     319     * Do a simple warmup / smoke test first.
    53320     */
    54     for (int i = 1; i < argc; i++)
    55     {
    56         RTLDRMOD hLdrMod = (RTLDRMOD)0xbaadffaa;
    57         int rc = RTLdrLoad(argv[i], &hLdrMod);
    58         if (RT_SUCCESS(rc))
    59         {
    60             RTPrintf("tstLdrLoad: %d - %s\n", i, argv[i]);
    61             rc = RTLdrClose(hLdrMod);
    62             if (RT_FAILURE(rc))
    63             {
    64                 RTPrintf("tstLdrLoad: rc=%Rrc RTLdrClose()\n", rc);
    65                 rcRet++;
    66             }
    67         }
    68         else
    69         {
    70             RTPrintf("tstLdrLoad: rc=%Rrc RTLdrOpen('%s')\n", rc, argv[i]);
    71             rcRet++;
    72         }
    73     }
     321    /* these two are for the default case. */
     322    tstHandleTableTest1(0,                              1,       65534,  128,           2048, false);
     323    tstHandleTableTest1(RTHANDLETABLE_FLAGS_LOCKED,     1,       65534,   63,           2048, false);
     324    /* Test that the retain and delete functions work. */
     325    tstHandleTableTest1(RTHANDLETABLE_FLAGS_LOCKED,     1,       1024,   256,            256,  true);
     326    /* For testing 1st level expansion / reallocation. */
     327    tstHandleTableTest1(0,                              1, 1024*1024*8,   3,          150000, false);
     328
     329    /*
     330     * Threaded tests.
     331     */
     332
    74333
    75334    /*
    76335     * Summary.
    77336     */
    78     if (!rcRet)
    79         RTPrintf("tstLdrLoad: SUCCESS\n");
     337    if (!g_cErrors)
     338        RTPrintf("tstHandleTable: SUCCESS\n");
    80339    else
    81         RTPrintf("tstLdrLoad: FAILURE - %d errors\n", rcRet);
    82 
    83     return !!rcRet;
     340        RTPrintf("tstHandleTable: FAILURE - %d errors\n", g_cErrors);
     341
     342    return !!g_cErrors;
    84343}
Note: See TracChangeset for help on using the changeset viewer.

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