tstUtf8.cpp revision 677833bc953b6cb418c701facbdcf4aa18d6c44e
0N/A/* $Id$ */
2362N/A/** @file
0N/A * InnoTek Portable Runtime Testcase - UTF-8 and UTF-16 string conversions.
0N/A */
0N/A
0N/A/*
2362N/A * Copyright (C) 2006 InnoTek Systemberatung GmbH
0N/A *
2362N/A * This file is part of VirtualBox Open Source Edition (OSE), as
0N/A * available from http://www.virtualbox.org. This file is free software;
0N/A * you can redistribute it and/or modify it under the terms of the GNU
0N/A * General Public License as published by the Free Software Foundation,
0N/A * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
0N/A * distribution. VirtualBox OSE is distributed in the hope that it will
0N/A * be useful, but WITHOUT ANY WARRANTY of any kind.
0N/A *
0N/A * If you received this file as part of a commercial VirtualBox
0N/A * distribution, then only the terms of your commercial VirtualBox
0N/A * license agreement apply instead of the previous paragraph.
0N/A */
2362N/A
2362N/A/*******************************************************************************
2362N/A* Header Files *
0N/A*******************************************************************************/
0N/A#include <iprt/string.h>
0N/A#include <iprt/uni.h>
0N/A#include <iprt/runtime.h>
0N/A#include <iprt/uuid.h>
0N/A#include <iprt/time.h>
0N/A#include <iprt/stream.h>
0N/A#include <iprt/alloc.h>
0N/A#include <iprt/assert.h>
0N/A#include <iprt/err.h>
0N/A
0N/A#include <stdlib.h>
0N/A
0N/A
0N/A/*******************************************************************************
0N/A* Global Variables *
0N/A*******************************************************************************/
0N/Astatic int g_cErrors = 0;
0N/A
0N/A
0N/A/**
0N/A * Generate a random codepoint for simple UTF-16 encoding.
0N/A */
0N/Astatic RTUTF16 GetRandUcs2(void)
0N/A{
0N/A RTUTF16 wc;
0N/A do
0N/A {
0N/A wc = (RTUTF16)((long long)rand() * 0xffff / RAND_MAX);
0N/A } while ((wc >= 0xd800 && wc <= 0xdfff) || wc == 0);
0N/A return wc;
0N/A}
0N/A
0N/A
0N/A/**
0N/A *
0N/A */
0N/Astatic void test1(void)
0N/A{
0N/A static const char s_szBadString1[] = "Bad \xe0\x13\x0";
0N/A static const char s_szBadString2[] = "Bad \xef\xbf\xc3";
0N/A int rc;
0N/A char *pszUtf8;
0N/A char *pszCurrent;
0N/A PRTUTF16 pwsz;
0N/A PRTUTF16 pwszRand;
0N/A
0N/A RTPrintf("tstUtf8: TEST 1\n");
0N/A
0N/A /*
0N/A * Invalid UTF-8 to UCS-2 test.
0N/A */
0N/A rc = RTStrToUtf16(s_szBadString1, &pwsz);
0N/A if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: Conversion of first bad UTF-8 string to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A rc = RTStrToUtf16(s_szBadString2, &pwsz);
0N/A if (rc != VERR_NO_TRANSLATION && rc != VERR_INVALID_UTF8_ENCODING)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: Conversion of second bad UTF-8 strings to UTF-16 apparantly succeeded. It shouldn't. rc=%Vrc\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * Test current CP convertion.
0N/A */
0N/A pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
0N/A srand((unsigned)RTTimeNanoTS());
0N/A for (int i = 0; i < 30; i++)
0N/A pwszRand[i] = GetRandUcs2();
0N/A pwszRand[30] = 0;
0N/A
0N/A rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A rc = RTStrUtf8ToCurrentCP(&pszCurrent, pszUtf8);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A rc = RTStrCurrentCPToUtf8(&pszUtf8, pszCurrent);
0N/A if (rc == VINF_SUCCESS)
0N/A RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> Current -> UTF-8 successful.\n");
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The third part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else if (rc == VERR_NO_TRANSLATION)
0N/A 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");
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> Current -> UTF-8 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * Generate a new random string.
0N/A */
0N/A pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
0N/A srand((unsigned)RTTimeNanoTS());
0N/A for (int i = 0; i < 30; i++)
0N/A pwszRand[i] = GetRandUcs2();
0N/A pwszRand[30] = 0;
0N/A rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A rc = RTStrToUtf16(pszUtf8, &pwsz);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A int i;
0N/A for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++)
0N/A /* nothing */;
0N/A if (pwszRand[i] == pwsz[i] && pwsz[i] == 0)
0N/A RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> UTF-16 successful.\n");
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed.\n", __LINE__);
0N/A RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * Generate yet another random string and convert it to a buffer.
0N/A */
0N/A pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
0N/A srand((unsigned)RTTimeNanoTS());
0N/A for (int i = 0; i < 30; i++)
0N/A pwszRand[i] = GetRandUcs2();
0N/A pwszRand[30] = 0;
0N/A
0N/A char szUtf8Array[120];
0N/A char *pszUtf8Array = szUtf8Array;
0N/A rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 120, NULL);
0N/A if (rc == 0)
0N/A {
0N/A rc = RTStrToUtf16(pszUtf8Array, &pwsz);
0N/A if (rc == 0)
0N/A {
0N/A int i;
0N/A for (i = 0; pwszRand[i] == pwsz[i] && pwsz[i] != 0; i++);
0N/A if (pwsz[i] == 0 && i >= 8)
0N/A RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 -> UTF-16 successful.\n");
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of UTF-16 -> fixed length UTF-8 -> UTF-16.\n", __LINE__);
0N/A RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz[i]);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> fixed length UTF-8 -> UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * And again.
0N/A */
0N/A pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
0N/A srand((unsigned)RTTimeNanoTS());
0N/A for (int i = 0; i < 30; i++)
0N/A pwszRand[i] = GetRandUcs2();
0N/A pwszRand[30] = 0;
0N/A
0N/A RTUTF16 wszBuf[70];
0N/A PRTUTF16 pwsz2Buf = wszBuf;
0N/A rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
0N/A if (rc == 0)
0N/A {
0N/A rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 70, NULL);
0N/A if (rc == 0)
0N/A {
0N/A int i;
0N/A for (i = 0; pwszRand[i] == pwsz2Buf[i] && pwsz2Buf[i] != 0; i++);
0N/A if (pwszRand[i] == 0 && pwsz2Buf[i] == 0)
0N/A RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 successful.\n");
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: Incorrect conversion of random UTF-16 -> UTF-8 -> fixed length UTF-16.\n", __LINE__);
0N/A RTPrintf("tstUtf8: First differing character is at position %d and has the value %x.\n", i, pwsz2Buf[i]);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The second part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
0N/A srand((unsigned)RTTimeNanoTS());
0N/A for (int i = 0; i < 30; i++)
0N/A pwszRand[i] = GetRandUcs2();
0N/A pwszRand[30] = 0;
0N/A
0N/A rc = RTUtf16ToUtf8Ex(pwszRand, RTSTR_MAX, &pszUtf8Array, 20, NULL);
0N/A if (rc == VERR_BUFFER_OVERFLOW)
0N/A RTPrintf("tstUtf8: Random UTF-16 -> fixed length UTF-8 with too short buffer successfully rejected.\n");
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d: Random UTF-16 -> fixed length UTF-8 with too small buffer returned value %d instead of VERR_BUFFER_OVERFLOW.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * last time...
0N/A */
0N/A pwszRand = (PRTUTF16)RTMemAlloc(31 * sizeof(*pwsz));
0N/A srand((unsigned)RTTimeNanoTS());
0N/A for (int i = 0; i < 30; i++)
0N/A pwszRand[i] = GetRandUcs2();
0N/A pwszRand[30] = 0;
0N/A
0N/A rc = RTUtf16ToUtf8(pwszRand, &pszUtf8);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A rc = RTStrToUtf16Ex(pszUtf8, RTSTR_MAX, &pwsz2Buf, 20, NULL);
0N/A if (rc == VERR_BUFFER_OVERFLOW)
0N/A RTPrintf("tstUtf8: Random UTF-16 -> UTF-8 -> fixed length UTF-16 with too short buffer successfully rejected.\n");
0N/A else
0N/A {
0N/A 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",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - %d:The first part of random UTF-16 -> UTF-8 -> fixed length UTF-16 failed with return value %Vrc.\n",
0N/A __LINE__, rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A}
0N/A
0N/A
0N/Astatic RTUNICP g_uszAll[0x110000 - 1 - 0x800 - 2 + 1];
0N/Astatic RTUTF16 g_wszAll[0xfffe - (0xe000 - 0xd800) + (0x110000 - 0x10000) * 2];
0N/Astatic char g_szAll[0x7f + (0x800 - 0x80) * 2 + (0xfffe - 0x800 - (0xe000 - 0xd800))* 3 + (0x110000 - 0x10000) * 4 + 1];
0N/A
0N/Astatic void whereami(int cBits, size_t off)
0N/A{
0N/A if (cBits == 8)
0N/A {
0N/A if (off < 0x7f)
0N/A RTPrintf("UTF-8 U+%#x\n", off + 1);
0N/A else if (off < 0xf7f)
0N/A RTPrintf("UTF-8 U+%#x\n", (off - 0x7f) / 2 + 0x80);
0N/A else if (off < 0x27f7f)
0N/A RTPrintf("UTF-8 U+%#x\n", (off - 0xf7f) / 3 + 0x800);
0N/A else if (off < 0x2df79)
0N/A RTPrintf("UTF-8 U+%#x\n", (off - 0x27f7f) / 3 + 0xe000);
0N/A else if (off < 0x42df79)
0N/A RTPrintf("UTF-8 U+%#x\n", (off - 0x2df79) / 4 + 0x10000);
0N/A else
0N/A RTPrintf("UTF-8 ???\n");
0N/A }
0N/A else if (cBits == 16)
0N/A {
0N/A if (off < 0xd7ff*2)
0N/A RTPrintf("UTF-16 U+%#x\n", off / 2 + 1);
0N/A else if (off < 0xf7fd*2)
0N/A RTPrintf("UTF-16 U+%#x\n", (off - 0xd7ff*2) / 2 + 0xe000);
0N/A else if (off < 0x20f7fd)
0N/A RTPrintf("UTF-16 U+%#x\n", (off - 0xf7fd*2) / 4 + 0x10000);
0N/A else
0N/A RTPrintf("UTF-16 ???\n");
0N/A }
0N/A else
0N/A {
0N/A if (off < (0xd800 - 1) * sizeof(RTUNICP))
0N/A RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 1);
0N/A else if (off < (0xfffe - 0x800 - 1) * sizeof(RTUNICP))
0N/A RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1);
0N/A else
0N/A RTPrintf("RTUNICP U+%#x\n", off / sizeof(RTUNICP) + 0x800 + 1 + 2);
0N/A }
0N/A}
0N/A
0N/Aint mymemcmp(const void *pv1, const void *pv2, size_t cb, int cBits)
0N/A{
0N/A const uint8_t *pb1 = (const uint8_t *)pv1;
0N/A const uint8_t *pb2 = (const uint8_t *)pv2;
0N/A for (size_t off = 0; off < cb; off++)
0N/A {
0N/A if (pb1[off] != pb2[off])
0N/A {
0N/A RTPrintf("mismatch at %#x: ", off);
0N/A whereami(cBits, off);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off-1, pb1[off-1], pb2[off-1]);
0N/A RTPrintf("*%#x: %02x != %02x!\n", off, pb1[off], pb2[off]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+1, pb1[off+1], pb2[off+1]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+2, pb1[off+2], pb2[off+2]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+3, pb1[off+3], pb2[off+3]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+4, pb1[off+4], pb2[off+4]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+5, pb1[off+5], pb2[off+5]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+6, pb1[off+6], pb2[off+6]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+7, pb1[off+7], pb2[off+7]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+8, pb1[off+8], pb2[off+8]);
0N/A RTPrintf(" %#x: %02x != %02x!\n", off+9, pb1[off+9], pb2[off+9]);
0N/A return 1;
0N/A }
0N/A }
0N/A return 0;
0N/A}
0N/A
0N/A
0N/Avoid InitStrings(void)
0N/A{
0N/A /*
0N/A * Generate unicode string containing all the legal UTF-16 codepoints, both UTF-16 and UTF-8 version.
0N/A */
0N/A /* the simple code point array first */
0N/A unsigned i = 0;
0N/A RTUNICP uc = 1;
0N/A while (uc < 0xd800)
0N/A g_uszAll[i++] = uc++;
0N/A uc = 0xe000;
0N/A while (uc < 0xfffe)
0N/A g_uszAll[i++] = uc++;
0N/A uc = 0x10000;
0N/A while (uc < 0x110000)
0N/A g_uszAll[i++] = uc++;
0N/A g_uszAll[i++] = 0;
0N/A Assert(ELEMENTS(g_uszAll) == i);
0N/A
0N/A /* the utf-16 one */
0N/A i = 0;
0N/A uc = 1;
0N/A //RTPrintf("tstUtf8: %#x=%#x", i, uc);
0N/A while (uc < 0xd800)
0N/A g_wszAll[i++] = uc++;
0N/A uc = 0xe000;
0N/A //RTPrintf(" %#x=%#x", i, uc);
0N/A while (uc < 0xfffe)
0N/A g_wszAll[i++] = uc++;
0N/A uc = 0x10000;
0N/A //RTPrintf(" %#x=%#x", i, uc);
0N/A while (uc < 0x110000)
0N/A {
0N/A g_wszAll[i++] = 0xd800 | ((uc - 0x10000) >> 10);
0N/A g_wszAll[i++] = 0xdc00 | ((uc - 0x10000) & 0x3ff);
0N/A uc++;
0N/A }
0N/A //RTPrintf(" %#x=%#x\n", i, uc);
0N/A g_wszAll[i++] = '\0';
0N/A Assert(ELEMENTS(g_wszAll) == i);
0N/A
0N/A /*
0N/A * The utf-8 one
0N/A */
0N/A i = 0;
0N/A uc = 1;
0N/A //RTPrintf("tstUtf8: %#x=%#x", i, uc);
0N/A while (uc < 0x80)
0N/A g_szAll[i++] = uc++;
0N/A //RTPrintf(" %#x=%#x", i, uc);
0N/A while (uc < 0x800)
0N/A {
0N/A g_szAll[i++] = 0xc0 | (uc >> 6);
0N/A g_szAll[i++] = 0x80 | (uc & 0x3f);
0N/A Assert(!((uc >> 6) & ~0x1f));
0N/A uc++;
0N/A }
0N/A //RTPrintf(" %#x=%#x", i, uc);
0N/A while (uc < 0xd800)
0N/A {
0N/A g_szAll[i++] = 0xe0 | (uc >> 12);
0N/A g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
0N/A g_szAll[i++] = 0x80 | (uc & 0x3f);
0N/A Assert(!((uc >> 12) & ~0xf));
0N/A uc++;
0N/A }
0N/A uc = 0xe000;
0N/A //RTPrintf(" %#x=%#x", i, uc);
0N/A while (uc < 0xfffe)
0N/A {
0N/A g_szAll[i++] = 0xe0 | (uc >> 12);
0N/A g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
0N/A g_szAll[i++] = 0x80 | (uc & 0x3f);
0N/A Assert(!((uc >> 12) & ~0xf));
0N/A uc++;
0N/A }
0N/A uc = 0x10000;
0N/A //RTPrintf(" %#x=%#x", i, uc);
0N/A while (uc < 0x110000)
0N/A {
0N/A g_szAll[i++] = 0xf0 | (uc >> 18);
0N/A g_szAll[i++] = 0x80 | ((uc >> 12) & 0x3f);
0N/A g_szAll[i++] = 0x80 | ((uc >> 6) & 0x3f);
0N/A g_szAll[i++] = 0x80 | (uc & 0x3f);
0N/A Assert(!((uc >> 18) & ~0x7));
0N/A uc++;
0N/A }
0N/A //RTPrintf(" %#x=%#x\n", i, uc);
0N/A g_szAll[i++] = '\0';
0N/A Assert(ELEMENTS(g_szAll) == i);
0N/A}
0N/A
0N/A
0N/Avoid test2(void)
0N/A{
0N/A RTPrintf("tstUtf8: TEST 2\n");
0N/A
0N/A /*
0N/A * Convert to UTF-8 and back.
0N/A */
0N/A RTPrintf("tstUtf8: #1: UTF-16 -> UTF-8 -> UTF-16...\n");
0N/A char *pszUtf8;
0N/A int rc = RTUtf16ToUtf8(&g_wszAll[0], &pszUtf8);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 mismatch!\n");
0N/A g_cErrors++;
0N/A }
0N/A
0N/A PRTUTF16 puszUcs2;
0N/A rc = RTStrToUtf16(pszUtf8, &puszUcs2);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A if (mymemcmp(puszUcs2, g_wszAll, sizeof(g_wszAll), 16))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed compare!\n");
0N/A g_cErrors++;
0N/A }
0N/A RTUtf16Free(puszUcs2);
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #1: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A RTStrFree(pszUtf8);
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #1: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Convert to UTF-16 and back. (just in case the above test fails)
0N/A */
0N/A RTPrintf("tstUtf8: #2: UTF-8 -> UTF-16 -> UTF-8...\n");
0N/A PRTUTF16 puszUcs2;
0N/A rc = RTStrToUtf16(&g_szAll[0], &puszUcs2);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A if (mymemcmp(puszUcs2, g_wszAll, sizeof(g_wszAll), 16))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed compare!\n");
0N/A g_cErrors++;
0N/A }
0N/A
0N/A char *pszUtf8;
0N/A rc = RTUtf16ToUtf8(puszUcs2, &pszUtf8);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A if (mymemcmp(pszUtf8, g_szAll, sizeof(g_szAll), 8))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed compare!\n");
0N/A g_cErrors++;
0N/A }
0N/A RTStrFree(pszUtf8);
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #2: UTF-16 -> UTF-8 failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A RTStrUcs2Free(puszUcs2);
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * Convert UTF-8 to CPs.
0N/A */
0N/A PRTUNICP paCps;
0N/A rc = RTStrToUni(g_szAll, &paCps);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A if (mymemcmp(paCps, g_uszAll, sizeof(g_uszAll), 32))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #2: UTF-8 -> UTF-16 failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A size_t cCps;
0N/A rc = RTStrToUniEx(g_szAll, RTSTR_MAX, &paCps, ELEMENTS(g_uszAll), &cCps);
0N/A if (rc == VINF_SUCCESS)
0N/A {
0N/A if (cCps != ELEMENTS(g_uszAll) - 1)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #3+: wrong Code Point count %zu, expected %zu\n", cCps, ELEMENTS(g_uszAll) - 1);
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #3+: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /** @todo RTCpsToUtf8 or something. */
0N/A }
0N/A else
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - the full #3a: UTF-8 -> Code Points failed, rc=%Rrc.\n", rc);
0N/A g_cErrors++;
0N/A }
0N/A
0N/A /*
0N/A * Check the various string lengths.
0N/A */
0N/A size_t cuc1 = RTStrCalcUtf16Len(g_szAll);
0N/A size_t cuc2 = RTUtf16Len(g_wszAll);
0N/A if (cuc1 != cuc2)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - cuc1=%zu != cuc2=%zu\n", cuc1, cuc2);
0N/A g_cErrors++;
0N/A }
0N/A //size_t cuc3 = RTUniLen(g_uszAll);
0N/A
0N/A
0N/A /*
0N/A * Enumerate the strings.
0N/A */
0N/A char *pszPut1Base = (char *)RTMemAlloc(sizeof(g_szAll));
0N/A AssertRelease(pszPut1Base);
0N/A char *pszPut1 = pszPut1Base;
0N/A PRTUTF16 pwszPut2Base = (PRTUTF16)RTMemAlloc(sizeof(g_wszAll));
0N/A AssertRelease(pwszPut2Base);
0N/A PRTUTF16 pwszPut2 = pwszPut2Base;
0N/A const char *psz1 = g_szAll;
0N/A const char *psz2 = g_szAll;
0N/A PCRTUTF16 pwsz3 = g_wszAll;
0N/A PCRTUTF16 pwsz4 = g_wszAll;
0N/A for (;;)
0N/A {
0N/A /*
0N/A * getters
0N/A */
0N/A RTUNICP uc1;
0N/A rc = RTStrGetCpEx(&psz1, &uc1);
0N/A if (RT_FAILURE(rc))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, psz2);
0N/A whereami(8, psz2 - &g_szAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A char *pszPrev1 = RTStrPrevCp(g_szAll, psz1);
0N/A if (pszPrev1 != psz2)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrPrevCp returned %p expected %p!\n", pszPrev1, psz2);
0N/A whereami(8, psz2 - &g_szAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A RTUNICP uc2 = RTStrGetCp(psz2);
0N/A if (uc2 != uc1)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc2, uc1);
0N/A whereami(8, psz2 - &g_szAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A psz2 = RTStrNextCp(psz2);
0N/A if (psz2 != psz1)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrGetCpEx and RTStrGetNext returned different next pointer!\n");
0N/A whereami(8, psz2 - &g_szAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A
0N/A RTUNICP uc3;
0N/A rc = RTUtf16GetCpEx(&pwsz3, &uc3);
0N/A if (RT_FAILURE(rc))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx failed with rc=%Rrc at %.10Rhxs\n", rc, pwsz4);
0N/A whereami(16, pwsz4 - &g_wszAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A if (uc3 != uc2)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTStrGetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc2);
0N/A whereami(16, pwsz4 - &g_wszAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A RTUNICP uc4 = RTUtf16GetCp(pwsz4);
0N/A if (uc3 != uc4)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetCp returned different CPs: %RTunicp != %RTunicp\n", uc3, uc4);
0N/A whereami(16, pwsz4 - &g_wszAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A pwsz4 = RTUtf16NextCp(pwsz4);
0N/A if (pwsz4 != pwsz3)
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16GetCpEx and RTUtf16GetNext returned different next pointer!\n");
0N/A whereami(8, pwsz4 - &g_wszAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * putters
0N/A */
0N/A pszPut1 = RTStrPutCp(pszPut1, uc1);
0N/A if (pszPut1 - pszPut1Base != psz1 - &g_szAll[0])
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
0N/A pszPut1 - pszPut1Base, psz1 - &g_szAll[0]);
0N/A whereami(8, psz2 - &g_szAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A
0N/A pwszPut2 = RTUtf16PutCp(pwszPut2, uc3);
0N/A if (pwszPut2 - pwszPut2Base != pwsz3 - &g_wszAll[0])
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrPutCp is not at the same offset! %p != %p\n",
0N/A pwszPut2 - pwszPut2Base, pwsz3 - &g_wszAll[0]);
0N/A whereami(8, pwsz4 - &g_wszAll[0]);
0N/A g_cErrors++;
0N/A break;
0N/A }
0N/A
0N/A
0N/A /* the end? */
0N/A if (!uc1)
0N/A break;
0N/A }
0N/A
0N/A /* check output if we seems to have made it thru it all. */
0N/A if (psz2 == &g_szAll[sizeof(g_szAll)])
0N/A {
0N/A if (mymemcmp(pszPut1Base, g_szAll, sizeof(g_szAll), 8))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTStrPutCp encoded the string incorrectly.\n");
0N/A g_cErrors++;
0N/A }
0N/A if (mymemcmp(pwszPut2Base, g_wszAll, sizeof(g_wszAll), 16))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16PutCp encoded the string incorrectly.\n");
0N/A g_cErrors++;
0N/A }
0N/A }
0N/A
0N/A RTMemFree(pszPut1Base);
0N/A RTMemFree(pwszPut2Base);
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Check case insensitivity.
0N/A */
0N/Avoid test3(void)
0N/A{
0N/A RTPrintf("tstUtf8: TEST 3\n");
0N/A
0N/A if ( RTUniCpToLower('a') != 'a'
0N/A || RTUniCpToLower('A') != 'a'
0N/A || RTUniCpToLower('b') != 'b'
0N/A || RTUniCpToLower('B') != 'b'
0N/A || RTUniCpToLower('Z') != 'z'
0N/A || RTUniCpToLower('z') != 'z'
0N/A || RTUniCpToUpper('c') != 'C'
0N/A || RTUniCpToUpper('C') != 'C'
0N/A || RTUniCpToUpper('z') != 'Z'
0N/A || RTUniCpToUpper('Z') != 'Z')
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUniToUpper/Lower failed basic tests.\n");
0N/A g_cErrors++;
0N/A }
0N/A
0N/A if (RTUtf16ICmp(g_wszAll, g_wszAll))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the basic test.\n");
0N/A g_cErrors++;
0N/A }
0N/A
0N/A if (RTUtf16Cmp(g_wszAll, g_wszAll))
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the basic test.\n");
0N/A g_cErrors++;
0N/A }
0N/A
0N/A 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 };
0N/A 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 };
0N/A if ( RTUtf16ICmp(s_wszTst1b, s_wszTst1b)
0N/A || RTUtf16ICmp(s_wszTst1a, s_wszTst1a)
0N/A || RTUtf16ICmp(s_wszTst1a, s_wszTst1b)
0N/A || RTUtf16ICmp(s_wszTst1b, s_wszTst1a)
0N/A )
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16ICmp failed the alphabet test.\n");
0N/A g_cErrors++;
0N/A }
0N/A
0N/A if ( RTUtf16Cmp(s_wszTst1b, s_wszTst1b)
0N/A || RTUtf16Cmp(s_wszTst1a, s_wszTst1a)
0N/A || !RTUtf16Cmp(s_wszTst1a, s_wszTst1b)
0N/A || !RTUtf16Cmp(s_wszTst1b, s_wszTst1a)
0N/A )
0N/A {
0N/A RTPrintf("tstUtf8: FAILURE - RTUtf16Cmp failed the alphabet test.\n");
0N/A g_cErrors++;
0N/A }
0N/A}
0N/A
0N/A
0N/A/**
0N/A * Benchmark stuff.
0N/A */
0N/Avoid Benchmarks(void)
0N/A{
0N/A RTPrintf("tstUtf8: BENCHMARKS\n");
0N/A static union
0N/A {
0N/A RTUTF16 wszBuf[sizeof(g_wszAll)];
0N/A char szBuf[sizeof(g_szAll)];
0N/A } s_Buf;
0N/A
0N/A PRTUTF16 pwsz = &s_Buf.wszBuf[0];
0N/A int rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, ELEMENTS(s_Buf.wszBuf), NULL);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A int i;
0N/A uint64_t u64Start = RTTimeNanoTS();
0N/A for (i = 0; i < 100; i++)
0N/A {
0N/A rc = RTStrToUtf16Ex(&g_szAll[0], RTSTR_MAX, &pwsz, ELEMENTS(s_Buf.wszBuf), NULL);
0N/A if (RT_FAILURE(rc))
0N/A {
0N/A RTPrintf("tstUtf8: UTF-8 -> UTF-16 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
0N/A break;
0N/A }
0N/A }
0N/A uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
0N/A RTPrintf("tstUtf8: UTF-8 -> UTF-16: %d in %RI64ns\n", i, u64Elapsed);
0N/A }
0N/A
0N/A char *psz = &s_Buf.szBuf[0];
0N/A rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, ELEMENTS(s_Buf.szBuf), NULL);
0N/A if (RT_SUCCESS(rc))
0N/A {
0N/A int i;
0N/A uint64_t u64Start = RTTimeNanoTS();
0N/A for (i = 0; i < 100; i++)
0N/A {
0N/A rc = RTUtf16ToUtf8Ex(&g_wszAll[0], RTSTR_MAX, &psz, ELEMENTS(s_Buf.szBuf), NULL);
0N/A if (RT_FAILURE(rc))
0N/A {
0N/A RTPrintf("tstUtf8: UTF-16 -> UTF-8 benchmark failed at i=%d, rc=%Rrc\n", i, rc);
0N/A break;
0N/A }
0N/A }
0N/A uint64_t u64Elapsed = RTTimeNanoTS() - u64Start;
0N/A RTPrintf("tstUtf8: UTF-16 -> UTF-8: %d in %RI64ns\n", i, u64Elapsed);
0N/A }
0N/A
0N/A}
0N/A
0N/A
0N/Aint main()
0N/A{
0N/A RTR3Init(false);
0N/A
0N/A InitStrings();
0N/A test1();
0N/A test2();
0N/A test3();
0N/A Benchmarks();
0N/A
0N/A /*
0N/A * Summary
0N/A */
0N/A if (!g_cErrors)
0N/A RTPrintf("tstUtf8: SUCCESS\n");
0N/A else
0N/A RTPrintf("tstUtf8: FAILURE - %d errors!\n", g_cErrors);
0N/A
0N/A return !!g_cErrors;
0N/A}
0N/A