plain text version:tests.csp
1| package tests; 2| 3| class tclass_base 4| { 5| var value; 6| function tclass_base(v) { value = v; } 7| function toString() { return string(value); } 8| } 9| 10| class tclass: tclass_base 11| { 12| function tclass(v) { tclass_base(v); } 13| } 14| 15| function basics_test() { 16| var v = new tclass(4); 17| var v_base = new tclass_base(4); 18| if (!(v instanceof tclass)) throw "instanceof 1"; 19| if (!(v instanceof tclass_base)) throw "instanceof 2"; 20| if (v_base instanceof tclass) throw "instanceof 3"; 21| print("basics test passed! \n"); 22| } 23| 24| function string_test() 25| { 26| var v = new tclass(4); 27| 28| if ( "1" + 2 + "3" != "123" ) throw "default toString in assignmet"; 29| if ( "1" + 2 + "3" + v != "1234" ) throw "user defined toString in assignmet"; 30| 31| var s = new string("0123456789"); 32| if( s[3] != '3' ) throw "item"; 33| 34| // substr test 35| if ( "0123456789".substr(2,3) != "234" ) throw "substr 1"; 36| if ( "0123456789".substr(2,10) != "23456789" ) throw "substr 2"; 37| if ( "0123456789".substr(-2,8) != "01234567" ) throw "substr 3"; 38| if ( "0123456789".substr(2,-8) != "" ) throw "substr 4"; 39| 40| // slice test 41| if ( "0123456789".slice(2,6) != "2345" ) throw "slice 1"; 42| if ( "0123456789".slice(2,-2) != "234567" ) throw "slice 2"; 43| 44| // case tests 45| if ( "Hello World".to_upper() != "HELLO WORLD" ) throw "to_upper"; 46| if ( "Hello World".to_lower() != "hello world" ) throw "to_lover"; 47| 48| // locale test /russian/. tested on win32 platform. others - not yet 49| // locale("Russian"); 50| // if ( "Привет Мир".to_lower() != "привет мир" ) throw "to_lover:" + "Привет Мир".to_lower(); 51| 52| // like test 53| if ( !"Hello world".like("??[a-z]l*") ) throw "like 0"; 54| if ( !"5*".like("5[*]") ) throw "like 1"; 55| if ( !"?n".like("[?]n") ) throw "like 2"; 56| if ( !"a".like("[a-cdf]") && !"f".like("[a-cdf]") ) throw "like 3"; 57| if ( !"-".like("[-acdf]") && !"f".like("[-acdf]") ) throw "like 4"; 58| if ( !"[".like("[[]") ) throw "like 5"; 59| if ( !"abc?d".like("abc[?]d*") ) throw "like 5"; 60| if ( !"abc?de".like("abc[?]d*") ) throw "like 6"; 61| if ( !"abce".like("abc[def]") && !"abcz".like("abc[def]") ) throw "like 7"; 62| if ( !"Hello".like("[a-zA-Z]*") && !"hello".like("[a-zA-Z]*") ) throw "like 8"; 63| // if ( !"Шишига".like("[А-Я]*") ) throw "like 9"; 64| 65| print(string::printf("%d,%.2f,%s\n",123,123.45,"it is a string")); 66| 67| var ts = "1.2.3.4."; 68| if( ts.replace(".","-") != "1-2-3-4-" ) throw "replace " + ts.replace(".","-") ; 69| 70| var sarr = "1,2,3,4,5,6".split(','); 71| if(sarr.length != 6) throw "split 1"; 72| if(int(sarr[0]) != 1 && int(sarr[5]) != 6) throw "split 2"; 73| 74| print("string test passed!\n"); 75| } 76| 77| //compararator function for array sort 78| function less(a,b) { 79| return a < b; 80| } 81| 82| function array_test() 83| { 84| var ar = new array(); 85| var i; 86| // push test 87| for(var j=9;j>=0;j--) ar.push(j); 88| if(ar.length != 10) throw "array.length"; 89| for(i=0;i<10;i++) if(ar[i] != (9-i) ) throw "array.push"; 90| 91| ar[9] = 0; if(ar[9] != 0) throw "array.item"; 92| 93| //slice test 94| var fragment = ar.slice(2,-2); 95| if (fragment.length != 6) throw "array.slice"; 96| 97| // sort test (default) 98| fragment.sort(); 99| for(i=0;i<fragment.length;i++) if(fragment[i] != (i+2) ) throw "array.slice"; 100| 101| // sort test (with user defined function) 102| ar.sort(less); 103| for(i=0;i<10;i++) if(ar[i] != i) throw "array.sort UDF"; 104| 105| // pop test 106| for(i=0;i<10;i++) if(ar.pop() != (9-i)) throw "array.pop order"; 107| if(ar.length != 0) throw "array.pop"; 108| 109| // concat test 110| for(i=0;i<10;i++) ar.push(i); 111| var ins = new array(12,13,14); 112| ar.concat(10,11,ins,15); 113| if(ar.length != 16) throw "array.concat length"; 114| for(i=0;i<ar.length;i++) if(ar[i] != i) throw "array.concat order"; 115| 116| // shift test 117| if(ar.shift() != 0) throw "array.shift return"; 118| if(ar.shift() != 1) throw "array.shift return"; 119| if(ar.length != 14) throw "array.shift length"; 120| for(i=0;i<ar.length;i++) if(ar[i] != (i+2)) throw "array.shift order"; 121| 122| // unshift test 123| ar.unshift(0,1); 124| if(ar.length != 16) throw "array.unshift length"; 125| for(i=0;i<ar.length;i++) if(ar[i] != i) throw "array.unshift order"; 126| 127| print(ar,"\n"); 128| 129| print("array test passed! \n"); 130| } 131| 132| function date_test() { 133| var d = new date (1962,1,18); 134| print ("my birthday is ", d ); 135| print (" wd=",d.day_of_week," yd=",d.day_of_year,"\n"); 136| 137| var mon = d.month; 138| d.day += 31; 139| if( d.month != (mon+1) % 12 ) throw "set day"; 140| 141| d = new date (1962,2,28); 142| d.day += 1; 143| 144| print ("date ", d, "\n" ); 145| 146| print ("time ", d.time, "\n" ); 147| 148| print("date test passed! \n"); 149| } 150| 151| function map_test() { 152| var m = new map(); 153| m.add("one",1); 154| m.add("two",2); 155| m["three"] = 3; 156| 157| //exists test 158| if( !m.exist("one") ) throw "exist"; 159| if( m["two"] != 2 ) throw "item"; 160| 161| // remove test 162| if( m.remove("two") != 2 ) throw "remove 1"; 163| if( m.exist("two") ) throw "remove 2"; 164| 165| // literal test 166| var m1 = { "one":1, "two":2, "three":3 }; 167| if( m1["two"] != 2 ) throw "literal"; 168| 169| print("map test passed! \n"); 170| } 171| 172| function regexp_test() { 173| var pat = "sdfdfsgfdg013alocalAB5163k000la"; 174| var reg = new regexp( "l[a-z]*" ); 175| if(reg.match( pat ) != "local") 176| throw "regexp failed to match!"; 177| 178| out.printf("regexp test passed! \n"); 179| } 180| 181| 182| // Filter TCP/IP addresses 183| const RE_TCP_IP_ADDR_NAME = "[_a-zA-Z0-9\\-]+([\\.]+[_a-zA-Z0-9\\-]+)*"; 184| const RE_TCP_IP_ADDR_IP = "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+"; 185| const RE_TCP_IP_ADDR = "(" + RE_TCP_IP_ADDR_IP + "|" + RE_TCP_IP_ADDR_NAME + ")"; 186| // Filter an e-mail address 187| const RE_EMAIL_ADDR = "[_a-zA-Z0-9\\-\\.]+@(" + RE_TCP_IP_ADDR + ")"; 188| // Filter an unix path 189| const RE_UNIX_PATH = "(/[_a-zA-Z0-9\\-\\.]+)+"; 190| // filters an URL - ftp or http. 191| const RE_URL = "([Ff][Tt][Pp]|[Hh][Tt][Tt][Pp])://(" + RE_TCP_IP_ADDR + ")(:[0-9]+)?(" + RE_UNIX_PATH + ")*"; 192| 193| function regexp_test_2() 194| { 195| var pat = "http://www.terra-informatica.org/c-smile/index.htm; ftp://ftp.terra-informatica.org"; 196| var reg = new regexp( RE_URL ); 197| var matches = reg.split(pat); 198| out.printf( "matching:%s\n", pat); 199| for(var i=0; i < matches.length; i++) 200| out.printf( " match #%d = <%s>\n",i, matches[i] ); 201| 202| reg.compile( RE_TCP_IP_ADDR ); 203| pat = "127.0.0.1 localhost"; 204| out.printf( "matching:%s\n", pat); 205| if(reg.test( pat )) { 206| out.printf( "matched:%s by %s\n", pat, reg); 207| for(var k=0; k < reg.length; k++) 208| out.printf( " element #%d = <%s>\n",k, reg[k] ); 209| } 210| 211| out.printf("regexp 2 test passed! \n"); 212| 213| } 214| 215| 216| function main() 217| { 218| try { 219| basics_test(); 220| string_test(); 221| array_test(); 222| date_test(); 223| map_test(); 224| regexp_test(); 225| regexp_test_2(); 226| print("all tests passed!\n"); 227| } 228| catch(e) { 229| print("ERROR:" + e + "\n"); 230| } 231| }