tstUtf8.cpp revision e9a584ee0777ab2612e206eeec264ccb1a8ce333
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * IPRT Testcase - UTF-8 and UTF-16 string conversions.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * available from http://www.virtualbox.org. This file is free software;
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * you can redistribute it and/or modify it under the terms of the GNU
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * General Public License (GPL) as published by the Free Software
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * The contents of this file may alternatively be used under the terms
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * of the Common Development and Distribution License Version 1.0
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * VirtualBox OSE distribution, in which case the provisions of the
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * CDDL are applicable instead of those of the GPL.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * You may elect to license modified versions of this file under the
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * terms and conditions of either the GPL or the CDDL or both.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * additional information or have any questions.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync/*******************************************************************************
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync* Header Files *
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync*******************************************************************************/
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync/*******************************************************************************
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync* Global Variables *
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync*******************************************************************************/
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsyncstatic int g_cErrors = 0;
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * Generate a random codepoint for simple UTF-16 encoding.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsyncstatic void test1(void)
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync static const char s_szBadString1[] = "Bad \xe0\x13\x0";
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync * Invalid UTF-8 to UCS-2 test.
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING)
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync RTPrintf("tstUtf8: FAILURE - %d: Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING)
7a0d67332f33bc21290d63bc7c8150b0cf0a4b21vboxsync RTPrintf("tstUtf8: FAILURE - %d: Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 returned VERR_NO_TRANSLATION. This is probably as it should be.\n");
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.\n", __LINE__);
RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
if (rc == 0)
if (rc == 0)
RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
if (rc == 0)
if (rc == 0)
RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
RTPrintf("tstUtf8: FAILURE - %d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
g_cErrors++;
RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer returned value %Vrc instead of VERR_BUFFER_OVERFLOW.\n",
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - %d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
static char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
void InitStrings(void)
* Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
g_uszAll[i++] = 0;
uc++;
uc++;
uc++;
uc++;
uc++;
void test2(void)
char *pszUtf8;
g_cErrors++;
g_cErrors++;
g_cErrors++;
g_cErrors++;
g_cErrors++;
char *pszUtf8;
g_cErrors++;
g_cErrors++;
g_cErrors++;
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - the full #3+: wrong Code Point count %zu, expected %zu\n", cCps, RT_ELEMENTS(g_uszAll) - 1);
g_cErrors++;
g_cErrors++;
g_cErrors++;
g_cErrors++;
g_cErrors++;
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc2, uc1);
g_cErrors++;
g_cErrors++;
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc2);
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc4);
g_cErrors++;
RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!\n");
g_cErrors++;
g_cErrors++;
g_cErrors++;
if (!uc1)
g_cErrors++;
g_cErrors++;
void test3(void)
g_cErrors++;
g_cErrors++;
g_cErrors++;
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 };
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 };
g_cErrors++;
g_cErrors++;
void TstRTStrXCmp(void)
g_cErrors++; \
void Benchmarks(void)
} s_Buf;
int main()
RTR3Init();
InitStrings();
test1();
test2();
test3();
TstRTStrXCmp();
Benchmarks();
if (!g_cErrors)
return !!g_cErrors;