tstUtf8.cpp revision 848b6c64616a9dc0d00e7aaad7b82b8eb58e370e
/* $Id$ */
/** @file
* innotek Portable Runtime Testcase - UTF-8 and UTF-16 string conversions.
*/
/*
* Copyright (C) 2006-2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <stdlib.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static int g_cErrors = 0;
/**
* Generate a random codepoint for simple UTF-16 encoding.
*/
static RTUTF16 GetRandUtf16(void)
{
do
{
return wc;
}
/**
*
*/
static void test1(void)
{
static const char s_szBadString1[] = "Bad \xe0\x13\x0";
static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
int rc;
char *pszUtf8;
char *pszCurrent;
RTPrintf("tstUtf8: TEST 1\n");
/*
* Invalid UTF-8 to UCS-2 test.
*/
{
RTPrintf("tstUtf8: FAILURE - %d: Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
g_cErrors++;
}
{
RTPrintf("tstUtf8: FAILURE - %d: Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
g_cErrors++;
}
/*
* Test current CP convertion.
*/
srand((unsigned)RTTimeNanoTS());
for (int i = 0; i < 30; i++)
pwszRand[i] = GetRandUtf16();
pwszRand[30] = 0;
if (rc == VINF_SUCCESS)
{
if (rc == VINF_SUCCESS)
{
if (rc == VINF_SUCCESS)
RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
else
{
RTPrintf("tstUtf8: FAILURE - %d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
g_cErrors++;
}
}
else if (rc == VERR_NO_TRANSLATION)
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");
else
{
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
g_cErrors++;
}
}
else
{
RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
g_cErrors++;
}
/*
* Generate a new random string.
*/
srand((unsigned)RTTimeNanoTS());
for (int i = 0; i < 30; i++)
pwszRand[i] = GetRandUtf16();
pwszRand[30] = 0;
if (rc == VINF_SUCCESS)
{
if (rc == VINF_SUCCESS)
{
int i;
/* nothing */;
RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
else
{
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++;
}
}
else
{
RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
}
}
else
{
RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
g_cErrors++;
}
/*
* Generate yet another random string and convert it to a buffer.
*/
srand((unsigned)RTTimeNanoTS());
for (int i = 0; i < 30; i++)
pwszRand[i] = GetRandUtf16();
pwszRand[30] = 0;
char szUtf8Array[120];
char *pszUtf8Array = szUtf8Array;
if (rc == 0)
{
if (rc == 0)
{
int i;
if (pwsz[i] == 0 && i >= 8)
RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
else
{
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++;
}
}
else
{
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++;
}
}
else
{
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++;
}
/*
* And again.
*/
srand((unsigned)RTTimeNanoTS());
for (int i = 0; i < 30; i++)
pwszRand[i] = GetRandUtf16();
pwszRand[30] = 0;
if (rc == 0)
{
if (rc == 0)
{
int i;
RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
else
{
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++;
}
}
else
{
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++;
}
}
else
{
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++;
}
srand((unsigned)RTTimeNanoTS());
for (int i = 0; i < 30; i++)
pwszRand[i] = GetRandUtf16();
pwszRand[30] = 0;
if (rc == VERR_BUFFER_OVERFLOW)
RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
else
{
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++;
}
/*
* last time...
*/
srand((unsigned)RTTimeNanoTS());
for (int i = 0; i < 30; i++)
pwszRand[i] = GetRandUtf16();
pwszRand[30] = 0;
if (rc == VINF_SUCCESS)
{
if (rc == VERR_BUFFER_OVERFLOW)
RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
else
{
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++;
}
}
else
{
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];
{
if (cBits == 8)
{
if (off < 0x7f)
else if (off < 0xf7f)
else if (off < 0x27f7f)
else if (off < 0x2df79)
else if (off < 0x42df79)
else
RTPrintf("UTF-8 ???\n");
}
else if (cBits == 16)
{
else if (off < 0x20f7fd)
else
RTPrintf("UTF-16 ???\n");
}
else
{
else
}
}
{
{
{
return 1;
}
}
return 0;
}
void InitStrings(void)
{
/*
* Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
*/
/* the simple code point array first */
unsigned i = 0;
while (uc < 0xd800)
uc = 0xe000;
while (uc < 0xfffe)
uc = 0x10000;
while (uc < 0x110000)
g_uszAll[i++] = 0;
/* the utf-16 one */
i = 0;
uc = 1;
//RTPrintf("tstUtf8: %#x=%#x", i, uc);
while (uc < 0xd800)
uc = 0xe000;
//RTPrintf(" %#x=%#x", i, uc);
while (uc < 0xfffe)
uc = 0x10000;
//RTPrintf(" %#x=%#x", i, uc);
while (uc < 0x110000)
{
uc++;
}
//RTPrintf(" %#x=%#x\n", i, uc);
g_wszAll[i++] = '\0';
/*
* The utf-8 one
*/
i = 0;
uc = 1;
//RTPrintf("tstUtf8: %#x=%#x", i, uc);
while (uc < 0x80)
//RTPrintf(" %#x=%#x", i, uc);
while (uc < 0x800)
{
uc++;
}
//RTPrintf(" %#x=%#x", i, uc);
while (uc < 0xd800)
{
uc++;
}
uc = 0xe000;
//RTPrintf(" %#x=%#x", i, uc);
while (uc < 0xfffe)
{
uc++;
}
uc = 0x10000;
//RTPrintf(" %#x=%#x", i, uc);
while (uc < 0x110000)
{
uc++;
}
//RTPrintf(" %#x=%#x\n", i, uc);
g_szAll[i++] = '\0';
}
void test2(void)
{
RTPrintf("tstUtf8: TEST 2\n");
/*
* Convert to UTF-8 and back.
*/
RTPrintf("tstUtf8: #1: UTF-16 -> UTF-8 -> UTF-16...\n");
char *pszUtf8;
if (rc == VINF_SUCCESS)
{
{
RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 mismatch!\n");
g_cErrors++;
}
if (rc == VINF_SUCCESS)
{
{
RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed compare!\n");
g_cErrors++;
}
}
else
{
g_cErrors++;
}
}
else
{
g_cErrors++;
}
/*
* Convert to UTF-16 and back. (just in case the above test fails)
*/
RTPrintf("tstUtf8: #2: UTF-8 -> UTF-16 -> UTF-8...\n");
if (rc == VINF_SUCCESS)
{
{
RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed compare!\n");
g_cErrors++;
}
char *pszUtf8;
if (rc == VINF_SUCCESS)
{
{
RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed compare!\n");
g_cErrors++;
}
}
else
{
g_cErrors++;
}
}
else
{
g_cErrors++;
}
/*
* Convert UTF-8 to CPs.
*/
if (rc == VINF_SUCCESS)
{
{
g_cErrors++;
}
if (rc == VINF_SUCCESS)
{
{
RTPrintf("tstUtf8: FAILURE - the full #3+: wrong Code Point count %zu, expected %zu\n", cCps, ELEMENTS(g_uszAll) - 1);
g_cErrors++;
}
}
else
{
g_cErrors++;
}
/** @todo RTCpsToUtf8 or something. */
}
else
{
g_cErrors++;
}
/*
* Check the various string lengths.
*/
{
g_cErrors++;
}
//size_t cuc3 = RTUniLen(g_uszAll);
/*
* Enumerate the strings.
*/
char *pszPut1 = pszPut1Base;
for (;;)
{
/*
* getters
*/
if (RT_FAILURE(rc))
{
g_cErrors++;
break;
}
{
g_cErrors++;
break;
}
{
RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc2, uc1);
g_cErrors++;
break;
}
{
RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetNext returned different next pointer!\n");
g_cErrors++;
break;
}
if (RT_FAILURE(rc))
{
g_cErrors++;
break;
}
{
RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc2);
g_cErrors++;
break;
}
{
RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc4);
g_cErrors++;
break;
}
{
RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!\n");
g_cErrors++;
break;
}
/*
* putters
*/
{
RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
g_cErrors++;
break;
}
{
RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
g_cErrors++;
break;
}
/* the end? */
if (!uc1)
break;
}
/* check output if we seems to have made it thru it all. */
{
{
RTPrintf("tstUtf8: FAILURE - RTStrPutCp encoded the string incorrectly.\n");
g_cErrors++;
}
{
RTPrintf("tstUtf8: FAILURE - RTUtf16PutCp encoded the string incorrectly.\n");
g_cErrors++;
}
}
}
/**
* Check case insensitivity.
*/
void test3(void)
{
RTPrintf("tstUtf8: TEST 3\n");
{
RTPrintf("tstUtf8: FAILURE - RTUniToUpper/Lower failed basic tests.\n");
g_cErrors++;
}
{
RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the basic test.\n");
g_cErrors++;
}
{
RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the basic test.\n");
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 };
)
{
RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the alphabet test.\n");
g_cErrors++;
}
)
{
RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the alphabet test.\n");
g_cErrors++;
}
}
/**
* Benchmark stuff.
*/
void Benchmarks(void)
{
RTPrintf("tstUtf8: BENCHMARKS\n");
static union
{
} s_Buf;
if (RT_SUCCESS(rc))
{
int i;
for (i = 0; i < 100; i++)
{
if (RT_FAILURE(rc))
{
break;
}
}
}
if (RT_SUCCESS(rc))
{
int i;
for (i = 0; i < 100; i++)
{
if (RT_FAILURE(rc))
{
break;
}
}
}
}
int main()
{
RTR3Init(false);
InitStrings();
test1();
test2();
test3();
Benchmarks();
/*
* Summary
*/
if (!g_cErrors)
RTPrintf("tstUtf8: SUCCESS\n");
else
return !!g_cErrors;
}