1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 // activate the extra needed ctor
11 #define RTL_STRING_UNITTEST
13 #include <sal/types.h>
14 #include <cppunit/TestFixture.h>
15 #include <cppunit/extensions/HelperMacros.h>
16 #include <rtl/string.h>
17 #include <rtl/string.hxx>
18 #include <rtl/strbuf.hxx>
20 bool rtl_string_unittest_const_literal
;
21 bool rtl_string_unittest_const_literal_function
;
22 static bool rtl_string_unittest_non_const_literal_function
;
24 namespace test::ostring
{
26 class StringLiterals
: public CppUnit::TestFixture
30 void checkConstexprCtor();
32 void checkNonConstUsage();
36 void testcall( const char str
[] );
38 static const char bad5
[];
41 // Check that OStringLiteral ctor is actually constexpr:
42 static constexpr rtlunittest::OStringLiteral dummy
{"dummy"};
44 CPPUNIT_TEST_SUITE(StringLiterals
);
45 CPPUNIT_TEST(checkCtors
);
46 CPPUNIT_TEST(checkConstexprCtor
);
47 CPPUNIT_TEST(checkUsage
);
48 CPPUNIT_TEST(checkNonConstUsage
);
49 CPPUNIT_TEST(checkBuffer
);
50 CPPUNIT_TEST(checkOstr
);
51 CPPUNIT_TEST_SUITE_END();
54 // reset the flag, call OString ctor with the given argument and return
55 // whether the string literal ctor was used
56 #define CONST_CTOR_USED( argument ) \
58 rtl_string_unittest_const_literal = false, \
59 ( void ) rtl::OString( argument ), \
60 result_tmp = rtl_string_unittest_const_literal, \
61 rtl_string_unittest_const_literal = false, \
62 ( void ) rtl::OStringBuffer( argument ), \
63 rtl_string_unittest_const_literal && result_tmp )
65 void test::ostring::StringLiterals::checkCtors()
68 CPPUNIT_ASSERT( CONST_CTOR_USED( "test" ));
69 const char good1
[] = "test";
70 CPPUNIT_ASSERT( CONST_CTOR_USED( good1
));
72 CPPUNIT_ASSERT( !CONST_CTOR_USED( static_cast<const char*>("test") ));
73 const char* bad1
= good1
;
74 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad1
));
76 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad2
));
78 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad3
));
79 const char* bad4
[] = { "test1" };
80 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad4
[ 0 ] ));
83 // this is actually not supposed to work (see discussion in stringutils.hxx),
84 // but gcc and clang somehow manage, so keep it used, just in case some other problem
85 // shows up somewhen in the future
86 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad5
)); // size is not known here
87 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad6
));
90 // This one is technically broken, since the first element is 6 characters test\0\0,
91 // but there does not appear a way to detect this by compile time (runtime will assert()).
92 // RTL_CONSTASCII_USTRINGPARAM() has the same flaw.
93 const char bad7
[][ 6 ] = { "test", "test2" };
94 // CPPUNIT_ASSERT( CONST_CTOR_USED( bad7[ 0 ] ));
95 CPPUNIT_ASSERT( CONST_CTOR_USED( bad7
[ 1 ] ));
97 // Check that contents are correct and equal to the case when const char* ctor is used.
98 CPPUNIT_ASSERT_EQUAL( rtl::OString( "" ), rtl::OString( static_cast<const char*>("") ) );
99 CPPUNIT_ASSERT_EQUAL( rtl::OString( "ab" ), rtl::OString( static_cast<const char*>("ab") ) );
101 // Check that contents are correct and equal to the case when RTL_CONSTASCII_STRINGPARAM is used.
102 CPPUNIT_ASSERT_EQUAL( rtl::OString( "" ), rtl::OString( RTL_CONSTASCII_STRINGPARAM( "" )) );
103 CPPUNIT_ASSERT_EQUAL( rtl::OString( "ab" ), rtl::OString( RTL_CONSTASCII_STRINGPARAM( "ab" )) );
106 const char test::ostring::StringLiterals::bad5
[] = "test";
107 char test::ostring::StringLiterals::bad6
[] = "test";
109 void test::ostring::StringLiterals::testcall( const char str
[] )
113 CPPUNIT_ASSERT( !CONST_CTOR_USED( str
));
115 // MSVC just errors out on this for some reason, which is fine as well
120 void test::ostring::StringLiterals::checkConstexprCtor()
122 static constinit
rtl::OString
s(dummy
);
123 CPPUNIT_ASSERT_EQUAL(oslInterlockedCount(0x40000000), s
.pData
->refCount
);
124 // SAL_STRING_STATIC_FLAG (sal/rtl/strimp.hxx)
125 CPPUNIT_ASSERT_EQUAL(sal_Int32(5), s
.getLength());
126 CPPUNIT_ASSERT_EQUAL(rtl::OString("dummy"), s
);
127 CPPUNIT_ASSERT_EQUAL(
128 static_cast<void const *>(dummy
.getStr()), static_cast<void const *>(s
.getStr()));
131 void test::ostring::StringLiterals::checkUsage()
133 // simply check that all string literal based calls work as expected
134 // also check that they really use string literal overload and do not convert to OString
135 rtl::OString
foo( "foo" );
136 rtl::OString
FoO( "FoO" );
137 rtl::OString
foobarfoo( "foobarfoo" );
138 rtl::OString
foobar( "foobar" );
139 rtl::OString
FooBaRfoo( "FooBaRfoo" );
140 rtl::OString
FooBaR( "FooBaR" );
141 rtl::OString
bar( "bar" );
143 rtl_string_unittest_const_literal
= false; // start checking for OString conversions
144 rtl_string_unittest_non_const_literal_function
= false; // and check for non-const variants
145 rtl_string_unittest_const_literal_function
= false;
146 CPPUNIT_ASSERT_EQUAL( foo
, rtl::OString() = "foo" );
147 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
148 rtl_string_unittest_const_literal_function
= false;
149 CPPUNIT_ASSERT( FoO
.equalsIgnoreAsciiCase( "fOo" ));
150 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
151 rtl_string_unittest_const_literal_function
= false;
152 CPPUNIT_ASSERT( foobarfoo
.match( "bar", 3 ));
153 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
154 rtl_string_unittest_const_literal_function
= false;
155 CPPUNIT_ASSERT( foobar
.match( "foo" ));
156 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
157 rtl_string_unittest_const_literal_function
= false;
158 CPPUNIT_ASSERT( FooBaRfoo
.matchIgnoreAsciiCase( "bAr", 3 ));
159 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
160 rtl_string_unittest_const_literal_function
= false;
161 CPPUNIT_ASSERT( FooBaR
.matchIgnoreAsciiCase( "fOo" ));
162 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
163 rtl_string_unittest_const_literal_function
= false;
164 CPPUNIT_ASSERT( foobar
.startsWith( "foo" ));
165 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
166 rtl_string_unittest_const_literal_function
= false;
167 CPPUNIT_ASSERT( foobar
.endsWith( "bar" ));
168 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
169 // rtl_string_unittest_const_literal_function = false;
170 // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" ));
171 // CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true );
172 rtl_string_unittest_const_literal_function
= false;
173 CPPUNIT_ASSERT( bool(foo
== "foo") );
174 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
175 rtl_string_unittest_const_literal_function
= false;
176 CPPUNIT_ASSERT( bool("foo" == foo
) );
177 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
178 rtl_string_unittest_const_literal_function
= false;
179 CPPUNIT_ASSERT( foo
!= "bar" );
180 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
181 rtl_string_unittest_const_literal_function
= false;
182 CPPUNIT_ASSERT( "foo" != bar
);
183 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
184 rtl_string_unittest_const_literal_function
= false;
185 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(6), foobarfoo
.indexOf( "foo", 1 ) );
186 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
187 // rtl_string_unittest_const_literal_function = false;
188 // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( "foo" ) == 6 );
189 // CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true );
190 // if this is not true, some of the calls above converted to OString
191 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal
);
192 // if this is not true, some of the calls above used non-const variants
193 CPPUNIT_ASSERT( !rtl_string_unittest_non_const_literal_function
);
196 void test::ostring::StringLiterals::checkNonConstUsage()
198 // check that (non-const) char[] overloads work and do not use const char[] overloads
199 rtl::OString
foo( "foo" );
200 rtl::OString
FoO( "FoO" );
201 rtl::OString
foobarfoo( "foobarfoo" );
202 rtl::OString
foobar( "foobar" );
203 rtl::OString
FooBaRfoo( "FooBaRfoo" );
204 rtl::OString
FooBaR( "FooBaR" );
205 rtl::OString
bar( "bar" );
206 char foo_c
[] = "foo";
207 char bar_c
[] = "bar";
208 char fOo_c
[] = "fOo";
209 char bAr_c
[] = "bAr";
211 rtl_string_unittest_const_literal
= false; // start checking for OString conversions
212 rtl_string_unittest_const_literal_function
= false; // and check for const variants
213 CPPUNIT_ASSERT_EQUAL( foo
, rtl::OString() = static_cast<const char*>(foo_c
) );
214 CPPUNIT_ASSERT_EQUAL( foo
, rtl::OString() = foo_c
);
215 CPPUNIT_ASSERT( FoO
.equalsIgnoreAsciiCase( static_cast<const char*>(fOo_c
) ));
216 CPPUNIT_ASSERT( FoO
.equalsIgnoreAsciiCase( fOo_c
));
217 CPPUNIT_ASSERT( foobarfoo
.match( static_cast<const char*>(bar_c
), 3 ));
218 CPPUNIT_ASSERT( foobarfoo
.match( bar_c
, 3 ));
219 CPPUNIT_ASSERT( foobar
.match( static_cast<const char*>(foo_c
) ));
220 CPPUNIT_ASSERT( foobar
.match( foo_c
));
221 CPPUNIT_ASSERT( FooBaRfoo
.matchIgnoreAsciiCase( static_cast<const char*>(bAr_c
), 3 ));
222 CPPUNIT_ASSERT( FooBaRfoo
.matchIgnoreAsciiCase( bAr_c
, 3 ));
223 CPPUNIT_ASSERT( FooBaR
.matchIgnoreAsciiCase( static_cast<const char*>(fOo_c
) ));
224 CPPUNIT_ASSERT( FooBaR
.matchIgnoreAsciiCase( fOo_c
));
225 CPPUNIT_ASSERT( foobar
.startsWith( static_cast<const char*>(foo_c
) ));
226 CPPUNIT_ASSERT( foobar
.startsWith( foo_c
));
227 CPPUNIT_ASSERT( foobar
.endsWith( static_cast<const char*>(bar_c
) ));
228 CPPUNIT_ASSERT( foobar
.endsWith( bar_c
));
229 // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( (const char*)bar_c ));
230 // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( bar_c ));
231 CPPUNIT_ASSERT( bool(foo
== static_cast<const char*>(foo_c
)) );
232 CPPUNIT_ASSERT( bool(foo
== foo_c
) );
233 CPPUNIT_ASSERT( bool(static_cast<const char*>(foo_c
) == foo
) );
234 CPPUNIT_ASSERT( bool(foo_c
== foo
) );
235 CPPUNIT_ASSERT( foo
!= static_cast<const char*>(bar_c
) );
236 CPPUNIT_ASSERT( foo
!= bar_c
);
237 CPPUNIT_ASSERT( static_cast<const char*>(foo_c
) != bar
);
238 CPPUNIT_ASSERT( foo_c
!= bar
);
239 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(6), foobarfoo
.indexOf( static_cast<const char*>(foo_c
), 1 ) );
240 CPPUNIT_ASSERT_EQUAL( static_cast<sal_Int32
>(6), foobarfoo
.indexOf( foo_c
, 1 ) );
241 // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( (const char*)foo_c ) == 6 );
242 // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( foo_c ) == 6 );
243 // if this is not true, some of the calls above used const variants
244 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal
);
245 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal_function
);
248 void test::ostring::StringLiterals::checkBuffer()
250 rtl::OStringBuffer buf
;
251 rtl_string_unittest_const_literal_function
= false;
253 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
254 CPPUNIT_ASSERT_EQUAL( rtl::OString( "foo" ), buf
.toString());
255 rtl_string_unittest_const_literal_function
= false;
257 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
258 CPPUNIT_ASSERT_EQUAL( rtl::OString( "foobar" ), buf
.toString());
259 rtl_string_unittest_const_literal_function
= false;
260 buf
.insert( 3, "baz" );
261 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function
);
262 CPPUNIT_ASSERT_EQUAL( rtl::OString( "foobazbar" ), buf
.toString());
264 rtl::OString
foobazbard( "foobazbard" );
265 rtl::OString
foodbazbard( "foodbazbard" );
266 rtl_string_unittest_const_literal
= false; // start checking for OString conversions
267 rtl_string_unittest_const_literal_function
= false; // and check for const variants
269 CPPUNIT_ASSERT_EQUAL( foobazbard
, buf
.append( d
).toString());
270 CPPUNIT_ASSERT_EQUAL( foodbazbard
, buf
.insert( 3, d
).toString() );
271 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal
);
272 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal_function
);
275 void test::ostring::StringLiterals::checkOstr() {
276 CPPUNIT_ASSERT_EQUAL(sal_Int32(0), ""_ostr
.getLength());
277 CPPUNIT_ASSERT_EQUAL(sal_Int32(6), "foobar"_ostr
.getLength());
278 CPPUNIT_ASSERT_EQUAL(sal_Int32(7), "foo\0bar"_ostr
.getLength());
279 CPPUNIT_ASSERT_EQUAL(sal_Int32(4), u8
"\U00010000"_ostr
.getLength());
280 CPPUNIT_ASSERT_EQUAL(""_ostr
, rtl::OString(""_tstr
));
281 CPPUNIT_ASSERT_EQUAL("foobar"_ostr
, rtl::OString("foobar"_tstr
));
282 CPPUNIT_ASSERT_EQUAL("foo\0bar"_ostr
, rtl::OString("foo\0bar"_tstr
));
285 #undef CONST_CTOR_USED
289 CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringLiterals
);
291 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */