Back to index

lightning-sunbird  0.9+nobinonly
nsStringTest2.h
Go to the documentation of this file.
00001 /********************************************************************************************
00002  *
00003  * MODULES NOTES:
00004  *
00005  *  This file is designed to help test the new nsString classes.
00006  * 
00007  * Contributor(s):
00008  *   Rick Gessner <rickg@netscape.com>
00009  * 
00010  * History:
00011  *
00012  *  02.29.2000: Original files (rickg)
00013  *  03.02.2000: Flesh out the interface to be compatible with old library (rickg)
00014  *  
00015  ********************************************************************************************/
00016 
00017 #ifndef _STRINGTEST2
00018 #define _STRINGTEST2
00019 
00020 
00021 #include "nsString.h"
00022 #include <time.h>
00023 
00024 #define USE_STL 
00025 
00026 #ifdef USE_STL
00027 #include <string>
00028 using namespace std;
00029 #endif
00030 
00031 #define USE_WIDE  1
00032 #ifdef  USE_WIDE
00033   #define stringtype  nsString
00034   #define astringtype nsAutoString
00035   #define chartype    PRUnichar
00036 #else
00037   #define stringtype  nsCString
00038   #define astringtype nsCAutoString
00039   #define chartype    char
00040 #endif
00041 
00042 #include <stdio.h>
00043 const double gTicks = 1.0e-7;
00044 
00045 
00046 
00047 
00048 
00049 /********************************************************
00050   
00051     This class's only purpose in life is to test the 
00052     netscape string library. We exercise the string
00053     API's here, and do a bit of performance testing
00054     against the standard c++ library string (from STL).
00055 
00056  ********************************************************/
00057 class CStringTester {
00058 
00059 public:
00060 
00061        int TestI18nString();
00062     
00063 };
00064 
00065 
00066 
00067 
00068 //test 1: unicode char is stripped correctly using StripChars()
00069 void nsStringTest1(){
00070        PRUnichar test[]={0x0041,0x0051,0x0052,0x0000};
00071        nsString T(test);
00072        PRUnichar result[]={0x0051,0x0052,0x0000};
00073        nsString R(result);
00074        T.StripChars("ABC");
00075        NS_ASSERTION(T.Equals(R), "Test 1: Unicode comparison error");
00076 }
00077 //test 2: unicode char is not matched and stripped when high-order byte is not 0x00
00078 void nsStringTest2(){
00079        PRUnichar test[]={0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00080        nsAutoString T(test);
00081        PRUnichar result[]={0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00082        nsAutoString R(result);
00083        T.StripChars("ABC");
00084        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00085 }
00086        //test 3: unicode char is not matched and stripped when high-order byte is 0xFF
00087 void nsStringTest3(){
00088 
00089        PRUnichar test[] =  {0xFF41,0x4e51,0x4e52,0x4e53,0x0000};
00090        nsAutoString T(test);
00091        PRUnichar result[] =  {0xFF41,0x4e51,0x4e52,0x4e53,0x0000};
00092        nsAutoString R(test);
00093        T.StripChars("ABC");
00094        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00095 }
00096 void nsStringTest4(){
00097        //test 4: unicode char is matched and stripped correctly using StripChar()
00098        PRUnichar test[] =  {0x0041,0x0051,0x0052,0x0000};
00099        nsAutoString T(test);
00100        PRUnichar result[] = {0x0051,0x0052,0x0000};
00101        nsAutoString R(result);
00102        T.StripChar('A');
00103        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00104 }
00105 void nsStringTest5(){
00106        //test 5: unicode char is not matched and stripped when high-order byte is not 0x00
00107 
00108        PRUnichar test[]={0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00109        nsAutoString T(test);
00110        PRUnichar result[]={0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00111        nsAutoString R(result);
00112        T.StripChar('A');
00113        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00114 }
00115 void nsStringTest6(){
00116        //test 6: unicode char is not matched and stripped when high-order byte is 0xFF
00117 
00118        PRUnichar test[] =  {0xFF41,0x4e51,0x4e52,0x4e53,0x0000};
00119        nsAutoString T(test);
00120        PRUnichar result[] =  {0xFF41,0x4e51,0x4e52,0x4e53,0x0000};
00121        nsAutoString R(result);
00122        T.StripChar('A');
00123        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00124 }
00125 void nsStringTest7(){
00126        //test 7: unicode char is matched and replaced correctly using ReplaceChar()
00127        
00128        PRUnichar test[] =  {0x0041,0x0051,0x0052,0x0000};
00129        nsAutoString T(test);
00130        PRUnichar result[] = {0x0050,0x0051,0x0052,0x0000};
00131        nsAutoString R(result);
00132        T.ReplaceChar('A',0x50);
00133        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00134 }
00135 void nsStringTest8(){
00136        //test 8: unicode char is not matched or replaced when high-order byte != 0x00
00137        
00138        PRUnichar test[] =  {0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00139        nsAutoString T(test);
00140        PRUnichar result[] =  {0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00141        nsAutoString R(result);
00142        T.ReplaceChar('A',0x50);
00143        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00144 }
00145 void nsStringTest9(){
00146        //test 9: unicode char is not matched or replaced when high-order byte matches search char
00147        
00148        PRUnichar test[] =  {0x4150,0x4e51,0x4e52,0x4e53,0x0000};
00149        nsAutoString T(test);
00150        PRUnichar result[] =  {0x4150,0x4e51,0x4e52,0x4e53,0x0000};
00151        nsAutoString R(result);
00152        T.ReplaceChar('A',0x50);
00153        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00154 }
00155 void nsStringTest10(){
00156        //test 10: unicode char is not matched or replaced when high-order byte == 0xFF
00157        
00158        PRUnichar test[] =  {0xFFc1,0x4e51,0x4e52,0x4e53,0x0000};
00159        nsAutoString T(test);
00160        PRUnichar result[] =  {0xFFc1,0x4e51,0x4e52,0x4e53,0x0000};
00161        nsAutoString R(result);
00162        T.ReplaceChar('A',0x50);
00163        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00164 }
00165 
00166 void nsStringTest11(){
00167        //test 11: unicode char is correctly replaced when parameter 1 is a string
00168        
00169        PRUnichar test[] =  {0x0041,0x0051,0x0052,0x0000};
00170        nsAutoString T(test);
00171        PRUnichar result[] = {0x0050,0x0051,0x0052,0x0000};
00172        nsAutoString R(result);
00173        T.ReplaceChar("ABC",0x50);
00174        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00175 }
00176 void nsStringTest12(){
00177        //test 12: unicode char is not replaced when high-order byte != 0x00
00178        
00179        PRUnichar test[] =  {0x4e41,0x0051,0x0052,0x0000};
00180        nsAutoString T(test);
00181        PRUnichar result[] =  {0x4e41,0x0051,0x0052,0x0000};
00182        nsAutoString R(result);
00183        T.ReplaceChar("ABC",0x50);
00184        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00185 
00186 }
00187 void nsStringTest13(){
00188        //test 13: unicode char is not replaced when high-order byte matches char in search string
00189        PRUnichar test[] =  {0x4150,0x4e51,0x4e52,0x4e53,0x0000};
00190        nsAutoString T(test);
00191        PRUnichar result[] =  {0x4150,0x4e51,0x4e52,0x4e53,0x0000};
00192        nsAutoString R(result);
00193        T.ReplaceChar("ABC",'T');
00194        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00195 
00196 }
00197 void nsStringTest14(){
00198        PRUnichar test[] =  {0xFFc2,0x4e51,0x4e52,0x4e53,0x0000};
00199        nsAutoString T(test);
00200        PRUnichar result[] =  {0xFFc2,0x4e51,0x4e52,0x4e53,0x0000};
00201        nsAutoString R(result);
00202        T.ReplaceChar("ABC",'T');
00203        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00204 }
00205 void nsStringTest15(){
00206        PRUnichar test[] =  {0xFFc2,0x4e51,0x4e52,0x4e53,0x0000};
00207        nsAutoString T(test);
00208        PRUnichar result[] =  {0xFFc2,0x4e51,0x4e52,0x4e53,0x0000};
00209        nsAutoString R(result);
00210        PRUnichar s = 0xc2; //compiler error on this line
00211        T.ReplaceChar(s,'T');
00212        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00213 }
00214 
00215 void nsStringTest16(){
00216        /* TESTS for ReplaceSubstring()*/
00217        
00218        PRUnichar test[] =  {0x0041,0x0042,0x0043,0x0044,0x0045,0x0000};
00219        nsAutoString T(test);
00220     PRUnichar result[] = {0x0044,0x0045,0x0046,0x0044,0x0045};
00221        nsAutoString R(result);
00222        T.ReplaceSubstring("ABC","DEF");
00223        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00224 
00225 }
00226 void nsStringTest17(){
00227 
00228        PRUnichar test[] =  {0x0041,0x4e42,0x0043,0x0044,0x0045,0x0000};
00229        nsAutoString T(test);
00230        PRUnichar result[] =  {0x0041,0x4e42,0x0043,0x0044,0x0045,0x0000};
00231        nsAutoString R(result);
00232        T.ReplaceSubstring("ABC","DEF");
00233        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00234 }
00235 void nsStringTest18(){
00236         /*TESTS for Trim()*/
00237 
00238        PRUnichar test[] =  {0x0041,0x4e51,0x4e52,0x4e53,0x0000};
00239        nsAutoString T(test);
00240     PRUnichar result[] = {0x4e51,0x4e52,0x4e53,0x0000};
00241        nsAutoString R (result);
00242        T.Trim("ABC");
00243        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00244 }
00245 void nsStringTest19(){
00246        PRUnichar test[] =  {0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00247        nsAutoString T(test);
00248        PRUnichar result[] =  {0x4e41,0x4e51,0x4e52,0x4e53,0x0000};
00249        nsAutoString R(result);
00250        T.Trim("ABC");
00251        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00252 }
00253 void nsStringTest22(){
00254        PRUnichar test[] =  {0x4e51,0x4e52,0x4e53,0x4e41,0x0000};
00255        nsAutoString T(test);
00256        PRUnichar result[] =  {0x4e51,0x4e52,0x4e53,0x4e41,0x0000};
00257        nsAutoString R(result);
00258        T.Trim("ABC");
00259        NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00260 
00261 }
00262 //void nsStringTest23(){
00263 //     PRUnichar test[] =  {0x4e51,0x4e52,0x4e53,0x4e22,0x0000};
00264 //     nsAutoString T(test);
00265 //     PRUnichar s(0x4e22);
00266 //     PRUnichar result[] =  {0x4e51,0x4e52,0x4e53,0x0000};
00267 //     nsAutoString R(result);
00268 //     T.Trim(s,PR_TRUE,PR_TRUE,PR_FALSE);
00269 //     NS_ASSERTION(T.Equals(R), "Unicode comparison error");
00270 //}
00271 void nsStringTest24(){
00272        /*TESTS for Find()*/
00273        PRUnichar test[] =  {0x0041,0x0042,0x0043,0x0051,0x0052,0x0000};
00274        nsAutoString T(test);
00275        NS_ASSERTION(T.Find("ABC") == 0, "Unicode comparison error");
00276 
00277 }
00278 void nsStringTest25(){
00279        PRUnichar test[] =  {0x4e41,0x4e42,0x4e43,0x4e53,0x0000};
00280        nsAutoString T(test);
00281        NS_ASSERTION(T.Find("ABC") == -1, "Unicode comparison error");
00282 
00283 }
00284 void nsStringTest26(){
00285        PRUnichar test[] =  {0xFFc1,0x0051,0x0052,0x0053,0x0000};
00286        nsAutoString T(test);
00287        PRUnichar str[] = {0xc1,0x51,0x52};
00288        nsAutoString S(str);
00289        NS_ASSERTION(T.Find(S) == -1, "Unicode comparison error");
00290 }
00291 void nsStringTest27(){
00292     /*TESTS for FindChar()*/
00293   
00294        PRUnichar test[] =  {0x0041,0x0051,0x0052,0x0000};
00295        nsAutoString T(test);
00296        NS_ASSERTION(T.FindChar('A') == 0, "Unicode comparison error");
00297 }
00298 void nsStringTest28(){
00299        PRUnichar test[] =  {0x4e41,0x4e42,0x4e43,0x4e53,0x0000};
00300        nsAutoString T(test);
00301        NS_ASSERTION(T.FindChar('A') == -1, "Unicode comparison error");
00302 }
00303 void nsStringTest29(){
00304        PRUnichar test[] =  {0xFFc1,0x4e51,0x4e52,0x4e53,0x0000};
00305        nsAutoString T(test);
00306        NS_ASSERTION(T.FindChar(0xc1) == -1, "Unicode comparison error");
00307 
00308 }
00309 void nsStringTest30(){
00310        PRUnichar test[] =  {0x4132,0x0051,0x0052,0x0000};
00311        nsAutoString T(test);
00312        NS_ASSERTION(T.FindChar('A1') == -1, "Unicode comparison error");
00313 }
00314        /*TESTS for FindCharInSet()*/
00315 void nsStringTest31(){
00316        PRUnichar test[] =  {0x0041,0x0051,0x0052,0x0000};
00317        nsAutoString T(test);
00318        NS_ASSERTION(T.FindCharInSet("ABC") == 0, "Unicode comparison error");
00319 }
00320 void nsStringTest32(){
00321        PRUnichar test[] =  {0x4e41,0x4e42,0x4e43,0x4e53,0x0000};
00322        nsAutoString T(test);
00323        NS_ASSERTION(T.FindCharInSet("ABC") == -1, "Unicode comparison error");
00324        
00325 }
00326 void nsStringTest33(){
00327        PRUnichar test[] =  {0xFFc1,0x4e51,0x4e52,0x4e53,0x0000};
00328        nsAutoString T(test);
00329        PRUnichar str[] = {0xc1,0x51,0x52};
00330        nsAutoString s(str);
00331        NS_ASSERTION(T.FindCharInSet(s) == -1, "Unicode comparison error");
00332 }
00333 void nsStringTest34(){
00334        PRUnichar test[] =  {0x4132,0x5132,0x5232,0x0000};
00335        nsAutoString T(test);
00336        NS_ASSERTION(T.FindCharInSet("ABC") == -1, "Unicode comparison error");
00337 }
00338        /*TESTS for RFind()*/
00339 void nsStringTest35(){
00340 
00341        PRUnichar test[] =  {0x0051,0x0052,0x0041,0x0042,0x0043,0x0000};
00342        nsAutoString T(test);
00343        NS_ASSERTION(T.RFind("ABC") == 2, "Unicode comparison error");
00344 }
00345 void nsStringTest36(){
00346        PRUnichar test[] =  {0x4e41,0x4e42,0x4e43,0x4e53,0x0000};
00347        nsAutoString T(test);
00348        NS_ASSERTION(T.RFind("ABC") == -1, "Unicode comparison error");
00349 }
00350 void nsStringTest37(){
00351        PRUnichar test[] =  {0xFFc1,0xFFc2,0xFFc3,0x4e53,0x0000};
00352        nsAutoString T(test);
00353        PRUnichar str[] = {0xc1,0xc2,0xc3};
00354        nsAutoString s(str); 
00355        NS_ASSERTION(T.RFind(s) == -1, "Unicode comparison error");
00356 }
00357        /*TESTS for RFindCharInSet*/
00358 void nsStringTest38(){
00359 
00360        PRUnichar test[] =  {0x0041,0x0042,0x0043,0x0000};
00361        nsAutoString T(test);
00362        int res = T.RFindCharInSet("ABC");
00363        NS_ASSERTION(res==0, "Unicode comparison error");
00364 }
00365 void nsStringTest39(){
00366        PRUnichar test[] =  {0x4e41,0x4e42,0x4e43,0x4e53,0x0000};
00367        nsAutoString T(test);
00368        NS_ASSERTION(T.RFindCharInSet("ABC") == -1, "Unicode comparison error");
00369 }
00370 void nsStringTest40(){
00371        PRUnichar test[] =  {0xFFc1,0x4e51,0x4e52,0x4e53,0x0000};
00372        nsAutoString T(test);
00373        PRUnichar str[] = {0xc1,0xc2,0xc3};
00374        nsAutoString s(str); 
00375        NS_ASSERTION(T.RFindCharInSet(s) == -1, "Unicode comparison error");
00376 }
00377 void nsStringTest41(){
00378        PRUnichar test[] =  {0x4132,0x0051,0x0052,0x0000};
00379        nsAutoString T(test);
00380        NS_ASSERTION(T.RFindCharInSet("ABC") == -1, "Unicode comparison error");
00381 }
00382        /*  TESTS for Compare() */
00383 void nsStringTest42(){
00384        PRUnichar test[] =  {0x0041,0x0042,0x0043,0x0000};
00385        nsAutoString T(test);
00386        NS_ASSERTION(T.Compare("ABC") == 0, "Unicode comparison error");
00387 }                              
00388 void nsStringTest43(){
00389        PRUnichar test[] =  {0xc341,0xc342,0xc343};
00390        nsAutoString T(test);
00391        NS_ASSERTION(T.Compare("ABC") == 1, "Unicode comparison error");
00392 }
00393 
00394 
00395 int CStringTester::TestI18nString(){
00396        nsStringTest1();
00397        nsStringTest2();
00398        nsStringTest3();
00399        nsStringTest4();
00400        nsStringTest5();
00401        nsStringTest6();
00402        nsStringTest7();
00403        nsStringTest8();
00404        nsStringTest9();
00405        nsStringTest10();
00406        nsStringTest11();
00407        nsStringTest12();
00408        nsStringTest13();
00409        nsStringTest14();
00410        nsStringTest15();
00411        nsStringTest16();
00412        nsStringTest17();
00413        nsStringTest18();
00414        nsStringTest19();
00415        //nsStringTest20();
00416        //nsStringTest21();
00417        nsStringTest22();
00418        //nsStringTest23();
00419        nsStringTest24();
00420        nsStringTest25();
00421        nsStringTest26();
00422        nsStringTest27();
00423        nsStringTest28();
00424        nsStringTest29();
00425        nsStringTest30();
00426        nsStringTest31();
00427        nsStringTest32();
00428        nsStringTest33();
00429        nsStringTest34();
00430        nsStringTest35();
00431        nsStringTest36();
00432        nsStringTest37();
00433        nsStringTest38();
00434        nsStringTest39();
00435        nsStringTest40();
00436        nsStringTest41();
00437        nsStringTest42();
00438        nsStringTest43();
00439 
00440        return 0;
00441 }
00442 
00443 #endif
00444