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