VirtualBox

儲存庫 vbox 的更動 18569


忽略:
時間撮記:
2009-3-31 下午01:07:20 (16 年 以前)
作者:
vboxsync
訊息:

RTTest: sub tests, more typical reporting of those.

位置:
trunk
檔案:
修改 3 筆資料

圖例:

未更動
新增
刪除
  • trunk/include/iprt/test.h

    r18366 r18569  
    5252#define NIL_RTTEST  ((RTTEST)0)
    5353
     54/**
     55 * Test message importance level.
     56 */
     57typedef enum RTTESTLVL
     58{
     59    /** Invalid 0. */
     60    RTTESTLVL_INVALID = 0,
     61    /** Message should always be printed. */
     62    RTTESTLVL_ALWAYS,
     63    /** Failure message. */
     64    RTTESTLVL_FAILURE,
     65    /** Sub-test banner. */
     66    RTTESTLVL_SUB_TEST,
     67    /** Info message. */
     68    RTTESTLVL_INFO,
     69    /** Debug message. */
     70    RTTESTLVL_DEBUG,
     71    /** The last (invalid). */
     72    RTTESTLVL_END
     73} RTTESTLVL;
     74
    5475
    5576/**
     
    121142 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    122143 *                      associated with the calling thread.
     144 * @param   enmLevel    Message importance level.
    123145 * @param   pszFormat   The message.
    124146 * @param   va          Arguments.
    125147 */
    126 RTR3DECL(int) RTTestPrintfNlV(RTTEST hTest, const char *pszFormat, va_list va);
     148RTR3DECL(int) RTTestPrintfNlV(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, va_list va);
    127149
    128150/**
     
    132154 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    133155 *                      associated with the calling thread.
     156 * @param   enmLevel    Message importance level.
    134157 * @param   pszFormat   The message.
    135158 * @param   ...         Arguments.
    136159 */
    137 RTR3DECL(int) RTTestPrintfNl(RTTEST hTest, const char *pszFormat, ...);
     160RTR3DECL(int) RTTestPrintfNl(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, ...);
    138161
    139162/**
     
    143166 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    144167 *                      associated with the calling thread.
     168 * @param   enmLevel    Message importance level.
    145169 * @param   pszFormat   The message.
    146170 * @param   va          Arguments.
    147171 */
    148 RTR3DECL(int) RTTestPrintfV(RTTEST hTest, const char *pszFormat, va_list va);
     172RTR3DECL(int) RTTestPrintfV(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, va_list va);
    149173
    150174/**
     
    154178 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
    155179 *                      associated with the calling thread.
     180 * @param   enmLevel    Message importance level.
    156181 * @param   pszFormat   The message.
    157182 * @param   ...         Arguments.
    158183 */
    159 RTR3DECL(int) RTTestPrintf(RTTEST hTest, const char *pszFormat, ...);
     184RTR3DECL(int) RTTestPrintf(RTTEST hTest, RTTESTLVL enmLevel, const char *pszFormat, ...);
    160185
    161186/**
     
    176201 */
    177202RTR3DECL(int) RTTestSummaryAndDestroy(RTTEST hTest);
     203
     204/**
     205 * Starts a sub-test.
     206 *
     207 * This will perform an implicit RTTestSubDone() call if that has not been done
     208 * since the last RTTestSub call.
     209 *
     210 * @returns Number of chars printed.
     211 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     212 *                      associated with the calling thread.
     213 * @param   pszSubTest  The sub-test name
     214 */
     215RTR3DECL(int) RTTestSub(RTTEST hTest, const char *pszSubTest);
     216
     217/**
     218 * Completes a sub-test.
     219 *
     220 * @returns Number of chars printed.
     221 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     222 *                      associated with the calling thread.
     223 */
     224RTR3DECL(int) RTTestSubDone(RTTEST hTest);
    178225
    179226/**
     
    220267    do { if (!(expr)) { RTTestFailed((hTest), "line %u: %s", __LINE__, #expr); } } while (0)
    221268
     269/** @def RTTEST_CHECK_MSG
     270 * Check whether a boolean expression holds true.
     271 *
     272 * If the expression is false, call RTTestFailed giving the line number and expression.
     273 *
     274 * @param   hTest           The test handle.
     275 * @param   expr            The expression to evaluate.
     276 * @param   TestPrintfArgs  Argument list for RTTestPrintf, including
     277 *                          parenthesis.
     278 */
     279#define RTTEST_CHECK_MSG(hTest, expr, TestPrintfArgs) \
     280    do { if (!(expr)) { \
     281            RTTestFailed((hTest), "line %u: %s", __LINE__, #expr); \
     282            RTTestPrintf TestPrintfArgs; \
     283         } \
     284    } while (0)
     285
     286
     287/**
     288 * Prints an extended PASSED message, optional.
     289 *
     290 * This does not conclude the sub-test, it could be used to report the passing
     291 * of a sub-sub-to-the-power-of-N-test.
     292 *
     293 * @returns IPRT status code.
     294 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     295 *                      associated with the calling thread.
     296 * @param   pszFormat   The message. No trailing newline.
     297 * @param   va          The arguments.
     298 */
     299RTR3DECL(int) RTTestPassedV(RTTEST hTest, const char *pszFormat, va_list va);
     300
     301/**
     302 * Prints an extended PASSED message, optional.
     303 *
     304 * This does not conclude the sub-test, it could be used to report the passing
     305 * of a sub-sub-to-the-power-of-N-test.
     306 *
     307 * @returns IPRT status code.
     308 * @param   hTest       The test handle. If NIL_RTTEST we'll use the one
     309 *                      associated with the calling thread.
     310 * @param   pszFormat   The message. No trailing newline.
     311 * @param   ...         The arguments.
     312 */
     313RTR3DECL(int) RTTestPassed(RTTEST hTest, const char *pszFormat, ...);
     314
    222315
    223316/** @}  */
  • 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)
注意: 瀏覽 TracChangeset 來幫助您使用更動檢視器

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