tstIprtMiniString.cpp revision e39f7584900a350071fb8aa0997caa99cf2cc5ee
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync/* $Id$ */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync/** @file
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * IPRT Testcase - iprt::MiniString.
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync/*
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * Copyright (C) 2006-2010 Oracle Corporation
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync *
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * available from http://www.virtualbox.org. This file is free software;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * you can redistribute it and/or modify it under the terms of the GNU
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * General Public License (GPL) as published by the Free Software
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync *
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * The contents of this file may alternatively be used under the terms
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * of the Common Development and Distribution License Version 1.0
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * VirtualBox OSE distribution, in which case the provisions of the
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * CDDL are applicable instead of those of the GPL.
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync *
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * You may elect to license modified versions of this file under the
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * terms and conditions of either the GPL or the CDDL or both.
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync/*******************************************************************************
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync* Header Files *
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync*******************************************************************************/
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#include <iprt/cpp/ministring.h>
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#include <iprt/err.h>
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#include <iprt/mem.h>
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#include <iprt/string.h>
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#include <iprt/test.h>
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#include <iprt/uni.h>
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncstatic void test1Hlp1(const char *pszExpect, const char *pszFormat, ...)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync{
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync va_list va;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync va_start(va, pszFormat);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString strTst(pszFormat, va);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync va_end(va);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(strTst.equals(pszExpect), ("strTst='%s' expected='%s'\n", strTst.c_str(), pszExpect));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync}
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncstatic void test1(RTTEST hTest)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync{
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestSub(hTest, "Basics");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#define CHECK(expr) RTTESTI_CHECK(expr)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#define CHECK_DUMP(expr, value) \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync do { \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (!(expr)) \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync } while (0)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#define CHECK_DUMP_I(expr) \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync do { \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (!(expr)) \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync } while (0)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString empty;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(empty.length() == 0);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(empty.capacity() == 0);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString sixbytes("12345");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.length() == 5);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.capacity() == 6);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync sixbytes.append(iprt::MiniString("678"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.length() == 8);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.capacity() >= 9);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync sixbytes.append("9a");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.length() == 10);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.capacity() >= 11);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync char *psz = sixbytes.mutableRaw();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync // 123456789a
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync // ^
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync // 0123456
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync psz[6] = '\0';
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync sixbytes.jolt();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.length() == 6);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(sixbytes.capacity() == 7);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString morebytes("tobereplaced");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync morebytes = "newstring ";
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync morebytes.append(sixbytes);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString third(morebytes);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync third.reserve(100 * 1024); // 100 KB
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(third.capacity() == 100 * 1024);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(third.length() == morebytes.length()); // must not have changed
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString copy1(morebytes);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString copy2 = morebytes;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(copy1 == copy2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy1 = NULL;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(copy1.length() == 0);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy1 = "";
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(copy1.length() == 0);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc") < iprt::MiniString("def"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc") != iprt::MiniString("def"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK_DUMP_I(iprt::MiniString("def") > iprt::MiniString("abc"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc") == iprt::MiniString("abc"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc") < "def");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc") != "def");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK_DUMP_I(iprt::MiniString("def") > "abc");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc") == "abc");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc").equals("abc"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(!iprt::MiniString("abc").equals("def"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc").equalsIgnoreCase("Abc"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABc"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABC"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(!iprt::MiniString("abc").equalsIgnoreCase("dBC"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("").equals(""));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(iprt::MiniString("").equalsIgnoreCase(""));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.setNull();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (int i = 0; i < 100; ++i)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.reserve(50); // should be ignored after 50 loops
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.append("1");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(copy2.length() == 100);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.setNull();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (int i = 0; i < 100; ++i)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.reserve(50); // should be ignored after 50 loops
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.append('1');
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(copy2.length() == 100);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* printf */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString StrFmt;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(StrFmt.printf("%s-%s-%d", "abc", "def", 42).equals("abc-def-42"));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync test1Hlp1("abc-42-def", "%s-%d-%s", "abc", 42, "def");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync test1Hlp1("", "");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync test1Hlp1("1", "1");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync test1Hlp1("foobar", "%s", "foobar");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#undef CHECK
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#undef CHECK_DUMP
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#undef CHECK_DUMP_I
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync}
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncstatic int mymemcmp(const char *psz1, const char *psz2, size_t cch)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync{
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (size_t off = 0; off < cch; off++)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (psz1[off] != psz2[off])
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RT_MIN(cch - off, 8), &psz1[off],
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RT_MIN(cch - off, 8), &psz2[off]);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync return psz1[off] > psz2[off] ? 1 : -1;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync return 0;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync}
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncstatic void test2(RTTEST hTest)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync{
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync#define CHECK_EQUAL(str1, str2) \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync do \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync { \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen((str1).c_str()) == (str1).length()); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK((str1).length() == (str2).length()); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(mymemcmp((str1).c_str(), (str2).c_str(), (str2).length() + 1) == 0); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync } while (0)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString strTmp;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync char szDst[16];
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Collect all upper and lower case code points. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString strLower("");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strLower.reserve(_4M);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString strUpper("");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strUpper.reserve(_4M);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (RTUNICP uc = 1; uc <= 0x10fffd; uc++)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (RTUniCpIsLower(uc))
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strLower.appendCodePoint(uc);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (RTUniCpIsUpper(uc))
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strUpper.appendCodePoint(uc);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Fold each code point in the lower case string and check that it encodes
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync into the same or less number of bytes. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t cch = 0;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char *pszCur = strLower.c_str();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString strUpper2("");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strUpper2.reserve(strLower.length() + 64);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (;;)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP ucLower;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char * const pszPrev = pszCur;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t const cchSrc = pszCur - pszPrev;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (!ucLower)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync break;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucUpper = RTUniCpToUpper(ucLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t const cchDst = pszDstEnd - &szDst[0];
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(cchSrc >= cchDst,
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ucLower, cchSrc, ucUpper, cchDst));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync cch += cchDst;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strUpper2.appendCodePoint(ucUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* roundtrip stability */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucUpper2 = RTUniCpToUpper(ucUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucLower2 = RTUniCpToLower(ucUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucUpper3 = RTUniCpToUpper(ucLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync pszDstEnd = RTStrPutCp(szDst, ucLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t const cchLower2 = pszDstEnd - &szDst[0];
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(cchDst == cchLower2,
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ucLower2, cchLower2, ucUpper, cchDst));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen(strUpper2.c_str()) == strUpper2.length());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(cch == strUpper2.length(), ("cch=%u length()=%u\n", cch, strUpper2.length()));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* the toUpper method shall do the same thing. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Ditto for the upper case string. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync cch = 0;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync pszCur = strUpper.c_str();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync iprt::MiniString strLower2("");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strLower2.reserve(strUpper.length() + 64);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (;;)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP ucUpper;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char * const pszPrev = pszCur;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t const cchSrc = pszCur - pszPrev;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (!ucUpper)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync break;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucLower = RTUniCpToLower(ucUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t const cchDst = pszDstEnd - &szDst[0];
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(cchSrc >= cchDst,
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ucUpper, cchSrc, ucLower, cchDst));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync cch += cchDst;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strLower2.appendCodePoint(ucLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* roundtrip stability */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucLower2 = RTUniCpToLower(ucLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucUpper2 = RTUniCpToUpper(ucLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTUNICP const ucLower3 = RTUniCpToLower(ucUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync pszDstEnd = RTStrPutCp(szDst, ucUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync size_t const cchUpper2 = pszDstEnd - &szDst[0];
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(cchDst == cchUpper2,
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ucUpper2, cchUpper2, ucLower, cchDst));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen(strLower2.c_str()) == strLower2.length());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(cch == strLower2.length(), ("cch=%u length()=%u\n", cch, strLower2.length()));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Checks of folding stability when nothing shall change. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Check folding stability for roundtrips. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toLower();
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync}
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncint main()
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync{
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTEST hTest;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync if (rcExit == RTEXITCODE_SUCCESS)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync {
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestBanner(hTest);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync test1(hTest);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync test2(hTest);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync rcExit = RTTestSummaryAndDestroy(hTest);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync }
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync return rcExit;
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync}
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync