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();
35 void testcall( const char str
[] );
37 CPPUNIT_TEST_SUITE(StringLiterals
);
38 CPPUNIT_TEST(checkCtors
);
39 CPPUNIT_TEST(checkUsage
);
40 CPPUNIT_TEST(checkExtraIntArgument
);
41 CPPUNIT_TEST(checkNonconstChar
);
42 CPPUNIT_TEST(checkBuffer
);
43 CPPUNIT_TEST_SUITE_END();
46 // reset the flag, evaluate the expression and return
47 // whether the string literal ctor was used (i.e. whether the conversion was valid)
48 #define VALID_CONVERSION( expression ) \
50 rtl_string_unittest_invalid_conversion = false, \
51 ( void ) rtl::OUString( expression ), \
52 ( void ) rtl::OUStringBuffer( expression ), \
53 !rtl_string_unittest_invalid_conversion )
55 void test::oustring::StringLiterals::checkCtors()
57 CPPUNIT_ASSERT( VALID_CONVERSION( "test" ));
58 const char good1
[] = "test";
59 CPPUNIT_ASSERT( VALID_CONVERSION( good1
));
61 CPPUNIT_ASSERT( !VALID_CONVERSION( (const char*) "test" ));
62 const char* bad1
= good1
;
63 CPPUNIT_ASSERT( !VALID_CONVERSION( bad1
));
65 CPPUNIT_ASSERT( !VALID_CONVERSION( bad2
));
67 CPPUNIT_ASSERT( !VALID_CONVERSION( bad3
));
68 const char* bad4
[] = { "test1" };
69 CPPUNIT_ASSERT( !VALID_CONVERSION( bad4
[ 0 ] ));
72 // This one is technically broken, since the first element is 6 characters test\0\0,
73 // but there does not appear a way to detect this by compile time (runtime will assert()).
74 // RTL_CONSTASCII_USTRINGPARAM() has the same flaw.
75 const char bad5
[][ 6 ] = { "test", "test2" };
76 // CPPUNIT_ASSERT( VALID_CONVERSION( bad5[ 0 ] ));
77 CPPUNIT_ASSERT( VALID_CONVERSION( bad5
[ 1 ] ));
79 // Check that contents are correct and equal to the case when RTL_CONSTASCII_USTRINGPARAM is used.
80 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "" ), rtl::OUString( "" ));
81 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "ab" ), rtl::OUString( "ab" ));
83 // Also check that embedded \0 is included.
84 // In fact, allowing this is probably just trouble, so this now asserts.
85 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "\0" ), rtl::OUString( "\0" ));
86 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "a\0b" ), rtl::OUString( "a\0b" ));
90 void test::oustring::StringLiterals::testcall( const char str
[] )
92 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( str
)));
95 void test::oustring::StringLiterals::checkUsage()
97 // simply check that all string literal based calls work as expected
98 // also check that they really use string literal overload and do not convert to OUString
99 rtl::OUString
foo( "foo" );
100 rtl::OUString
FoO( "FoO" );
101 rtl::OUString
foobarfoo( "foobarfoo" );
102 rtl::OUString
foobar( "foobar" );
103 rtl::OUString
FooBaRfoo( "FooBaRfoo" );
104 rtl::OUString
FooBaR( "FooBaR" );
105 rtl::OUString
bar( "bar" );
106 rtl::OUString
test( "test" );
108 rtl_string_unittest_const_literal
= false; // start checking for OUString conversions
109 CPPUNIT_ASSERT_EQUAL( foo
, rtl::OUString() = "foo" );
110 CPPUNIT_ASSERT( FoO
.equalsIgnoreAsciiCase( "fOo" ));
111 CPPUNIT_ASSERT( foobarfoo
.match( "bar", 3 ));
112 CPPUNIT_ASSERT( foobar
.match( "foo" ));
113 CPPUNIT_ASSERT( FooBaRfoo
.matchIgnoreAsciiCase( "bAr", 3 ));
114 CPPUNIT_ASSERT( FooBaR
.matchIgnoreAsciiCase( "fOo" ));
115 CPPUNIT_ASSERT( foobar
.startsWith( "foo" ));
116 CPPUNIT_ASSERT( FooBaR
.startsWithIgnoreAsciiCase( "foo" ));
117 CPPUNIT_ASSERT( foobar
.endsWith( "bar" ));
118 CPPUNIT_ASSERT( FooBaR
.endsWithIgnoreAsciiCase( "bar" ));
119 CPPUNIT_ASSERT( foo
== "foo" );
120 CPPUNIT_ASSERT( "foo" == foo
);
121 CPPUNIT_ASSERT( foo
!= "bar" );
122 CPPUNIT_ASSERT( "foo" != bar
);
123 CPPUNIT_ASSERT( foobarfoo
.indexOf( "foo", 1 ) == 6 );
124 CPPUNIT_ASSERT( foobarfoo
.lastIndexOf( "foo" ) == 6 );
125 CPPUNIT_ASSERT( foobarfoo
.replaceFirst( "foo", test
) == "testbarfoo" );
126 CPPUNIT_ASSERT( foobarfoo
.replaceFirst( "foo", "test" ) == "testbarfoo" );
127 CPPUNIT_ASSERT( foobarfoo
.replaceAll( "foo", test
) == "testbartest" );
128 CPPUNIT_ASSERT( foobarfoo
.replaceAll( "foo", "test" ) == "testbartest" );
129 CPPUNIT_ASSERT( foo
.reverseCompareTo( "foo" ) == 0 );
130 // if this is not true, some of the calls above converted to OUString
131 CPPUNIT_ASSERT( rtl_string_unittest_const_literal
== false );
134 void test::oustring::StringLiterals::checkExtraIntArgument()
136 // This makes sure that using by mistake RTL_CONSTASCII_STRINGPARAM does not trigger a different
137 // overload, i.e. the second argument to match() in this case is the indexFrom argument,
138 // but with the macro it would contain the length of the string. Therefore
139 // match( RTL_CONSTASCII_STRINGPARAM( "bar" )) would be match( "bar", 3 ), which would be
140 // true when called for OUString( "foobar" ). But this should not happen because of the
141 // &foo[0] trick in the RTL_CONSTASCII_STRINGPARAM macro.
142 CPPUNIT_ASSERT( !rtl::OUString("foobar").match( "bar" ));
143 CPPUNIT_ASSERT( !rtl::OUString("foobar").match( RTL_CONSTASCII_STRINGPARAM( "bar" )));
146 void test::oustring::StringLiterals::checkNonconstChar()
147 { // check that non-const char[] data do not trigger string literal overloads
148 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), rtl::OUString( "footest" ).replaceAll( "test", "bar" ));
149 char test
[] = "test";
151 const char consttest
[] = "test";
152 const char constbar
[] = "bar";
153 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test
, bar
)));
154 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( consttest
, bar
)));
155 CPPUNIT_ASSERT( !VALID_CONVERSION( rtl::OUString( "footest" ).replaceAll( test
, constbar
)));
156 CPPUNIT_ASSERT( rtl::OUString( "foobar" ) == rtl::OUString( "footest" ).replaceAll( consttest
, constbar
));
159 void test::oustring::StringLiterals::checkBuffer()
161 rtl::OUStringBuffer buf
;
163 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foo" ), buf
.toString());
165 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobar" ), buf
.toString());
166 buf
.insert( 3, "baz" );
167 CPPUNIT_ASSERT_EQUAL( rtl::OUString( "foobazbar" ), buf
.toString());
169 CPPUNIT_ASSERT( !VALID_CONVERSION( buf
.append( rtl::OUString( d
))));
170 CPPUNIT_ASSERT( !VALID_CONVERSION( buf
.append( rtl::OUStringBuffer( d
))));
171 CPPUNIT_ASSERT( !VALID_CONVERSION( buf
.insert( 0, d
)));
176 CPPUNIT_TEST_SUITE_REGISTRATION(test::oustring::StringLiterals
);
178 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */