Debian Codegraph @

sal/qa/rtl/strings/nonconstarray.cxx

raw
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
9
10 #include <sal/config.h>
11
12 #include <cppunit/TestAssert.h>
13 #include <cppunit/TestFixture.h>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <rtl/strbuf.hxx>
16 #include <rtl/string.hxx>
17 #include <rtl/ustring.hxx>
18
19 namespace
20 {
21 OString returnOString()
22 {
23 char buf[] = "foo\0bar";
24 return buf;
25 }
26
27 OStringBuffer returnOStringBuffer()
28 {
29 char buf[] = "foo\0bar";
30 return buf;
31 }
32
33 class Test : public CppUnit::TestFixture
34 {
35 private:
36 void testOString()
37 {
38 struct S
39 {
40 char a[4];
41 };
42 S s{ "x\0y" };
43 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OString(s.a).getLength());
44 // Ideally, the below would work the same as the above. However, the const reference makes
45 // the ConstCharArrayDetector instead of the NonConstCharArrayDetector kick in, so that the
46 // call to OString(r.a) would fire the ConstCharArrayDetector<T>::isValid assert (and in
47 // NDEBUG builds the CPPUNIT_ASSERT_EQUAL would fail with 3 != 1):
48 if ((false))
49 {
50 S const& r = s;
51 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OString(r.a).getLength());
52 }
53 }
54
55 void testOUStringChar()
56 {
57 struct S
58 {
59 char a[4];
60 };
61 S s{ "x\0y" };
62 // This would fail to compile, as there is no OUString ctor taking a
63 // NonConstCharArrayDetector char array:
64 #if 0
65 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(s.a).getLength());
66 #endif
67 // Ideally, the below would fail to compile the same as the above. However, the const
68 // reference makes the ConstCharArrayDetector instead of the NonConstCharArrayDetector kick
69 // in, so that the call to OUString(r.a) would fire the ConstCharArrayDetector<T>::isValid
70 // assert (and in NDEBUG builds the CPPUNIT_ASSERT_EQUAL would fail with 3 != 1):
71 if ((false))
72 {
73 S const& r = s;
74 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(r.a).getLength());
75 }
76 }
77
78 void testOUStringChar16t()
79 {
80 struct S
81 {
82 char16_t a[4];
83 };
84 S s{ u"x\0y" };
85 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(s.a).getLength());
86 // Ideally, the below would work the same as the above. However, the const reference makes
87 // the ConstCharArrayDetector instead of the NonConstCharArrayDetector kick in, so that the
88 // call to OUString(r.a) would pick the deleted ConstCharArrayDetector overload:
89 // S const& r = s;
90 // CPPUNIT_ASSERT_EQUAL(sal_Int32(1), OUString(r.a).getLength());
91 }
92
93 void testP2266R3()
94 {
95 CPPUNIT_ASSERT_EQUAL("foo"_ostr, returnOString());
96 CPPUNIT_ASSERT_EQUAL("foo"_ostr, returnOStringBuffer().makeStringAndClear());
97 }
98
99 CPPUNIT_TEST_SUITE(Test);
100 CPPUNIT_TEST(testOString);
101 CPPUNIT_TEST(testOUStringChar);
102 CPPUNIT_TEST(testOUStringChar16t);
103 CPPUNIT_TEST(testP2266R3);
104 CPPUNIT_TEST_SUITE_END();
105 };
106
107 CPPUNIT_TEST_SUITE_REGISTRATION(Test);
108 }
109
110 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */