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