tstIprtMiniString.cpp revision e39f7584900a350071fb8aa0997caa99cf2cc5ee
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * IPRT Testcase - iprt::MiniString.
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync * Copyright (C) 2006-2010 Oracle Corporation
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 * 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 * 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* Header Files *
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync*******************************************************************************/
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncstatic void test1Hlp1(const char *pszExpect, const char *pszFormat, ...)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(strTst.equals(pszExpect), ("strTst='%s' expected='%s'\n", strTst.c_str(), pszExpect));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestFailed(hTest, "%d: FAILED %s, got \"%s\"", __LINE__, #expr, value); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync } while (0)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestFailed(hTest, "%d: FAILED %s, got \"%d\"", __LINE__, #expr, expr); \
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync } while (0)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync // 123456789a
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK_DUMP(morebytes == "newstring 123456", morebytes.c_str());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK_DUMP(third == "newstring 123456", morebytes.c_str() );
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync CHECK(third.length() == morebytes.length()); // must not have changed
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 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 for (int i = 0; i < 100; ++i)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.reserve(50); // should be ignored after 50 loops
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync for (int i = 0; i < 100; ++i)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync copy2.reserve(50); // should be ignored after 50 loops
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* printf */
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");
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsyncstatic int mymemcmp(const char *psz1, const char *psz2, size_t cch)
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestIFailed("off=%#x psz1=%.*Rhxs psz2=%.*Rhxs\n", off,
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTestSub(hTest, "UTF-8 upper/lower encoding assumption");
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 /* Collect all upper and lower case code points. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(uc < 0xd800 || (uc > 0xdfff && uc != 0xfffe && uc != 0xffff), ("%#x\n", uc));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen(strLower.c_str()) == strLower.length());
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK(strlen(strUpper.c_str()) == strUpper.length());
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 RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucLower), VINF_SUCCESS);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char *pszDstEnd = RTStrPutCp(szDst, ucUpper);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucLower=%#x %u bytes; ucUpper=%#x %u bytes\n",
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* roundtrip stability */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucUpper2 == ucUpper, ("ucUpper2=%#x ucUpper=%#x\n", ucUpper2, ucUpper));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucUpper3 == ucUpper, ("ucUpper3=%#x ucUpper=%#x\n", ucUpper3, ucUpper));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucLower2=%#x %u bytes; ucUpper=%#x %u bytes\n",
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 /* the toUpper method shall do the same thing. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Ditto for the upper case string. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_RC_BREAK(RTStrGetCpEx(&pszCur, &ucUpper), VINF_SUCCESS);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync const char *pszDstEnd = RTStrPutCp(szDst, ucLower);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucUpper=%#x %u bytes; ucLower=%#x %u bytes\n",
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* roundtrip stability */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucLower2 == ucLower, ("ucLower2=%#x ucLower=%#x\n", ucLower2, ucLower));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTTESTI_CHECK_MSG(ucLower3 == ucLower, ("ucLower3=%#x ucLower=%#x\n", ucLower3, ucLower));
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync ("ucUpper2=%#x %u bytes; ucLower=%#x %u bytes\n",
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 /* Checks of folding stability when nothing shall change. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strUpper2; CHECK_EQUAL(strTmp, strUpper2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync strTmp = strLower2; CHECK_EQUAL(strTmp, strLower2);
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync /* Check folding stability for roundtrips. */
dee9e52b1688c0617890cbbd8a8488f9f315d1b7vboxsync RTEXITCODE rcExit = RTTestInitAndCreate("tstIprtMiniString", &hTest);