1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
31 // this file is converted to use with testshl2
32 // original was placed in sal/test/textenc.cxx
37 #include <rtl/string.hxx>
39 #include <testshl/simpleheader.hxx>
41 #define TEST_ENSURE(c, m) CPPUNIT_ASSERT_MESSAGE((m), (c))
45 class oldtests
: public CppUnit::TestFixture
50 CPPUNIT_TEST_SUITE( oldtests
);
51 CPPUNIT_TEST( test_OString
);
52 CPPUNIT_TEST_SUITE_END( );
57 #pragma warning( disable : 4723 )
60 void oldtests::test_OString()
62 TEST_ENSURE( sal_True
, "_USENAMEPSACE defined");
64 // "Mein erster RTL OString\n"
67 OString
s1("Mein erster RTL OString\n");
68 TEST_ENSURE( s1
== "Mein erster RTL OString\n", "test_OString error 1");
69 TEST_ENSURE( s1
.getLength() == 24, "test_OString error 2");
72 TEST_ENSURE( s2
[16] == 'O', "test_OString error 3");
73 TEST_ENSURE( s2
.equals(s1
), "test_OString error 4");
74 TEST_ENSURE( s2
.indexOf('O') == 16, "test_OString error 5");
75 TEST_ENSURE( s2
.indexOf('O', 5) == 16, "test_OString error 5a");
76 TEST_ENSURE( s2
.lastIndexOf('r') == 19, "test_OString error 6");
77 TEST_ENSURE( s2
[19] == 'r', "test_OString error 7");
78 TEST_ENSURE( s2
[23] == '\n', "test_OString error 8");
79 TEST_ENSURE( s2
.lastIndexOf('\n') == 23, "test_OString error 9");
80 TEST_ENSURE( s2
.lastIndexOf('M') == 0, "test_OString error 10");
81 TEST_ENSURE( s2
.lastIndexOf('t', s2
.getLength() - 8) == 8, "test_OString error 9");
83 // "Mein erster RTL OString ist ein String aus der RTL Library\n"
84 // | | | | | | | | | | | |
85 // Index 0 5 10 15 20 25 30 35 40 45 50 55
86 OString s3
= s2
.copy(0, s2
.getLength() - 1);
87 OString s4
= s3
.concat(" ist ein String aus der RTL Library\n");
88 TEST_ENSURE( s4
.getLength() == 59, "test_OString error 11");
92 s5
= s1
+ " aus der RTL Library\n";
93 TEST_ENSURE( s5
.compareTo(s4
) == 0 , "test_OString error 12");
94 TEST_ENSURE( s5
.indexOf("RTL") == 12, "test_OString error 13");
95 TEST_ENSURE( s5
.lastIndexOf("RTL") == 47, "test_OString error 13");
97 sal_Bool b
= sal_False
;
98 OString s6
= s5
.valueOf(b
);
99 TEST_ENSURE( s6
.compareTo("false") == 0, "test_OString error 14");
100 s6
= s5
.valueOf('H');
101 TEST_ENSURE( s6
.compareTo("H") == 0, "test_OString error 15");
102 sal_Int32 n
= 123456789L;
104 TEST_ENSURE( s6
.compareTo("123456789") == 0, "test_OString error 16");
107 sal_Int64 m
= -3223372036854775807LL;
109 sal_Int64 m
= -3223372036854775807;
112 TEST_ENSURE( s6
.compareTo("-3223372036854775807") == 0, "test_OString error 17");
115 s7
= s7
.toAsciiLowerCase();
116 TEST_ENSURE( s7
== "hallo", "test_OString error 19");
117 s7
= s7
.toAsciiUpperCase();
118 TEST_ENSURE( s7
== "HALLO", "test_OString error 20");
120 OString
s8("HaLLo ICH BIn eIn StRiNg");
121 s7
= s8
.toAsciiLowerCase();
123 TEST_ENSURE( s8
.equalsIgnoreAsciiCase(s7
), "test_OString error 21");
125 s8
= s7
.toAsciiUpperCase();
126 TEST_ENSURE( s8
== "HALLO ICH BIN EIN STRING", "test_OString error 22");
130 TEST_ENSURE( s8
== " HALLO ICH BIN EIN STRING ",
131 "test_OString error 23");
134 TEST_ENSURE( s7
== "HALLO ICH BIN EIN STRING", "test_OString error 24");
135 TEST_ENSURE( strcmp(s7
.getStr(), "HALLO ICH BIN EIN STRING") == 0,
136 "test_OString error 25");
141 TEST_ENSURE( s7
< s8
, "test_OString error 26");
142 TEST_ENSURE( s8
> s7
, "test_OString error 27");
143 TEST_ENSURE( s7
!= s8
, "test_OString error 28");
144 TEST_ENSURE( s7
!= "blabla", "test_OString error 29");
145 TEST_ENSURE( "blabla" != s7
, "test_OString error 30");
148 TEST_ENSURE( s7
<= s8
, "test_OString error 31");
149 TEST_ENSURE( s7
>= s8
, "test_OString error 32");
151 s8
= s8
.replace('l', 'r');
152 TEST_ENSURE( s8
== "Harro", "test_OString error 33");
154 sal_Int32 nIndex
= 0;
155 s8
= "|hallo1|hallo2|hallo3|hallo4|hallo5|hallo6|hallo7|hallo8|";
156 TEST_ENSURE( s8
.getToken(3,'|', nIndex
) == "hallo3", "test_OString error 40");
158 char* Tokens
[10] = { "", "hallo1", "hallo2", "hallo3", "hallo4",
159 "hallo5", "hallo6", "hallo7", "hallo8", "" };
165 TEST_ENSURE( s8
.getToken(0,'|',nIndex
) == Tokens
[i
], "test_OString error 40e");
168 while ( nIndex
>= 0 );
172 TEST_ENSURE( s7
== s8
, "test_OString error 41");
174 s7
= s8
.replaceAt(8, 6, "mmmmmmmmmm");
175 TEST_ENSURE( s7
.getLength() == 61, "test_OString error 42");
177 s8
= s7
.replaceAt(8, 11, "");
178 TEST_ENSURE( s8
.getLength() == 50, "test_OString error 43");
180 s7
= s8
.replaceAt(8, 0, "hallo2|");
181 TEST_ENSURE( s7
.getLength() == 57, "test_OString error 44");
184 while ((pos
= s7
.indexOf("|")) >= 0)
186 s8
= s7
.replaceAt(pos
, 1, "**");
189 TEST_ENSURE( s7
.getLength() == 66, "test_OString error 45");
191 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "bbb" ) ) < 0, "test_OString error 46" );
192 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "aaa" ) ) == 0, "test_OString error 47" );
193 TEST_ENSURE( OString( "bbb" ).compareTo( OString( "aaa" ) ) > 0, "test_OString error 48" );
194 TEST_ENSURE( OString( "aaaa" ).compareTo( OString( "bbb" ) ) < 0, "test_OString error 49" );
195 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "bbbb" ) ) < 0, "test_OString error 50" );
196 TEST_ENSURE( OString( "aaa" ).compareTo( OString( "aaaa" ) ) < 0, "test_OString error 51" );
197 TEST_ENSURE( OString( "aaaa" ).compareTo( OString( "aaa" ) ) > 0, "test_OString error 52" );
198 TEST_ENSURE( OString( "bbbb" ).compareTo( OString( "bbb" ) ) > 0, "test_OString error 53" );
199 TEST_ENSURE( OString( "bbb" ) == OString( "bbb" ), "test_OString error 54" );
200 TEST_ENSURE( OString( "bbb" ) == "bbb", "test_OString error 55" );
203 * As clarified in #104229#, calling copy with invalid arguments causes
204 * undefined behaviour, so the following test does no longer work:
206 s7 = "Hallo jetzt komm ich";
207 s8 = s7.copy(0, s7.indexOf(':'));
208 TEST_ENSURE( s8.getLength() == 0, "test_OString error 56");
209 TEST_ENSURE( s8.compareTo("") == 0, "test_OString error 57");
212 double f
= OString("1.7e-10").toDouble();
213 TEST_ENSURE(f
> 1E-10 && f
< 2E-10, "1.7e-10 problem");
214 f
= OString("1.7e+10").toDouble();
215 TEST_ENSURE(f
> 1E10
&& f
< 2E10
, "1.7e+10 problem");
216 f
= OString("1.7e10").toDouble();
217 TEST_ENSURE(f
> 1E10
&& f
< 2E10
, "1.7e308 problem");
222 float fInf
= f1
/ f0
;
223 OString
aStr1(OString::valueOf(fInf
));
224 OString
aStr2("1.#INF");
225 bool bSuccess
= aStr1
== aStr2
;
227 printf("ERROR: OString::valueOf(1f/0f): %s\n", aStr1
.getStr());
228 TEST_ENSURE(bSuccess
, "OString::valueOf(1f/0f)");
231 printf("test_OString OK !!!\n");
235 } // namespace rtl_OString
237 // -----------------------------------------------------------------------------
238 CPPUNIT_TEST_SUITE_NAMED_REGISTRATION( rtl_OString::oldtests
, "rtl_OString" );
240 // -----------------------------------------------------------------------------
243 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */