1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: rtl_old_testostring.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
35 // this file is converted to use with testshl2
36 // original was placed in sal/test/textenc.cxx
39 // -----------------------------------------------------------------------------
43 // #ifndef _OSL_DIAGNOSE_H_
44 // #include <osl/diagnose.h>
47 #ifndef _RTL_STRING_HXX
48 #include <rtl/string.hxx>
51 #include <cppunit/simpleheader.hxx>
53 #define TEST_ENSURE(c, m) CPPUNIT_ASSERT_MESSAGE((m), (c))
55 // #if OSL_DEBUG_LEVEL > 0
56 // #define TEST_ENSHURE(c, m) OSL_ENSURE(c, m)
58 // #define TEST_ENSHURE(c, m) OSL_VERIFY(c)
63 // -----------------------------------------------------------------------------
66 class oldtests
: public CppUnit::TestFixture
71 CPPUNIT_TEST_SUITE( oldtests
);
72 CPPUNIT_TEST( test_OString
);
73 CPPUNIT_TEST_SUITE_END( );
78 #pragma warning( disable : 4723 )
81 void oldtests::test_OString()
83 TEST_ENSURE( sal_True
, "_USENAMEPSACE defined");
85 // "Mein erster RTL OString\n"
88 OString
s1("Mein erster RTL OString\n");
89 TEST_ENSURE( s1
== "Mein erster RTL OString\n", "test_OString error 1");
90 TEST_ENSURE( s1
.getLength() == 24, "test_OString error 2");
93 TEST_ENSURE( s2
[16] == 'O', "test_OString error 3");
94 TEST_ENSURE( s2
.equals(s1
), "test_OString error 4");
95 TEST_ENSURE( s2
.indexOf('O') == 16, "test_OString error 5");
96 TEST_ENSURE( s2
.indexOf('O', 5) == 16, "test_OString error 5a");
97 TEST_ENSURE( s2
.lastIndexOf('r') == 19, "test_OString error 6");
98 TEST_ENSURE( s2
[19] == 'r', "test_OString error 7");
99 TEST_ENSURE( s2
[23] == '\n', "test_OString error 8");
100 TEST_ENSURE( s2
.lastIndexOf('\n') == 23, "test_OString error 9");
101 TEST_ENSURE( s2
.lastIndexOf('M') == 0, "test_OString error 10");
102 TEST_ENSURE( s2
.lastIndexOf('t', s2
.getLength() - 8) == 8, "test_OString error 9");
105 // "Mein erster RTL OString ist ein String aus der RTL Library\n"
106 // | | | | | | | | | | | |
107 // Index 0 5 10 15 20 25 30 35 40 45 50 55
108 OString s3
= s2
.copy(0, s2
.getLength() - 1);
109 OString s4
= s3
.concat(" ist ein String aus der RTL Library\n");
110 TEST_ENSURE( s4
.getLength() == 59, "test_OString error 11");
114 s5
= s1
+ " aus der RTL Library\n";
115 TEST_ENSURE( s5
.compareTo(s4
) == 0 , "test_OString error 12");
116 TEST_ENSURE( s5
.indexOf("RTL") == 12, "test_OString error 13");
117 TEST_ENSURE( s5
.lastIndexOf("RTL") == 47, "test_OString error 13");
119 sal_Bool b
= sal_False
;
120 OString s6
= s5
.valueOf(b
);
121 TEST_ENSURE( s6
.compareTo("false") == 0, "test_OString error 14");
122 s6
= s5
.valueOf('H');
123 TEST_ENSURE( s6
.compareTo("H") == 0, "test_OString error 15");
124 sal_Int32 n
= 123456789L;
126 TEST_ENSURE( s6
.compareTo("123456789") == 0, "test_OString error 16");
130 sal_Int64 m
= -3223372036854775807LL;
131 #elif defined(SAL_OS2)
133 sal_setInt64(&m
, 3965190145L, -750499787L);
135 sal_Int64 m
= -3223372036854775807;
138 TEST_ENSURE( s6
.compareTo("-3223372036854775807") == 0, "test_OString error 17");
142 s7
= s7
.toAsciiLowerCase();
143 TEST_ENSURE( s7
== "hallo", "test_OString error 19");
144 s7
= s7
.toAsciiUpperCase();
145 TEST_ENSURE( s7
== "HALLO", "test_OString error 20");
147 OString
s8("HaLLo ICH BIn eIn StRiNg");
148 s7
= s8
.toAsciiLowerCase();
150 TEST_ENSURE( s8
.equalsIgnoreAsciiCase(s7
), "test_OString error 21");
152 s8
= s7
.toAsciiUpperCase();
153 TEST_ENSURE( s8
== "HALLO ICH BIN EIN STRING", "test_OString error 22");
157 TEST_ENSURE( s8
== " HALLO ICH BIN EIN STRING ",
158 "test_OString error 23");
161 TEST_ENSURE( s7
== "HALLO ICH BIN EIN STRING", "test_OString error 24");
162 TEST_ENSURE( strcmp(s7
.getStr(), "HALLO ICH BIN EIN STRING") == 0,
163 "test_OString error 25");
168 TEST_ENSURE( s7
< s8
, "test_OString error 26");
169 TEST_ENSURE( s8
> s7
, "test_OString error 27");
170 TEST_ENSURE( s7
!= s8
, "test_OString error 28");
171 TEST_ENSURE( s7
!= "blabla", "test_OString error 29");
172 TEST_ENSURE( "blabla" != s7
, "test_OString error 30");
175 TEST_ENSURE( s7
<= s8
, "test_OString error 31");
176 TEST_ENSURE( s7
>= s8
, "test_OString error 32");
178 s8
= s8
.replace('l', 'r');
179 TEST_ENSURE( s8
== "Harro", "test_OString error 33");
181 sal_Int32 nIndex
= 0;
182 s8
= "|hallo1|hallo2|hallo3|hallo4|hallo5|hallo6|hallo7|hallo8|";
183 TEST_ENSURE( s8
.getToken(3,'|', nIndex
) == "hallo3", "test_OString error 40");
185 char* Tokens
[10] = { "", "hallo1", "hallo2", "hallo3", "hallo4",
186 "hallo5", "hallo6", "hallo7", "hallo8", "" };
192 TEST_ENSURE( s8
.getToken(0,'|',nIndex
) == Tokens
[i
], "test_OString error 40e");
195 while ( nIndex
>= 0 );
199 TEST_ENSURE( s7
== s8
, "test_OString error 41");
201 s7
= s8
.replaceAt(8, 6, "mmmmmmmmmm");
202 TEST_ENSURE( s7
.getLength() == 61, "test_OString error 42");
204 s8
= s7
.replaceAt(8, 11, "");
205 TEST_ENSURE( s8
.getLength() == 50, "test_OString error 43");
207 s7
= s8
.replaceAt(8, 0, "hallo2|");
208 TEST_ENSURE( s7
.getLength() == 57, "test_OString error 44");
211 while ((pos
= s7
.indexOf("|")) >= 0)
213 s8
= s7
.replaceAt(pos
, 1, "**");
216 TEST_ENSURE( s7
.getLength() == 66, "test_OString error 45");
218 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "bbb" ) ) < 0, "test_OString error 46" );
219 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "aaa" ) ) == 0, "test_OString error 47" );
220 TEST_ENSURE( OString( "bbb" ).compareTo( OString( "aaa" ) ) > 0, "test_OString error 48" );
221 TEST_ENSURE( OString( "aaaa" ).compareTo( OString( "bbb" ) ) < 0, "test_OString error 49" );
222 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "bbbb" ) ) < 0, "test_OString error 50" );
223 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "aaaa" ) ) < 0, "test_OString error 51" );
224 TEST_ENSURE( OString( "aaaa" ).compareTo( OString( "aaa" ) ) > 0, "test_OString error 52" );
225 TEST_ENSURE( OString( "bbbb" ).compareTo( OString( "bbb" ) ) > 0, "test_OString error 53" );
226 TEST_ENSURE( OString( "bbb" ) == OString( "bbb" ), "test_OString error 54" );
227 TEST_ENSURE( OString( "bbb" ) == "bbb", "test_OString error 55" );
230 * As clarified in #104229#, calling copy with invalid arguments causes
231 * undefined behaviour, so the following test does no longer work:
233 s7 = "Hallo jetzt komm ich";
234 s8 = s7.copy(0, s7.indexOf(':'));
235 TEST_ENSURE( s8.getLength() == 0, "test_OString error 56");
236 TEST_ENSURE( s8.compareTo("") == 0, "test_OString error 57");
239 double f
= OString("1.7e-10").toDouble();
240 TEST_ENSURE(f
> 1E-10 && f
< 2E-10, "1.7e-10 problem");
241 f
= OString("1.7e+10").toDouble();
242 TEST_ENSURE(f
> 1E10
&& f
< 2E10
, "1.7e+10 problem");
243 f
= OString("1.7e10").toDouble();
244 TEST_ENSURE(f
> 1E10
&& f
< 2E10
, "1.7e308 problem");
249 float fInf
= f1
/ f0
;
250 OString
aStr1(OString::valueOf(fInf
));
251 OString
aStr2("1.#INF");
252 bool bSuccess
= aStr1
== aStr2
;
254 printf("ERROR: OString::valueOf(1f/0f): %s\n", aStr1
.getStr());
255 TEST_ENSURE(bSuccess
, "OString::valueOf(1f/0f)");
258 printf("test_OString OK !!!\n");
262 } // namespace rtl_OString
264 // -----------------------------------------------------------------------------
265 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( rtl_OString::oldtests
, "rtl_OString" );
267 // -----------------------------------------------------------------------------