summaryrefslogtreecommitdiffstats
path: root/comphelper/qa/string
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /comphelper/qa/string
parentInitial commit. (diff)
downloadlibreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.tar.xz
libreoffice-ed5640d8b587fbcfed7dd7967f3de04b37a76f26.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comphelper/qa/string')
-rw-r--r--comphelper/qa/string/NaturalStringSortTest.cxx95
-rw-r--r--comphelper/qa/string/test_string.cxx246
2 files changed, 341 insertions, 0 deletions
diff --git a/comphelper/qa/string/NaturalStringSortTest.cxx b/comphelper/qa/string/NaturalStringSortTest.cxx
new file mode 100644
index 000000000..bfdcaff6e
--- /dev/null
+++ b/comphelper/qa/string/NaturalStringSortTest.cxx
@@ -0,0 +1,95 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <comphelper/string.hxx>
+#include <comphelper/processfactory.hxx>
+#include <cppuhelper/implbase.hxx>
+#include <com/sun/star/i18n/CharType.hpp>
+#include <com/sun/star/i18n/XBreakIterator.hpp>
+#include <com/sun/star/i18n/XCollator.hpp>
+
+#include <unotest/bootstrapfixturebase.hxx>
+#include <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+#include <rtl/ustring.hxx>
+
+using namespace css;
+
+namespace
+{
+class TestStringNaturalCompare : public test::BootstrapFixtureBase
+{
+public:
+ void testNatural()
+ {
+ lang::Locale aLocale;
+ aLocale.Language = "en";
+ aLocale.Country = "US";
+
+ comphelper::string::NaturalStringSorter aSorter(comphelper::getProcessComponentContext(),
+ aLocale);
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+0), aSorter.compare("ABC", "ABC"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("ABC", "abc"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("abc", "ABC"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("alongstring", "alongerstring"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("alongerstring", "alongstring"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("Heading 9", "Heading 10"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("Heading 10", "Heading 9"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("July, the 4th", "July, the 10th"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("July, the 10th", "July, the 4th"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("abc08", "abc010"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("abc010", "abc08"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+0), aSorter.compare("apple10apple", "apple10apple"));
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("KA1", "KA0"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+0), aSorter.compare("KA1", "KA1"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("KA1", "KA2"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("KA50", "KA5"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("KA50", "KA100"));
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("1", "0"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+0), aSorter.compare("1", "1"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("1", "2"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("11", "1"));
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("50", "100"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("0", "100000"));
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("0", "A"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("A", "0"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("A", "99"));
+
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("00ABC2", "00ABC1"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("00ABC1", "00ABC2"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(+1), aSorter.compare("00ABC11", "00ABC2"));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(-1), aSorter.compare("00ABC2", "00ABC11"));
+ }
+
+ CPPUNIT_TEST_SUITE(TestStringNaturalCompare);
+ CPPUNIT_TEST(testNatural);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TestStringNaturalCompare);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx
new file mode 100644
index 000000000..58f9c3f63
--- /dev/null
+++ b/comphelper/qa/string/test_string.cxx
@@ -0,0 +1,246 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * This file incorporates work covered by the following license notice:
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed
+ * with this work for additional information regarding copyright
+ * ownership. The ASF licenses this file to you under the Apache
+ * License, Version 2.0 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.apache.org/licenses/LICENSE-2.0 .
+ */
+
+#include <sal/config.h>
+
+#include <iterator>
+
+#include <comphelper/string.hxx>
+#include <cppuhelper/implbase.hxx>
+#include <com/sun/star/i18n/CharType.hpp>
+#include <com/sun/star/i18n/XBreakIterator.hpp>
+#include <com/sun/star/i18n/XCollator.hpp>
+
+#include <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+#include <rtl/string.hxx>
+#include <rtl/ustring.hxx>
+
+namespace {
+
+class TestString: public CppUnit::TestFixture
+{
+public:
+ void testStripStart();
+ void testStripEnd();
+ void testStrip();
+ void testToken();
+ void testTokenCount();
+ void testDecimalStringToNumber();
+ void testIsdigitAsciiString();
+ void testReverseString();
+ void testReverseCodePoints();
+ void testSplit();
+ void testRemoveAny();
+
+ CPPUNIT_TEST_SUITE(TestString);
+ CPPUNIT_TEST(testStripStart);
+ CPPUNIT_TEST(testStripEnd);
+ CPPUNIT_TEST(testStrip);
+ CPPUNIT_TEST(testToken);
+ CPPUNIT_TEST(testTokenCount);
+ CPPUNIT_TEST(testDecimalStringToNumber);
+ CPPUNIT_TEST(testIsdigitAsciiString);
+ CPPUNIT_TEST(testReverseString);
+ CPPUNIT_TEST(testReverseCodePoints);
+ CPPUNIT_TEST(testSplit);
+ CPPUNIT_TEST(testRemoveAny);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void TestString::testDecimalStringToNumber()
+{
+ OUString s1("1234");
+ CPPUNIT_ASSERT_EQUAL(sal_uInt32(1234), comphelper::string::decimalStringToNumber(s1));
+ s1 += u"\u07C6";
+ CPPUNIT_ASSERT_EQUAL(sal_uInt32(12346), comphelper::string::decimalStringToNumber(s1));
+ // Codepoints on 2 16bits words
+ s1 = u"\U0001D7FE\U0001D7F7"; // MATHEMATICAL MONOSPACE DIGIT EIGHT and ONE
+ CPPUNIT_ASSERT_EQUAL(sal_uInt32(81), comphelper::string::decimalStringToNumber(s1));
+}
+
+void TestString::testIsdigitAsciiString()
+{
+ CPPUNIT_ASSERT_EQUAL(true, comphelper::string::isdigitAsciiString("1234"));
+
+ CPPUNIT_ASSERT_EQUAL(false, comphelper::string::isdigitAsciiString("1A34"));
+
+ CPPUNIT_ASSERT_EQUAL(true, comphelper::string::isdigitAsciiString(""));
+}
+
+void TestString::testStripStart()
+{
+ OString aIn("abc");
+ OString aOut;
+
+ aOut = ::comphelper::string::stripStart(aIn, 'b');
+ CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
+
+ aOut = ::comphelper::string::stripStart(aIn, 'a');
+ CPPUNIT_ASSERT_EQUAL(OString("bc"), aOut);
+
+ aIn = "aaa";
+ aOut = ::comphelper::string::stripStart(aIn, 'a');
+ CPPUNIT_ASSERT(aOut.isEmpty());
+
+ aIn = "aba";
+ aOut = ::comphelper::string::stripStart(aIn, 'a');
+ CPPUNIT_ASSERT_EQUAL(OString("ba"), aOut);
+}
+
+void TestString::testStripEnd()
+{
+ OString aIn("abc");
+ OString aOut;
+
+ aOut = ::comphelper::string::stripEnd(aIn, 'b');
+ CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
+
+ aOut = ::comphelper::string::stripEnd(aIn, 'c');
+ CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
+
+ aIn = "aaa";
+ aOut = ::comphelper::string::stripEnd(aIn, 'a');
+ CPPUNIT_ASSERT(aOut.isEmpty());
+
+ aIn = "aba";
+ aOut = ::comphelper::string::stripEnd(aIn, 'a');
+ CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
+}
+
+void TestString::testStrip()
+{
+ OString aIn("abc");
+ OString aOut;
+
+ aOut = ::comphelper::string::strip(aIn, 'b');
+ CPPUNIT_ASSERT_EQUAL(OString("abc"), aOut);
+
+ aOut = ::comphelper::string::strip(aIn, 'c');
+ CPPUNIT_ASSERT_EQUAL(OString("ab"), aOut);
+
+ aIn = "aaa";
+ aOut = ::comphelper::string::strip(aIn, 'a');
+ CPPUNIT_ASSERT(aOut.isEmpty());
+
+ aIn = "aba";
+ aOut = ::comphelper::string::strip(aIn, 'a');
+ CPPUNIT_ASSERT_EQUAL(OString("b"), aOut);
+}
+
+void TestString::testToken()
+{
+ OString aIn("10.11.12");
+ OString aOut;
+
+ aOut = aIn.getToken(-1, '.');
+ CPPUNIT_ASSERT(aOut.isEmpty());
+
+ aOut = aIn.getToken(0, '.');
+ CPPUNIT_ASSERT_EQUAL(OString("10"), aOut);
+
+ aOut = aIn.getToken(1, '.');
+ CPPUNIT_ASSERT_EQUAL(OString("11"), aOut);
+
+ aOut = aIn.getToken(2, '.');
+ CPPUNIT_ASSERT_EQUAL(OString("12"), aOut);
+
+ aOut = aIn.getToken(3, '.');
+ CPPUNIT_ASSERT(aOut.isEmpty());
+}
+
+void TestString::testTokenCount()
+{
+ OString aIn("10.11.12");
+ sal_Int32 nOut;
+
+ nOut = ::comphelper::string::getTokenCount(aIn, '.');
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(3), nOut);
+
+ nOut = ::comphelper::string::getTokenCount(aIn, 'X');
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(1), nOut);
+
+ nOut = ::comphelper::string::getTokenCount("", 'X');
+ CPPUNIT_ASSERT_EQUAL(static_cast<sal_Int32>(0), nOut);
+}
+
+void TestString::testReverseString()
+{
+ CPPUNIT_ASSERT_EQUAL(OUString(), comphelper::string::reverseString(u""));
+ CPPUNIT_ASSERT_EQUAL(OUString("cba"), comphelper::string::reverseString(u"abc"));
+ static sal_Unicode const rev[] = {'w', 0xDFFF, 0xDBFF, 'v', 0xDC00, 0xD800, 'u'};
+ CPPUNIT_ASSERT_EQUAL(
+ OUString(rev, std::size(rev)),
+ comphelper::string::reverseString(u"u\U00010000v\U0010FFFFw"));
+ static sal_Unicode const malformed[] = {0xDC00, 0xD800};
+ CPPUNIT_ASSERT_EQUAL(
+ OUString(u"\U00010000"),
+ comphelper::string::reverseString(std::u16string_view(malformed, std::size(malformed))));
+}
+
+void TestString::testReverseCodePoints() {
+ CPPUNIT_ASSERT_EQUAL(OUString(), comphelper::string::reverseCodePoints(""));
+ CPPUNIT_ASSERT_EQUAL(OUString("cba"), comphelper::string::reverseCodePoints("abc"));
+ CPPUNIT_ASSERT_EQUAL(
+ OUString(u"w\U0010FFFFv\U00010000u"),
+ comphelper::string::reverseCodePoints(u"u\U00010000v\U0010FFFFw"));
+ static sal_Unicode const malformed[] = {0xDC00, 0xD800};
+ CPPUNIT_ASSERT_EQUAL(
+ OUString(u"\U00010000"),
+ comphelper::string::reverseCodePoints(OUString(malformed, std::size(malformed))));
+}
+
+void TestString::testSplit()
+{
+ std::vector<OUString> aRet = ::comphelper::string::split(u"CTRL+ALT+F1", '+');
+ CPPUNIT_ASSERT_EQUAL(size_t(3), aRet.size());
+ CPPUNIT_ASSERT_EQUAL(OUString("CTRL"), aRet[0]);
+ CPPUNIT_ASSERT_EQUAL(OUString("ALT"), aRet[1]);
+ CPPUNIT_ASSERT_EQUAL(OUString("F1"), aRet[2]);
+}
+
+void TestString::testRemoveAny()
+{
+ using namespace ::comphelper::string;
+ OUString in("abcAAAbbC");
+ sal_Unicode const test1 [] = { 'a', 0 };
+ CPPUNIT_ASSERT_EQUAL(OUString("bcAAAbbC"), removeAny(in, test1));
+ sal_Unicode const test2 [] = { 0 };
+ CPPUNIT_ASSERT_EQUAL(in, removeAny(in, test2));
+ sal_Unicode const test3 [] = { 'A', 0 };
+ CPPUNIT_ASSERT_EQUAL(OUString("abcbbC"), removeAny(in, test3));
+ sal_Unicode const test4 [] = { 'A', 'a', 0 };
+ CPPUNIT_ASSERT_EQUAL(OUString("bcbbC"), removeAny(in, test4));
+ sal_Unicode const test5 [] = { 'C', 0 };
+ CPPUNIT_ASSERT_EQUAL(OUString("abcAAAbb"), removeAny(in, test5));
+ sal_Unicode const test6 [] = { 'X', 0 };
+ CPPUNIT_ASSERT_EQUAL(in, removeAny(in, test6));
+ sal_Unicode const test7 [] = { 'A', 'B', 'C', 'a', 'b', 'c', 0 };
+ CPPUNIT_ASSERT_EQUAL(OUString(), removeAny(in, test7));
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
+
+}
+
+CPPUNIT_PLUGIN_IMPLEMENT();
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */