VirtualBox

Changeset 62915 in vbox for trunk


Ignore:
Timestamp:
Aug 3, 2016 1:59:43 PM (8 years ago)
Author:
vboxsync
Message:

IPRT/test: Added RTTestDisableAssertions and RTTestRestoreAssertions with RTTestI aliases. This simplifies the use of RTAssertSetQuiet/MayPanic by keeping the saved state in the test handle and dealing with nesting, thus reducing the code necessary when testing error paths in APIs which uses assertions.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/mangling.h

    r62473 r62915  
    19811981# define RTTestCreateEx                                 RT_MANGLER(RTTestCreateEx)
    19821982# define RTTestDestroy                                  RT_MANGLER(RTTestDestroy)
     1983# define RTTestDisableAssertions                        RT_MANGLER(RTTestDisableAssertions)
    19831984# define RTTestErrorCount                               RT_MANGLER(RTTestErrorCount)
    19841985# define RTTestErrorInc                                 RT_MANGLER(RTTestErrorInc)
     
    19911992# define RTTestGuardedAllocTail                         RT_MANGLER(RTTestGuardedAllocTail)
    19921993# define RTTestGuardedFree                              RT_MANGLER(RTTestGuardedFree)
     1994# define RTTestIDisableAssertions                       RT_MANGLER(RTTestIDisableAssertions)
    19931995# define RTTestIErrorCount                              RT_MANGLER(RTTestIErrorCount)
    19941996# define RTTestIErrorInc                                RT_MANGLER(RTTestIErrorInc)
     
    20052007# define RTTestIPrintf                                  RT_MANGLER(RTTestIPrintf)
    20062008# define RTTestIPrintfV                                 RT_MANGLER(RTTestIPrintfV)
     2009# define RTTestIRestoreAssertions                       RT_MANGLER(RTTestIRestoreAssertions)
    20072010# define RTTestISub                                     RT_MANGLER(RTTestISub)
    20082011# define RTTestISubDone                                 RT_MANGLER(RTTestISubDone)
     
    20182021# define RTTestPrintfNlV                                RT_MANGLER(RTTestPrintfNlV)
    20192022# define RTTestPrintfV                                  RT_MANGLER(RTTestPrintfV)
     2023# define RTTestRestoreAssertions                        RT_MANGLER(RTTestRestoreAssertions)
    20202024# define RTTestSetDefault                               RT_MANGLER(RTTestSetDefault)
    20212025# define RTTestSkipAndDestroy                           RT_MANGLER(RTTestSkipAndDestroy)
  • trunk/include/iprt/test.h

    r62473 r62915  
    636636RTR3DECL(int) RTTestFailureDetails(RTTEST hTest, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
    637637
     638/**
     639 * Disables and shuts up assertions.
     640 *
     641 * Max 8 nestings.
     642 *
     643 * @returns IPRT status code.
     644 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     645 *                      associated with the calling thread.
     646 * @sa      RTAssertSetMayPanic, RTAssertSetQuiet.
     647 */
     648RTR3DECL(int) RTTestDisableAssertions(RTTEST hTest);
     649
     650/**
     651 * Restores the previous call to RTTestDisableAssertions.
     652 *
     653 * @returns IPRT status code.
     654 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     655 *                      associated with the calling thread.
     656 */
     657RTR3DECL(int) RTTestRestoreAssertions(RTTEST hTest);
     658
    638659
    639660/** @def RTTEST_CHECK
     
    10921113 */
    10931114RTR3DECL(int) RTTestIFailureDetails(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
     1115
     1116/**
     1117 * Disables and shuts up assertions.
     1118 *
     1119 * Max 8 nestings.
     1120 *
     1121 * @returns IPRT status code.
     1122 * @sa      RTAssertSetMayPanic, RTAssertSetQuiet.
     1123 */
     1124RTR3DECL(int) RTTestIDisableAssertions(void);
     1125
     1126/**
     1127 * Restores the previous call to RTTestDisableAssertions.
     1128 *
     1129 * @returns IPRT status code.
     1130 */
     1131RTR3DECL(int) RTTestIRestoreAssertions(void);
    10941132
    10951133
  • trunk/src/VBox/Runtime/r3/test.cpp

    r62477 r62915  
    145145    /** XML element stack. */
    146146    const char         *apszXmlElements[10];
     147
     148    /** Number of times assertions has been disabled and quieted. */
     149    uint32_t volatile   cAssertionsDisabledAndQuieted;
     150    /** Saved RTAssertSetQuiet return code. */
     151    bool                fAssertSavedQuiet;
     152    /** Saved RTAssertSetMayPanic return code. */
     153    bool                fAssertSavedMayPanic;
    147154} RTTESTINT;
    148155/** Pointer to a test instance. */
     
    269276    pTest->hXmlFile         = NIL_RTFILE;
    270277    pTest->cXmlElements     = 0;
     278    pTest->cAssertionsDisabledAndQuieted = 0;
     279    pTest->fAssertSavedMayPanic          = true;
     280    pTest->fAssertSavedQuiet             = false;
    271281
    272282    rc = RTCritSectInit(&pTest->Lock);
     
    17611771}
    17621772
     1773
     1774RTR3DECL(int) RTTestDisableAssertions(RTTEST hTest)
     1775{
     1776    PRTTESTINT pTest = hTest;
     1777    RTTEST_GET_VALID_RETURN(pTest);
     1778
     1779    uint32_t cTimes = ASMAtomicIncU32(&pTest->cAssertionsDisabledAndQuieted);
     1780    if (cTimes >= 2 && cTimes <= 8)
     1781        return VINF_SUCCESS;
     1782    if (cTimes > 8)
     1783    {
     1784        RTAssertSetMayPanic(pTest->fAssertSavedMayPanic);
     1785        RTAssertSetQuiet(pTest->fAssertSavedQuiet);
     1786        Assert(cTimes <= 8);
     1787    }
     1788    pTest->fAssertSavedMayPanic = RTAssertSetMayPanic(false);
     1789    pTest->fAssertSavedQuiet    = RTAssertSetQuiet(true);
     1790    return VINF_SUCCESS;
     1791}
     1792
     1793
     1794RTR3DECL(int) RTTestRestoreAssertions(RTTEST hTest)
     1795{
     1796    PRTTESTINT pTest = hTest;
     1797    RTTEST_GET_VALID_RETURN(pTest);
     1798
     1799    uint32_t cTimes = ASMAtomicDecU32(&pTest->cAssertionsDisabledAndQuieted);
     1800    if (cTimes == 0)
     1801    {
     1802        RTAssertSetMayPanic(pTest->fAssertSavedMayPanic);
     1803        RTAssertSetQuiet(pTest->fAssertSavedQuiet);
     1804    }
     1805    else
     1806        AssertStmt(cTimes < UINT32_MAX / 2, ASMAtomicIncU32(&pTest->cAssertionsDisabledAndQuieted));
     1807    return VINF_SUCCESS;
     1808}
     1809
  • trunk/src/VBox/Runtime/r3/testi.cpp

    r62477 r62915  
    175175}
    176176
     177
     178RTR3DECL(int) RTTestIDisableAssertions(void)
     179{
     180    return RTTestDisableAssertions(NIL_RTTEST);
     181}
     182
     183
     184RTR3DECL(int) RTTestIRestoreAssertions(void)
     185{
     186    return RTTestRestoreAssertions(NIL_RTTEST);
     187}
     188
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