tstIprtMiniString.cpp revision 427a6e001f3fee7f81f96af114313a0a5ba73bef
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync/* $Id$ */
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync/** @file
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * IPRT Testcase - iprt::MiniString.
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync */
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync/*
904810c4c6668233349b025cc58013cb7c11c701vboxsync * Copyright (C) 2006-2010 Oracle Corporation
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync *
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * available from http://www.virtualbox.org. This file is free software;
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * you can redistribute it and/or modify it under the terms of the GNU
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * General Public License (GPL) as published by the Free Software
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync *
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync * The contents of this file may alternatively be used under the terms
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync * of the Common Development and Distribution License Version 1.0
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync * VirtualBox OSE distribution, in which case the provisions of the
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync * CDDL are applicable instead of those of the GPL.
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync *
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync * You may elect to license modified versions of this file under the
1ce069685b24d243eb0464f46d4c56b250c64445vboxsync * terms and conditions of either the GPL or the CDDL or both.
a55af63ead2dcca370bfc0dfe49771d9dcc61b93vboxsync */
e0778e583cb4a0bdc9bcc48f5957e00a01108388vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync/*******************************************************************************
a55af63ead2dcca370bfc0dfe49771d9dcc61b93vboxsync* Header Files *
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync*******************************************************************************/
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync#include <iprt/cpp/ministring.h>
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync#include <iprt/err.h>
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync#include <iprt/mem.h>
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync#include <iprt/string.h>
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync#include <iprt/test.h>
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync#include <iprt/uni.h>
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsyncstatic void test1Hlp1(const char *pszExpect, const char *pszFormat, ...)
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync{
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync va_list va;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync va_start(va, pszFormat);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync iprt::MiniString strTst(pszFormat, va);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync va_end(va);
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync RTTESTI_CHECK_MSG(strTst.equals(pszExpect), ("strTst='%s' expected='%s'\n", strTst.c_str(), pszExpect));
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync}
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsync
b0ad0bbadf3a5b5258acda1bfe16f0ad8bee5ff0vboxsyncstatic void test1(RTTEST hTest)
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync{
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTestSub(hTest, "Basics");
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync#define CHECK(expr) RTTESTI_CHECK(expr)
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync#define CHECK_DUMP(expr, value) \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync do { \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync if (!(expr)) \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync } while (0)
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync#define CHECK_DUMP_I(expr) \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync do { \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync if (!(expr)) \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync } while (0)
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync iprt::MiniString empty;
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(empty.length() == 0);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync CHECK(empty.capacity() == 0);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync iprt::MiniString sixbytes("12345");
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync CHECK(sixbytes.length() == 5);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync CHECK(sixbytes.capacity() == 6);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync sixbytes.append(iprt::MiniString("678"));
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(sixbytes.length() == 8);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(sixbytes.capacity() >= 9);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
a2d9f81753c129b5e3bbe3769627dcd25a3724bdvboxsync sixbytes.append("9a");
a2d9f81753c129b5e3bbe3769627dcd25a3724bdvboxsync CHECK(sixbytes.length() == 10);
a2d9f81753c129b5e3bbe3769627dcd25a3724bdvboxsync CHECK(sixbytes.capacity() >= 11);
a2d9f81753c129b5e3bbe3769627dcd25a3724bdvboxsync
a2d9f81753c129b5e3bbe3769627dcd25a3724bdvboxsync char *psz = sixbytes.mutableRaw();
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync // 123456789a
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync // ^
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync // 0123456
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync psz[6] = '\0';
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync sixbytes.jolt();
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(sixbytes.length() == 6);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(sixbytes.capacity() == 7);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync iprt::MiniString morebytes("tobereplaced");
a2d9f81753c129b5e3bbe3769627dcd25a3724bdvboxsync morebytes = "newstring ";
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync morebytes.append(sixbytes);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync iprt::MiniString third(morebytes);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync third.reserve(100 * 1024); // 100 KB
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(third.capacity() == 100 * 1024);
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync CHECK(third.length() == morebytes.length()); // must not have changed
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync iprt::MiniString copy1(morebytes);
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync iprt::MiniString copy2 = morebytes;
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(copy1 == copy2);
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync copy1 = NULL;
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(copy1.length() == 0);
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync copy1 = "";
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(copy1.length() == 0);
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("abc") < iprt::MiniString("def"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("") < iprt::MiniString("def"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("abc") > iprt::MiniString(""));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("abc") != iprt::MiniString("def"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK_DUMP_I(iprt::MiniString("def") > iprt::MiniString("abc"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("abc") == iprt::MiniString("abc"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("").compare("") == 0);
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("").compare(NULL) == 0);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(iprt::MiniString("").compare("a") < 0);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(iprt::MiniString("a").compare("") > 0);
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync CHECK(iprt::MiniString("a").compare(NULL) > 0);
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync
7bf07b1592dfaab1a4fb6d497fd0ff1302fb7585vboxsync CHECK(iprt::MiniString("abc") < "def");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync CHECK(iprt::MiniString("abc") != "def");
7c1f498692cd2393f8ba68cb62be482495106f93vboxsync CHECK_DUMP_I(iprt::MiniString("def") > "abc");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync CHECK(iprt::MiniString("abc") == "abc");
7c1f498692cd2393f8ba68cb62be482495106f93vboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync CHECK(iprt::MiniString("abc").equals("abc"));
4715f6e2473402353acb43c06a8986f957758259vboxsync CHECK(!iprt::MiniString("abc").equals("def"));
4715f6e2473402353acb43c06a8986f957758259vboxsync CHECK(iprt::MiniString("abc").equalsIgnoreCase("Abc"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABc"));
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(iprt::MiniString("abc").equalsIgnoreCase("ABC"));
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(!iprt::MiniString("abc").equalsIgnoreCase("dBC"));
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync CHECK(iprt::MiniString("").equals(""));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(iprt::MiniString("").equals(NULL));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync CHECK(!iprt::MiniString("").equals("a"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(!iprt::MiniString("a").equals(""));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(!iprt::MiniString("a").equals(NULL));
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(iprt::MiniString("").equalsIgnoreCase(""));
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(iprt::MiniString("").equalsIgnoreCase(NULL));
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync CHECK(!iprt::MiniString("").equalsIgnoreCase("a"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(!iprt::MiniString("a").equalsIgnoreCase(""));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync copy2.setNull();
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync for (int i = 0; i < 100; ++i)
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync {
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync copy2.reserve(50); // should be ignored after 50 loops
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync copy2.append("1");
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync }
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync CHECK(copy2.length() == 100);
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync copy2.setNull();
1147e980f6ab0b7ff1d08c13ad1c03eea30d102dvboxsync for (int i = 0; i < 100; ++i)
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync {
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync copy2.reserve(50); // should be ignored after 50 loops
7e8d44e04156d78379666f20d5daa2a79e5cbf44vboxsync copy2.append('1');
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync }
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync CHECK(copy2.length() == 100);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync /* printf */
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync iprt::MiniString StrFmt;
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync CHECK(StrFmt.printf("%s-%s-%d", "abc", "def", 42).equals("abc-def-42"));
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync test1Hlp1("abc-42-def", "%s-%d-%s", "abc", 42, "def");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync test1Hlp1("", "");
1147e980f6ab0b7ff1d08c13ad1c03eea30d102dvboxsync test1Hlp1("1", "1");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync test1Hlp1("foobar", "%s", "foobar");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync#undef CHECK
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync#undef CHECK_DUMP
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync#undef CHECK_DUMP_I
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync}
505ddd00252720bfb5569fcb17bfda53dc141e3bvboxsync
505ddd00252720bfb5569fcb17bfda53dc141e3bvboxsync
505ddd00252720bfb5569fcb17bfda53dc141e3bvboxsyncstatic int mymemcmp(const char *psz1, const char *psz2, size_t cch)
a1b4fb3917412d2632d358ff8989f1ec971f2d5bvboxsync{
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync for (size_t off = 0; off < cch; off++)
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync if (psz1[off] != psz2[off])
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync {
85bc82f92f4d7a7b95e96d133c4847b2847703dfvboxsync RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RT_MIN(cch - off, 8), &psz1[off],
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RT_MIN(cch - off, 8), &psz2[off]);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync return psz1[off] > psz2[off] ? 1 : -1;
8fdb854581fe3cb394d84835dc09b02e6e18d4edvboxsync }
8fdb854581fe3cb394d84835dc09b02e6e18d4edvboxsync return 0;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync}
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsyncstatic void test2(RTTEST hTest)
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync{
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync#define CHECK_EQUAL(str1, str2) \
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync do \
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync { \
657b2c9f6d33f08001e5fa6f6e0572dcf0391013vboxsync RTTESTI_CHECK(strlen((str1).c_str()) == (str1).length()); \
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK((str1).length() == (str2).length()); \
8b36957d815c23b479eb35d93ac76c66392e9402vboxsync RTTESTI_CHECK(mymemcmp((str1).c_str(), (str2).c_str(), (str2).length() + 1) == 0); \
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync } while (0)
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
49e54e2ffe0c10864d06e9d1ebe24a8eb1327a6bvboxsync iprt::MiniString strTmp;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync char szDst[16];
49e54e2ffe0c10864d06e9d1ebe24a8eb1327a6bvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync /* Collect all upper and lower case code points. */
7a29aa5ce149ccd344a2929d2815b8e212690b92vboxsync iprt::MiniString strLower("");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strLower.reserve(_4M);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync iprt::MiniString strUpper("");
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strUpper.reserve(_4M);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync for (RTUNICP uc = 1; uc <= 0x10fffd; uc++)
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync {
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync if (RTUniCpIsLower(uc))
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync {
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strLower.appendCodePoint(uc);
8c58249d7f93d82395aa6c8b31526443892bc375vboxsync }
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync if (RTUniCpIsUpper(uc))
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync {
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strUpper.appendCodePoint(uc);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync }
5453eb2f2719bf3895ac3daeec4c26e0b6e2eb4avboxsync }
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
9bb98d54790a98dad0ad6d9bdc5d319b6fdf0bffvboxsync RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
9bb98d54790a98dad0ad6d9bdc5d319b6fdf0bffvboxsync /* Fold each code point in the lower case string and check that it encodes
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync into the same or less number of bytes. */
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync size_t cch = 0;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync const char *pszCur = strLower.c_str();
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync iprt::MiniString strUpper2("");
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync strUpper2.reserve(strLower.length() + 64);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync for (;;)
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync {
b604fbf16eda38d14b4999c245f032bfaa5aa85avboxsync RTUNICP ucLower;
93c114476cca834cde109c60bfb95094fcd2c995vboxsync const char * const pszPrev = pszCur;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
629e8f58c569e4c51a98284c41754fd9f0b973f8vboxsync size_t const cchSrc = pszCur - pszPrev;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync if (!ucLower)
7c1f498692cd2393f8ba68cb62be482495106f93vboxsync break;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTUNICP const ucUpper = RTUniCpToUpper(ucLower);
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync size_t const cchDst = pszDstEnd - &szDst[0];
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK_MSG(cchSrc >= cchDst,
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync ucLower, cchSrc, ucUpper, cchDst));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync cch += cchDst;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strUpper2.appendCodePoint(ucUpper);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync /* roundtrip stability */
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTUNICP const ucUpper2 = RTUniCpToUpper(ucUpper);
aa0553becec2abc2e781f839ba1d399c31c2c07fvboxsync RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTUNICP const ucLower2 = RTUniCpToLower(ucUpper);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTUNICP const ucUpper3 = RTUniCpToUpper(ucLower2);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync pszDstEnd = RTStrPutCp(szDst, ucLower2);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync size_t const cchLower2 = pszDstEnd - &szDst[0];
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK_MSG(cchDst == cchLower2,
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync ucLower2, cchLower2, ucUpper, cchDst));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync }
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK(strlen(strUpper2.c_str()) == strUpper2.length());
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTESTI_CHECK_MSG(cch == strUpper2.length(), ("cch=%u length()=%u\n", cch, strUpper2.length()));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
67ee25dd0b63a61dc35a35d0aade75ca6cd06350vboxsync /* the toUpper method shall do the same thing. */
67ee25dd0b63a61dc35a35d0aade75ca6cd06350vboxsync strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
67ee25dd0b63a61dc35a35d0aade75ca6cd06350vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
67ee25dd0b63a61dc35a35d0aade75ca6cd06350vboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync /* Ditto for the upper case string. */
914fe6f35c397cfd7d97b44a63398c6422579c49vboxsync cch = 0;
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync pszCur = strUpper.c_str();
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync iprt::MiniString strLower2("");
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync strLower2.reserve(strUpper.length() + 64);
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync for (;;)
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync {
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync RTUNICP ucUpper;
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync const char * const pszPrev = pszCur;
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync size_t const cchSrc = pszCur - pszPrev;
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync if (!ucUpper)
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync break;
5704a317c3b6bdf4b59f6a6ebe45d443296b1995vboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTUNICP const ucLower = RTUniCpToLower(ucUpper);
5704a317c3b6bdf4b59f6a6ebe45d443296b1995vboxsync const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync size_t const cchDst = pszDstEnd - &szDst[0];
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTESTI_CHECK_MSG(cchSrc >= cchDst,
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync ucUpper, cchSrc, ucLower, cchDst));
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync cch += cchDst;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strLower2.appendCodePoint(ucLower);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync /* roundtrip stability */
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTUNICP const ucLower2 = RTUniCpToLower(ucLower);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync RTUNICP const ucUpper2 = RTUniCpToUpper(ucLower);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync RTUNICP const ucLower3 = RTUniCpToLower(ucUpper2);
a0644dbbd30adb9bd2937110d6f016e56c4cc52bvboxsync RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync pszDstEnd = RTStrPutCp(szDst, ucUpper2);
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync size_t const cchUpper2 = pszDstEnd - &szDst[0];
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync RTTESTI_CHECK_MSG(cchDst == cchUpper2,
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync ucUpper2, cchUpper2, ucLower, cchDst));
229a63857c5fd2e441a62206eacce4156d0b2a26vboxsync }
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync RTTESTI_CHECK(strlen(strLower2.c_str()) == strLower2.length());
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync RTTESTI_CHECK_MSG(cch == strLower2.length(), ("cch=%u length()=%u\n", cch, strLower2.length()));
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync /* Checks of folding stability when nothing shall change. */
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
2711c80499bbd95e3da4a6cd2dffd9f81a5dce98vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
2a171646d32f8a15e9820d6fb3bf3f9b9990ca3fvboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
cc74f15083bf80fbc96723a89faa06c15d0dead8vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
3933885bc0c2c93436d858a14564c6179ec72872vboxsync
9ca017ceee656f9d33f2cb6652e401b5f17fcfb7vboxsync strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
cc74f15083bf80fbc96723a89faa06c15d0dead8vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
36a04912b64bea8318327fe0723535f1b3f041b0vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
36a04912b64bea8318327fe0723535f1b3f041b0vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower);
3933885bc0c2c93436d858a14564c6179ec72872vboxsync
6e25221ce8ef8e656d1e15eb7ec5cf8ae758ceb2vboxsync strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
3933885bc0c2c93436d858a14564c6179ec72872vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
3933885bc0c2c93436d858a14564c6179ec72872vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
3933885bc0c2c93436d858a14564c6179ec72872vboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
3933885bc0c2c93436d858a14564c6179ec72872vboxsync
824ae3158a8b8f8233fec3f5a12c81f139933698vboxsync /* Check folding stability for roundtrips. */
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp = strUpper; CHECK_EQUAL(strTmp, strUpper);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp.toUpper();
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp.toUpper();
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp.toLower(); CHECK_EQUAL(strTmp, strLower2);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync
23d9dbdaf1b83107abf9882246a4a46933f733efvboxsync strTmp = strLower; CHECK_EQUAL(strTmp, strLower);
a9315925c69e4c3bb342bb317ca5b6d29e1ee467vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
a9315925c69e4c3bb342bb317ca5b6d29e1ee467vboxsync strTmp.toLower();
a9315925c69e4c3bb342bb317ca5b6d29e1ee467vboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
80b09746c07120c881b61b18f30c9c4a2a4af9c5vboxsync strTmp.toLower();
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync strTmp.toUpper(); CHECK_EQUAL(strTmp, strUpper2);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync}
80b09746c07120c881b61b18f30c9c4a2a4af9c5vboxsync
d7c2269005543188a6fb12775f6d416431e39ebcvboxsync
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsyncint main()
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync{
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTTEST hTest;
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);
3a8aa22ef125135ef67bfc396771bcee15ef02dfvboxsync if (rcExit == RTEXITCODE_SUCCESS)
7708252d252a55417a6a817041e4356797e34255vboxsync {
62ab017295981c81484e5a5f93ff8b5f85f7defbvboxsync RTTestBanner(hTest);
62ab017295981c81484e5a5f93ff8b5f85f7defbvboxsync
62ab017295981c81484e5a5f93ff8b5f85f7defbvboxsync test1(hTest);
7708252d252a55417a6a817041e4356797e34255vboxsync test2(hTest);
7708252d252a55417a6a817041e4356797e34255vboxsync
d7c2269005543188a6fb12775f6d416431e39ebcvboxsync rcExit = RTTestSummaryAndDestroy(hTest);
45a01ef53b009e9f56ce427bd8688da02ad32389vboxsync }
45a01ef53b009e9f56ce427bd8688da02ad32389vboxsync return rcExit;
9a32cc64d7694b4b8c46bbf17fdbb5e5f60d2d4avboxsync}
94c4b844625733fedcd2b473c207981b31316bd9vboxsync
94c4b844625733fedcd2b473c207981b31316bd9vboxsync