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
12 extern bool rtl_string_unittest_const_literal
;
13 extern bool rtl_string_unittest_invalid_conversion
;
14 extern bool rtl_string_unittest_const_literal_function
;
15 extern 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/ustring.hxx"
22 #include "rtl/ustrbuf.hxx"
24 namespace test
{ namespace oustring
{
26 class StringLiterals
: public CppUnit::TestFixture
31 void checkExtraIntArgument();
32 void checkNonconstChar();
34 void checkOUStringLiteral();
35 void checkOUStringLiteral1();
37 void testcall( const char str
[] );
39 CPPUNIT_TEST_SUITE(StringLiterals
);
40 CPPUNIT_TEST(checkCtors
);
41 CPPUNIT_TEST(checkUsage
);
42 CPPUNIT_TEST(checkExtraIntArgument
);
43 CPPUNIT_TEST(checkNonconstChar
);
44 CPPUNIT_TEST(checkBuffer
);
45 CPPUNIT_TEST(checkOUStringLiteral
);
46 CPPUNIT_TEST(checkOUStringLiteral1
);
47 CPPUNIT_TEST_SUITE_END();
50 // reset the flag, evaluate the expression and return
51 // whether the string literal ctor was used (i.e. whether the conversion was valid)
52 #define VALID_CONVERSION( expression ) \
54 rtl_string_unittest_invalid_conversion = false, \
55 ( void ) rtl::OUString( expression ), \
56 ( void ) rtl::OUStringBuffer( expression ), \
57 !rtl_string_unittest_invalid_conversion )
59 void test::oustring::StringLiterals::checkCtors()
61 CPPUNIT_ASSERT( VALID_CONVERSION( "test" ));
62 const char good1
[] = "test";
63 CPPUNIT_ASSERT( VALID_CONVERSION( good1
));
65 CPPUNIT_ASSERT( !VALID_CONVERSION( (const char*) "test" ));
66 const char* bad1
= good1
;
67 CPPUNIT_ASSERT( !VALID_CONVERSION( bad1
));
69 CPPUNIT_ASSERT( !VALID_CONVERSION( bad2
));
71 CPPUNIT_ASSERT( !VALID_CONVERSION( bad3
));
72 const char* bad4
[] = { "test1" };
73 CPPUNIT_ASSERT( !VALID_CONVERSION( bad4
[ 0 ] ));
76 // This one is technically broken, since the first element is 6 characters test\0\0,
77 // but there does not appear a way to detect this by compile time (runtime will assert()).
78 // RTL_CONSTASCII_USTRINGPARAM() has the same flaw.
79 const char bad5
[][ 6 ] = { "test", "test2" };
80 // CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 0 ] ));
81 CPPUNIT_ASSERT( VALID_CONVERSION( bad5
[ 1 ] ));
83 // Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used.
84 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "" ), rtl::OUString( "" ));
85 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "ab" ), rtl::OUString( "ab" ));
87 // Also check that embedded \0 is included.
88 // In fact, allowing this is probably just trouble, so this now asserts.
89 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "\0" ), rtl::OUString( "\0" ));
90 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "a\0b" ), rtl::OUString( "a\0b" ));
94 void test::oustring::StringLiterals::testcall( const char str
[] )
96 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( str
)));
99 void test::oustring::StringLiterals::checkUsage()
101 // simply check that all string literal based calls work as expected
102 // also check that they really use string literal overload and do not convert to OUString
103 rtl::OUString
foo( "foo" );
104 rtl::OUString
FoO( "FoO" );
105 rtl::OUString
foobarfoo( "foobarfoo" );
106 rtl::OUString
foobar( "foobar" );
107 rtl::OUString
FooBaRfoo( "FooBaRfoo" );
108 rtl::OUString
FooBaR( "FooBaR" );
109 rtl::OUString
bar( "bar" );
110 rtl::OUString
test( "test" );
112 rtl_string_unittest_const_literal
= false; // start checking for OUString conversions
113 CPPUNIT_ASSERT_EQUAL( foo
, rtl::OUString() = "foo" );
114 CPPUNIT_ASSERT( FoO
.equalsIgnoreAsciiCase( "fOo" ));
115 CPPUNIT_ASSERT( foobarfoo
.match( "bar", 3 ));
116 CPPUNIT_ASSERT( foobar
.match( "foo" ));
117 CPPUNIT_ASSERT( FooBaRfoo
.matchIgnoreAsciiCase( "bAr", 3 ));
118 CPPUNIT_ASSERT( FooBaR
.matchIgnoreAsciiCase( "fOo" ));
119 CPPUNIT_ASSERT( foobar
.startsWith( "foo" ));
120 CPPUNIT_ASSERT( FooBaR
.startsWithIgnoreAsciiCase( "foo" ));
121 CPPUNIT_ASSERT( foobar
.endsWith( "bar" ));
122 CPPUNIT_ASSERT( FooBaR
.endsWithIgnoreAsciiCase( "bar" ));
123 CPPUNIT_ASSERT( foo
== "foo" );
124 CPPUNIT_ASSERT( "foo" == foo
);
125 CPPUNIT_ASSERT( foo
!= "bar" );
126 CPPUNIT_ASSERT( "foo" != bar
);
127 CPPUNIT_ASSERT( foobarfoo
.indexOf( "foo", 1 ) == 6 );
128 CPPUNIT_ASSERT( foobarfoo
.lastIndexOf( "foo" ) == 6 );
129 CPPUNIT_ASSERT( foobarfoo
.replaceFirst( "foo", test
) == "testbarfoo" );
130 CPPUNIT_ASSERT( foobarfoo
.replaceFirst( "foo", "test" ) == "testbarfoo" );
131 CPPUNIT_ASSERT( foobarfoo
.replaceAll( "foo", test
) == "testbartest" );
132 CPPUNIT_ASSERT( foobarfoo
.replaceAll( "foo", "test" ) == "testbartest" );
133 CPPUNIT_ASSERT( foo
.reverseCompareTo( "foo" ) == 0 );
134 // if this is not true, some of the calls above converted to OUString
135 CPPUNIT_ASSERT( !rtl_string_unittest_const_literal
);
138 void test::oustring::StringLiterals::checkExtraIntArgument()
140 // This makes sure that using by mistake RTL_CONSTASCII_STRINGPARAM does not trigger a different
141 // overload, i.e. the second argument to match() in this case is the indexFrom argument,
142 // but with the macro it would contain the length of the string. Therefore
143 // match( RTL_CONSTASCII_STRINGPARAM( "bar" )) would be match( "bar", 3 ), which would be
144 // true when called for OUString( "foobar" ). But this should not happen because of the
145 // &foo[0] trick in the RTL_CONSTASCII_STRINGPARAM macro.
146 CPPUNIT_ASSERT( !rtl::OUString("foobar").match( "bar" ));
147 CPPUNIT_ASSERT( !rtl::OUString("foobar").match( RTL_CONSTASCII_STRINGPARAM( "bar" )));
150 void test::oustring::StringLiterals::checkNonconstChar()
151 { // check that non-const char[] data do not trigger string literal overloads
152 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), rtl::OUString( "footest" ).replaceAll( "test", "bar" ));
153 char test
[] = "test";
155 const char consttest
[] = "test";
156 const char constbar
[] = "bar";
157 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test
, bar
)));
158 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( consttest
, bar
)));
159 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test
, constbar
)));
160 CPPUNIT_ASSERT( rtl::OUString( "foobar" ) == rtl::OUString( "footest" ).replaceAll( consttest
, constbar
));
163 void test::oustring::StringLiterals::checkBuffer()
165 rtl::OUStringBuffer buf
;
167 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foo" ), buf
.toString());
169 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), buf
.toString());
170 buf
.insert( 3, "baz" );
171 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobazbar" ), buf
.toString());
173 CPPUNIT_ASSERT( !VALID_CONVERSION( buf
.append( rtl::OUString( d
))));
174 CPPUNIT_ASSERT( !VALID_CONVERSION( buf
.append( rtl::OUStringBuffer( d
))));
179 rtl::OUString
conditional(bool flag
) {
181 ? rtlunittest::OUStringLiteral("a")
182 : rtlunittest::OUStringLiteral("bb");
187 void test::oustring::StringLiterals::checkOUStringLiteral()
189 CPPUNIT_ASSERT(conditional(true) == "a");
190 CPPUNIT_ASSERT(conditional(false) == "bb");
193 void test::oustring::StringLiterals::checkOUStringLiteral1()
196 s1
= rtlunittest::OUStringLiteral1
<'A'>();
197 CPPUNIT_ASSERT_EQUAL(sal_Int32(1), s1
.getLength());
198 CPPUNIT_ASSERT_EQUAL(sal_Unicode('A'), s1
[0]);
200 CPPUNIT_ASSERT_EQUAL(
201 true, rtl::OUString("A") == rtlunittest::OUStringLiteral1
<'A'>());
202 CPPUNIT_ASSERT_EQUAL(
203 false, rtl::OUString("AB") == rtlunittest::OUStringLiteral1
<'A'>());
204 CPPUNIT_ASSERT_EQUAL(
205 false, rtl::OUString("A") != rtlunittest::OUStringLiteral1
<'A'>());
206 CPPUNIT_ASSERT_EQUAL(
207 true, rtl::OUString("AB") != rtlunittest::OUStringLiteral1
<'A'>());
209 rtl::OUString
s2("A" + rtlunittest::OUStringLiteral1
<'b'>());
210 CPPUNIT_ASSERT_EQUAL(sal_Int32(2), s2
.getLength());
211 CPPUNIT_ASSERT_EQUAL(sal_Unicode('A'), s2
[0]);
212 CPPUNIT_ASSERT_EQUAL(sal_Unicode('b'), s2
[1]);
217 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals
);
219 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */