VirtualBox

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


Ignore:
Timestamp:
Jul 5, 2019 8:37:19 PM (6 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
131852
Message:

IPRT: Added RTSTRCONVERTHEXBYTES_F_SEP_COLON as well as a RTStrConvertHexBytesEx variant. bugref:9288

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Runtime/testcase/tstStrToNum.cpp

    r76553 r79562  
    2525 */
    2626
    27 #include <iprt/initterm.h>
     27
     28/*********************************************************************************************************************************
     29*   Header Files                                                                                                                 *
     30*********************************************************************************************************************************/
     31#include <iprt/test.h>
    2832#include <iprt/string.h>
    2933#include <iprt/stream.h>
     
    6973        int rc = Fun(Test.psz, NULL, Test.uBase, &Result); \
    7074        if (Result != Test.Result) \
    71         { \
    72             RTPrintf("failure: '%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
    73             cErrors++; \
    74         } \
     75            RTTestIFailed("'%s' -> " Fmt " expected " Fmt ". (%s/%u)\n", Test.psz, Result, Test.Result, #Fun, iTest); \
    7576        else if (rc != Test.rc) \
    76         { \
    77             RTPrintf("failure: '%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
    78             cErrors++; \
    79         } \
     77            RTTestIFailed("'%s' -> rc=%Rrc expected %Rrc. (%s/%u)\n", Test.psz, rc, Test.rc, #Fun, iTest); \
    8078    } while (0)
    8179
     
    9290int main()
    9391{
    94     RTR3InitExeNoArguments(0);
    95 
    96     int cErrors = 0;
     92    RTTEST     hTest;
     93    RTEXITCODE rcExit = RTTestInitAndCreate("tstRTStrToNum", &hTest);
     94    if (rcExit != RTEXITCODE_SUCCESS)
     95        return rcExit;
     96
    9797    static const struct TstU64 aTstU64[] =
    9898    {
     
    159159
    160160
    161 
    162161    static const struct TstI32 aTstI32[] =
    163162    {
     
    223222    RUN_TESTS(aTstU32, uint32_t, "%#x", RTStrToUInt32Ex);
    224223
     224
     225    /*
     226     * Test the some hex stuff too.
     227     */
     228    static const struct
     229    {
     230        const char *pszHex;
     231        size_t      cbOut;
     232        size_t      offNext;
     233        uint8_t     bLast;
     234        bool        fColon;
     235        int         rc;
     236    } s_aConvertHexTests[] =
     237    {
     238        { "00",          1,  2, 0x00,  true, VINF_SUCCESS },
     239        { "00",          1,  2, 0x00, false, VINF_SUCCESS },
     240        { "000102",      3,  6, 0x02,  true, VINF_SUCCESS },
     241        { "00019",       2,  4, 0x01, false, VERR_UNEVEN_INPUT },
     242        { "00019",       2,  4, 0x01,  true, VERR_UNEVEN_INPUT },
     243        { "0001:9",      3,  6, 0x09,  true, VINF_SUCCESS},
     244        { "000102",      3,  6, 0x02, false, VINF_SUCCESS },
     245        { "0:1",         2,  3, 0x01,  true, VINF_SUCCESS },
     246        { ":",           2,  1, 0x00,  true, VINF_SUCCESS },
     247        { "0:01",        2,  4, 0x01,  true, VINF_SUCCESS },
     248        { "00:01",       2,  5, 0x01,  true, VINF_SUCCESS },
     249        { ":1:2:3:4:5",  6, 10, 0x05,  true, VINF_SUCCESS },
     250        { ":1:2:3::5",   6,  9, 0x05,  true, VINF_SUCCESS },
     251        { ":1:2:3:4:",   6,  9, 0x00,  true, VINF_SUCCESS },
     252    };
     253    for (unsigned i = 0; i < RT_ELEMENTS(s_aConvertHexTests); i++)
     254    {
     255        uint8_t abBuf[1024];
     256        memset(abBuf, 0xf6, sizeof(abBuf));
     257        const char *pszExpectNext = &s_aConvertHexTests[i].pszHex[s_aConvertHexTests[i].offNext];
     258        const char *pszNext       = "";
     259        size_t      cbReturned    = 77777;
     260        int rc = RTStrConvertHexBytesEx(s_aConvertHexTests[i].pszHex, abBuf, s_aConvertHexTests[i].cbOut,
     261                                        s_aConvertHexTests[i].fColon ? RTSTRCONVERTHEXBYTES_F_SEP_COLON : 0,
     262                                        &pszNext, &cbReturned);
     263        if (   rc      != s_aConvertHexTests[i].rc
     264            || pszNext != pszExpectNext
     265            || abBuf[s_aConvertHexTests[i].cbOut - 1] != s_aConvertHexTests[i].bLast
     266            )
     267            RTTestFailed(hTest, "RTStrConvertHexBytesEx/#%u %s -> %Rrc %p %#zx %#02x, expected %Rrc %p %#zx %#02x\n",
     268                         i, s_aConvertHexTests[i].pszHex,
     269                         rc, pszNext, cbReturned, abBuf[s_aConvertHexTests[i].cbOut - 1],
     270                         s_aConvertHexTests[i].rc, pszExpectNext, s_aConvertHexTests[i].cbOut, s_aConvertHexTests[i].bLast);
     271    }
     272
     273
    225274    /*
    226275     * Summary.
    227276     */
    228     if (!cErrors)
    229         RTPrintf("tstStrToNum: SUCCESS\n");
    230     else
    231         RTPrintf("tstStrToNum: FAILURE - %d errors\n", cErrors);
    232     return !!cErrors;
     277    return RTTestSummaryAndDestroy(hTest);
    233278}
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