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