tstUtf8.cpp revision 904ef975124106054684a976dfb30f70372372e1
77b1a2d8b5dbe2c0b5200794914239fee3c8ee5dvboxsync * IPRT Testcase - UTF-8 and UTF-16 string conversions.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * available from http://www.virtualbox.org. This file is free software;
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * you can redistribute it and/or modify it under the terms of the GNU
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * General Public License (GPL) as published by the Free Software
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
a16eb14ad7a4b5ef91ddc22d3e8e92d930f736fcvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * The contents of this file may alternatively be used under the terms
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * of the Common Development and Distribution License Version 1.0
1c94c0a63ba68be1a7b2c640e70d7a06464e4fcavboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * VirtualBox OSE distribution, in which case the provisions of the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * CDDL are applicable instead of those of the GPL.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * You may elect to license modified versions of this file under the
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * terms and conditions of either the GPL or the CDDL or both.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
da957c069c2a3c582fe265ff88170ce4c42b499dvboxsync * additional information or have any questions.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync/*******************************************************************************
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync* Header Files *
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync*******************************************************************************/
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync * Generate a random codepoint for simple UTF-16 encoding.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
0abd77741a608f6c41c8dfcd4781b8b84adf1044vboxsync static const char s_szBadString1[] = "Bad \xe0\x13\x0";
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
9496f2d398b49813176939d7a339ae513d5175efvboxsync * Invalid UTF-8 to UCS-2 test.
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestSub(hTest, "Feeding bad UTF-8 to RTStrToUtf16");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
9496f2d398b49813176939d7a339ae513d5175efvboxsync (hTest, "Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTEST_CHECK_MSG(hTest, rc == VERR_NO_TRANSLATION || rc == VERR_INVALID_UTF8_ENCODING,
9496f2d398b49813176939d7a339ae513d5175efvboxsync (hTest, "Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Rrc\n", rc));
9496f2d398b49813176939d7a339ae513d5175efvboxsync * Test current CP convertion.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestSub(hTest, "Rand UTF-16 -> UTF-8 -> CP -> UTF-8");
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync for (int i = 0; i < 30; i++)
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "%d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPassed(hTest, "The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Rrc.",
9496f2d398b49813176939d7a339ae513d5175efvboxsync * Generate a new random string.
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestSub(hTest, "Random UTF-16 -> UTF-8 -> UTF-16");
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync for (int i = 0; i < 30; i++)
9496f2d398b49813176939d7a339ae513d5175efvboxsync for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
9496f2d398b49813176939d7a339ae513d5175efvboxsync /* nothing */;
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.", __LINE__);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Rrc.",
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync * Generate yet another random string and convert it to a buffer.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestSub(hTest, "Random RTUtf16ToUtf8Ex + RTStrToUtf16");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync for (int i = 0; i < 30; i++)
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestFailed(hTest, "%d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestFailed(hTest, "%d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestFailed(hTest, "%d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync * And again.
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
9496f2d398b49813176939d7a339ae513d5175efvboxsync for (int i = 0; i < 30; i++)
9496f2d398b49813176939d7a339ae513d5175efvboxsync rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
9496f2d398b49813176939d7a339ae513d5175efvboxsync for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++)
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync RTTestPrintf(hTest, RTTESTLVL_FAILURE, "First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n", __LINE__, rc);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
9496f2d398b49813176939d7a339ae513d5175efvboxsync pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
9496f2d398b49813176939d7a339ae513d5175efvboxsync for (int i = 0; i < 30; i++)
9496f2d398b49813176939d7a339ae513d5175efvboxsync rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPassed(hTest, "Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
9496f2d398b49813176939d7a339ae513d5175efvboxsync * last time...
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestSub(hTest, "Random RTUtf16ToUtf8 + RTStrToUtf16Ex");
9496f2d398b49813176939d7a339ae513d5175efvboxsync pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
9496f2d398b49813176939d7a339ae513d5175efvboxsync for (int i = 0; i < 30; i++)
9496f2d398b49813176939d7a339ae513d5175efvboxsync rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPassed(hTest, "Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer returned value %Rrc instead of VERR_BUFFER_OVERFLOW.\n",
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "%d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Rrc.\n",
9496f2d398b49813176939d7a339ae513d5175efvboxsyncstatic RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
9496f2d398b49813176939d7a339ae513d5175efvboxsyncstatic RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
9496f2d398b49813176939d7a339ae513d5175efvboxsyncstatic char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", off + 1);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-8 ???\n");
9c149a2789022f5011e88fb62f02a1cc8068e88fvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", off / 2 + 1);
9c149a2789022f5011e88fb62f02a1cc8068e88fvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
9c149a2789022f5011e88fb62f02a1cc8068e88fvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
975ad9d9bc9c4dc96b41d9f67a65228b1b338e2avboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "UTF-16 ???\n");
975ad9d9bc9c4dc96b41d9f67a65228b1b338e2avboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
9c149a2789022f5011e88fb62f02a1cc8068e88fvboxsync else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
975ad9d9bc9c4dc96b41d9f67a65228b1b338e2avboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
9c149a2789022f5011e88fb62f02a1cc8068e88fvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsyncint mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "mismatch at %#x: ", off);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, "*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(NIL_RTTEST, RTTESTLVL_FAILURE, " %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync /* the simple code point array first */
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync unsigned i = 0;
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync /* the utf-16 one */
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf("tstUtf8: %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x\n", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync * The utf-8 one
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf("tstUtf8: %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync //RTPrintf(" %#x=%#x\n", i, uc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync * Convert to UTF-8 and back.
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync * Convert to UTF-16 and back. (just in case the above test fails)
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync if (mymemcmp(pwszUtf16, g_wszAll, sizeof(g_wszAll), 16))
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "UTF-8 -> UTF-16 failed compare!");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "UTF-16 -> UTF-8 failed compare!");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "UTF-16 -> UTF-8 failed, rc=%Rrc.", rc);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync * Convert UTF-8 to CPs.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "UTF-8 -> UTF-16 failed, rc=%Rrc.", rc);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, RT_ELEMENTS(g_uszAll), &cCps);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync /** @todo RTCpsToUtf8 or something. */
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Check the various string lengths.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync //size_t cuc3 = RTUniLen(g_uszAll);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync * Enumerate the strings.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestSub(hTest, "Code Point Getters and Putters");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs", rc, psz2);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "RTStrPrevCp returned %p expected %p!", pszPrev1, psz2);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc2, uc1);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "RTStrGetCpEx and RTStrGetNext returned different next pointer!");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs", rc, pwsz4);
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync RTTestFailed(hTest, "RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc2);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp", uc3, uc4);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!");
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
9496f2d398b49813176939d7a339ae513d5175efvboxsync if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "RTStrPutCp is not at the same offset! %p != %p",
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync /* the end? */
9496f2d398b49813176939d7a339ae513d5175efvboxsync /* check output if we seems to have made it thru it all. */
9496f2d398b49813176939d7a339ae513d5175efvboxsync if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "RTStrPutCp encoded the string incorrectly.");
9496f2d398b49813176939d7a339ae513d5175efvboxsync if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "RTUtf16PutCp encoded the string incorrectly.");
9496f2d398b49813176939d7a339ae513d5175efvboxsync * Check case insensitivity.
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "RTUniToUpper/Lower failed basic tests.\n");
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync RTTestFailed(hTest, "RTUtf16ICmp failed the basic test.\n");
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync RTTestFailed(hTest, "RTUtf16Cmp failed the basic test.\n");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync static RTUTF16 s_wszTst1a[] = { 'a', 'B', 'c', 'D', 'E', 'f', 'g', 'h', 'i', 'j', 'K', 'L', 'm', 'N', 'o', 'P', 'q', 'r', 'S', 't', 'u', 'V', 'w', 'x', 'Y', 'Z', 0xc5, 0xc6, 0xf8, 0 };
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync static RTUTF16 s_wszTst1b[] = { 'A', 'B', 'c', 'd', 'e', 'F', 'G', 'h', 'i', 'J', 'k', 'l', 'M', 'n', 'O', 'p', 'Q', 'R', 's', 't', 'U', 'v', 'w', 'X', 'y', 'z', 0xe5, 0xe6, 0xd8, 0 };
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "RTUtf16ICmp failed the alphabet test.\n");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "RTUtf16Cmp failed the alphabet test.\n");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Test the RTStr*Cmp functions.
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync RTTestFailed(hTest, "%d: %d " #op " 0: %s\n", __LINE__, iDiff, #expr); \
8083a259c13e6e26e56ca2582edbad4a8cfac25avboxsync } while (0)
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync/** @todo test the non-ascii bits. */
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcdef", "abcdef", RTSTR_MAX), == );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcdef", "abcde", RTSTR_MAX), > );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcde", "abcdef", RTSTR_MAX), < );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcdeg", "abcdef", RTSTR_MAX), > );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcdef", "abcdeg", RTSTR_MAX), < );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcdeF", "abcdef", RTSTR_MAX), < );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNCmp("abcdef", "abcdeF", RTSTR_MAX), > );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrICmp("AbCdEG", "aBcDef"), > ); /* diff performed on the lower case cp. */
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNICmp(NULL, NULL, RTSTR_MAX), == );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNICmp("abcdef", "abcdef", RTSTR_MAX), == );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNICmp("abcdef", "abcde", RTSTR_MAX), > );
975ad9d9bc9c4dc96b41d9f67a65228b1b338e2avboxsync CHECK_DIFF(RTStrNICmp("abcde", "abcdef", RTSTR_MAX), < );
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNICmp("abcdeg", "abcdef", RTSTR_MAX), > );
975ad9d9bc9c4dc96b41d9f67a65228b1b338e2avboxsync CHECK_DIFF(RTStrNICmp("abcdef", "abcdeg", RTSTR_MAX), < );
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync CHECK_DIFF(RTStrNICmp("abcdeF", "abcdef", RTSTR_MAX), ==);
d80c85a1bc7317da7d0cd1254fae6a20db039c8cvboxsync CHECK_DIFF(RTStrNICmp("abcdef", "abcdeF", RTSTR_MAX), ==);
9c149a2789022f5011e88fb62f02a1cc8068e88fvboxsync CHECK_DIFF(RTStrNICmp("ABCDEF", "abcdef", RTSTR_MAX), ==);
9496f2d398b49813176939d7a339ae513d5175efvboxsync CHECK_DIFF(RTStrNICmp("abcdef", "ABCDEF", RTSTR_MAX), ==);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync CHECK_DIFF(RTStrNICmp("AbCdEf", "aBcDeF", RTSTR_MAX), ==);
9496f2d398b49813176939d7a339ae513d5175efvboxsync CHECK_DIFF(RTStrNICmp("AbCdEg", "aBcDeF", RTSTR_MAX), > );
b7a5b3f9f9ecce32ddacf8404c625ce0451bbdc1vboxsync CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", RTSTR_MAX), > ); /* diff performed on the lower case cp. */
9496f2d398b49813176939d7a339ae513d5175efvboxsync CHECK_DIFF(RTStrNICmp("AbCdEG", "aBcDef", 6), > ); /* diff performed on the lower case cp. */
9496f2d398b49813176939d7a339ae513d5175efvboxsync /* We should continue using byte comparison when we hit the invalid CP. Will assert in debug builds. */
9496f2d398b49813176939d7a339ae513d5175efvboxsync // CHECK_DIFF(RTStrNICmp("AbCd\xff""eg", "aBcD\xff""eF", 6), ==);
090d729e786b999dc285f8ea267f9effd1319544vboxsync * Benchmark stuff.
fe813b3594039ba864493438e78ee0e7132bc445vboxsync static union
090d729e786b999dc285f8ea267f9effd1319544vboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTStrToUtf16Ex: "); /** @todo figure this stuff into the test framework. */
090d729e786b999dc285f8ea267f9effd1319544vboxsync int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
548ca31b6b47c36bacce49bed3339cb8075b9681vboxsync for (i = 0; i < 100; i++)
090d729e786b999dc285f8ea267f9effd1319544vboxsync rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, RT_ELEMENTS(s_Buf.wszBuf), NULL);
9496f2d398b49813176939d7a339ae513d5175efvboxsync RTTestFailed(hTest, "UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %RI64ns\n", i, u64Elapsed);
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "Benchmarking RTUtf16ToUtf8Ex: ");
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync for (i = 0; i < 100; i++)
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, RT_ELEMENTS(s_Buf.szBuf), NULL);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestFailed(hTest, "UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync RTTestPrintf(hTest, RTTESTLVL_ALWAYS, "%d in %RI64ns\n", i, u64Elapsed);
a0240ff4f7663045c848fdbc192ea3d4d9f70a11vboxsync * Tests RTStrStr and RTStrIStr.
0ccdfa1953b2f57311fb9ec01a2baf5e1e366f5avboxsync RTTestFailed(hTest, "%d: %#x -> %s expected NULL", __LINE__, #expr, pszRet); \
0ccdfa1953b2f57311fb9ec01a2baf5e1e366f5avboxsync } while (0)
0ccdfa1953b2f57311fb9ec01a2baf5e1e366f5avboxsync RTTestFailed(hTest, "%d: %#x -> %s expected %s", __LINE__, #expr, pszRet, (expect)); \
0ccdfa1953b2f57311fb9ec01a2baf5e1e366f5avboxsync } while (0)
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync * Init the runtime and stuff.
51fe8789a74f6c118894aaa12eb69ec155386dbdvboxsync RTPrintf("tstBitstUtf8: fatal initialization error\n");