VirtualBox

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


Ignore:
Timestamp:
Apr 20, 2020 12:34:06 AM (5 years ago)
Author:
vboxsync
Message:

IPRT: Added RTUtf16Printf, RTUtf16PrintfV, RTUtf16PrintfEx and RTUtf16PrintfExV. bugref:8489

File:
1 edited

Legend:

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

    r82968 r83837  
    3030*********************************************************************************************************************************/
    3131#include <iprt/string.h>
     32#include <iprt/utf16.h>
    3233
    3334#include <iprt/initterm.h>
     
    9394        RTTestIFailed("at line %d: Invalid length %d returned, expected %u!\n",
    9495                      iLine, cch, 5 + cchExpect + 5);
     96}
     97
     98
     99static void testUtf16Printf(RTTEST hTest)
     100{
     101    RTTestSub(hTest, "RTUtf16Printf");
     102    size_t const   cwcBuf  = 120;
     103    PRTUTF16 const pwszBuf = (PRTUTF16)RTTestGuardedAllocTail(hTest, cwcBuf * sizeof(RTUTF16));
     104
     105    static const char    s_szSimpleExpect[] = "Hello world!";
     106    static const ssize_t s_cwcSimpleExpect  = sizeof(s_szSimpleExpect) - 1;
     107    ssize_t cwc = RTUtf16Printf(pwszBuf, cwcBuf, "Hello%c%s!", ' ', "world");
     108    if (RTUtf16CmpAscii(pwszBuf, s_szSimpleExpect))
     109        RTTestIFailed("error: '%ls'\n"
     110                      "wanted '%s'\n", pwszBuf, s_szSimpleExpect);
     111    if (cwc != s_cwcSimpleExpect)
     112        RTTestIFailed("error: got %zd, expected %zd (#1)\n", cwc, s_cwcSimpleExpect);
     113
     114    RTTestDisableAssertions(hTest);
     115    for (size_t cwcThisBuf = 0; cwcThisBuf < sizeof(s_szSimpleExpect) + 8; cwcThisBuf++)
     116    {
     117        memset(pwszBuf, '0x88', cwcBuf * sizeof(*pwszBuf));
     118
     119        PRTUTF16 pwszThisBuf = &pwszBuf[cwcBuf - cwcThisBuf];
     120        cwc = RTUtf16Printf(pwszThisBuf, cwcThisBuf, "Hello%c%s!", ' ', "world");
     121
     122        if (cwcThisBuf <= s_cwcSimpleExpect)
     123        {
     124            if (cwcThisBuf > 1)
     125            {
     126                if (RTUtf16NCmpAscii(pwszThisBuf, s_szSimpleExpect, cwcThisBuf - 1))
     127                    RTTestIFailed("error: '%.*ls'\n"
     128                                  "wanted '%.*s'\n", cwcThisBuf - 1, pwszThisBuf, cwcThisBuf - 1, s_szSimpleExpect);
     129            }
     130            if (cwcThisBuf > 1 && pwszThisBuf[cwcThisBuf - 1] != '\0')
     131                RTTestIFailed("error: cwcThisBuf=%zu not null terminated! %#x\n", cwcThisBuf, pwszThisBuf[cwcThisBuf - 1]);
     132            if (cwc != -s_cwcSimpleExpect - 1)
     133                RTTestIFailed("error: cwcThisBuf=%zu got %zd, expected %zd (#1)\n", cwcThisBuf, cwc, -s_cwcSimpleExpect - 1);
     134        }
     135        else
     136        {
     137            if (RTUtf16CmpAscii(pwszThisBuf, s_szSimpleExpect))
     138                RTTestIFailed("error: '%ls'\n"
     139                              "wanted '%s'\n", pwszThisBuf, s_szSimpleExpect);
     140            if (cwc != s_cwcSimpleExpect)
     141                RTTestIFailed("error: cwcThisBuf=%zu got %zd, expected %zd (#1)\n", cwcThisBuf, cwc, s_cwcSimpleExpect);
     142        }
     143    }
     144    RTTestRestoreAssertions(hTest);
    95145}
    96146
     
    876926    RTTESTI_CHECK_RC(RTStrFormatTypeDeregister("type3"), VINF_SUCCESS);
    877927
     928
     929    testUtf16Printf(hTest);
     930
    878931    /*
    879932     * Summarize and exit.
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