VirtualBox

Changeset 18569 in vbox for trunk/src/VBox/Runtime


Ignore:
Timestamp:
Mar 31, 2009 1:07:20 PM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
45463
Message:

RTTest: sub tests, more typical reporting of those.

Location:
trunk/src/VBox/Runtime
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/r3/test.cpp

    r18427 r18569  
    8686    const char         *pszTest;
    8787    /** The length of the test name.  */
    88     unsigned            cchTest;
     88    size_t              cchTest;
    8989    /** The size of a guard. Multiple of PAGE_SIZE. */
    9090    uint32_t            cbGuard;
    91 
    92     /** Critical section seralizing access to the members following it. */
    93     RTCRITSECT          Lock;
    94     /** The list of guarded memory allocations. */
    95     PRTTESTGUARDEDMEM   pGuardedMem;
     91    /** The verbosity level. */
     92    RTTESTLVL           enmMaxLevel;
     93
    9694
    9795    /** Critical section seralizing output. */
     
    10199    /** Whether we're currently at a newline. */
    102100    bool                fNewLine;
     101
     102
     103    /** Critical section seralizing access to the members following it. */
     104    RTCRITSECT          Lock;
     105
     106    /** The list of guarded memory allocations. */
     107    PRTTESTGUARDEDMEM   pGuardedMem;
     108
     109    /** The current sub-test. */
     110    const char         *pszSubTest;
     111    /** The lenght of the sub-test name. */
     112    size_t              cchSubTest;
     113    /** Whether we've reported the sub-test result or not. */
     114    bool                fSubTestReported;
     115    /** The start error count of the current subtest. */
     116    uint32_t            cSubTestAtErrors;
     117
     118    /** The number of sub tests. */
     119    uint32_t            cSubTests;
     120    /** The number of sub tests that failed. */
     121    uint32_t            cSubTestsFailed;
    103122
    104123} RTTESTINT;
     
    195214    if (!pTest)
    196215        return VERR_NO_MEMORY;
    197     pTest->u32Magic = RTTESTINT_MAGIC;
    198     pTest->cbGuard  = PAGE_SIZE * 7;
    199     pTest->pszTest  = RTStrDup(pszTest);
    200     pTest->cchTest  = (unsigned)strlen(pszTest);
    201     pTest->pOutStrm = g_pStdOut;
    202     pTest->fNewLine = true;
     216    pTest->u32Magic         = RTTESTINT_MAGIC;
     217    pTest->pszTest          = RTStrDup(pszTest);
     218    pTest->cchTest          = strlen(pszTest);
     219    pTest->cbGuard          = PAGE_SIZE * 7;
     220    pTest->enmMaxLevel      = RTTESTLVL_SUB_TEST;
     221
     222    pTest->pOutStrm         = g_pStdOut;
     223    pTest->fNewLine         = true;
     224
     225    pTest->pGuardedMem      = NULL;
     226
     227    pTest->pszSubTest       = NULL;
     228    pTest->cchSubTest       = 0;
     229    pTest->fSubTestReported = true;
     230    pTest->cSubTestAtErrors = 0;
     231    pTest->cSubTests        = 0;
     232    pTest->cSubTestsFailed  = 0;
    203233
    204234    rc = RTCritSectInit(&pTest->Lock);
     
    269299    }
    270300
     301    RTStrFree((char *)pTest->pszSubTest);
     302    pTest->pszSubTest = NULL;
    271303    RTStrFree((char *)pTest->pszTest);
    272304    pTest->pszTest = NULL;
     
    544576 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    545577 *                      associated with the calling thread.
     578 * @param   enmLevel    Message importance level.
    546579 * @param   pszFormat   The message.
    547580 * @param   va          Arguments.
    548581 */
    549 RTR3DECL(int) RTTestPrintfNlV(RTTEST hTest, const char *pszFormat, va_list va)
     582RTR3DECL(int) RTTestPrintfNlV(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, va_list va)
    550583{
    551584    PRTTESTINT pTest = hTest;
     
    555588
    556589    int cch = 0;
    557     if (!pTest->fNewLine)
    558         cch += rtTestPrintf(pTest, "\n");
    559     cch += rtTestPrintfV(pTest, pszFormat, va);
     590    if (enmLevel <= pTest->enmMaxLevel)
     591    {
     592        if (!pTest->fNewLine)
     593            cch += rtTestPrintf(pTest, "\n");
     594        cch += rtTestPrintfV(pTest, pszFormat, va);
     595    }
    560596
    561597    RTCritSectLeave(&pTest->OutputLock);
     
    571607 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    572608 *                      associated with the calling thread.
     609 * @param   enmLevel    Message importance level.
    573610 * @param   pszFormat   The message.
    574611 * @param   ...         Arguments.
    575612 */
    576 RTR3DECL(int) RTTestPrintfNl(RTTEST hTest, const char *pszFormat, ...)
     613RTR3DECL(int) RTTestPrintfNl(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, ...)
    577614{
    578615    va_list va;
    579616
    580617    va_start(va, pszFormat);
    581     int cch = RTTestPrintfNlV(hTest, pszFormat, va);
     618    int cch = RTTestPrintfNlV(hTest, enmLevel, pszFormat, va);
    582619    va_end(va);
    583620
     
    592629 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    593630 *                      associated with the calling thread.
     631 * @param   enmLevel    Message importance level.
    594632 * @param   pszFormat   The message.
    595633 * @param   va          Arguments.
    596634 */
    597 RTR3DECL(int) RTTestPrintfV(RTTEST hTest, const char *pszFormat, va_list va)
     635RTR3DECL(int) RTTestPrintfV(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, va_list va)
    598636{
    599637    PRTTESTINT pTest = hTest;
     
    601639
    602640    RTCritSectEnter(&pTest->OutputLock);
    603     int cch = rtTestPrintfV(pTest, pszFormat, va);
     641    int cch = 0;
     642    if (enmLevel <= pTest->enmMaxLevel)
     643        cch += rtTestPrintfV(pTest, pszFormat, va);
    604644    RTCritSectLeave(&pTest->OutputLock);
    605645
     
    614654 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    615655 *                      associated with the calling thread.
     656 * @param   enmLevel    Message importance level.
    616657 * @param   pszFormat   The message.
    617658 * @param   ...         Arguments.
    618659 */
    619 RTR3DECL(int) RTTestPrintf(RTTEST hTest, const char *pszFormat, ...)
     660RTR3DECL(int) RTTestPrintf(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, ...)
    620661{
    621662    va_list va;
    622663
    623664    va_start(va, pszFormat);
    624     int cch = RTTestPrintfV(hTest, pszFormat, va);
     665    int cch = RTTestPrintfV(hTest, enmLevel, pszFormat, va);
    625666    va_end(va);
    626667
     
    638679RTR3DECL(int) RTTestBanner(RTTEST hTest)
    639680{
    640     return RTTestPrintfNl(hTest, "TESTING...\n");
     681    return RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "TESTING...\n");
     682}
     683
     684
     685/**
     686 * Prints the result of a sub-test if necessary.
     687 *
     688 * @returns Number of chars printed.
     689 * @param   pTest       The test instance.
     690 * @remarks Caller own the test Lock.
     691 */
     692static int rtTestSubTestReport(PRTTESTINT pTest)
     693{
     694    int cch = 0;
     695    if (    !pTest->fSubTestReported
     696        &&  pTest->pszSubTest)
     697    {
     698        pTest->fSubTestReported = true;
     699        uint32_t cErrors = ASMAtomicUoReadU32(&pTest->cErrors) - pTest->cSubTestAtErrors;
     700        if (!cErrors)
     701            cch += RTTestPrintf(pTest, RTTESTLVL_SUB_TEST, "%-50s: PASSED\n", pTest->pszSubTest);
     702        else
     703        {
     704            pTest->cSubTestsFailed++;
     705            cch += RTTestPrintf(pTest, RTTESTLVL_SUB_TEST, "%-50s: FAILED (%u errors)\n",
     706                                pTest->pszSubTest, cErrors);
     707        }
     708    }
     709    return cch;
     710}
     711
     712
     713/**
     714 * RTTestSub and RTTestSubDone worker that cleans up the current (if any)
     715 * sub test.
     716 *
     717 * @returns Number of chars printed.
     718 * @param   pTest       The test instance.
     719 * @remarks Caller own the test Lock.
     720 */
     721static int rtTestSubCleanup(PRTTESTINT pTest)
     722{
     723    int cch = 0;
     724    if (pTest->pszSubTest)
     725    {
     726        cch += rtTestSubTestReport(pTest);
     727
     728        RTStrFree((char *)pTest->pszSubTest);
     729        pTest->pszSubTest = NULL;
     730        pTest->fSubTestReported = true;
     731    }
     732    return cch;
    641733}
    642734
     
    654746    RTTEST_GET_VALID_RETURN_RC(pTest, 2);
    655747
     748    RTCritSectEnter(&pTest->Lock);
     749    rtTestSubTestReport(pTest);
     750    RTCritSectLeave(&pTest->Lock);
     751
    656752    int rc;
    657753    if (!pTest->cErrors)
    658754    {
    659         RTTestPrintfNl(hTest, "SUCCESS\n", pTest->cErrors);
     755        RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "SUCCESS\n", pTest->cErrors);
    660756        rc = 0;
    661757    }
    662758    else
    663759    {
    664         RTTestPrintfNl(hTest, "FAILURE - %u errors\n", pTest->cErrors);
     760        RTTestPrintfNl(hTest, RTTESTLVL_ALWAYS, "FAILURE - %u errors\n", pTest->cErrors);
    665761        rc = 1;
    666762    }
     763
    667764
    668765    RTTestDestroy(pTest);
     
    672769
    673770/**
     771 * Starts a sub-test.
     772 *
     773 * This will perform an implicit RTTestSubDone() call if that has not been done
     774 * since the last RTTestSub call.
     775 *
     776 * @returns Number of chars printed.
     777 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     778 *                      associated with the calling thread.
     779 * @param   pszSubTest  The sub-test name
     780 */
     781RTR3DECL(int) RTTestSub(RTTEST hTest, const char *pszSubTest)
     782{
     783    PRTTESTINT pTest = hTest;
     784    RTTEST_GET_VALID_RETURN_RC(pTest, -1);
     785
     786    RTCritSectEnter(&pTest->Lock);
     787
     788    /* Cleanup, reporting if necessary previous sub test. */
     789    rtTestSubCleanup(pTest);
     790
     791    /* Start new sub test. */
     792    pTest->cSubTests++;
     793    pTest->cSubTestAtErrors = ASMAtomicUoReadU32(&pTest->cErrors);
     794    pTest->pszSubTest = RTStrDup(pszSubTest);
     795    pTest->cchSubTest = strlen(pszSubTest);
     796    pTest->fSubTestReported = false;
     797
     798    int cch = 0;
     799    if (pTest->enmMaxLevel >= RTTESTLVL_DEBUG)
     800        cch = RTTestPrintfNl(hTest, RTTESTLVL_DEBUG, "debug: Starting sub-test '%s'\n", pszSubTest);
     801
     802    RTCritSectLeave(&pTest->Lock);
     803
     804    return cch;
     805}
     806
     807
     808/**
     809 * Completes a sub-test.
     810 *
     811 * @returns Number of chars printed.
     812 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     813 *                      associated with the calling thread.
     814 */
     815RTR3DECL(int) RTTestSubDone(RTTEST hTest)
     816{
     817    PRTTESTINT pTest = hTest;
     818    RTTEST_GET_VALID_RETURN_RC(pTest, -1);
     819
     820    RTCritSectEnter(&pTest->Lock);
     821    int cch = rtTestSubCleanup(pTest);
     822    RTCritSectLeave(&pTest->Lock);
     823
     824    return cch;
     825}
     826
     827
     828/**
    674829 * Increments the error counter.
    675830 *
     
    682837    PRTTESTINT pTest = hTest;
    683838    RTTEST_GET_VALID_RETURN(pTest);
     839
    684840    ASMAtomicIncU32(&pTest->cErrors);
     841
    685842    return VINF_SUCCESS;
    686843}
     
    701858    RTTEST_GET_VALID_RETURN_RC(pTest, -1);
    702859
    703     va_list va2;
    704     va_copy(va2, va);
    705 
    706     RTCritSectEnter(&pTest->OutputLock);
    707     ASMAtomicIncU32(&pTest->cErrors);
    708 
    709     int cch = rtTestPrintf(pTest, "FAILED - %N\n", pszFormat, &va2);
    710 
    711     RTCritSectLeave(&pTest->OutputLock);
    712 
    713     va_end(va2);
     860    RTTestErrorInc(pTest);
     861
     862    int cch = 0;
     863    if (pTest->enmMaxLevel >= RTTESTLVL_FAILURE)
     864    {
     865        va_list va2;
     866        va_copy(va2, va);
     867
     868        RTCritSectEnter(&pTest->OutputLock);
     869        cch += rtTestPrintf(pTest, "%N\n", pszFormat, &va2);
     870        RTCritSectLeave(&pTest->OutputLock);
     871
     872        va_end(va2);
     873    }
    714874
    715875    return cch;
     
    737897}
    738898
     899
     900/**
     901 * Prints an extended PASSED message, optional.
     902 *
     903 * This does not conclude the sub-test, it could be used to report the passing
     904 * of a sub-sub-to-the-power-of-N-test.
     905 *
     906 * @returns IPRT status code.
     907 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     908 *                      associated with the calling thread.
     909 * @param   pszFormat   The message. No trailing newline.
     910 * @param   va          The arguments.
     911 */
     912RTR3DECL(int) RTTestPassedV(RTTEST hTest, const char *pszFormat, va_list va)
     913{
     914    PRTTESTINT pTest = hTest;
     915    RTTEST_GET_VALID_RETURN_RC(pTest, -1);
     916
     917    int cch = 0;
     918    if (pTest->enmMaxLevel >= RTTESTLVL_INFO)
     919    {
     920        va_list va2;
     921        va_copy(va2, va);
     922
     923        RTCritSectEnter(&pTest->OutputLock);
     924        cch += rtTestPrintf(pTest, "%N\n", pszFormat, &va2);
     925        RTCritSectLeave(&pTest->OutputLock);
     926
     927        va_end(va2);
     928    }
     929
     930    return cch;
     931}
     932
     933
     934/**
     935 * Prints an extended PASSED message, optional.
     936 *
     937 * This does not conclude the sub-test, it could be used to report the passing
     938 * of a sub-sub-to-the-power-of-N-test.
     939 *
     940 * @returns IPRT status code.
     941 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     942 *                      associated with the calling thread.
     943 * @param   pszFormat   The message. No trailing newline.
     944 * @param   ...         The arguments.
     945 */
     946RTR3DECL(int) RTTestPassed(RTTEST hTest, const char *pszFormat, ...)
     947{
     948    va_list va;
     949
     950    va_start(va, pszFormat);
     951    int cch = RTTestPassedV(hTest, pszFormat, va);
     952    va_end(va);
     953
     954    return cch;
     955}
     956
  • trunk/src/VBox/Runtime/testcase/tstBitOperations.cpp

    r18425 r18569  
    157157    struct TestMap *p = (struct TestMap *)RTTestGuardedAllocTail(hTest, sizeof(*p));
    158158#endif
    159 #define DUMP()          RTTestPrintf(hTest, "au32={%08x,%08x,%08x,%08x}", p->au32[0], p->au32[1], p->au32[2], p->au32[3])
     159#define DUMP()          RTTestPrintf(hTest, RTTESTLVL_INFO, "au32={%08x,%08x,%08x,%08x}", p->au32[0], p->au32[1], p->au32[2], p->au32[3])
    160160#define CHECK(expr)     do { if (!(expr)) { RTTestFailed(hTest, "line %d: %s", __LINE__, #expr); DUMP(); } CHECK_GUARD(s); } while (0)
    161161#define CHECK_BIT(expr,  b1)            do { if (!(expr)) { RTTestFailed(hTest, "line %d, b1=%d: %s", __LINE__, b1, #expr); } CHECK_GUARD(s); } while (0)
Note: See TracChangeset for help on using the changeset viewer.

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