summaryrefslogtreecommitdiffstats
path: root/sal/qa/rtl/oustringbuffer
diff options
context:
space:
mode:
Diffstat (limited to 'sal/qa/rtl/oustringbuffer')
-rw-r--r--sal/qa/rtl/oustringbuffer/test_oustringbuffer_appendchar.cxx43
-rw-r--r--sal/qa/rtl/oustringbuffer/test_oustringbuffer_appenduninitialized.cxx66
-rw-r--r--sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx92
-rw-r--r--sal/qa/rtl/oustringbuffer/test_oustringbuffer_tostring.cxx61
-rw-r--r--sal/qa/rtl/oustringbuffer/test_oustringbuffer_utf32.cxx121
5 files changed, 383 insertions, 0 deletions
diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_appendchar.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_appendchar.cxx
new file mode 100644
index 000000000..f5168b60d
--- /dev/null
+++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_appendchar.cxx
@@ -0,0 +1,43 @@
+/* -*- 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/.
+ */
+
+#include <sal/config.h>
+
+#include <o3tl/cppunittraitshelper.hxx>
+#include <sal/types.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/TestAssert.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <rtl/ustrbuf.hxx>
+
+namespace test::oustringbuffer {
+
+class AppendChar: public CppUnit::TestFixture {
+private:
+ void testAppendChar();
+
+ CPPUNIT_TEST_SUITE(AppendChar);
+ CPPUNIT_TEST(testAppendChar);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void AppendChar::testAppendChar() {
+ // Check that append('a') does not unexpectedly pick
+ // append(sal_Int32 i, sal_Int16 radix = 10):
+ OUStringBuffer s;
+ s.append('a');
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s.getLength());
+ CPPUNIT_ASSERT_EQUAL(u'a', s[0]);
+}
+
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test::oustringbuffer::AppendChar);
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_appenduninitialized.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_appenduninitialized.cxx
new file mode 100644
index 000000000..7530d6a32
--- /dev/null
+++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_appenduninitialized.cxx
@@ -0,0 +1,66 @@
+/* -*- 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/.
+ */
+
+#include <sal/config.h>
+
+#include <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <rtl/ustrbuf.hxx>
+#include <sal/types.h>
+
+namespace {
+
+class Test: public CppUnit::TestFixture {
+private:
+ void testEmpty();
+
+ void testNonEmpty();
+
+ void testZero();
+
+ CPPUNIT_TEST_SUITE(Test);
+ CPPUNIT_TEST(testEmpty);
+ CPPUNIT_TEST(testNonEmpty);
+ CPPUNIT_TEST(testZero);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void Test::testEmpty() {
+ OUStringBuffer s;
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
+ sal_Unicode * p = s.appendUninitialized(5);
+ CPPUNIT_ASSERT_EQUAL(
+ static_cast<void const *>(s.getStr()), static_cast<void const *>(p));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s.getLength());
+}
+
+void Test::testNonEmpty() {
+ OUStringBuffer s("ab");
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s.getLength());
+ sal_Unicode * p = s.appendUninitialized(5);
+ CPPUNIT_ASSERT_EQUAL(
+ static_cast<void const *>(s.getStr() + 2),
+ static_cast<void const *>(p));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(7), s.getLength());
+}
+
+void Test::testZero() {
+ OUStringBuffer s;
+ sal_Unicode * p = s.appendUninitialized(0);
+ CPPUNIT_ASSERT_EQUAL(
+ static_cast<void const *>(s.getStr()), static_cast<void const *>(p));
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(0), s.getLength());
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(Test);
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx
new file mode 100644
index 000000000..3c3f436ee
--- /dev/null
+++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_assign.cxx
@@ -0,0 +1,92 @@
+/* -*- 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/.
+ */
+
+#include <sal/config.h>
+
+#include <string_view>
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/TestAssert.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <o3tl/cppunittraitshelper.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <rtl/ustring.hxx>
+
+namespace
+{
+class Test : public CppUnit::TestFixture
+{
+private:
+ void test()
+ {
+ OUStringBuffer b1;
+ OUString s1("123456789012345");
+ b1 = s1;
+ CPPUNIT_ASSERT_EQUAL(s1, b1.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b1.getCapacity());
+ OUString s2("abc");
+ b1 = s2;
+ CPPUNIT_ASSERT_EQUAL(s2, b1.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b1.getCapacity());
+ OUString s3("1234567890123456");
+ b1 = s3;
+ CPPUNIT_ASSERT_EQUAL(s3, b1.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(32), b1.getCapacity());
+ OUStringBuffer b2;
+ b2 = "123456789012345";
+ CPPUNIT_ASSERT_EQUAL(s1, b2.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b2.getCapacity());
+ b2 = "abc";
+ CPPUNIT_ASSERT_EQUAL(s2, b2.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b2.getCapacity());
+ b2 = "1234567890123456";
+ CPPUNIT_ASSERT_EQUAL(s3, b2.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(32), b2.getCapacity());
+ OUStringBuffer b3;
+ b3 = u"123456789012345";
+ CPPUNIT_ASSERT_EQUAL(s1, b3.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b3.getCapacity());
+ b3 = u"abc";
+ CPPUNIT_ASSERT_EQUAL(s2, b3.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b3.getCapacity());
+ b3 = u"1234567890123456";
+ CPPUNIT_ASSERT_EQUAL(s3, b3.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(32), b3.getCapacity());
+ OUStringBuffer b4;
+ b4 = OUStringLiteral(u"1") + "23456789012345";
+ CPPUNIT_ASSERT_EQUAL(s1, b4.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b4.getCapacity());
+ b4 = OUStringLiteral(u"a") + "bc";
+ CPPUNIT_ASSERT_EQUAL(s2, b4.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(16), b4.getCapacity());
+ b4 = OUStringLiteral(u"1") + "234567890123456";
+ CPPUNIT_ASSERT_EQUAL(s3, b4.toString());
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(32), b4.getCapacity());
+ b4 = OUStringChar('a');
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(1), b4.getLength());
+ CPPUNIT_ASSERT_EQUAL(u'a', b4.getStr()[0]);
+ CPPUNIT_ASSERT_EQUAL(u'\0', b4.getStr()[1]);
+ b4 = std::u16string_view(u"abc").substr(
+ 0, 2); // avoid the string_view accidentally being NUL-terminated
+ CPPUNIT_ASSERT_EQUAL(sal_Int32(2), b4.getLength());
+ CPPUNIT_ASSERT_EQUAL(u'a', b4.getStr()[0]);
+ CPPUNIT_ASSERT_EQUAL(u'b', b4.getStr()[1]);
+ CPPUNIT_ASSERT_EQUAL(u'\0', b4.getStr()[2]);
+ }
+
+ CPPUNIT_TEST_SUITE(Test);
+ CPPUNIT_TEST(test);
+ CPPUNIT_TEST_SUITE_END();
+};
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(Test);
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_tostring.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_tostring.cxx
new file mode 100644
index 000000000..d942480d1
--- /dev/null
+++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_tostring.cxx
@@ -0,0 +1,61 @@
+/* -*- 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/types.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/TestAssert.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <rtl/ustrbuf.hxx>
+#include <rtl/ustring.hxx>
+
+namespace test::oustringbuffer {
+
+class ToString: public CppUnit::TestFixture {
+private:
+ void testEmptyToString();
+ void testToString();
+
+ CPPUNIT_TEST_SUITE(ToString);
+ CPPUNIT_TEST(testEmptyToString);
+ CPPUNIT_TEST(testToString);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test::oustringbuffer::ToString);
+
+void test::oustringbuffer::ToString::testEmptyToString() {
+ OUStringBuffer sb;
+ OUString str = sb.toString();
+ CPPUNIT_ASSERT_EQUAL(OUString(), str);
+}
+
+void test::oustringbuffer::ToString::testToString() {
+ OUStringBuffer sb("test string");
+ OUString str = sb.toString();
+ CPPUNIT_ASSERT_EQUAL( OUString("test string"), str );
+ // returned OUString must be independent from sb
+ sb.append( 'a' );
+ CPPUNIT_ASSERT_EQUAL( OUString("test string"), str );
+ sb.setLength(0);
+ CPPUNIT_ASSERT_EQUAL( OUString("test string"), str );
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sal/qa/rtl/oustringbuffer/test_oustringbuffer_utf32.cxx b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_utf32.cxx
new file mode 100644
index 000000000..764b03d8c
--- /dev/null
+++ b/sal/qa/rtl/oustringbuffer/test_oustringbuffer_utf32.cxx
@@ -0,0 +1,121 @@
+/* -*- 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/types.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/TestAssert.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <rtl/strbuf.hxx>
+#include <rtl/ustrbuf.hxx>
+#include <rtl/ustring.h>
+#include <rtl/ustring.hxx>
+
+namespace test::oustringbuffer {
+
+class Utf32: public CppUnit::TestFixture {
+private:
+ void appendUtf32();
+
+ void insertUtf32();
+
+ CPPUNIT_TEST_SUITE(Utf32);
+ CPPUNIT_TEST(appendUtf32);
+ CPPUNIT_TEST(insertUtf32);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+}
+
+CPPUNIT_TEST_SUITE_REGISTRATION(test::oustringbuffer::Utf32);
+
+namespace {
+
+void appendString(OStringBuffer & buffer, OUString const & string) {
+ buffer.append('"');
+ for (int i = 0; i < string.getLength(); ++i) {
+ buffer.append("\\u");
+ sal_Unicode c = string[i];
+ if (c < 0x1000) {
+ buffer.append('0');
+ if (c < 0x100) {
+ buffer.append('0');
+ if (c < 0x10) {
+ buffer.append('0');
+ }
+ }
+ }
+ buffer.append(
+ static_cast< sal_Int32 >(c), static_cast< sal_Int16 >(16));
+ }
+ buffer.append('"');
+}
+
+void createMessage(
+ OStringBuffer & message, OUString const & string1,
+ OUString const & string2)
+{
+ message.setLength(0);
+ appendString(message, string1);
+ message.append(" vs. ");
+ appendString(message, string2);
+}
+
+}
+
+void test::oustringbuffer::Utf32::appendUtf32() {
+ int const str1Len = 3;
+ sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
+ static constexpr OUStringLiteral str2 = u"abcd";
+ static constexpr OUStringLiteral str3 = u"abcd\U00010000";
+ OStringBuffer message;
+ OUStringBuffer buf1(std::u16string_view(str1, str1Len));
+ buf1.appendUtf32('d');
+ OUString res1(buf1.makeStringAndClear());
+ createMessage(message, res1, str2);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ message.getStr(), OUString(str2), res1);
+ OUStringBuffer buf2(str2);
+ buf2.appendUtf32(0x10000);
+ OUString res2(buf2.makeStringAndClear());
+ createMessage(message, res2, str3);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ message.getStr(), OUString(str3), res2);
+}
+
+void test::oustringbuffer::Utf32::insertUtf32() {
+ int const str1Len = 3;
+ sal_Unicode const str1[str1Len] = { 'a', 'b', 'c' };
+ static constexpr OUStringLiteral str2 = u"abdc";
+ static constexpr OUStringLiteral str3 = u"ab\U0010FFFFdc";
+ OStringBuffer message;
+ OUStringBuffer buf1(std::u16string_view(str1, str1Len));
+ buf1.insertUtf32(2, 'd');
+ OUString res1(buf1.makeStringAndClear());
+ createMessage(message, res1, str2);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ message.getStr(), OUString(str2), res1);
+ OUStringBuffer buf2(str2);
+ buf2.insertUtf32(2, 0x10FFFF);
+ OUString res2(buf2.makeStringAndClear());
+ createMessage(message, res2, str3);
+ CPPUNIT_ASSERT_EQUAL_MESSAGE(
+ message.getStr(), OUString(str3), res2);
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */