bump product version to 5.0.4.1
[LibreOffice.git] / sal / qa / rtl / strings / test_ostring_stringliterals.cxx
blobaf547ed8e9eb614635f186c0359190e7efdfcded
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
10 // activate the extra needed ctor
11 #define RTL_STRING_UNITTEST
12 bool rtl_string_unittest_const_literal;
13 bool rtl_string_unittest_invalid_conversion;
14 bool rtl_string_unittest_const_literal_function;
15 bool rtl_string_unittest_non_const_literal_function;
17 #include <sal/types.h>
18 #include <cppunit/TestFixture.h>
19 #include <cppunit/extensions/HelperMacros.h>
20 #include "rtl/string.h"
21 #include "rtl/string.hxx"
22 #include "rtl/strbuf.hxx"
24 namespace test { namespace ostring {
26 class StringLiterals: public CppUnit::TestFixture
28 private:
29 void checkCtors();
30 void checkUsage();
31 void checkNonConstUsage();
32 void checkBuffer();
34 void testcall( const char str[] );
36 static const char bad5[];
37 static char bad6[];
39 CPPUNIT_TEST_SUITE(StringLiterals);
40 CPPUNIT_TEST(checkCtors);
41 CPPUNIT_TEST(checkUsage);
42 CPPUNIT_TEST(checkNonConstUsage);
43 CPPUNIT_TEST(checkBuffer);
44 CPPUNIT_TEST_SUITE_END();
47 // reset the flag, call OString ctor with the given argument and return
48 // whether the string literal ctor was used
49 #define CONST_CTOR_USED( argument ) \
50 ( \
51 rtl_string_unittest_const_literal = false, \
52 ( void ) rtl::OString( argument ), \
53 result_tmp = rtl_string_unittest_const_literal, \
54 rtl_string_unittest_const_literal = false, \
55 ( void ) rtl::OStringBuffer( argument ), \
56 rtl_string_unittest_const_literal && result_tmp )
58 void test::ostring::StringLiterals::checkCtors()
60 bool result_tmp;
61 CPPUNIT_ASSERT( CONST_CTOR_USED( "test" ));
62 const char good1[] = "test";
63 CPPUNIT_ASSERT( CONST_CTOR_USED( good1 ));
65 CPPUNIT_ASSERT( !CONST_CTOR_USED( (const char*) "test" ));
66 const char* bad1 = good1;
67 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad1 ));
68 char bad2[] = "test";
69 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad2 ));
70 char* bad3 = bad2;
71 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad3 ));
72 const char* bad4[] = { "test1" };
73 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad4[ 0 ] ));
74 testcall( good1 );
75 #ifndef _MSC_VER
76 // this is actually not supposed to work (see discussion in stringutils.hxx),
77 // but gcc and clang somehow manage, so keep it used, just in case some other problem
78 // shows up somewhen in the future
79 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad5 )); // size is not known here
80 CPPUNIT_ASSERT( !CONST_CTOR_USED( bad6 ));
81 #endif
83 // This one is technically broken, since the first element is 6 characters test\0\0,
84 // but there does not appear a way to detect this by compile time (runtime will assert()).
85 // RTL_CONSTASCII_USTRINGPARAM() has the same flaw.
86 const char bad7[][ 6 ] = { "test", "test2" };
87 // CPPUNIT_ASSERT( CONST_CTOR_USED( bad7[ 0 ] ));
88 CPPUNIT_ASSERT( CONST_CTOR_USED( bad7[ 1 ] ));
90 // Check that contents are correct and equal to the case when const char* ctor is used.
91 CPPUNIT_ASSERT( rtl::OString( (const char*)"" ) == rtl::OString( "" ));
92 CPPUNIT_ASSERT( rtl::OString( (const char*)"ab" ) == rtl::OString( "ab" ));
94 // Check that contents are correct and equal to the case when RTL_CONSTASCII_STRINGPARAM is used.
95 CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "" )) == rtl::OString( "" ));
96 CPPUNIT_ASSERT( rtl::OString( RTL_CONSTASCII_STRINGPARAM( "ab" )) == rtl::OString( "ab" ));
99 const char test::ostring::StringLiterals::bad5[] = "test";
100 char test::ostring::StringLiterals::bad6[] = "test";
102 void test::ostring::StringLiterals::testcall( const char str[] )
104 #ifndef _MSC_VER
105 bool result_tmp;
106 CPPUNIT_ASSERT( !CONST_CTOR_USED( str ));
107 #else
108 // MSVC just errors out on this for some reason, which is fine as well
109 (void)str;
110 #endif
113 void test::ostring::StringLiterals::checkUsage()
115 // simply check that all string literal based calls work as expected
116 // also check that they really use string literal overload and do not convert to OString
117 rtl::OString foo( "foo" );
118 rtl::OString FoO( "FoO" );
119 rtl::OString foobarfoo( "foobarfoo" );
120 rtl::OString foobar( "foobar" );
121 rtl::OString FooBaRfoo( "FooBaRfoo" );
122 rtl::OString FooBaR( "FooBaR" );
123 rtl::OString bar( "bar" );
125 rtl_string_unittest_const_literal = false; // start checking for OString conversions
126 rtl_string_unittest_non_const_literal_function = false; // and check for non-const variants
127 rtl_string_unittest_const_literal_function = false;
128 CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = "foo" );
129 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
130 rtl_string_unittest_const_literal_function = false;
131 CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( "fOo" ));
132 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
133 rtl_string_unittest_const_literal_function = false;
134 CPPUNIT_ASSERT( foobarfoo.match( "bar", 3 ));
135 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
136 rtl_string_unittest_const_literal_function = false;
137 CPPUNIT_ASSERT( foobar.match( "foo" ));
138 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
139 rtl_string_unittest_const_literal_function = false;
140 CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( "bAr", 3 ));
141 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
142 rtl_string_unittest_const_literal_function = false;
143 CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( "fOo" ));
144 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
145 rtl_string_unittest_const_literal_function = false;
146 CPPUNIT_ASSERT( foobar.startsWith( "foo" ));
147 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
148 rtl_string_unittest_const_literal_function = false;
149 CPPUNIT_ASSERT( foobar.endsWith( "bar" ));
150 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
151 // rtl_string_unittest_const_literal_function = false;
152 // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( "bar" ));
153 // CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true );
154 rtl_string_unittest_const_literal_function = false;
155 CPPUNIT_ASSERT( foo == "foo" );
156 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
157 rtl_string_unittest_const_literal_function = false;
158 CPPUNIT_ASSERT( "foo" == foo );
159 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
160 rtl_string_unittest_const_literal_function = false;
161 CPPUNIT_ASSERT( foo != "bar" );
162 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
163 rtl_string_unittest_const_literal_function = false;
164 CPPUNIT_ASSERT( "foo" != bar );
165 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
166 rtl_string_unittest_const_literal_function = false;
167 CPPUNIT_ASSERT( foobarfoo.indexOf( "foo", 1 ) == 6 );
168 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
169 // rtl_string_unittest_const_literal_function = false;
170 // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( "foo" ) == 6 );
171 // CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function == true );
172 // if this is not true, some of the calls above converted to OString
173 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal );
174 // if this is not true, some of the calls above used non-const variants
175 CPPUNIT_ASSERT( !rtl_string_unittest_non_const_literal_function );
178 void test::ostring::StringLiterals::checkNonConstUsage()
180 // check that (non-const) char[] overloads work and do not use const char[] overloads
181 rtl::OString foo( "foo" );
182 rtl::OString FoO( "FoO" );
183 rtl::OString foobarfoo( "foobarfoo" );
184 rtl::OString foobar( "foobar" );
185 rtl::OString FooBaRfoo( "FooBaRfoo" );
186 rtl::OString FooBaR( "FooBaR" );
187 rtl::OString bar( "bar" );
188 char foo_c[] = "foo";
189 char bar_c[] = "bar";
190 char fOo_c[] = "fOo";
191 char bAr_c[] = "bAr";
193 rtl_string_unittest_const_literal = false; // start checking for OString conversions
194 rtl_string_unittest_const_literal_function = false; // and check for const variants
195 CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = (const char*)foo_c );
196 CPPUNIT_ASSERT_EQUAL( foo, rtl::OString() = foo_c );
197 CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( (const char*)fOo_c ));
198 CPPUNIT_ASSERT( FoO.equalsIgnoreAsciiCase( fOo_c ));
199 CPPUNIT_ASSERT( foobarfoo.match( (const char*)bar_c, 3 ));
200 CPPUNIT_ASSERT( foobarfoo.match( bar_c, 3 ));
201 CPPUNIT_ASSERT( foobar.match( (const char*)foo_c ));
202 CPPUNIT_ASSERT( foobar.match( foo_c ));
203 CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( (const char*)bAr_c, 3 ));
204 CPPUNIT_ASSERT( FooBaRfoo.matchIgnoreAsciiCase( bAr_c, 3 ));
205 CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( (const char*)fOo_c ));
206 CPPUNIT_ASSERT( FooBaR.matchIgnoreAsciiCase( fOo_c ));
207 CPPUNIT_ASSERT( foobar.startsWith( (const char*)foo_c ));
208 CPPUNIT_ASSERT( foobar.startsWith( foo_c ));
209 CPPUNIT_ASSERT( foobar.endsWith( (const char*)bar_c ));
210 CPPUNIT_ASSERT( foobar.endsWith( bar_c ));
211 // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( (const char*)bar_c ));
212 // CPPUNIT_ASSERT( FooBaR.endsWithIgnoreAsciiCase( bar_c ));
213 CPPUNIT_ASSERT( foo == (const char*)foo_c );
214 CPPUNIT_ASSERT( foo == foo_c );
215 CPPUNIT_ASSERT( (const char*)foo_c == foo );
216 CPPUNIT_ASSERT( foo_c == foo );
217 CPPUNIT_ASSERT( foo != (const char*)bar_c );
218 CPPUNIT_ASSERT( foo != bar_c );
219 CPPUNIT_ASSERT( (const char*)foo_c != bar );
220 CPPUNIT_ASSERT( foo_c != bar );
221 CPPUNIT_ASSERT( foobarfoo.indexOf( (const char*)foo_c, 1 ) == 6 );
222 CPPUNIT_ASSERT( foobarfoo.indexOf( foo_c, 1 ) == 6 );
223 // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( (const char*)foo_c ) == 6 );
224 // CPPUNIT_ASSERT( foobarfoo.lastIndexOf( foo_c ) == 6 );
225 // if this is not true, some of the calls above used const variants
226 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal );
227 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal_function );
230 void test::ostring::StringLiterals::checkBuffer()
232 rtl::OStringBuffer buf;
233 rtl_string_unittest_const_literal_function = false;
234 buf.append( "foo" );
235 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
236 CPPUNIT_ASSERT_EQUAL( rtl::OString( "foo" ), buf.toString());
237 rtl_string_unittest_const_literal_function = false;
238 buf.append( "bar" );
239 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
240 CPPUNIT_ASSERT_EQUAL( rtl::OString( "foobar" ), buf.toString());
241 rtl_string_unittest_const_literal_function = false;
242 buf.insert( 3, "baz" );
243 CPPUNIT_ASSERT( rtl_string_unittest_const_literal_function );
244 CPPUNIT_ASSERT_EQUAL( rtl::OString( "foobazbar" ), buf.toString());
246 rtl::OString foobazbard( "foobazbard" );
247 rtl::OString foodbazbard( "foodbazbard" );
248 rtl_string_unittest_const_literal = false; // start checking for OString conversions
249 rtl_string_unittest_const_literal_function = false; // and check for const variants
250 char d[] = "d";
251 CPPUNIT_ASSERT_EQUAL( foobazbard, buf.append( d ).toString());
252 CPPUNIT_ASSERT_EQUAL( foodbazbard, buf.insert( 3, d ).toString() );
253 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal );
254 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal_function );
257 #undef CONST_CTOR_USED
259 }} // namespace
261 CPPUNIT_TEST_SUITE_REGISTRATION(test::ostring::StringLiterals);
263 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */