1 | /********************************************************************************************
|
---|
2 | *
|
---|
3 | * MODULES NOTES:
|
---|
4 | *
|
---|
5 | * This file is designed to help test the new nsString classes.
|
---|
6 | *
|
---|
7 | * Contributor(s):
|
---|
8 | * Rick Gessner <[email protected]>
|
---|
9 | *
|
---|
10 | * History:
|
---|
11 | *
|
---|
12 | * 02.29.2000: Original files (rickg)
|
---|
13 | * 03.02.2000: Flesh out the interface to be compatible with old library (rickg)
|
---|
14 | *
|
---|
15 | ********************************************************************************************/
|
---|
16 |
|
---|
17 | #ifndef _STRINGTEST
|
---|
18 | #define _STRINGTEST
|
---|
19 |
|
---|
20 |
|
---|
21 | #include "nsString.h"
|
---|
22 | #include "nsReadableUtils.h"
|
---|
23 | #include <time.h>
|
---|
24 |
|
---|
25 | #define USE_STL
|
---|
26 |
|
---|
27 | #ifdef USE_STL
|
---|
28 | #include <string>
|
---|
29 | using namespace std;
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | #define USE_WIDE 1
|
---|
33 | #ifdef USE_WIDE
|
---|
34 | #define stringtype nsString
|
---|
35 | #define astringtype nsAutoString
|
---|
36 | #define chartype PRUnichar
|
---|
37 | #else
|
---|
38 | #define stringtype nsCString
|
---|
39 | #define astringtype nsCAutoString
|
---|
40 | #define chartype char
|
---|
41 | #endif
|
---|
42 |
|
---|
43 |
|
---|
44 | #include <stdio.h>
|
---|
45 |
|
---|
46 |
|
---|
47 |
|
---|
48 | static const char* kConstructorError = "constructor error";
|
---|
49 | static const char* kComparisonError = "Comparision error!";
|
---|
50 | static const char* kEqualsError = "Equals error!";
|
---|
51 |
|
---|
52 | static char* kNumbers[]={"0123456789","0\01\02\03\04\05\06\07\08\09\0\0\0"};
|
---|
53 | static char* kLetters[]={"abcdefghij","a\0b\0c\0d\0e\0f\0g\0h\0i\0j\0\0\0"};
|
---|
54 | static char* kAAA[]={"AAA","A\0A\0A\0\0\0"};
|
---|
55 | static char* kBBB[]={"BBB","B\0B\0B\0\0\0"};
|
---|
56 | static char* kHello[]={"hello","h\0e\0l\0l\0o\0\0\0"};
|
---|
57 | static char* kWSHello[]={" hello "," \0 \0h\0e\0l\0l\0o\0 \0 \0\0\0"};
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | /********************************************************
|
---|
62 |
|
---|
63 | This class's only purpose in life is to test the
|
---|
64 | netscape string library. We exercise the string
|
---|
65 | API's here, and do a bit of performance testing
|
---|
66 | against the standard c++ library string (from STL).
|
---|
67 |
|
---|
68 | ********************************************************/
|
---|
69 | class CStringTester {
|
---|
70 | public:
|
---|
71 | CStringTester() {
|
---|
72 | TestConstructors();
|
---|
73 | TestAutoStrings();
|
---|
74 | TestAppend();
|
---|
75 | TestAssignAndAdd();
|
---|
76 | TestInsert();
|
---|
77 | TestDelete();
|
---|
78 | TestTruncate();
|
---|
79 | TestLogical();
|
---|
80 | TestLexomorphic();
|
---|
81 | TestCreators();
|
---|
82 | TestNumerics();
|
---|
83 | TestExtractors();
|
---|
84 | TestSearching();
|
---|
85 | TestSubsumables();
|
---|
86 | TestRandomOps();
|
---|
87 | TestReplace();
|
---|
88 | TestRegressions();
|
---|
89 | TestStringPerformance();
|
---|
90 | TestWideStringPerformance();
|
---|
91 | }
|
---|
92 | protected:
|
---|
93 | int TestConstructors();
|
---|
94 | int TestLogical();
|
---|
95 | int TestAutoStrings();
|
---|
96 | int TestAppend();
|
---|
97 | int TestAssignAndAdd();
|
---|
98 | int TestInsert();
|
---|
99 | int TestDelete();
|
---|
100 | int TestTruncate();
|
---|
101 | int TestLexomorphic();
|
---|
102 | int TestCreators();
|
---|
103 | int TestNumerics();
|
---|
104 | int TestExtractors();
|
---|
105 | int TestSearching();
|
---|
106 | int TestSubsumables();
|
---|
107 | int TestRandomOps();
|
---|
108 | int TestReplace();
|
---|
109 | int TestStringPerformance();
|
---|
110 | int TestWideStringPerformance();
|
---|
111 | int TestRegressions();
|
---|
112 | };
|
---|
113 |
|
---|
114 |
|
---|
115 | class Stopwatch {
|
---|
116 | public:
|
---|
117 | Stopwatch() {
|
---|
118 | start=clock();
|
---|
119 | }
|
---|
120 |
|
---|
121 | void Stop() {
|
---|
122 | stop=clock();
|
---|
123 | }
|
---|
124 |
|
---|
125 | double Elapsed() {
|
---|
126 | return (double)(stop - start) / CLOCKS_PER_SEC;
|
---|
127 | }
|
---|
128 |
|
---|
129 | void Print(const char* msg= "") {
|
---|
130 | printf("%s %f\n",msg,Elapsed());
|
---|
131 | }
|
---|
132 |
|
---|
133 | clock_t start,stop;
|
---|
134 | };
|
---|
135 |
|
---|
136 | /**
|
---|
137 | *
|
---|
138 | * @update gess10/30/98
|
---|
139 | * @param
|
---|
140 | * @return
|
---|
141 | */
|
---|
142 | int CStringTester::TestSearching(){
|
---|
143 | int result=0;
|
---|
144 |
|
---|
145 |
|
---|
146 |
|
---|
147 | PRUnichar pbuf[10]={'e','f','g',0};
|
---|
148 | PRUnichar pbuf2[10]={'a','b','c',0};
|
---|
149 |
|
---|
150 |
|
---|
151 | //Since there is so much ground to cover with searching, we use a typedef to
|
---|
152 | //allow you to vary the string type being searched...
|
---|
153 |
|
---|
154 |
|
---|
155 | stringtype theDest("abcdefghijkabc");
|
---|
156 | nsString s1("ijk");
|
---|
157 | nsCString c1("ijk");
|
---|
158 |
|
---|
159 | PRInt32 pos=theDest.Find(pbuf);
|
---|
160 | NS_ASSERTION(pos==4,"Error: Find routine");
|
---|
161 |
|
---|
162 | pos=theDest.Find(pbuf2,PR_FALSE,-1);
|
---|
163 | NS_ASSERTION(pos==0,"Error: Find routine");
|
---|
164 |
|
---|
165 | pos=theDest.Find(pbuf2,PR_FALSE,pos+1);
|
---|
166 | NS_ASSERTION(pos==11,"Error: Find routine");
|
---|
167 |
|
---|
168 | pos=theDest.FindChar('a');
|
---|
169 | NS_ASSERTION(pos==0,"Error: Find routine");
|
---|
170 |
|
---|
171 | pos=theDest.FindChar('a',PR_FALSE,pos+1);
|
---|
172 | NS_ASSERTION(pos==11,"Error: Find routine");
|
---|
173 |
|
---|
174 | pos=theDest.Find("efg");
|
---|
175 | NS_ASSERTION(pos==4,"Error: Find routine");
|
---|
176 |
|
---|
177 | pos=theDest.Find("EFG",PR_TRUE);
|
---|
178 | NS_ASSERTION(pos==4,"Error: Find routine");
|
---|
179 |
|
---|
180 | pos=theDest.FindChar('d');
|
---|
181 | NS_ASSERTION(pos==3,"Error: Find char routine");
|
---|
182 |
|
---|
183 | pos=theDest.Find(s1);
|
---|
184 | NS_ASSERTION(pos==8,"Error: Find char routine");
|
---|
185 |
|
---|
186 | pos=theDest.FindCharInSet("12k");
|
---|
187 | NS_ASSERTION(pos==10,"Error: Findcharinset routine");
|
---|
188 |
|
---|
189 | pos=theDest.FindCharInSet(pbuf);
|
---|
190 | NS_ASSERTION(pos==4,"Error: Findcharinset routine");
|
---|
191 |
|
---|
192 | pos=theDest.FindCharInSet(s1);
|
---|
193 | NS_ASSERTION(pos==8,"Error: Findcharinset routine");
|
---|
194 |
|
---|
195 | pos=theDest.Find("efg",PR_FALSE,2);
|
---|
196 | NS_ASSERTION(pos==4,"Error: Find routine");
|
---|
197 |
|
---|
198 | pos=theDest.RFindCharInSet("12k");
|
---|
199 | NS_ASSERTION(pos==10,"Error: RFindcharinset routine");
|
---|
200 |
|
---|
201 | pos=theDest.RFindCharInSet("xyz");
|
---|
202 | NS_ASSERTION(pos==-1,"Error: RFindcharinset routine");
|
---|
203 |
|
---|
204 | pos=theDest.RFindCharInSet(pbuf);
|
---|
205 | NS_ASSERTION(pos==6,"Error: RFindcharinset routine");
|
---|
206 |
|
---|
207 | pos=theDest.RFindCharInSet(s1);
|
---|
208 | NS_ASSERTION(pos==10,"Error: RFindcharinset routine");
|
---|
209 |
|
---|
210 | pos=theDest.RFind("efg");
|
---|
211 | NS_ASSERTION(pos==4,"Error: RFind routine");
|
---|
212 |
|
---|
213 | pos=theDest.RFind("xxx");
|
---|
214 | NS_ASSERTION(pos==-1,"Error: RFind routine"); //this should fail
|
---|
215 |
|
---|
216 | pos=theDest.RFind("");
|
---|
217 | NS_ASSERTION(pos==-1,"Error: RFind routine"); //this too should fail.
|
---|
218 |
|
---|
219 | pos=theDest.RFindChar('a',PR_FALSE,4);
|
---|
220 | NS_ASSERTION(pos==-1,"Error: RFind routine");
|
---|
221 |
|
---|
222 |
|
---|
223 | //now try searching with FindChar using offset and count...
|
---|
224 | {
|
---|
225 | stringtype s1("hello there rick");
|
---|
226 |
|
---|
227 | PRInt32 pos=s1.FindChar('r'); //this will search from the beginning, and for the length of the string.
|
---|
228 | NS_ASSERTION(pos==9,"Error: FindChar() with offset and count");
|
---|
229 |
|
---|
230 | pos=s1.FindChar('r',PR_FALSE,0,5); //this will search from the front using count. THIS WILL FAIL!
|
---|
231 | NS_ASSERTION(pos==-1,"Error: FindChar() with offset and count");
|
---|
232 |
|
---|
233 | pos=s1.FindChar('r',PR_FALSE,0,10); //this will search from the front using count. THIS WILL SUCCEED!
|
---|
234 | NS_ASSERTION(pos==9,"Error: FindChar() with offset and count");
|
---|
235 |
|
---|
236 | pos=s1.FindChar('i',PR_FALSE,5,5); //this will search from the middle using count. THIS WILL FAIL!
|
---|
237 | NS_ASSERTION(pos==-1,"Error: FindChar() with offset and count");
|
---|
238 |
|
---|
239 | pos=s1.FindChar('i',PR_FALSE,5,10); //this will search from the middle using count. THIS WILL SUCCEED!
|
---|
240 | NS_ASSERTION(pos==13,"Error: FindChar() with offset and count");
|
---|
241 |
|
---|
242 | pos=s1.FindChar('k',PR_FALSE,10,2); //this will search from near the end using count. THIS WILL FAIL!
|
---|
243 | NS_ASSERTION(pos==-1,"Error: FindChar() with offset and count");
|
---|
244 |
|
---|
245 | pos=s1.FindChar('k',PR_FALSE,10,7); //this will search from near the end using count. THIS WILL SUCCEED!
|
---|
246 | NS_ASSERTION(pos==15,"Error: FindChar() with offset and count");
|
---|
247 |
|
---|
248 | //now let's try a few with bad data...
|
---|
249 |
|
---|
250 | pos=s1.FindChar('k',PR_FALSE,100,2); //this will search from a bad offset. THIS WILL FAIL!
|
---|
251 | NS_ASSERTION(pos==-1,"Error: FindChar() with offset and count");
|
---|
252 |
|
---|
253 | pos=s1.FindChar('k',PR_FALSE,10,0); //this will search for a bad count. THIS WILL FAIL!
|
---|
254 | NS_ASSERTION(pos==-1,"Error: FindChar() with offset and count");
|
---|
255 |
|
---|
256 | pos=s1.FindChar('k',PR_FALSE,10,20); //this will search for a bad count. THIS WILL SUCCEED!
|
---|
257 | NS_ASSERTION(pos==15,"Error: FindChar() with offset and count");
|
---|
258 |
|
---|
259 | pos=s1.FindChar('k',PR_FALSE,10,4); //this will search for a bad count. THIS WILL FAIL!
|
---|
260 | NS_ASSERTION(pos==-1,"Error: FindChar() with offset and count");
|
---|
261 |
|
---|
262 | pos=10;
|
---|
263 | }
|
---|
264 |
|
---|
265 | //now try searching with RFindChar using offset and count...
|
---|
266 | {
|
---|
267 | stringtype s1("hello there rick");
|
---|
268 |
|
---|
269 | PRInt32 pos=s1.RFindChar('o'); //this will search from the end, and for the length of the string.
|
---|
270 | NS_ASSERTION(pos==4,"Error: RFindChar() with offset and count");
|
---|
271 |
|
---|
272 | pos=s1.RFindChar('i'); //this will search from the end, and for the length of the string.
|
---|
273 | NS_ASSERTION(pos==13,"Error: RFindChar() with offset and count");
|
---|
274 |
|
---|
275 | pos=s1.RFindChar(' ',PR_FALSE,-1,4); //this will search from the end, and for the length of the string.
|
---|
276 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count"); //THIS WILL FAIL
|
---|
277 |
|
---|
278 | pos=s1.RFindChar(' ',PR_FALSE,12,1); //this will search from the middle, and for the length of 1.
|
---|
279 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count"); //THIS WILL FAIL
|
---|
280 |
|
---|
281 | pos=s1.RFindChar(' ',PR_FALSE,12,2); //this will search from the middle, and for the length of 2.
|
---|
282 | NS_ASSERTION(pos==11,"Error: RFindChar() with offset and count"); //THIS WILL SUCCEED
|
---|
283 |
|
---|
284 | pos=s1.RFindChar('o',PR_FALSE,-1,5); //this will search from the end using count. THIS WILL FAIL!
|
---|
285 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count");
|
---|
286 |
|
---|
287 | pos=s1.RFindChar('o',PR_FALSE,-1,12); //this will search from the front using count. THIS WILL SUCCEED!
|
---|
288 | NS_ASSERTION(pos==4,"Error: RFindChar() with offset and count");
|
---|
289 |
|
---|
290 | pos=s1.RFindChar('l',PR_FALSE,8,2); //this will search from the middle using count. THIS WILL FAIL!
|
---|
291 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count");
|
---|
292 |
|
---|
293 | pos=s1.RFindChar('l',PR_FALSE,8,7); //this will search from the middle using count. THIS WILL SUCCEED!
|
---|
294 | NS_ASSERTION(pos==3,"Error: RFindChar() with offset and count");
|
---|
295 |
|
---|
296 | //***
|
---|
297 |
|
---|
298 | pos=s1.RFindChar('h',PR_FALSE,3,2); //this will search from near the end using count. THIS WILL FAIL!
|
---|
299 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count");
|
---|
300 |
|
---|
301 | pos=s1.RFindChar('h',PR_FALSE,3,7); //this will search from near the end using count. THIS WILL SUCCEED!
|
---|
302 | NS_ASSERTION(pos==0,"Error: RFindChar() with offset and count");
|
---|
303 |
|
---|
304 | //now let's try a few with bad data...
|
---|
305 |
|
---|
306 | pos=s1.RFindChar('k',PR_FALSE,100,2); //this will search from a bad offset. THIS WILL FAIL!
|
---|
307 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count");
|
---|
308 |
|
---|
309 | pos=s1.RFindChar('k',PR_FALSE,10,0); //this will search for a bad count. THIS WILL FAIL!
|
---|
310 | NS_ASSERTION(pos==-1,"Error: RFindChar() with offset and count");
|
---|
311 |
|
---|
312 | pos=10;
|
---|
313 | }
|
---|
314 |
|
---|
315 | //now try searching with Find() using offset and count...
|
---|
316 | {
|
---|
317 | stringtype s1("hello there rick");
|
---|
318 |
|
---|
319 | PRInt32 pos= s1.Find("there",PR_FALSE,0,4); //first search from front using offset
|
---|
320 | NS_ASSERTION(pos==-1,"Error: Find() with offset and count"); //THIS WILL FAIL
|
---|
321 |
|
---|
322 | pos= s1.Find("there",PR_FALSE,0,8); //first search from front using count
|
---|
323 | NS_ASSERTION(pos==6,"Error: Find) with offset and count"); //THIS WILL SUCCEED
|
---|
324 |
|
---|
325 | pos= s1.Find("there",PR_FALSE,4,1); //first search from front using count
|
---|
326 | NS_ASSERTION(pos==-1,"Error: Find() with offset and count"); //THIS WILL FAIL
|
---|
327 |
|
---|
328 | pos= s1.Find("there",PR_FALSE,5,2); //first search from front using count
|
---|
329 | NS_ASSERTION(pos==6,"Error: Find() with offset and count"); //THIS WILL SUCCEED
|
---|
330 |
|
---|
331 | pos= s1.Find("there",PR_FALSE,6,1); //first search from front using count
|
---|
332 | NS_ASSERTION(pos==6,"Error: Find() with offset and count"); //THIS WILL SUCCEED
|
---|
333 |
|
---|
334 | pos= s1.Find("there",PR_FALSE,6,0); //first search from front using a bogus count
|
---|
335 | NS_ASSERTION(pos==-1,"Error: Find() with offset and count"); //THIS WILL FAIL
|
---|
336 |
|
---|
337 | pos= s1.Find("k",PR_FALSE,15,1); //first search from end using a count
|
---|
338 | NS_ASSERTION(pos==15,"Error: Find() with offset and count"); //THIS WILL SUCCEED
|
---|
339 |
|
---|
340 | pos= s1.Find("k",PR_FALSE,15,10); //first search from end using a LARGE count
|
---|
341 | NS_ASSERTION(pos==15,"Error: Find() with offset and count"); //THIS WILL SUCCEED
|
---|
342 |
|
---|
343 | pos= s1.Find("k",PR_FALSE,25,10); //first search from bogus offset using a LARGE count
|
---|
344 | NS_ASSERTION(pos==-1,"Error: Find() with offset and count"); //THIS WILL FAIL
|
---|
345 |
|
---|
346 | pos=10;
|
---|
347 | }
|
---|
348 |
|
---|
349 | //now try substringsearching with RFind() using offset and count...
|
---|
350 | {
|
---|
351 | nsString s1("abcdefghijklmnopqrstuvwxyz");
|
---|
352 |
|
---|
353 | PRInt32 pos= s1.RFind("ghi"); //first search from end using count
|
---|
354 | NS_ASSERTION(pos==6,"Error: RFind() with offset and count"); //THIS WILL SUCCEED!
|
---|
355 |
|
---|
356 | pos= s1.RFind("nop",PR_FALSE,-1,4); //first search from end using count
|
---|
357 | NS_ASSERTION(pos==-1,"Error: RFind() with offset and count"); //THIS WILL FAIL
|
---|
358 |
|
---|
359 | pos= s1.RFind("nop",PR_FALSE,-1,15); //first search from end using count
|
---|
360 | NS_ASSERTION(pos==13,"Error: RFind() with offset and count"); //THIS WILL SUCCEED
|
---|
361 |
|
---|
362 | pos= s1.RFind("nop",PR_FALSE,16,3); //first search from middle using count
|
---|
363 | NS_ASSERTION(pos==-1,"Error: RFind() with offset and count"); //THIS WILL FAIL
|
---|
364 |
|
---|
365 | pos= s1.RFind("nop",PR_FALSE,16,7); //first search from middle using count
|
---|
366 | NS_ASSERTION(pos==13,"Error: RFind() with offset and count"); //THIS WILL SUCCEED
|
---|
367 |
|
---|
368 | pos= s1.RFind("nop",PR_FALSE,0,1); //first search from front using count
|
---|
369 | NS_ASSERTION(pos==-1,"Error: RFind() with offset and count"); //THIS WILL FAIL
|
---|
370 |
|
---|
371 | pos= s1.RFind("abc",PR_FALSE,0,1); //first search from middle using count
|
---|
372 | NS_ASSERTION(pos==0,"Error: RFind() with offset and count"); //THIS WILL SUCCEED
|
---|
373 |
|
---|
374 | pos= s1.RFind("foo",PR_FALSE,10,100); //first search from front using bogus count
|
---|
375 | NS_ASSERTION(pos==-1,"Error: RFind() with offset and count"); //THIS WILL FAIL
|
---|
376 |
|
---|
377 | pos= s1.RFind("ghi",PR_FALSE,30,1); //first search from middle using bogus offset
|
---|
378 | NS_ASSERTION(pos==-1,"Error: RFind() with offset and count"); //THIS WILL FAIL
|
---|
379 |
|
---|
380 | pos=10;
|
---|
381 | }
|
---|
382 |
|
---|
383 | //Various UNICODE tests...
|
---|
384 | {
|
---|
385 | //do some searching against chinese unicode chars...
|
---|
386 |
|
---|
387 | PRUnichar chinese[] = {0x4e41,0x4e42, 0x4e43, 0x0000}; // 3 chinese unicode
|
---|
388 |
|
---|
389 | nsString T2(chinese);
|
---|
390 | nsString T2copy(chinese);
|
---|
391 |
|
---|
392 | pos = T2.FindCharInSet("A");
|
---|
393 | NS_ASSERTION(kNotFound==pos,"Error in FindCharInSet");
|
---|
394 |
|
---|
395 | pos=T2.RFindCharInSet("A",2);
|
---|
396 | NS_ASSERTION(kNotFound==pos,"Error in RFindCharInSet");
|
---|
397 |
|
---|
398 | pos=T2.Find("A", PR_FALSE, 0, 1);
|
---|
399 | NS_ASSERTION(kNotFound==pos,"Error in Find");
|
---|
400 |
|
---|
401 | pos=T2.RFind("A", PR_FALSE, 2, 1);
|
---|
402 | NS_ASSERTION(kNotFound==pos,"Error in RFind");
|
---|
403 |
|
---|
404 | T2.ReplaceChar("A",' ');
|
---|
405 | NS_ASSERTION(T2==T2copy,"Error in ReplaceChar");
|
---|
406 |
|
---|
407 | //Here's the 3rd FTang unicode test...
|
---|
408 |
|
---|
409 | static char test4[]="ABCDEF";
|
---|
410 | static char test4b[]=" BCDEF";
|
---|
411 |
|
---|
412 | PRUnichar test5[]={0x4e41, 0x0000};
|
---|
413 | PRUnichar test6[]={0x0041, 0x0000};
|
---|
414 |
|
---|
415 | nsCString T4(test4);
|
---|
416 | nsCString T4copy(test4);
|
---|
417 | nsCString T4copyb(test4b);
|
---|
418 | nsCString T5(test5);
|
---|
419 | nsCString T6(test6);
|
---|
420 |
|
---|
421 | pos = T4.FindCharInSet(T5.get());
|
---|
422 | NS_ASSERTION(0==pos,"Error in FindcharInSet"); //This should succeed.
|
---|
423 |
|
---|
424 | pos = T4.FindCharInSet(T6.get());
|
---|
425 | NS_ASSERTION(kNotFound<pos,"Error in FindcharInSet"); //This should succeed.
|
---|
426 |
|
---|
427 | pos = T4.RFindCharInSet(T5.get(),2);
|
---|
428 | NS_ASSERTION(0==pos,"Error in RFindCharInSet"); //This should fail.
|
---|
429 |
|
---|
430 | pos = T4.RFindCharInSet(T6.get(),2);
|
---|
431 | NS_ASSERTION(kNotFound<pos,"Error in RFindCharInSet"); //This should fail.
|
---|
432 |
|
---|
433 | pos = T4.Find(T5.get(), PR_FALSE, 0, 1);
|
---|
434 | NS_ASSERTION(0==pos,"Error in Find"); //This should succeed.
|
---|
435 |
|
---|
436 | pos= T4.Find(T6.get(), PR_FALSE, 0, 1);
|
---|
437 | NS_ASSERTION(kNotFound<pos,"Error in Find"); //This should fail.
|
---|
438 |
|
---|
439 | pos = T4.RFind(T5.get(), PR_FALSE, 2, 1);
|
---|
440 | NS_ASSERTION(kNotFound==pos,"Error in RFind");
|
---|
441 |
|
---|
442 | pos =T4.RFind(T6.get(), PR_FALSE, 2, 1);
|
---|
443 | NS_ASSERTION(kNotFound==pos,"Error in RFind");
|
---|
444 |
|
---|
445 |
|
---|
446 | /*
|
---|
447 | NOT WORKING IN NEW STRING YET...
|
---|
448 |
|
---|
449 | T4.ReplaceChar(PRUnichar(0x4E41),PRUnichar(' '));
|
---|
450 | if(T4 != T4copy)
|
---|
451 | printf("nsCString::ReplaceChar(PRUnichar, PRUnichar) error- replace when it should not\n");
|
---|
452 |
|
---|
453 | T4 = test4;
|
---|
454 | T4.ReplaceChar(PRUnichar(0x0041),PRUnichar(' '));
|
---|
455 | if(T4 != T4copyb)
|
---|
456 | printf("nsCString::ReplaceChar(PRUnichar, PRUnichar) error- not replace when it should\n");
|
---|
457 |
|
---|
458 | */
|
---|
459 |
|
---|
460 | }
|
---|
461 |
|
---|
462 | return result;
|
---|
463 | }
|
---|
464 |
|
---|
465 | /**
|
---|
466 | *
|
---|
467 | * @update gess10/30/98
|
---|
468 | * @param
|
---|
469 | * @return
|
---|
470 | */
|
---|
471 | int CStringTester::TestConstructors(){
|
---|
472 | int result=0;
|
---|
473 | PRUnichar pbuf[10]={'f','o','o',0};
|
---|
474 | PRUnichar* buf=pbuf;
|
---|
475 |
|
---|
476 | nsString s1("hello world");
|
---|
477 | nsCString c1("what's up");
|
---|
478 |
|
---|
479 |
|
---|
480 | //Test nsCString constructors...
|
---|
481 | {
|
---|
482 | nsCString temp0;
|
---|
483 | nsCString temp1(s1);
|
---|
484 | NS_ASSERTION(temp1==s1,"nsCString Constructor error");
|
---|
485 |
|
---|
486 | nsCString temp2(c1);
|
---|
487 | NS_ASSERTION(temp2==c1,"nsCString Constructor error");
|
---|
488 |
|
---|
489 | nsCString temp3("hello world");
|
---|
490 | NS_ASSERTION(temp3=="hello world","nsCString Constructor error");
|
---|
491 |
|
---|
492 | nsCString temp4(pbuf);
|
---|
493 | NS_ASSERTION(temp4==pbuf,"nsCString Constructor error");
|
---|
494 |
|
---|
495 | nsCString temp5('a');
|
---|
496 | NS_ASSERTION(temp5=="a","nsCString Constructor error");
|
---|
497 |
|
---|
498 | nsCString temp6(PRUnichar('a'));
|
---|
499 | NS_ASSERTION(temp5=="a","nsCString Constructor error");
|
---|
500 | }
|
---|
501 |
|
---|
502 | //now just for fun, let's construct 2byte from 1byte and back...
|
---|
503 | {
|
---|
504 | nsCString temp0("hello");
|
---|
505 | nsString temp1(temp0);
|
---|
506 | nsCString temp2(temp1);
|
---|
507 | }
|
---|
508 |
|
---|
509 | return result;
|
---|
510 | }
|
---|
511 |
|
---|
512 | /**
|
---|
513 | *
|
---|
514 | * @update gess10/30/98
|
---|
515 | * @param
|
---|
516 | * @return
|
---|
517 | */
|
---|
518 | int CStringTester::TestLogical(){
|
---|
519 | int result=0;
|
---|
520 | stringtype temp8("aaaa");
|
---|
521 | stringtype temp8a("AAAA");
|
---|
522 | stringtype temp9("bbbb");
|
---|
523 |
|
---|
524 | const char* aaaa="aaaa";
|
---|
525 | const char* bbbb="bbbb";
|
---|
526 |
|
---|
527 |
|
---|
528 | //First, test a known error that got checked into the tree...
|
---|
529 | {
|
---|
530 | nsString mURL("file:///c|/test/foo.txt");
|
---|
531 | PRBool result=mURL.Equals("file:/",PR_FALSE);
|
---|
532 | NS_ASSERTION(!result,kEqualsError);
|
---|
533 | result=mURL.Equals("file:/",PR_FALSE,5);
|
---|
534 | NS_ASSERTION(result,kEqualsError);
|
---|
535 | result=mURL.Equals("file:/",PR_FALSE,-1);
|
---|
536 | NS_ASSERTION(!result,kEqualsError);
|
---|
537 |
|
---|
538 | nsString s1("rick");
|
---|
539 | result=s1.Equals("rick",PR_FALSE,6);
|
---|
540 | result++;
|
---|
541 | }
|
---|
542 |
|
---|
543 |
|
---|
544 | {
|
---|
545 | //this little piece of code tests to see whether the PL_strcmp series works
|
---|
546 | //correctly even when we have strings whose buffers contain nulls.
|
---|
547 |
|
---|
548 | char *buf1 = "what's up \0\0 doc?";
|
---|
549 | char *buf2 = "what's up \0\0 dog?";
|
---|
550 |
|
---|
551 | nsString s1(buf2,17);
|
---|
552 |
|
---|
553 | PRInt32 result=s1.Compare(buf1,PR_TRUE,17);
|
---|
554 | result=s1.FindChar('?');
|
---|
555 | result++;
|
---|
556 | }
|
---|
557 |
|
---|
558 | {
|
---|
559 | nsString foo("__moz_text");
|
---|
560 | PRInt32 cmp=foo.Compare("pre",PR_FALSE,-1);
|
---|
561 | cmp=cmp;
|
---|
562 | }
|
---|
563 |
|
---|
564 | //First test the string compare routines...
|
---|
565 |
|
---|
566 | NS_ASSERTION(0>temp8.Compare(bbbb),kComparisonError);
|
---|
567 | NS_ASSERTION(0>temp8.Compare(temp9),kComparisonError);
|
---|
568 | NS_ASSERTION(0<temp9.Compare(temp8),kComparisonError);
|
---|
569 | NS_ASSERTION(0==temp8.Compare(temp8a,PR_TRUE),kComparisonError);
|
---|
570 | NS_ASSERTION(0==temp8.Compare(aaaa),kComparisonError);
|
---|
571 |
|
---|
572 | //Now test the boolean operators...
|
---|
573 | NS_ASSERTION(temp8==temp8,kComparisonError);
|
---|
574 | NS_ASSERTION(temp8==aaaa,kComparisonError);
|
---|
575 |
|
---|
576 | NS_ASSERTION(temp8!=temp9,kComparisonError);
|
---|
577 | NS_ASSERTION(temp8!=bbbb,kComparisonError);
|
---|
578 |
|
---|
579 | NS_ASSERTION(((temp8<temp9) && (temp9>=temp8)),kComparisonError);
|
---|
580 |
|
---|
581 | NS_ASSERTION(((temp9>temp8) && (temp8<=temp9)),kComparisonError);
|
---|
582 | NS_ASSERTION(temp9>aaaa,kComparisonError);
|
---|
583 |
|
---|
584 | NS_ASSERTION(temp8<=temp8,kComparisonError);
|
---|
585 | NS_ASSERTION(temp8<=temp9,kComparisonError);
|
---|
586 | NS_ASSERTION(temp8<=bbbb,kComparisonError);
|
---|
587 |
|
---|
588 | NS_ASSERTION(((temp9>=temp8) && (temp8<temp9)),kComparisonError);
|
---|
589 | NS_ASSERTION(temp9>=temp8,kComparisonError);
|
---|
590 | NS_ASSERTION(temp9>=aaaa,kComparisonError);
|
---|
591 |
|
---|
592 | NS_ASSERTION(temp8.Equals(temp8),kEqualsError);
|
---|
593 | NS_ASSERTION(temp8.Equals(aaaa),kEqualsError);
|
---|
594 |
|
---|
595 | stringtype temp10(temp8);
|
---|
596 | ToUpperCase(temp10);
|
---|
597 | NS_ASSERTION(temp8.Equals(temp10,PR_TRUE),kEqualsError);
|
---|
598 | NS_ASSERTION(temp8.Equals("AAAA",PR_TRUE),kEqualsError);
|
---|
599 |
|
---|
600 |
|
---|
601 | //now test the new string Equals APIs..
|
---|
602 | {
|
---|
603 | nsCString s1("hello there");
|
---|
604 | NS_ASSERTION(s1.Equals("hello there"),kEqualsError);
|
---|
605 | NS_ASSERTION(s1.Equals("hello rick",PR_FALSE,5),kEqualsError);
|
---|
606 | NS_ASSERTION(!s1.Equals("hello rick",PR_FALSE-1),kEqualsError);
|
---|
607 |
|
---|
608 | nsCString s2("");
|
---|
609 | NS_ASSERTION(s2.Equals(""),kEqualsError);
|
---|
610 |
|
---|
611 | nsCString s3("view-source:");
|
---|
612 | NS_ASSERTION(s3.Equals("VIEW-SOURCE:",PR_TRUE,12),kEqualsError);
|
---|
613 | }
|
---|
614 |
|
---|
615 | //now test the count argument...
|
---|
616 | {
|
---|
617 | nsString s1("rickgessner");
|
---|
618 | nsString s2("rickricardo");
|
---|
619 | PRInt32 result=s1.Compare(s2); //assume no case conversion, and full-length comparison...
|
---|
620 | result=s1.Compare(s2,PR_FALSE,4);
|
---|
621 | result=s1.Compare(s2,PR_FALSE,5);
|
---|
622 |
|
---|
623 | PRBool b=s1.Equals(s2);
|
---|
624 | b=s1.Equals("rick",PR_FALSE,4);
|
---|
625 | b=s1.Equals("rickz",PR_FALSE,5);
|
---|
626 |
|
---|
627 | result=10;
|
---|
628 |
|
---|
629 | nsString s3("view");
|
---|
630 |
|
---|
631 | #define kString "view-source"
|
---|
632 |
|
---|
633 | b=s3.Equals(kString);
|
---|
634 | result=10;
|
---|
635 | }
|
---|
636 |
|
---|
637 | return result;
|
---|
638 | }
|
---|
639 |
|
---|
640 | /**
|
---|
641 | *
|
---|
642 | * @update gess10/30/98
|
---|
643 | * @param
|
---|
644 | * @return
|
---|
645 | */
|
---|
646 | int CStringTester::TestAssignAndAdd(){
|
---|
647 | int result=0;
|
---|
648 |
|
---|
649 | static const char* s1="hello";
|
---|
650 | static const char* s2="world";
|
---|
651 | static const PRUnichar pbuf[] = {' ','w','o','r','l','d',0};
|
---|
652 |
|
---|
653 | nsString ns1("I'm an nsString");
|
---|
654 | nsCString nc1("I'm an nsCString");
|
---|
655 |
|
---|
656 | nsAutoString as1("nsAutoString source");
|
---|
657 | nsCAutoString ac1("nsCAutoString source");
|
---|
658 |
|
---|
659 |
|
---|
660 | {
|
---|
661 | //**** Test assignments to nsCString...
|
---|
662 |
|
---|
663 | nsCString theDest;
|
---|
664 | theDest.Assign(theDest); //assign nsString to itself
|
---|
665 | theDest.Assign(ns1); //assign an nsString to an nsString
|
---|
666 | NS_ASSERTION(theDest==ns1,"Assignment error");
|
---|
667 |
|
---|
668 | theDest.Assign(nc1); //assign an nsCString to an nsString
|
---|
669 | NS_ASSERTION(theDest==nc1,"Assignment error");
|
---|
670 |
|
---|
671 | theDest.Assign(as1); //assign an nsAutoString to an nsString
|
---|
672 | // NS_ASSERTION(theDest==as1,"Assignment error");
|
---|
673 |
|
---|
674 | theDest.Assign(ac1); //assign an nsCAutoString to an nsString
|
---|
675 | // NS_ASSERTION(theDest==ac1,"Assignment error");
|
---|
676 |
|
---|
677 | theDest.Assign("simple char*"); //assign a char* to an nsString
|
---|
678 | NS_ASSERTION(theDest=="simple char*","Assignment error");
|
---|
679 |
|
---|
680 | theDest.Assign(pbuf); //assign a PRUnichar* to an nsString
|
---|
681 | NS_ASSERTION(theDest==pbuf,"Assignment error");
|
---|
682 |
|
---|
683 | theDest.Assign('!'); //assign a char to an nsString
|
---|
684 | NS_ASSERTION(theDest=="!","Assignment error");
|
---|
685 |
|
---|
686 | theDest.Assign(PRUnichar('$')); //assign a char to an nsString
|
---|
687 | NS_ASSERTION(theDest=="$","Assignment error");
|
---|
688 |
|
---|
689 | theDest=ns1;
|
---|
690 | NS_ASSERTION(theDest==ns1,"Assignment error");
|
---|
691 |
|
---|
692 | theDest=nc1;
|
---|
693 | NS_ASSERTION(theDest==nc1,"Assignment error");
|
---|
694 |
|
---|
695 | theDest='a';
|
---|
696 | NS_ASSERTION(theDest=="a","Assignment error");
|
---|
697 |
|
---|
698 | theDest=PRUnichar('a');
|
---|
699 | NS_ASSERTION(theDest=="a","Assignment error");
|
---|
700 |
|
---|
701 | theDest=s1;
|
---|
702 | NS_ASSERTION(theDest==s1,"Assignment error");
|
---|
703 |
|
---|
704 | theDest=pbuf;
|
---|
705 | NS_ASSERTION(theDest==pbuf,"Assignment error");
|
---|
706 |
|
---|
707 | }
|
---|
708 |
|
---|
709 | //test operator+()...
|
---|
710 | {
|
---|
711 |
|
---|
712 | /* NOT WORKING YET...
|
---|
713 | nsString s1("hello");
|
---|
714 | nsString s2(" world");
|
---|
715 | nsCString c1(" world");
|
---|
716 |
|
---|
717 | stringtype theDest;
|
---|
718 |
|
---|
719 | theDest=s1+s2;
|
---|
720 | NS_ASSERTION(theDest=="hello world","Assignment error");
|
---|
721 |
|
---|
722 | theDest=s1+c1;
|
---|
723 | NS_ASSERTION(theDest=="hello world","Assignment error");
|
---|
724 |
|
---|
725 | theDest=s1+" world";
|
---|
726 | NS_ASSERTION(theDest=="hello world","Assignment error");
|
---|
727 |
|
---|
728 | theDest=s1+pbuf;
|
---|
729 | NS_ASSERTION(theDest=="hello world","Assignment error");
|
---|
730 |
|
---|
731 | theDest=s1+'!';
|
---|
732 | NS_ASSERTION(theDest=="hello!","Assignment error");
|
---|
733 |
|
---|
734 | theDest=s1+PRUnichar('!');
|
---|
735 | NS_ASSERTION(theDest=="hello!","Assignment error");
|
---|
736 | */
|
---|
737 |
|
---|
738 | }
|
---|
739 |
|
---|
740 | return result;
|
---|
741 | }
|
---|
742 |
|
---|
743 |
|
---|
744 | /**
|
---|
745 | *
|
---|
746 | * @update gess10/30/98
|
---|
747 | * @param
|
---|
748 | * @return
|
---|
749 | */
|
---|
750 | int CStringTester::TestAppend(){
|
---|
751 | int result=0;
|
---|
752 |
|
---|
753 |
|
---|
754 | static const char* s1="hello";
|
---|
755 | static const char* s2="world";
|
---|
756 | static const PRUnichar pbuf[] = {' ','w','o','r','l','d',0};
|
---|
757 | static const PRUnichar pbuf1[] = {'a','b','c','d','l','d'};
|
---|
758 |
|
---|
759 |
|
---|
760 | stringtype theDest;
|
---|
761 | theDest.Append((float)100.100);
|
---|
762 | NS_ASSERTION(theDest=="100.1","Append(float) error");
|
---|
763 |
|
---|
764 | theDest.Truncate();
|
---|
765 | theDest.Append(12345);
|
---|
766 | NS_ASSERTION(theDest=="12345","Append(int) error");
|
---|
767 |
|
---|
768 | theDest.Truncate();
|
---|
769 | theDest.Append(pbuf1,1);
|
---|
770 | NS_ASSERTION(theDest=="a","Append(PRUnichar*,count) error");
|
---|
771 |
|
---|
772 | theDest.Truncate();
|
---|
773 | theDest.Append('a');
|
---|
774 | NS_ASSERTION(theDest=="a","Append(char) error");
|
---|
775 |
|
---|
776 |
|
---|
777 | static const PRUnichar pbuf2[] = {'w','h','a','t','s',' ','u','p',' ','d','o','c','?',0};
|
---|
778 |
|
---|
779 | theDest.Truncate();
|
---|
780 | theDest.Append(pbuf,20); //try appending more chars than actual length of pbuf
|
---|
781 | NS_ASSERTION(theDest!=pbuf,"Append(PRUnichar*) error");
|
---|
782 | //(NOTE: if you tell me to append X chars, I'll assume you really have X chars, and the length
|
---|
783 | // get's set accordingly. This test really is correct; it just seems odd.
|
---|
784 |
|
---|
785 | theDest.Truncate(0);
|
---|
786 | theDest.Append(pbuf,5); //try appending fewer chars than actual length of pbuf
|
---|
787 | NS_ASSERTION(theDest==" worl","Append(PRUnichar*) error");
|
---|
788 |
|
---|
789 | theDest.Truncate(0);
|
---|
790 | theDest.Append(pbuf); //try appending all of pbuf
|
---|
791 | NS_ASSERTION(theDest==pbuf,"Append(PRUnichar*) error");
|
---|
792 |
|
---|
793 | char* ss=0;
|
---|
794 | theDest.Truncate();
|
---|
795 | theDest.Append(ss); //try appending NULL
|
---|
796 | NS_ASSERTION(theDest=="","Append(nullstr) error");
|
---|
797 |
|
---|
798 | theDest.Append(pbuf,0); //try appending nothing
|
---|
799 | NS_ASSERTION(theDest=="","Append(nullstr) error");
|
---|
800 |
|
---|
801 |
|
---|
802 | {
|
---|
803 | //test improvement to unichar appends...
|
---|
804 | stringtype s1("hello");
|
---|
805 | char c='!';
|
---|
806 | s1+=c;
|
---|
807 | NS_ASSERTION(s1=="hello!","operator+=() error");
|
---|
808 |
|
---|
809 | c=(char)0xfa;
|
---|
810 | s1+=c;
|
---|
811 | s1.Append(c);
|
---|
812 |
|
---|
813 | PRUnichar theChar='f';
|
---|
814 | s1+=theChar;
|
---|
815 |
|
---|
816 | char theChar2='g';
|
---|
817 | s1+=theChar2;
|
---|
818 |
|
---|
819 | // long theLong= 1234;
|
---|
820 | // s1+=theLong;
|
---|
821 |
|
---|
822 | }
|
---|
823 |
|
---|
824 | {
|
---|
825 | //this just proves we can append nulls in our buffers...
|
---|
826 | stringtype c("hello");
|
---|
827 | stringtype s(" there");
|
---|
828 | c.Append(s);
|
---|
829 | char buf[]={'a','b',0,'d','e'};
|
---|
830 | s.Append(buf,5);
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | stringtype temp2("there");
|
---|
835 |
|
---|
836 | theDest.Append(temp2);
|
---|
837 | theDest.Append(" xxx ");
|
---|
838 | theDest.Append(pbuf);
|
---|
839 | theDest.Append('4');
|
---|
840 | theDest.Append(PRUnichar('Z'));
|
---|
841 |
|
---|
842 | stringtype a(s1);
|
---|
843 | stringtype b(s2);
|
---|
844 |
|
---|
845 | theDest.Truncate();
|
---|
846 | temp2.Truncate();
|
---|
847 |
|
---|
848 | /* NOT WORKING YET...
|
---|
849 | theDest=a+b;
|
---|
850 | temp2=a+"world!";
|
---|
851 | temp2=a+pbuf;
|
---|
852 | stringtype temp3;
|
---|
853 | temp3=temp2+'!';
|
---|
854 | */
|
---|
855 | return result;
|
---|
856 | }
|
---|
857 |
|
---|
858 |
|
---|
859 | /**
|
---|
860 | *
|
---|
861 | * @update gess10/30/98
|
---|
862 | * @param
|
---|
863 | * @return
|
---|
864 | */
|
---|
865 | int CStringTester::TestExtractors(){
|
---|
866 | int result=0;
|
---|
867 |
|
---|
868 | //first test the 2 byte version...
|
---|
869 |
|
---|
870 |
|
---|
871 | {
|
---|
872 | nsString temp1("hello there rick");
|
---|
873 | nsString temp2;
|
---|
874 |
|
---|
875 | temp1.Left(temp2,10);
|
---|
876 | NS_ASSERTION(temp2=="hello ther","Left() error");
|
---|
877 |
|
---|
878 | temp1.Mid(temp2,6,5);
|
---|
879 | NS_ASSERTION(temp2=="there","Mid() error");
|
---|
880 |
|
---|
881 | temp1.Right(temp2,4);
|
---|
882 | NS_ASSERTION(temp2=="rick","Right() error");
|
---|
883 |
|
---|
884 | //Now test the character accessor methods...
|
---|
885 | nsString theString("hello");
|
---|
886 | PRUint32 len=theString.Length();
|
---|
887 | PRUnichar theChar;
|
---|
888 | for(PRUint32 i=0;i<len;i++) {
|
---|
889 | theChar=theString.CharAt(i);
|
---|
890 | }
|
---|
891 |
|
---|
892 | /* NOT WORKING YET...
|
---|
893 | theChar=theString.First();
|
---|
894 | theChar=theString.Last();
|
---|
895 | theChar=theString[3];
|
---|
896 | theString.SetCharAt('X',3);
|
---|
897 | */
|
---|
898 | }
|
---|
899 |
|
---|
900 | //now test the 1 byte version
|
---|
901 | {
|
---|
902 | nsCString temp1("hello there rick");
|
---|
903 | nsCString temp2;
|
---|
904 | temp1.Left(temp2,10);
|
---|
905 | temp1.Mid(temp2,6,5);
|
---|
906 | temp1.Right(temp2,4);
|
---|
907 |
|
---|
908 | //Now test the character accessor methods...
|
---|
909 | nsCString theString("hello");
|
---|
910 | PRUint32 len=theString.Length();
|
---|
911 | char ch;
|
---|
912 | for(PRUint32 i=0;i<len;i++) {
|
---|
913 | ch=theString.CharAt(i);
|
---|
914 | }
|
---|
915 |
|
---|
916 | /* NOT WORKING YET...
|
---|
917 |
|
---|
918 | ch=theString.First();
|
---|
919 | ch=theString.Last();
|
---|
920 | ch=theString[3];
|
---|
921 | theString.SetCharAt('X',3);
|
---|
922 | */
|
---|
923 | }
|
---|
924 |
|
---|
925 |
|
---|
926 | return result;
|
---|
927 | }
|
---|
928 |
|
---|
929 |
|
---|
930 |
|
---|
931 | /**
|
---|
932 | *
|
---|
933 | * @update gess10/30/98
|
---|
934 | * @param
|
---|
935 | * @return
|
---|
936 | */
|
---|
937 | int CStringTester::TestCreators(){
|
---|
938 | int result=0;
|
---|
939 |
|
---|
940 | /* NOT WORKING YET
|
---|
941 | {
|
---|
942 | nsString theString5("");
|
---|
943 | char* str0 = ToNewCString(theString5);
|
---|
944 | Recycle(str0);
|
---|
945 |
|
---|
946 | theString5+="hello rick";
|
---|
947 | nsString* theString6=theString5.ToNewString();
|
---|
948 | delete theString6;
|
---|
949 |
|
---|
950 | nsString theString1("hello again");
|
---|
951 | PRUnichar* thePBuf=ToNewUnicode(theString1);
|
---|
952 | if(thePBuf)
|
---|
953 | Recycle(thePBuf);
|
---|
954 |
|
---|
955 | char* str = ToNewCString(theString5);
|
---|
956 | if(str)
|
---|
957 | Recycle(str);
|
---|
958 |
|
---|
959 | char buffer[6];
|
---|
960 | theString5.ToCString(buffer,sizeof(buffer));
|
---|
961 |
|
---|
962 | }
|
---|
963 |
|
---|
964 | {
|
---|
965 | nsCString theString5("hello rick");
|
---|
966 | nsCString* theString6=theString5.ToNewString();
|
---|
967 | delete theString6;
|
---|
968 |
|
---|
969 | PRUnichar* thePBuf=ToNewUnicode(theString5);
|
---|
970 | if(thePBuf)
|
---|
971 | Recycle(thePBuf);
|
---|
972 |
|
---|
973 | char* str = ToNewCString(theString5);
|
---|
974 | if(str)
|
---|
975 | Recycle(str);
|
---|
976 |
|
---|
977 | char buffer[100];
|
---|
978 | theString5.ToCString(buffer,sizeof(buffer)-1);
|
---|
979 | nsCString theOther=theString5.GetBuffer();
|
---|
980 | }
|
---|
981 | */
|
---|
982 | return result;
|
---|
983 | }
|
---|
984 |
|
---|
985 |
|
---|
986 | /**
|
---|
987 | *
|
---|
988 | * @update gess10/30/98
|
---|
989 | * @param
|
---|
990 | * @return
|
---|
991 | */
|
---|
992 | int CStringTester::TestInsert(){
|
---|
993 | int result=0;
|
---|
994 |
|
---|
995 | {
|
---|
996 | static const char pbuf[] = " world";
|
---|
997 |
|
---|
998 | nsCString temp1("hello rick");
|
---|
999 | temp1.Insert("there ",6); //char* insertion
|
---|
1000 | NS_ASSERTION(temp1=="hello there rick","Insert error");
|
---|
1001 |
|
---|
1002 | temp1.Insert(pbuf,3); //prunichar* insertion
|
---|
1003 | NS_ASSERTION(temp1=="hel worldlo there rick","Insert error");
|
---|
1004 |
|
---|
1005 | temp1.Insert("?",10); //char insertion
|
---|
1006 | NS_ASSERTION(temp1=="hel worldl?o there rick","Insert error");
|
---|
1007 |
|
---|
1008 | temp1.Insert('*',10); //char insertion
|
---|
1009 | NS_ASSERTION(temp1=="hel worldl*?o there rick","Insert error");
|
---|
1010 |
|
---|
1011 | temp1.Insert("xxx",100,3); //this should append.
|
---|
1012 | NS_ASSERTION(temp1=="hel worldl*?o there rickxxx","Insert error");
|
---|
1013 |
|
---|
1014 | nsCString temp2("abcdefghijklmnopqrstuvwxyz");
|
---|
1015 | // temp2.Insert(temp1,10);
|
---|
1016 | temp2.Cut(20,5);
|
---|
1017 | NS_ASSERTION(temp2=="abcdefghijklmnopqrstz","Insert error");
|
---|
1018 |
|
---|
1019 | temp2.Cut(100,100); //this should fail.
|
---|
1020 | NS_ASSERTION(temp2=="abcdefghijklmnopqrstz","Insert error");
|
---|
1021 |
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 | {
|
---|
1025 | static const PRUnichar pbuf[] = {' ','w','o','r','l','d',0};
|
---|
1026 |
|
---|
1027 |
|
---|
1028 | nsString temp1("llo rick");
|
---|
1029 |
|
---|
1030 | temp1.Insert("he",0); //char* insertion
|
---|
1031 | NS_ASSERTION(temp1=="hello rick","Insert error");
|
---|
1032 |
|
---|
1033 | temp1.Insert("there ",6); //char* insertion
|
---|
1034 | NS_ASSERTION(temp1=="hello there rick","Insert error");
|
---|
1035 |
|
---|
1036 | temp1.Insert(pbuf,3); //prunichar* insertion
|
---|
1037 | NS_ASSERTION(temp1=="hel worldlo there rick","Insert error");
|
---|
1038 |
|
---|
1039 | temp1.Insert("?",10); //char insertion
|
---|
1040 | NS_ASSERTION(temp1=="hel worldl?o there rick","Insert error");
|
---|
1041 |
|
---|
1042 | temp1.Insert('*',10); //char insertion
|
---|
1043 | NS_ASSERTION(temp1=="hel worldl*?o there rick","Insert error");
|
---|
1044 |
|
---|
1045 | temp1.Insert("xxx",100,3); //this should append.
|
---|
1046 | NS_ASSERTION(temp1=="hel worldl*?o there rickxxx","Insert error");
|
---|
1047 |
|
---|
1048 | nsString temp2("abcdefghijklmnopqrstuvwxyz");
|
---|
1049 | // temp2.Insert(temp1,10);
|
---|
1050 | temp2.Cut(20,5);
|
---|
1051 | NS_ASSERTION(temp2=="abcdefghijklmnopqrstz","Insert error");
|
---|
1052 |
|
---|
1053 | temp2.Cut(100,100); //this should fail.
|
---|
1054 | NS_ASSERTION(temp2=="abcdefghijklmnopqrstz","Insert error");
|
---|
1055 |
|
---|
1056 | }
|
---|
1057 |
|
---|
1058 | return result;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 |
|
---|
1062 |
|
---|
1063 | /**
|
---|
1064 | *
|
---|
1065 | * @update gess10/30/98
|
---|
1066 | * @param
|
---|
1067 | * @return
|
---|
1068 | */
|
---|
1069 | int CStringTester::TestDelete(){
|
---|
1070 | int result=0;
|
---|
1071 |
|
---|
1072 | //NOTE: You need to run this test program twice for this method to work.
|
---|
1073 | // Once with USE_WIDE defined, and once without (to test nsCString)
|
---|
1074 |
|
---|
1075 | //let's try some whacky string deleting calls
|
---|
1076 | {
|
---|
1077 | const char* pbuf = "whats up doc?";
|
---|
1078 |
|
---|
1079 | nsCString s1(pbuf);
|
---|
1080 | s1.Cut(3,20); //try deleting more chars than actual length of pbuf
|
---|
1081 | NS_ASSERTION(s1=="wha","Cut error");
|
---|
1082 |
|
---|
1083 | s1=pbuf;
|
---|
1084 | s1.Cut(3,-10);
|
---|
1085 | NS_ASSERTION(s1==pbuf,"Cut error");
|
---|
1086 |
|
---|
1087 | s1=pbuf;
|
---|
1088 | s1.Cut(3,2);
|
---|
1089 | NS_ASSERTION(s1=="wha up doc?","Cut error");
|
---|
1090 |
|
---|
1091 | }
|
---|
1092 |
|
---|
1093 | //let's try some whacky string deleting calls
|
---|
1094 | {
|
---|
1095 | const PRUnichar pbuf[] = {'w','h','a','t','s',' ','u','p',' ','d','o','c','?',0};
|
---|
1096 |
|
---|
1097 | nsString s1(pbuf);
|
---|
1098 | s1.Cut(3,20); //try deleting more chars than actual length of pbuf
|
---|
1099 | NS_ASSERTION(s1=="wha","Cut error");
|
---|
1100 |
|
---|
1101 | s1=pbuf;
|
---|
1102 | s1.Cut(3,-10);
|
---|
1103 | NS_ASSERTION(s1==pbuf,"Cut error");
|
---|
1104 |
|
---|
1105 | s1=pbuf;
|
---|
1106 | s1.Cut(3,2);
|
---|
1107 | NS_ASSERTION(s1=="wha up doc?","Cut error");
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 |
|
---|
1111 | {
|
---|
1112 | nsCString s;
|
---|
1113 | int i;
|
---|
1114 | for(i=0;i<518;i++) {
|
---|
1115 | s.Append("0123456789");
|
---|
1116 | }
|
---|
1117 | s+="abc";
|
---|
1118 | s.Cut(0,5);
|
---|
1119 | }
|
---|
1120 |
|
---|
1121 | {
|
---|
1122 | astringtype ab("ab");
|
---|
1123 | stringtype abcde("cde");
|
---|
1124 | stringtype cut("abcdef");
|
---|
1125 | cut.Cut(7,10); //this is out of bounds, so ignore...
|
---|
1126 | //cut.DebugDump(cout);
|
---|
1127 | cut.Cut(5,2); //cut last chars
|
---|
1128 | //cut.DebugDump(cout);
|
---|
1129 | cut.Cut(1,1); //cut first char
|
---|
1130 | //cut.DebugDump(cout);
|
---|
1131 | cut.Cut(2,1); //cut one from the middle
|
---|
1132 | //cut.DebugDump(cout);
|
---|
1133 | cut="Hello there Rick";
|
---|
1134 | //cut.DebugDump(cout);
|
---|
1135 | }
|
---|
1136 |
|
---|
1137 | return result;
|
---|
1138 | }
|
---|
1139 |
|
---|
1140 | /**
|
---|
1141 | *
|
---|
1142 | * @update gess10/30/98
|
---|
1143 | * @param
|
---|
1144 | * @return
|
---|
1145 | */
|
---|
1146 | int CStringTester::TestTruncate(){
|
---|
1147 | int result=0;
|
---|
1148 | //test delete against a twobyte string...
|
---|
1149 |
|
---|
1150 | {
|
---|
1151 | nsCString s0;
|
---|
1152 | nsCString s1(kNumbers[0]);
|
---|
1153 | s0=s1;
|
---|
1154 | s0.Truncate(100); //should fail
|
---|
1155 | s0.Truncate(5); //should work
|
---|
1156 | s0.Truncate(0); //should work
|
---|
1157 | }
|
---|
1158 |
|
---|
1159 | {
|
---|
1160 | nsString s0;
|
---|
1161 | nsString s1(kNumbers[0]);
|
---|
1162 | s0=s1;
|
---|
1163 | s0.Truncate(100); //should fail
|
---|
1164 | s0.Truncate(5); //should work
|
---|
1165 | s0.Truncate(0); //should work
|
---|
1166 | }
|
---|
1167 | return result;
|
---|
1168 | }
|
---|
1169 |
|
---|
1170 | /**
|
---|
1171 | *
|
---|
1172 | * @update gess10/30/98
|
---|
1173 | * @param
|
---|
1174 | * @return
|
---|
1175 | */
|
---|
1176 | int CStringTester::TestNumerics(){
|
---|
1177 | int result=0;
|
---|
1178 |
|
---|
1179 | //try a few numeric conversion routines...
|
---|
1180 | {
|
---|
1181 | nsCString str1("-12345");
|
---|
1182 | NS_ASSERTION(str1=="-12345","Append(int) error");
|
---|
1183 |
|
---|
1184 | nsString str2("hello");
|
---|
1185 | nsString str3;
|
---|
1186 | nsString str4("0");
|
---|
1187 | nsString str5("·");
|
---|
1188 | nsString str6("ξ");
|
---|
1189 | nsString str7("#xe3;");
|
---|
1190 | nsString str8("#FF;");
|
---|
1191 | nsCString str9("-#xa?a;"); //should return -10 (decimal)
|
---|
1192 | nsCString str10("¿");
|
---|
1193 | nsCString str11("&#vvv;");
|
---|
1194 | nsCString str12("-vvv;");
|
---|
1195 | nsCString str13("-f");
|
---|
1196 |
|
---|
1197 | PRInt32 err;
|
---|
1198 | PRInt32 theInt=str1.ToInteger(&err);
|
---|
1199 | theInt=str2.ToInteger(&err);
|
---|
1200 | NS_ASSERTION(theInt==14,"ToInteger error");
|
---|
1201 |
|
---|
1202 | theInt=str3.ToInteger(&err);
|
---|
1203 | NS_ASSERTION(theInt==0,"ToInteger error");
|
---|
1204 |
|
---|
1205 | theInt=str4.ToInteger(&err);
|
---|
1206 | NS_ASSERTION(theInt==0,"ToInteger error");
|
---|
1207 |
|
---|
1208 | theInt=str5.ToInteger(&err);
|
---|
1209 | NS_ASSERTION(theInt==183,"ToInteger error");
|
---|
1210 |
|
---|
1211 | theInt=str6.ToInteger(&err);
|
---|
1212 | NS_ASSERTION(theInt==0,"ToInteger error");
|
---|
1213 |
|
---|
1214 | theInt=str7.ToInteger(&err,16);
|
---|
1215 | NS_ASSERTION(theInt==227,"ToInteger error");
|
---|
1216 |
|
---|
1217 | theInt=str8.ToInteger(&err,kAutoDetect);
|
---|
1218 | NS_ASSERTION(theInt==255,"ToInteger error");
|
---|
1219 |
|
---|
1220 | theInt=str9.ToInteger(&err,kAutoDetect);
|
---|
1221 | NS_ASSERTION(theInt==-10,"ToInteger error");
|
---|
1222 |
|
---|
1223 | theInt=str10.ToInteger(&err);
|
---|
1224 | NS_ASSERTION(theInt==191,"ToInteger error");
|
---|
1225 |
|
---|
1226 | theInt=str11.ToInteger(&err);
|
---|
1227 | NS_ASSERTION(theInt==0,"ToInteger error");
|
---|
1228 |
|
---|
1229 | theInt=str12.ToInteger(&err);
|
---|
1230 | NS_ASSERTION(theInt==0,"ToInteger error");
|
---|
1231 |
|
---|
1232 | theInt=str13.ToInteger(&err);
|
---|
1233 | NS_ASSERTION(theInt==-15,"ToInteger error");
|
---|
1234 |
|
---|
1235 |
|
---|
1236 | Stopwatch watch;
|
---|
1237 | int i;
|
---|
1238 | for(i=0;i<1000000;i++){
|
---|
1239 | theInt=str1.ToInteger(&err,16);
|
---|
1240 | }
|
---|
1241 | watch.Stop();
|
---|
1242 | watch.Print("ToInteger() ");
|
---|
1243 |
|
---|
1244 | str1="100.100";
|
---|
1245 | float theFloat=str1.ToFloat(&err);
|
---|
1246 | }
|
---|
1247 | //try a few numeric conversion routines...
|
---|
1248 | {
|
---|
1249 | nsCString str1("10000");
|
---|
1250 | nsCString str2("hello");
|
---|
1251 | nsCString str3;
|
---|
1252 | PRInt32 err;
|
---|
1253 | PRInt32 theInt=str1.ToInteger(&err);
|
---|
1254 | NS_ASSERTION(theInt==10000,"ToInteger error");
|
---|
1255 |
|
---|
1256 | theInt=str2.ToInteger(&err);
|
---|
1257 | NS_ASSERTION(theInt==14,"ToInteger error");
|
---|
1258 |
|
---|
1259 | theInt=str3.ToInteger(&err);
|
---|
1260 | NS_ASSERTION(theInt==0,"ToInteger error");
|
---|
1261 |
|
---|
1262 | str1="100.100";
|
---|
1263 | float theFloat=str1.ToFloat(&err);
|
---|
1264 | }
|
---|
1265 | return 0;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | /**
|
---|
1269 | *
|
---|
1270 | * @update gess10/30/98
|
---|
1271 | * @param
|
---|
1272 | * @return
|
---|
1273 | */
|
---|
1274 | int CStringTester::TestLexomorphic(){
|
---|
1275 | int result=0;
|
---|
1276 | //test delete against a twobyte string...
|
---|
1277 |
|
---|
1278 | //NOTE: You need to run this test program twice for this method to work.
|
---|
1279 | // Once with USE_WIDE defined, and once without (to test nsCString)
|
---|
1280 |
|
---|
1281 |
|
---|
1282 | {
|
---|
1283 | nsString theTag("FOO");
|
---|
1284 | nsString theLower;
|
---|
1285 | theTag.ToLowerCase(theLower);
|
---|
1286 | int x=5;
|
---|
1287 | }
|
---|
1288 |
|
---|
1289 | PRUnichar pbuf[] = {'h','e','l','l','o','\n','\n','\n','\n',250,'\n','\n','\n','\n','\n','\n','r','i','c','k',0};
|
---|
1290 |
|
---|
1291 | //and hey, why not do a few lexo-morphic tests...
|
---|
1292 | nsString s0(pbuf);
|
---|
1293 | ToUpperCase(s0);
|
---|
1294 | ToLowerCase(s0);
|
---|
1295 | s0.StripChars("l");
|
---|
1296 | s0.StripChars("\n");
|
---|
1297 | s0.StripChar(250);
|
---|
1298 | NS_ASSERTION(s0=="heorick","Stripchars error");
|
---|
1299 |
|
---|
1300 | {
|
---|
1301 | nsAutoString s1(pbuf);
|
---|
1302 | s1.CompressSet("\n ",' ');
|
---|
1303 | }
|
---|
1304 |
|
---|
1305 | {
|
---|
1306 |
|
---|
1307 | char pbuf[] = { 0x1C, 0x04, 0x1D, 0x04, 0x20, 0x00, 0x2D, 0x00, 0x20, 0x00, 0x23, 0x04, 0x20, 0x00, 0x40, 0x04,
|
---|
1308 | 0x43, 0x04, 0x3B, 0x04, 0x4F, 0x04, 0x20, 0x00, 0x30, 0x04, 0x40, 0x04, 0x3C, 0x04, 0x38, 0x04,
|
---|
1309 | 0x38, 0x04, 0x20, 0x00, 0x2D, 0x00, 0x20, 0x00, 0x32, 0x04, 0x42, 0x04, 0x3E, 0x04, 0x40, 0x04,
|
---|
1310 | 0x3E, 0x04, 0x41, 0x04, 0x42, 0x04, 0x35, 0x04, 0x3F, 0x04, 0x35, 0x04, 0x3D, 0x04, 0x3D, 0x04,
|
---|
1311 | 0x4B, 0x04, 0x35, 0x04, 0x20, 0x00, 0x3B, 0x04, 0x38, 0x04, 0x46, 0x04, 0x30, 0x04, 0x20, 0x00,
|
---|
1312 | 0x2D, 0x00, 0x20, 0x00, 0x30, 0x00, 0x39, 0x00, 0x2F, 0x00, 0x32, 0x00, 0x30, 0x00, 0x30, 0x00,
|
---|
1313 | 0x30, 0x00, 0x00, 0x00};
|
---|
1314 |
|
---|
1315 | nsAutoString temp((PRUnichar*)pbuf);
|
---|
1316 | nsAutoString temp1(temp);
|
---|
1317 | temp.CompressWhitespace();
|
---|
1318 | PRBool equals=temp.Equals(temp1);
|
---|
1319 |
|
---|
1320 | }
|
---|
1321 |
|
---|
1322 | {
|
---|
1323 | nsString s1(" ");
|
---|
1324 | s1.Trim("; \t");
|
---|
1325 | s1="helvetica";
|
---|
1326 | s1.Trim("; \t");
|
---|
1327 | }
|
---|
1328 |
|
---|
1329 | s0.Insert(" ",0);
|
---|
1330 | NS_ASSERTION(s0==" heorick","Stripchars error");
|
---|
1331 |
|
---|
1332 | s0.Append(" ");
|
---|
1333 | NS_ASSERTION(s0==" heorick ","Stripchars error");
|
---|
1334 |
|
---|
1335 | s0.Trim(" ",PR_TRUE,PR_TRUE);
|
---|
1336 | NS_ASSERTION(s0=="heorick","Stripchars error");
|
---|
1337 |
|
---|
1338 | s0.Append(" abc 123 xyz ");
|
---|
1339 | s0.CompressWhitespace();
|
---|
1340 | NS_ASSERTION(s0=="heorick abc 123 xyz","CompressWS error");
|
---|
1341 |
|
---|
1342 | s0.ReplaceChar('r','b');
|
---|
1343 | NS_ASSERTION(s0=="heobick abc 123 xyz","ReplaceChar error");
|
---|
1344 |
|
---|
1345 | s0=pbuf;
|
---|
1346 | ToUpperCase(s0);
|
---|
1347 | ToLowerCase(s0);
|
---|
1348 | s0.Append("\n\n\n \r \r \r \t \t \t");
|
---|
1349 | s0.StripWhitespace();
|
---|
1350 |
|
---|
1351 | nsCAutoString s1("\n\r hello \n\n\n\r\r\r\t rick \t\t ");
|
---|
1352 | ToUpperCase(s1);
|
---|
1353 | ToLowerCase(s1);
|
---|
1354 | s1.StripChars("o");
|
---|
1355 | s1.Trim(" ",PR_TRUE,PR_TRUE);
|
---|
1356 | s1.CompressWhitespace();
|
---|
1357 | NS_ASSERTION(s1=="hell rick","Compress Error");
|
---|
1358 |
|
---|
1359 | s1.ReplaceChar('h','w');
|
---|
1360 | NS_ASSERTION(s1=="well rick","Compress Error");
|
---|
1361 |
|
---|
1362 | ToUpperCase(s1);
|
---|
1363 | NS_ASSERTION(s1=="WELL RICK","Compress Error");
|
---|
1364 |
|
---|
1365 | ToLowerCase(s1);
|
---|
1366 | s1.Append("\n\n\n \r \r \r \t \t \t");
|
---|
1367 | s1.StripWhitespace();
|
---|
1368 | NS_ASSERTION(s1=="wellrick","Compress Error");
|
---|
1369 |
|
---|
1370 | {
|
---|
1371 | nsCString temp("aaaa");
|
---|
1372 | int i;
|
---|
1373 | for(i=0;i<100;i++) {
|
---|
1374 | temp+="0123456789.";
|
---|
1375 | }
|
---|
1376 | temp.StripChars("a2468");
|
---|
1377 | i=5;
|
---|
1378 |
|
---|
1379 | temp=" hello rick ";
|
---|
1380 | temp.StripChars("\n\r\t\b ");
|
---|
1381 | NS_ASSERTION(temp=="hellorick","This isn't good");
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | return result;
|
---|
1385 | }
|
---|
1386 |
|
---|
1387 | /**
|
---|
1388 | *
|
---|
1389 | * @update gess10/30/98
|
---|
1390 | * @param
|
---|
1391 | * @return
|
---|
1392 | */
|
---|
1393 | int CStringTester::TestAutoStrings(){
|
---|
1394 | int result=0;
|
---|
1395 |
|
---|
1396 | PRUnichar pbuf[] = {'h','e','l','l','o',0};
|
---|
1397 |
|
---|
1398 | //this test makes sure that autostrings who assume ownership of a buffer,
|
---|
1399 | //don't also try to copy that buffer onto itself... (was a bug)
|
---|
1400 | {
|
---|
1401 | nsAutoString temp0;
|
---|
1402 | nsAutoString temp1("hello rick");
|
---|
1403 | nsAutoString temp3(pbuf);
|
---|
1404 |
|
---|
1405 | /* NOT WORKING...
|
---|
1406 | nsAutoString temp4(CBufDescriptor((char*)pbuf,PR_TRUE,5,5));
|
---|
1407 | */
|
---|
1408 | nsAutoString temp5(temp3);
|
---|
1409 | nsString s(pbuf);
|
---|
1410 | nsAutoString temp6(s);
|
---|
1411 |
|
---|
1412 | /* NOT WORKING...
|
---|
1413 | char buffer[500];
|
---|
1414 | nsAutoString temp7(CBufDescriptor((PRUnichar*)buffer,PR_TRUE,sizeof(buffer)-10/2,0));
|
---|
1415 | temp7="hello, my name is inigo montoya.";
|
---|
1416 | */
|
---|
1417 |
|
---|
1418 | nsAutoString as;
|
---|
1419 | int i;
|
---|
1420 | for(i=0;i<30;i++){
|
---|
1421 | as+='a';
|
---|
1422 | }
|
---|
1423 | PRBool b=PR_TRUE; //this line doesn't do anything, it just gives a convenient breakpoint.
|
---|
1424 | NS_ASSERTION(as=="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","error in operator+=()");
|
---|
1425 | }
|
---|
1426 |
|
---|
1427 |
|
---|
1428 | {
|
---|
1429 | nsCAutoString temp0;
|
---|
1430 | nsCAutoString temp1("hello rick");
|
---|
1431 |
|
---|
1432 |
|
---|
1433 | //nsCAutoString temp2("hello rick",PR_TRUE,5);
|
---|
1434 | nsCAutoString temp3(pbuf);
|
---|
1435 |
|
---|
1436 | {
|
---|
1437 | const char* buf="hello rick";
|
---|
1438 | int len=strlen(buf);
|
---|
1439 |
|
---|
1440 | // nsAutoString temp4(CBufDescriptor(buf,PR_TRUE,len+1,len)); //NOT WORKING
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | nsCAutoString temp5(temp3);
|
---|
1444 | nsString s(pbuf);
|
---|
1445 | nsCAutoString temp6(s); //create one from a nsString
|
---|
1446 |
|
---|
1447 | nsCAutoString as;
|
---|
1448 | int i;
|
---|
1449 | for(i=0;i<30;i++){
|
---|
1450 | as+='a';
|
---|
1451 | }
|
---|
1452 | PRBool b=PR_TRUE;
|
---|
1453 |
|
---|
1454 | #if 0
|
---|
1455 |
|
---|
1456 | /* NOT WORKING
|
---|
1457 | char buffer[500];
|
---|
1458 |
|
---|
1459 | nsCAutoString s3(CBufDescriptor(buffer,PR_TRUE,sizeof(buffer)-1,0));
|
---|
1460 | s3="hello, my name is inigo montoya.";
|
---|
1461 | */
|
---|
1462 | //make an autostring that copies an nsString...
|
---|
1463 | nsString s0("eat icecream");
|
---|
1464 | nsCAutoString s4(s0);
|
---|
1465 | nsCAutoString s5(s0.get());
|
---|
1466 | nsString aaa("hi there rick");
|
---|
1467 | #endif
|
---|
1468 | }
|
---|
1469 |
|
---|
1470 | const char* st="hello again";
|
---|
1471 | nsString s; //this also forces nsString to run it's selftest.
|
---|
1472 | s=st;
|
---|
1473 | nsAutoString astr;
|
---|
1474 | astr=st;
|
---|
1475 |
|
---|
1476 | return result;
|
---|
1477 | }
|
---|
1478 |
|
---|
1479 | /**
|
---|
1480 | *
|
---|
1481 | * @update gess10/30/98
|
---|
1482 | * @param
|
---|
1483 | * @return
|
---|
1484 | */
|
---|
1485 | int CStringTester::TestSubsumables(){
|
---|
1486 | int result=0;
|
---|
1487 | char* buf="hello rick";
|
---|
1488 |
|
---|
1489 | return result;
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 | /**
|
---|
1493 | *
|
---|
1494 | * @update gess10/30/98
|
---|
1495 | * @param
|
---|
1496 | * @return
|
---|
1497 | */
|
---|
1498 | int CStringTester::TestRandomOps(){
|
---|
1499 | int result=0;
|
---|
1500 |
|
---|
1501 | #if 0
|
---|
1502 |
|
---|
1503 | char* str[]={"abc ","xyz ","123 ","789 ","*** ","... "};
|
---|
1504 |
|
---|
1505 | string theSTLString;
|
---|
1506 | nsString theString;
|
---|
1507 | nsString thePrevString;
|
---|
1508 |
|
---|
1509 | enum ops {eNOP,eAppend,eDelete,eInsert};
|
---|
1510 | char* opStrs[] = {"nop","append","delete","insert"};
|
---|
1511 |
|
---|
1512 | srand( (unsigned)time( NULL ) );
|
---|
1513 |
|
---|
1514 | int err[] = {1,7,9,6,0,4,1,1,1,1};
|
---|
1515 |
|
---|
1516 | ops theOp=eNOP;
|
---|
1517 | int pos,len,index;
|
---|
1518 |
|
---|
1519 | fstream output("c:/temp/out.file",ios::out);
|
---|
1520 | output<<"dump!";
|
---|
1521 |
|
---|
1522 | for(int theOpIndex=0;theOpIndex<1000000;theOpIndex++){
|
---|
1523 |
|
---|
1524 | int r=rand();
|
---|
1525 | char buf[100];
|
---|
1526 | sprintf(buf,"%i",r);
|
---|
1527 | len=strlen(buf);
|
---|
1528 | index=buf[len-1]-'0';
|
---|
1529 |
|
---|
1530 | //debug... index=err[theOp];
|
---|
1531 |
|
---|
1532 | switch(index) {
|
---|
1533 | case 0:
|
---|
1534 | case 1:
|
---|
1535 | case 2:
|
---|
1536 | theSTLString.append(str[index]);
|
---|
1537 | theString.Append(str[index]);
|
---|
1538 | theOp=eAppend;
|
---|
1539 | break;
|
---|
1540 |
|
---|
1541 | case 3:
|
---|
1542 | case 4:
|
---|
1543 | case 5:
|
---|
1544 | theOp=eInsert;
|
---|
1545 | if (theString.Length()>2) {
|
---|
1546 | pos=theString.Length()/2;
|
---|
1547 | theSTLString.insert(pos,str[index],4);
|
---|
1548 | theString.Insert(str[index],pos);
|
---|
1549 | }
|
---|
1550 | break;
|
---|
1551 |
|
---|
1552 | case 6:
|
---|
1553 | case 7:
|
---|
1554 | case 8:
|
---|
1555 | case 9:
|
---|
1556 | theOp=eDelete;
|
---|
1557 | if(theString.Length()>10) {
|
---|
1558 | len=theString.Length()/2;
|
---|
1559 | pos=theString.Length()/4;
|
---|
1560 | theSTLString.erase(pos,len);
|
---|
1561 | theString.Cut(pos,len);
|
---|
1562 | }
|
---|
1563 | break;
|
---|
1564 |
|
---|
1565 | default:
|
---|
1566 | theOp=eNOP;
|
---|
1567 |
|
---|
1568 | } //for
|
---|
1569 |
|
---|
1570 | if(eNOP<theOp) {
|
---|
1571 |
|
---|
1572 | int lendiff=theString.Length()-theSTLString.length();
|
---|
1573 | PRBool equals=theString.Equals(theSTLString.c_str());
|
---|
1574 | if(((lendiff)) || (!equals)) {
|
---|
1575 | printf("Error in %s at:%i len:%i (%s)!\n",opStrs[theOp],pos,len,str[index]);
|
---|
1576 | output.close();
|
---|
1577 | theString.Equals(theSTLString.c_str());
|
---|
1578 |
|
---|
1579 | if(theOp==eInsert) {
|
---|
1580 | thePrevString.Insert(str[index],pos);
|
---|
1581 | }
|
---|
1582 | else if(theOp==eAppend) {
|
---|
1583 | thePrevString.Append(str[index]);
|
---|
1584 | }
|
---|
1585 | return 0;
|
---|
1586 | }
|
---|
1587 |
|
---|
1588 | #if FOO
|
---|
1589 | output<< opStrs[theOp];
|
---|
1590 | if((eInsert==theOp) || (eAppend==theOp)){
|
---|
1591 | output<< "(" << str[index] << ", " << pos << ") ";
|
---|
1592 | }
|
---|
1593 |
|
---|
1594 | thePrevString.ToCString(buffer,1000,0);
|
---|
1595 | output << " Old: [" << buffer << "]";
|
---|
1596 | theString.ToCString(buffer,1000,0);
|
---|
1597 | output << " New: [" << buffer << "]";
|
---|
1598 | output << " STL: [" << theSTLString.c_str() << "]" << endl;
|
---|
1599 |
|
---|
1600 | if(theString.mStringValue.mLength>300) {
|
---|
1601 | theString.Truncate();
|
---|
1602 | theSTLString.erase();
|
---|
1603 | }
|
---|
1604 | #endif
|
---|
1605 | thePrevString=theString;
|
---|
1606 | }
|
---|
1607 | }
|
---|
1608 | #endif
|
---|
1609 | return result;
|
---|
1610 | }
|
---|
1611 |
|
---|
1612 |
|
---|
1613 | /**
|
---|
1614 | *
|
---|
1615 | * @update gess10/30/98
|
---|
1616 | * @param
|
---|
1617 | * @return
|
---|
1618 | */
|
---|
1619 | int CStringTester::TestReplace(){
|
---|
1620 | int result=0;
|
---|
1621 |
|
---|
1622 | const char* find="..";
|
---|
1623 | const char* rep= "+";
|
---|
1624 |
|
---|
1625 | const char* s1="hello..there..rick..gessner.";
|
---|
1626 | const char* s2="hello+there+rick+gessner.";
|
---|
1627 |
|
---|
1628 | nsCString s(s1);
|
---|
1629 | s.ReplaceSubstring(find,rep);
|
---|
1630 | NS_ASSERTION(s==s2,"ReplaceSubstring error");
|
---|
1631 |
|
---|
1632 | s.ReplaceSubstring(rep,find);
|
---|
1633 | NS_ASSERTION(s==s1,"ReplaceSubstring error");
|
---|
1634 |
|
---|
1635 | return result;
|
---|
1636 | }
|
---|
1637 |
|
---|
1638 |
|
---|
1639 | /**
|
---|
1640 | * This method tests the performance of various methods.
|
---|
1641 | *
|
---|
1642 | *
|
---|
1643 | * @return
|
---|
1644 | */
|
---|
1645 | int CStringTester::TestWideStringPerformance() {
|
---|
1646 |
|
---|
1647 | printf("Widestring performance tests...\n");
|
---|
1648 |
|
---|
1649 | char* libname[] = {"STL","nsString",0};
|
---|
1650 |
|
---|
1651 |
|
---|
1652 | //**************************************************
|
---|
1653 | //Test Construction against STL::wstring...
|
---|
1654 | //**************************************************
|
---|
1655 | {
|
---|
1656 | nsString theConst;
|
---|
1657 | for(int z=0;z<10;z++){
|
---|
1658 | theConst.Append("0123456789");
|
---|
1659 | }
|
---|
1660 |
|
---|
1661 | Stopwatch watch1;
|
---|
1662 | int i;
|
---|
1663 | for(i=0;i<1000000;i++){
|
---|
1664 | nsString s(theConst);
|
---|
1665 | }
|
---|
1666 | watch1.Stop();
|
---|
1667 |
|
---|
1668 | wchar_t wbuf[] = {'a','b','c','d','e','f','g','h','i','j',0};
|
---|
1669 | wstring theConst2;
|
---|
1670 | for(int w=0;w<10;w++){
|
---|
1671 | theConst2.append(wbuf);
|
---|
1672 | }
|
---|
1673 |
|
---|
1674 | Stopwatch watch2;
|
---|
1675 | #ifdef USE_STL
|
---|
1676 | for(i=0;i<1000000;i++){
|
---|
1677 | wstring s(theConst2);
|
---|
1678 | }
|
---|
1679 | #endif
|
---|
1680 | watch2.Stop();
|
---|
1681 |
|
---|
1682 | printf("Construct(abcde) NSString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 |
|
---|
1686 | //**************************************************
|
---|
1687 | //Test append("abcde") against STL::wstring...
|
---|
1688 | //**************************************************
|
---|
1689 | {
|
---|
1690 |
|
---|
1691 | PRUnichar pbuf[10]={'a','b','c','d','e',0};
|
---|
1692 |
|
---|
1693 | Stopwatch watch1;
|
---|
1694 | int i;
|
---|
1695 | for(i=0;i<1000;i++){
|
---|
1696 | nsString s;
|
---|
1697 | for(int j=0;j<200;j++){
|
---|
1698 | s.Append("abcde");
|
---|
1699 | }
|
---|
1700 | }
|
---|
1701 | watch1.Stop();
|
---|
1702 |
|
---|
1703 | wchar_t wbuf[10] = {'a','b','c','d','e',0};
|
---|
1704 |
|
---|
1705 | Stopwatch watch2;
|
---|
1706 | #ifdef USE_STL
|
---|
1707 | for(i=0;i<1000;i++){
|
---|
1708 | wstring s;
|
---|
1709 | for(int j=0;j<200;j++){
|
---|
1710 | s.append(wbuf);
|
---|
1711 | }
|
---|
1712 | }
|
---|
1713 | #endif
|
---|
1714 | watch2.Stop();
|
---|
1715 |
|
---|
1716 | printf("Append(abcde) NSString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1717 | }
|
---|
1718 |
|
---|
1719 | //**************************************************
|
---|
1720 | //Test append(char) against STL::wstring
|
---|
1721 | //**************************************************
|
---|
1722 | {
|
---|
1723 |
|
---|
1724 | Stopwatch watch1;
|
---|
1725 | int i;
|
---|
1726 | for(i=0;i<500;i++){
|
---|
1727 | nsString s;
|
---|
1728 | for(int j=0;j<200;j++){
|
---|
1729 | s.Append('a');
|
---|
1730 | }
|
---|
1731 | }
|
---|
1732 | watch1.Stop();
|
---|
1733 |
|
---|
1734 |
|
---|
1735 | Stopwatch watch2;
|
---|
1736 | #ifdef USE_STL
|
---|
1737 | for(i=0;i<500;i++){
|
---|
1738 | wstring s;
|
---|
1739 | wchar_t theChar('a');
|
---|
1740 | for(int j=0;j<200;j++){
|
---|
1741 | s.append('a',1);
|
---|
1742 | }
|
---|
1743 | }
|
---|
1744 | #endif
|
---|
1745 | watch2.Stop();
|
---|
1746 |
|
---|
1747 | printf("Append('a') NSString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1748 | int x=0;
|
---|
1749 | }
|
---|
1750 |
|
---|
1751 | //**************************************************
|
---|
1752 | //Test insert("123") against STL::wstring
|
---|
1753 | //**************************************************
|
---|
1754 | {
|
---|
1755 |
|
---|
1756 | PRUnichar pbuf1[10]={'a','b','c','d','e','f',0};
|
---|
1757 | PRUnichar pbuf2[10]={'1','2','3',0};
|
---|
1758 |
|
---|
1759 | Stopwatch watch1;
|
---|
1760 | int i;
|
---|
1761 | for(i=0;i<1000;i++){
|
---|
1762 | nsString s("abcdef");
|
---|
1763 | int inspos=3;
|
---|
1764 | for(int j=0;j<100;j++){
|
---|
1765 | s.Insert(pbuf2,inspos);
|
---|
1766 | inspos+=3;
|
---|
1767 | }
|
---|
1768 | }
|
---|
1769 | watch1.Stop();
|
---|
1770 |
|
---|
1771 |
|
---|
1772 | wchar_t wbuf1[10] = {'a','b','c','d','e','f',0};
|
---|
1773 | wchar_t wbuf2[10] = {'1','2','3',0};
|
---|
1774 |
|
---|
1775 | Stopwatch watch2;
|
---|
1776 | #ifdef USE_STL
|
---|
1777 | for(i=0;i<1000;i++){
|
---|
1778 | wstring s(wbuf1);
|
---|
1779 | int inspos=3;
|
---|
1780 | for(int j=0;j<100;j++){
|
---|
1781 | s.insert(inspos,wbuf2);
|
---|
1782 | inspos+=3;
|
---|
1783 | }
|
---|
1784 | }
|
---|
1785 | #endif
|
---|
1786 | watch2.Stop();
|
---|
1787 |
|
---|
1788 | printf("Insert(123) NSString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1789 | int x=0;
|
---|
1790 | }
|
---|
1791 |
|
---|
1792 | //**************************************************
|
---|
1793 | //Let's test substring searching performance...
|
---|
1794 | //**************************************************
|
---|
1795 | {
|
---|
1796 | PRUnichar pbuf1[] = {'a','a','a','a','a','a','a','a','a','a','b',0};
|
---|
1797 | PRUnichar pbuf2[] = {'a','a','b',0};
|
---|
1798 |
|
---|
1799 | nsString s(pbuf1);
|
---|
1800 | nsString target(pbuf2);
|
---|
1801 |
|
---|
1802 | Stopwatch watch1;
|
---|
1803 | int i;
|
---|
1804 | for(i=-1;i<200000;i++) {
|
---|
1805 | PRInt32 result=s.Find(target,PR_FALSE);
|
---|
1806 | }
|
---|
1807 | watch1.Stop();
|
---|
1808 |
|
---|
1809 | Stopwatch watch2;
|
---|
1810 | #ifdef USE_STL
|
---|
1811 | wchar_t wbuf1[] = {'a','a','a','a','a','a','a','a','a','a','b',0};
|
---|
1812 | wchar_t wbuf2[] = {'a','a','b',0};
|
---|
1813 | wstring ws(wbuf1);
|
---|
1814 | wstring wtarget(wbuf2);
|
---|
1815 |
|
---|
1816 | for(i=-1;i<200000;i++) {
|
---|
1817 | PRInt32 result=ws.find(wtarget);
|
---|
1818 | }
|
---|
1819 | #endif
|
---|
1820 | watch2.Stop();
|
---|
1821 |
|
---|
1822 | printf("Find(aab) NSString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1823 | }
|
---|
1824 |
|
---|
1825 | //**************************************************
|
---|
1826 | //Now let's test comparisons...
|
---|
1827 | //**************************************************
|
---|
1828 | {
|
---|
1829 | nsString s("aaaaaaaaaaaaaaaaaaab");
|
---|
1830 | PRUnichar target[]={'a','a','a','a','a','a','a','a','a','a','a','a','a','b',0};
|
---|
1831 | size_t theLen=(sizeof(target)-1)/2;
|
---|
1832 |
|
---|
1833 | Stopwatch watch1;
|
---|
1834 | int result=0;
|
---|
1835 | int i;
|
---|
1836 | for(i=-1;i<1000000;i++) {
|
---|
1837 | result=s.Compare(target,PR_FALSE,theLen);
|
---|
1838 | result++;
|
---|
1839 | }
|
---|
1840 | watch1.Stop();
|
---|
1841 |
|
---|
1842 | Stopwatch watch2;
|
---|
1843 | #ifdef USE_STL
|
---|
1844 | wchar_t buf[]={'a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','a','b',0};
|
---|
1845 | wstring ws(buf);
|
---|
1846 | wchar_t wtarget[]={'a','a','a','a','a','a','a','a','a','a','a','a','a','b',0};
|
---|
1847 |
|
---|
1848 | for(i=-1;i<1000000;i++) {
|
---|
1849 | result=ws.compare(0,theLen,wtarget);
|
---|
1850 | result++;
|
---|
1851 | }
|
---|
1852 | #endif
|
---|
1853 | watch2.Stop();
|
---|
1854 |
|
---|
1855 | printf("Compare(aaaaaaaab) NSString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1856 |
|
---|
1857 | }
|
---|
1858 |
|
---|
1859 | //**************************************************
|
---|
1860 | //Now lets test string deletions...
|
---|
1861 | //**************************************************
|
---|
1862 | {
|
---|
1863 |
|
---|
1864 | int strcount=6000;
|
---|
1865 | int outerIter=100;
|
---|
1866 | int innerIter=100;
|
---|
1867 |
|
---|
1868 | PRUnichar pbuf[] = {'1','2','3','4','5','6','7','8','9','0',0};
|
---|
1869 | nsString source1; //build up our target string...
|
---|
1870 | int i;
|
---|
1871 | for(i=0;i<strcount;i++) {
|
---|
1872 | source1.Append(pbuf);
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | Stopwatch watch1;
|
---|
1876 |
|
---|
1877 | for(i=0;i<outerIter;i++) {
|
---|
1878 | nsString s1(source1);
|
---|
1879 | for(int j=0;j<100;j++){
|
---|
1880 | s1.Cut(20,50);
|
---|
1881 | }
|
---|
1882 | }
|
---|
1883 | watch1.Stop();
|
---|
1884 | printf("Cut(...) NSString: %f ",watch1.Elapsed());
|
---|
1885 |
|
---|
1886 | #ifdef USE_STL
|
---|
1887 |
|
---|
1888 | wchar_t wbuf[] = {'1','2','3','4','5','6','7','8','9','0',0};
|
---|
1889 | wstring source2; //build up our target string...
|
---|
1890 |
|
---|
1891 | for(i=0;i<strcount;i++) {
|
---|
1892 | source2.append(wbuf);
|
---|
1893 | }
|
---|
1894 |
|
---|
1895 | Stopwatch watch2;
|
---|
1896 |
|
---|
1897 | for(i=0;i<outerIter;i++) {
|
---|
1898 | wstring s2(source2);
|
---|
1899 | for(int j=0;j<100;j++){
|
---|
1900 | s2.erase(20,50);
|
---|
1901 | }
|
---|
1902 | }
|
---|
1903 | watch2.Stop();
|
---|
1904 |
|
---|
1905 | printf(" STL: %f",watch2.Elapsed());
|
---|
1906 | #endif
|
---|
1907 | printf("\n");
|
---|
1908 |
|
---|
1909 | }
|
---|
1910 |
|
---|
1911 | //**************************************************
|
---|
1912 | //Now let's test the findChar routine...
|
---|
1913 | //**************************************************
|
---|
1914 | {
|
---|
1915 |
|
---|
1916 | nsString s1;
|
---|
1917 | int i;
|
---|
1918 | for(i=0;i<100;i++) {
|
---|
1919 | s1.Append("1234567890",10);
|
---|
1920 | }
|
---|
1921 | s1+="xyz";
|
---|
1922 |
|
---|
1923 | Stopwatch watch1;
|
---|
1924 | for(i=0;i<100000;i++) {
|
---|
1925 | int f=s1.FindChar('z',PR_FALSE,0);
|
---|
1926 | }
|
---|
1927 | watch1.Stop();
|
---|
1928 | printf("FindChar('z') NSString: %f",watch1.Elapsed());
|
---|
1929 |
|
---|
1930 | #ifdef USE_STL
|
---|
1931 | wchar_t wbuf[] = {'1','2','3','4','5','6','7','8','9','0',0};
|
---|
1932 | wstring s2;
|
---|
1933 | for( i=0;i<100;i++) {
|
---|
1934 | s2.append(wbuf);
|
---|
1935 | }
|
---|
1936 | wchar_t wbuf2[] = {'x','y','z',0};
|
---|
1937 | s2.append(wbuf2);
|
---|
1938 |
|
---|
1939 | Stopwatch watch2;
|
---|
1940 |
|
---|
1941 | for(i=0;i<100000;i++) {
|
---|
1942 | int f=s2.find_first_of('z',0);
|
---|
1943 | }
|
---|
1944 | watch2.Stop();
|
---|
1945 | printf(" STL: %f",watch2.Elapsed());
|
---|
1946 | #endif
|
---|
1947 | printf("\n");
|
---|
1948 |
|
---|
1949 | }
|
---|
1950 | return 0;
|
---|
1951 | }
|
---|
1952 |
|
---|
1953 |
|
---|
1954 | /************************************************************************************************
|
---|
1955 | *
|
---|
1956 | * This method tests the performance of various methods.
|
---|
1957 | *
|
---|
1958 | ************************************************************************************************/
|
---|
1959 | int CStringTester::TestStringPerformance() {
|
---|
1960 | printf("c-String performance tests...\n");
|
---|
1961 |
|
---|
1962 | char* libname[] = {"STL","nsString",0};
|
---|
1963 |
|
---|
1964 | //**************************************************
|
---|
1965 | //Test Construction against STL::wstring...
|
---|
1966 | //**************************************************
|
---|
1967 | {
|
---|
1968 | nsCString theConst;
|
---|
1969 | for(int z=0;z<10;z++){
|
---|
1970 | theConst.Append("0123456789");
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 | Stopwatch watch1;
|
---|
1974 | int i;
|
---|
1975 | for(i=0;i<1000000;i++){
|
---|
1976 | nsCString s(theConst);
|
---|
1977 | }
|
---|
1978 | watch1.Stop();
|
---|
1979 |
|
---|
1980 | char wbuf[] = {'a','b','c','d','e','f','g','h','i','j',0};
|
---|
1981 | string theConst2;
|
---|
1982 | for(int w=0;w<10;w++){
|
---|
1983 | theConst2.append(wbuf);
|
---|
1984 | }
|
---|
1985 |
|
---|
1986 | Stopwatch watch2;
|
---|
1987 | #ifdef USE_STL
|
---|
1988 | for(i=0;i<1000000;i++){
|
---|
1989 | string s(theConst2);
|
---|
1990 | }
|
---|
1991 | #endif
|
---|
1992 | watch2.Stop();
|
---|
1993 |
|
---|
1994 | printf("Construct(abcde) NSCString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | //**************************************************
|
---|
1998 | //Test append("abcde") against STL...
|
---|
1999 | //**************************************************
|
---|
2000 |
|
---|
2001 | {
|
---|
2002 |
|
---|
2003 | Stopwatch watch1;
|
---|
2004 | int i;
|
---|
2005 | for(i=0;i<1000;i++){
|
---|
2006 | nsCString s;
|
---|
2007 | for(int j=0;j<200;j++){
|
---|
2008 | s.Append("abcde",5);
|
---|
2009 | }
|
---|
2010 | }
|
---|
2011 | watch1.Stop();
|
---|
2012 |
|
---|
2013 | Stopwatch watch2;
|
---|
2014 | #ifdef USE_STL
|
---|
2015 | for(i=0;i<1000;i++){
|
---|
2016 | string s;
|
---|
2017 | for(int j=0;j<200;j++){
|
---|
2018 | s.append("abcde",5);
|
---|
2019 | }
|
---|
2020 | }
|
---|
2021 | #endif
|
---|
2022 | watch2.Stop();
|
---|
2023 |
|
---|
2024 | printf("Append(abcde) NSCString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
2025 | int x=0;
|
---|
2026 | }
|
---|
2027 |
|
---|
2028 | //**************************************************
|
---|
2029 | //Test append(char) against STL
|
---|
2030 | //**************************************************
|
---|
2031 | {
|
---|
2032 |
|
---|
2033 | Stopwatch watch1;
|
---|
2034 | int i;
|
---|
2035 | for(i=0;i<500;i++){
|
---|
2036 | nsCString s;
|
---|
2037 | for(int j=0;j<200;j++){
|
---|
2038 | s.Append('a');
|
---|
2039 | }
|
---|
2040 | }
|
---|
2041 | watch1.Stop();
|
---|
2042 |
|
---|
2043 | Stopwatch watch2;
|
---|
2044 | #ifdef USE_STL
|
---|
2045 | for(i=0;i<500;i++){
|
---|
2046 | string s;
|
---|
2047 | wchar_t theChar('a');
|
---|
2048 | for(int j=0;j<200;j++){
|
---|
2049 | s.append('a',1);
|
---|
2050 | }
|
---|
2051 | }
|
---|
2052 | #endif
|
---|
2053 | watch2.Stop();
|
---|
2054 |
|
---|
2055 | printf("Append('a') NSCString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
2056 | int x=0;
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | //**************************************************
|
---|
2060 | //Test insert("123") against STL
|
---|
2061 | //**************************************************
|
---|
2062 | {
|
---|
2063 |
|
---|
2064 | char pbuf1[10]={'a','b','c','d','e','f',0};
|
---|
2065 | char pbuf2[10]={'1','2','3',0};
|
---|
2066 |
|
---|
2067 | Stopwatch watch1;
|
---|
2068 | int i;
|
---|
2069 | for(i=0;i<1000;i++){
|
---|
2070 | nsCString s("abcdef");
|
---|
2071 | int inspos=3;
|
---|
2072 | for(int j=0;j<100;j++){
|
---|
2073 | s.Insert(pbuf2,inspos);
|
---|
2074 | inspos+=3;
|
---|
2075 | }
|
---|
2076 | }
|
---|
2077 | watch1.Stop();
|
---|
2078 |
|
---|
2079 | Stopwatch watch2;
|
---|
2080 | #ifdef USE_STL
|
---|
2081 | for(i=0;i<1000;i++){
|
---|
2082 | string s(pbuf1);
|
---|
2083 | int inspos=3;
|
---|
2084 | for(int j=0;j<100;j++){
|
---|
2085 | s.insert(inspos,pbuf2);
|
---|
2086 | inspos+=3;
|
---|
2087 | }
|
---|
2088 | }
|
---|
2089 | #endif
|
---|
2090 | watch2.Stop();
|
---|
2091 |
|
---|
2092 | printf("insert(123) NSCString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
2093 | int x=0;
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 | //**************************************************
|
---|
2097 | //Let's test substring searching performance...
|
---|
2098 | //**************************************************
|
---|
2099 | {
|
---|
2100 | char *pbuf1 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
|
---|
2101 | "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
---|
2102 | "aab";
|
---|
2103 |
|
---|
2104 | char *pbuf2 = "aab";
|
---|
2105 |
|
---|
2106 | nsCString s(pbuf1);
|
---|
2107 |
|
---|
2108 | Stopwatch watch1;
|
---|
2109 | int i;
|
---|
2110 | for(i=-1;i<20000;i++) {
|
---|
2111 | PRInt32 result=s.Find(pbuf2,PR_FALSE);
|
---|
2112 | }
|
---|
2113 | watch1.Stop();
|
---|
2114 |
|
---|
2115 | Stopwatch watch2;
|
---|
2116 | #ifdef USE_STL
|
---|
2117 | string ws(pbuf1);
|
---|
2118 | for(i=-1;i<20000;i++) {
|
---|
2119 | PRInt32 result=ws.find(pbuf2);
|
---|
2120 | }
|
---|
2121 | #endif
|
---|
2122 | watch2.Stop();
|
---|
2123 |
|
---|
2124 | printf("Find(aab) NSCString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
2125 | }
|
---|
2126 |
|
---|
2127 | //**************************************************
|
---|
2128 | //Now let's test comparisons...
|
---|
2129 | //**************************************************
|
---|
2130 | {
|
---|
2131 | char* target="aaaaaaaaaaaaab";
|
---|
2132 | size_t theLen=strlen(target);
|
---|
2133 |
|
---|
2134 | Stopwatch watch1;
|
---|
2135 | nsCString s("aaaaaaaaaaaaaaaaaaab");
|
---|
2136 | int result=0;
|
---|
2137 | int i;
|
---|
2138 | for(i=-1;i<1000000;i++) {
|
---|
2139 | result=s.Compare(target,PR_FALSE,theLen);
|
---|
2140 | result++;
|
---|
2141 | }
|
---|
2142 | watch1.Stop();
|
---|
2143 |
|
---|
2144 | Stopwatch watch2;
|
---|
2145 | #ifdef USE_STL
|
---|
2146 | string ws("aaaaaaaaaaaaaaaaaaab");
|
---|
2147 | for(i=-1;i<1000000;i++) {
|
---|
2148 | result=ws.compare(0,theLen,target);
|
---|
2149 | result++;
|
---|
2150 | }
|
---|
2151 | #endif
|
---|
2152 | watch2.Stop();
|
---|
2153 |
|
---|
2154 | printf("Compare(aaaaaaaab) NSCString: %f STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
2155 |
|
---|
2156 | }
|
---|
2157 |
|
---|
2158 | //**************************************************
|
---|
2159 | //Now lets test string deletions...
|
---|
2160 | //**************************************************
|
---|
2161 | {
|
---|
2162 |
|
---|
2163 | int strcount=6000;
|
---|
2164 | int outerIter=100;
|
---|
2165 | int innerIter=100;
|
---|
2166 |
|
---|
2167 | char* buffer = "1234567890";
|
---|
2168 | nsCString source1; //build up our target string...
|
---|
2169 | int i;
|
---|
2170 | for(i=0;i<strcount;i++) {
|
---|
2171 | source1.Append(buffer);
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | Stopwatch watch1;
|
---|
2175 | for(i=0;i<outerIter;i++) {
|
---|
2176 | nsCString s1(source1);
|
---|
2177 | for(int j=0;j<innerIter;j++){
|
---|
2178 | s1.Cut(20,50);
|
---|
2179 | }
|
---|
2180 | }
|
---|
2181 | watch1.Stop();
|
---|
2182 | printf("Cut(...) NSCString: %f ",watch1.Elapsed());
|
---|
2183 |
|
---|
2184 | #ifdef USE_STL
|
---|
2185 | string source2; //build up our target string...
|
---|
2186 | for(i=0;i<strcount;i++) {
|
---|
2187 | source2.append(buffer);
|
---|
2188 | }
|
---|
2189 |
|
---|
2190 | Stopwatch watch2;
|
---|
2191 | for(i=0;i<outerIter;i++) {
|
---|
2192 | string s2(source2);
|
---|
2193 | for(int j=0;j<innerIter;j++){
|
---|
2194 | s2.erase(20,50);
|
---|
2195 | }
|
---|
2196 | }
|
---|
2197 | watch2.Stop();
|
---|
2198 |
|
---|
2199 | printf(" STL: %f",watch2.Elapsed());
|
---|
2200 | #endif
|
---|
2201 | printf("\n");
|
---|
2202 | }
|
---|
2203 |
|
---|
2204 |
|
---|
2205 | //**************************************************
|
---|
2206 | //Now let's test the findChar routine...
|
---|
2207 | //**************************************************
|
---|
2208 | {
|
---|
2209 |
|
---|
2210 | nsCString s1;
|
---|
2211 | int i;
|
---|
2212 | for(i=0;i<100;i++) {
|
---|
2213 | s1.Append("1234567890",10);
|
---|
2214 | }
|
---|
2215 | s1+="xyz";
|
---|
2216 |
|
---|
2217 | Stopwatch watch1;
|
---|
2218 | for(i=0;i<100000;i++) {
|
---|
2219 | int f=s1.FindChar('z',PR_FALSE,0);
|
---|
2220 | }
|
---|
2221 | watch1.Stop();
|
---|
2222 | printf("FindChar('z') NSCString: %f ",watch1.Elapsed());
|
---|
2223 |
|
---|
2224 | #ifdef USE_STL
|
---|
2225 | string s2;
|
---|
2226 | for( i=0;i<100;i++) {
|
---|
2227 | s2.append("1234567890");
|
---|
2228 | }
|
---|
2229 | s2.append("xyz");
|
---|
2230 |
|
---|
2231 | Stopwatch watch2;
|
---|
2232 | for(i=0;i<100000;i++) {
|
---|
2233 | int f=s2.find_first_of('z',0);
|
---|
2234 | }
|
---|
2235 | watch2.Stop();
|
---|
2236 | printf(" STL: %f\n",watch1.Elapsed(),watch2.Elapsed());
|
---|
2237 | #endif
|
---|
2238 | printf("\n");
|
---|
2239 |
|
---|
2240 | }
|
---|
2241 | return 0;
|
---|
2242 | }
|
---|
2243 |
|
---|
2244 |
|
---|
2245 | /**
|
---|
2246 | * This method is here as a place to put known regressions...
|
---|
2247 | *
|
---|
2248 | *
|
---|
2249 | * @return
|
---|
2250 | */
|
---|
2251 | int CStringTester::TestRegressions() {
|
---|
2252 |
|
---|
2253 | nsString s("FTP: Netscape</a>");
|
---|
2254 | PRInt32 result=s.Find("</A>",PR_TRUE);
|
---|
2255 |
|
---|
2256 | //this was a known bug...
|
---|
2257 | {
|
---|
2258 | nsString s0("");
|
---|
2259 | PRInt32 pos=s0.RFind("-->");
|
---|
2260 | nsString s1("debug");
|
---|
2261 | pos=s1.RFind("b");
|
---|
2262 | pos=s1.RFind("de",PR_FALSE,1);
|
---|
2263 | pos=10;
|
---|
2264 | }
|
---|
2265 |
|
---|
2266 | //this was a known bug...
|
---|
2267 | {
|
---|
2268 | nsString s0("RDF:RDF");
|
---|
2269 | PRInt32 pos=s0.Find(":");
|
---|
2270 | pos=10;
|
---|
2271 | }
|
---|
2272 |
|
---|
2273 | return result;
|
---|
2274 | }
|
---|
2275 |
|
---|
2276 | #endif
|
---|
2277 |
|
---|