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