| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018 |
- #ifndef __SQUILU__
- #include <stdio.h>
- #endif
- //the above is only for syntax check in SquiLu
- #include "minctest.nut"
- local sqt = minctest();
- #ifdef __SQUILU_INT_SZ8__
- print("__SQUILU_INT_SZ8__ defined");
- #endif
- #ifdef __SQUILU_INT_SZ4__
- print("__SQUILU_INT_SZ4__ defined");
- #endif
- #ifdef __SQUILU_FLOAT_SZ8__
- print("__SQUILU_FLOAT_SZ8__ defined");
- #endif
- #ifdef __SQUILU_FLOAT_SZ4__
- print("__SQUILU_FLOAT_SZ4__ defined");
- #endif
- #ifdef __SQUILU_PTR_SZ8__
- print("__SQUILU_PTR_SZ8__ defined");
- #endif
- #ifdef __SQUILU_PTR_SZ4__
- print("__SQUILU_PTR_SZ4__ defined");
- #endif
- print("_versionnumber_", _versionnumber_);
- print("_version_", _version_);
- print("_charsize_", _charsize_);
- print("_intsize_", _intsize_);
- print("_floatsize_", _floatsize_);
- local Is32Bits = _intsize_ == 4;
- //print("Is32Bits", Is32Bits);
- local globals = getroottable();
- sqt.run("closures", function(){
- local A = 0, B = {g=10}
- local f = function(x)
- {
- local a = []
- for(local i=1; i < 1000; ++i)
- {
- local y = 0
- {
- a.append(function () {++B.g; y += x; return y+A;});
- }
- }
- local dummy = function () {return a[A];}
- collectgarbage()
- A = 1;
- sqt.ok(dummy() == a[1]);
- A = 0;
- //print("a[0]", a[0]())
- sqt.ok(a[0]() == x)
- sqt.ok(a[2]() == x)
- collectgarbage()
- sqt.ok(B.g == 12)
- return a
- }
- local a = f(10)
- // testing closures with 'for' control variable
- a = []
- for(local i=0; i < 10; ++i)
- {
- local lv = i;
- a.append({set = function(x){ lv=x; }, get = function (){ return lv; }})
- if( i == 2 ) break;
- }
- sqt.ok(a.len() == 3)
- sqt.ok(a[0].get() == 0)
- a[0].set(10)
- sqt.ok(a[0].get() == 10)
- sqt.ok(a[1].get() == 1)
- a[1].set('a')
- sqt.ok(a[1].get() == 'a')
- //a[2].set(2)
- sqt.ok(a[2].get() == 2)
- a = []
- foreach( i, k in ['a', 'b'])
- {
- local li = i
- local lk = k
- a.append( {set = function(x, y) {li=x; lk=y},
- get = function () {return [li, lk];}} )
- if( i == 2 ) break;
- }
- a[0].set(10, 20)
- local rs = a[1].get()
- sqt.ok(rs[0] == 1 && rs[1] == 'b')
- rs = a[0].get()
- sqt.ok(rs[0] == 10 && rs[1] == 20)
- a[1].set('a', 'b')
- rs = a[1].get()
- sqt.ok(rs[0] == 'a' && rs[1] == 'b')
- // testing closures with 'for' control variable x break
- for(local i=1; i <= 3; ++i)
- {
- local li = i
- f = function () { return li;}
- break
- }
- sqt.ok(f() == 1)
- foreach( k, v in ["a", "b"])
- {
- local lk = k, lv = v
- f = function () {return [lk, lv]; }
- break
- }
- sqt.ok(([f()])[0][0] == 0)
- sqt.ok(([f()])[0][1] == "a")
- // testing closure x break x return x errors
- local b
- f = function (x)
- {
- local first = 1
- while( 1 ) {
- if( x == 3 && ! first ) return
- local la = "xuxu"
- b = function (op, y=0) {
- if( op == "set" )
- la = x+y
- else
- return la
- }
- if( x == 1 ) { break }
- else if( x == 2 ) return
- else if( x != 3 ) throw("x != 3")
- first = null
- }
- }
- for(local i=1; i <= 3; ++i) {
- f(i)
- sqt.ok(b("get") == "xuxu")
- b("set", 10); sqt.ok(b("get") == 10+i)
- b = null
- }
- //pcall(f, 4);
- try{ f(4);}catch(e){}
- sqt.ok(b("get") == "xuxu")
- b("set", 10); sqt.ok(b("get") == 14)
- // testing multi-level closure
- local w
- f = function(x)
- {
- return function (y)
- {
- return function (z) {return w+x+y+z;}
- }
- }
- local y = f(10)
- w = 1.345
- sqt.ok(y(20)(30) == 60+w)
- // test for correctly closing upvalues in tail calls of vararg functions
- local function t ()
- {
- local function c(a,b) {sqt.ok(a=="test" && b=="OK") }
- local function v(f, ...) {c("test", f() != 1 && "FAILED" || "OK") }
- local lx = 1
- return v(function() {return lx;})
- }
- t()
- });
- sqt.run("calls", function(){
- // get the opportunity to test "type" too ;)
- local Klass = class {};
- local aklass = Klass();
- sqt.ok(type(1<2) == "bool")
- sqt.ok(type(true) == "bool" && type(false) == "bool")
- sqt.ok(type(null) == "null")
- sqt.ok(type(-3) == "integer")
- sqt.ok(type(-3.14) == "float")
- sqt.ok(type("x") == "string")
- sqt.ok(type({}) == "table")
- sqt.ok(type(type) == "function")
- sqt.ok(type(Klass) == "class")
- sqt.ok(type(aklass) == "instance")
- sqt.ok(type(sqt.ok) == type(print))
- local f = null
- f = function (x) {return a.x(x);}
- sqt.ok(type(f) == "function")
- // testing local-function recursion
- local fact = false
- {
- local res = 1
- local function lfact (n)
- {
- if( n==0 ) return res
- else return n*lfact(n-1)
- }
- sqt.ok(lfact(5) == 120)
- }
- sqt.ok(fact == false)
- // testing declarations
- local a = {i = 10}
- local self = 20
- a.x <- function(x) {return x+this.i;}
- a.y <- function(x) {return x+self;}
- sqt.ok(a.x(1)+10 == a.y(1))
- a.t <- {i=-100}
- a["t"].x <- function (a,b) {return this.i+a+b;}
- sqt.ok(a.t.x(2,3) == -95)
- {
- local la = {x=0}
- la.add <- function(x) {this.x = this.x+x; la.y <- 20; return this; }
- sqt.ok(la.add(10).add(20).add(30).x == 60 && la.y == 20)
- }
- a = {b={c={}}}
- a.b.c.f1 <- function(x) {return x+1;}
- a.b.c.f2 <- function(x,y) {this[x] <- y;}
- sqt.ok(a.b.c.f1(4) == 5)
- a.b.c.f2("k", 12); sqt.ok(a.b.c.k == 12)
- local t = null // 'declare' t
- f = function(a,b,c=null, e=null) {local d = 'a'; t=[a,b,c,d];}
- f( // this line change must be valid
- 1,2)
- sqt.ok(t[0] == 1 && t[1] == 2 && t[2] == null && t[3] == 'a')
- f(1,2, // this one too
- 3,4)
- sqt.ok(t[0] == 1 && t[1] == 2 && t[2] == 3 && t[3] == 'a')
- function fat(x)
- {
- if( x <= 1 ) return 1
- else return x*loadstring("return fat(" +( x-1) + ")")()
- }
- sqt.ok(loadstring( "loadstring( \"sqt.ok(fat(6)==720)\" )()" ))
- a = loadstring("return [fat(5), 3]")
- a = a()
- sqt.ok(a[0] == 120 && a[1] == 3)
- // fixed-point operator
- local Y = function (le)
- {
- local function la (f)
- {
- return le(function (x) {return f(f)(x);})
- }
- return la(la)
- }
- // non-recursive factorial
- local F = function (f)
- {
- return function (n)
- {
- if( n == 0 ) return 1
- else return n*f(n-1)
- }
- }
- fat = Y(F)
- sqt.ok(fat(0) == 1 && fat(4) == 24 && Y(F)(5)==5*Y(F)(4))
- local function g (z)
- {
- local function lf (a,b,c,d)
- {
- return function (x,y) {return a+b+c+d+a+x+y+z;}
- }
- return lf(z,z+1,z+2,z+3)
- }
- f = g(10)
- sqt.ok(f(9, 16) == 10+11+12+13+10+9+16+10)
- Y, F, f = null
- try { assert(true); sqt.ok(true);} catch(e) {sqt.ok(false);};
- try { assert(true, "opt msg"); sqt.ok(true);} catch(e) {sqt.ok(false);};
- try { assert(false); sqt.ok(false);} catch(e) {sqt.ok(true);};
- try { assert(false, "opt msg"); sqt.ok(false);} catch(e) {sqt.ok(e == "opt msg");};
- local function bugRecursionLocal(str, num=666, car=777){
- if(str == "recurse") return bugRecursionLocal("recurring with recurse", 5);
- return str + num;
- }
- sqt.ok(bugRecursionLocal("dad") == "dad666");
- sqt.ok(bugRecursionLocal("recurse") == "recurring with recurse5");
- local function f1(){ return "f1";}
- local function f2(){ return "f2";}
- local function f3(f=f2){ return "f3" + f();}
- sqt.ok(f3() == "f3f2");
- local function f4(f=f2){ return "f4" + type(f);}
- sqt.ok(f4() == "f4function");
- local ary = ["blue"]
- local function f5(s, f=ary){ return "f5" + s + type(f);}
- sqt.ok(f5("ded") == "f5dedarray");
- local ncount = 0;
- local function nested(p=88, q=66)
- {
- ++ncount;
- local result = ncount + ":" + p + ":" + q;
- local function ni(x=99)
- {
- result += ":" + x;
- }
- ni();
- if(p==88) result += "::" + nested(22);
- return result;
- }
- sqt.ok(nested() == "1:88:66:99::2:22:66:99");
- sqt.ok(nested("n") == "3:n:66:99");
- });
- sqt.run("sort", function(){
- local function check (a, f=null)
- {
- f = f || function (x,y) {return x<y;};
- for(local n=a.len()-1; n > 2; --n) sqt.ok(! f(a[n], a[n-1]))
- }
- local a = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
- "Oct", "Nov", "Dec"]
- a.sort()
- check(a)
- limit <- 30000
- if( table_rawget(globals, "_soft", false) ) limit = 5000
- a = []
- for(local i=0; i < limit; ++i) a.append(math.random())
- local x = os.clock()
- a.sort()
- //print(format("Sorting %d elements in %.2f sec.", limit, os.clock()-x))
- check(a)
- x = os.clock()
- a.sort()
- //print(format("Re-sorting %d elements in %.2f sec.", limit, os.clock()-x))
- check(a)
- a = []
- for(local i=0; i < limit; ++i) a.append(math.random())
- x = os.clock();
- local li=0
- a.sort(function(x,y) {li=li+1; return y<=>x;})
- //print(format("Invert-sorting other %d elements in %.2f sec., with %i comparisons",
- // limit, os.clock()-x, li))
- check(a, function(x,y) {return y<x;})
- a = []
- a.sort() // empty array
- for(local i=0; i < limit; ++i) a.append(false)
- x = os.clock();
- a.sort(function(x,y) {return 0;})
- //print(format("Sorting %d equal elements in %.2f sec.", limit, os.clock()-x))
- check(a, function(x,y) {return 0;})
- foreach( i,v in a) sqt.ok(! v || i=='n' && v==limit)
- a = ["álo", "\0first :-)", "alo", "then this one", "45", "and a new"]
- a.sort()
- check(a)
- });
- sqt.run("pattern matching", function(){
- local re = regexp(@"^[^\n]+");
- sqt.ok(re.match("abc\ndef") == "abc");
- sqt.ok("abc\ndef".match("^[^\n]+") == "abc");
- re = regexp(@"abc..def");
- sqt.ok(re.match("abc\u00b5def") == "abc\u00b5def");
- sqt.ok("abc\u00b5def".match("abc..def") == "abc\u00b5def");
- re = regexp("ab\u00b5\u2704."); //need not use @ to expand \uXXXX
- sqt.ok(re.match("ab\u00b5\u2704x") == "ab\u00b5\u2704x");
- sqt.ok("ab\u00b5\u2704x".match("ab\u00b5\u2704.") == "ab\u00b5\u2704x");
- sqt.ok("aBcaBc" == "abcabc".gsub("b", "B"));
- sqt.ok("aBcaBc" == "abcabc".gsub("b", function(w){return w.toupper(); }));
- sqt.ok("$a$a$" == "#a#a#".gsub("#", "$"));
- sqt.ok("$a$a$" == "#a#a#".gsub("#", function(w){return "$"; }));
- sqt.ok("$$a$$" == "##a##".gsub("##", "$$"));
- sqt.ok("$$a$$" == "##a##".gsub("##", function(w){ return "$$"; }));
- sqt.ok("A" == "a".gsub("a", "A"));
- sqt.ok("A" == "a".gsub("a", function(w){return w.toupper(); }));
- //sqt.ok("<a><><>" == "a".gsub("a", "<%0><%1><%2>"));
- sqt.ok(".h.e.l.l.o." == "hello".gsub("", "."));
- local a, b,c, d, e, f, r, s, t, x
- f = function (s, p)
- {
- local res = []
- if( s.find_lua(p, res) >= 0) return s.slice(res[0], res[1])
- }
- function f1(s, p)
- {
- p = p.gsub("%%([0-9])", function (s) {return "%" + (s.tointeger()+1);})
- p = p.gsub("^(^?)", "%1()", 1)
- p = p.gsub("($?)$", "()%1", 1)
- local tl = []
- s.gmatch(p, function(...){foreach(elm in vargv) tl.append(elm); return true;})
- return s.slice(tl[0], tl[tl.len()-1] )
- }
- a = []
- b = "".find_lua("", a) // empty patterns are tricky
- sqt.ok(a[0] == 0 && a[1] == 0);
- b = "alo".find_lua("", a)
- sqt.ok(a[0] == 0 && a[1] == 0)
- b = "a\0o a\0o a\0o".find_lua("a", a, 0) // first position
- sqt.ok(a[0] == 0 && a[1] == 1)
- b = "a\0o a\0o a\0o".find_lua("a\0o", a, 1) // starts in the midle
- sqt.ok(a[0] == 4 && a[1] == 7)
- b = "a\0o a\0o a\0o".find_lua("a\0o", a, 8) // starts in the midle
- sqt.ok(a[0] == 8 && a[1] == 11)
- b = "a\0a\0a\0a\0\0ab".find_lua("\0ab", a, 1); // finds at the end
- sqt.ok(a[0] == 8 && a[1] == 11);
- b = "a\0a\0a\0a\0\0ab".find_lua("b", a) // last position
- //print(a[0], a[1])
- sqt.ok(a[0] == 10 && a[1] == 11)
- sqt.ok("a\0a\0a\0a\0\0ab".find_lua("b\0") < 0) // check ending
- sqt.ok("".find_lua("\0") < 0)
- sqt.ok("alo123alo".find_lua("12") == 3)
- sqt.ok("alo123alo".find_lua("^12") < 0)
- sqt.ok(f("aloALO", "%l*") == "alo")
- sqt.ok(f("aLo_ALO", "%a*") == "aLo")
- sqt.ok(f("aaab", "a*") == "aaa");
- sqt.ok(f("aaa", "^.*$") == "aaa");
- sqt.ok(f("aaa", "b*") == "");
- sqt.ok(f("aaa", "ab*a") == "aa")
- sqt.ok(f("aba", "ab*a") == "aba")
- sqt.ok(f("aaab", "a+") == "aaa")
- sqt.ok(f("aaa", "^.+$") == "aaa")
- sqt.ok(f("aaa", "b+") == null)
- sqt.ok(f("aaa", "ab+a") == null)
- sqt.ok(f("aba", "ab+a") == "aba")
- sqt.ok(f("a$a", ".$") == "a")
- sqt.ok(f("a$a", ".%$") == "a$")
- sqt.ok(f("a$a", ".$.") == "a$a")
- sqt.ok(f("a$a", "$$") == null)
- sqt.ok(f("a$b", "a$") == null)
- sqt.ok(f("a$a", "$") == "")
- sqt.ok(f("", "b*") == "")
- sqt.ok(f("aaa", "bb*") == null)
- sqt.ok(f("aaab", "a-") == "")
- sqt.ok(f("aaa", "^.-$") == "aaa")
- sqt.ok(f("aabaaabaaabaaaba", "b.*b") == "baaabaaabaaab")
- sqt.ok(f("aabaaabaaabaaaba", "b.-b") == "baaab")
- sqt.ok(f("alo xo", ".o$") == "xo")
- sqt.ok(f(" \n isto é assim", "%S%S*") == "isto")
- sqt.ok(f(" \n isto é assim", "%S*$") == "assim")
- sqt.ok(f(" \n isto é assim", "[a-z]*$") == "assim")
- sqt.ok(f("um caracter ? extra", "[^%sa-z]") == "?")
- sqt.ok(f("", "a?") == "")
- sqt.ok(f("á", "á?") == "á")
- sqt.ok(f("ábl", "á?b?l?") == "ábl")
- sqt.ok(f(" ábl", "á?b?l?") == "")
- sqt.ok(f("aa", "^aa?a?a") == "aa")
- sqt.ok(f("]]]áb", "[^]]") == "á")
- sqt.ok(f("0alo alo", "%x*") == "0a")
- sqt.ok(f("alo alo", "%C+") == "alo alo")
- //print("+")
- sqt.ok(f1("alo alx 123 b\0o b\0o", "(..*) %1") == "b\0o b\0o")
- sqt.ok(f1("axz123= 4= 4 34", "(.+)=(.*)=%2 %1") == "3= 4= 4 3")
- sqt.ok(f1("=======", "^(=*)=%1$") == "=======")
- sqt.ok("==========".match("^([=]*)=%1$") == null)
- local function range (i, j)
- {
- local res_len = j-i
- local res = array(res_len+1)
- local count = 0;
- for(local idx = i; idx <= j; ++idx) res[count++] = idx;
- return res
- }
- local ar = range(0, 255)
- //print(ar.len())
- ar.insert(0, "a");
- local abc = str_from_chars.acall(ar);
- //print("abc.len()", abc.len() , 256);
- sqt.ok(abc.len() == 256)
- function strset (p)
- {
- local res = {s=""}
- abc.gsub(p, function (c) {res.s = res.s + c;})
- return res.s
- }
- sqt.ok(strset("[\xC8-\xD2]").len() == 11)
- sqt.ok(strset("[a-z]") == "abcdefghijklmnopqrstuvwxyz")
- sqt.ok(strset("[a-z%d]") == strset("[%da-uu-z]"))
- sqt.ok(strset("[a-]") == "-a")
- sqt.ok(strset("[^%W]") == strset("[%w]"))
- sqt.ok(strset("[]%%]") == "%]")
- sqt.ok(strset("[a%-z]") == "-az")
- sqt.ok(strset("[%^%[%-a%]%-b]") == "-[]^ab")
- sqt.ok(strset("%Z") == strset("[\x01-\xff]"))
- sqt.ok(strset(".") == strset("[\x01-\xff%z]"))
- //print("+");
- sqt.ok("alo xyzK".match("(%w+)K") == "xyz")
- sqt.ok("254 K".match("(%d*)K") == "")
- sqt.ok("alo ".match("(%w*)$") == "")
- sqt.ok("alo ".match("(%w+)$") == null)
- sqt.ok("(álo)".find_lua("%(á") == 0)
- a = b = c = d = e =null
- "âlo alo".gmatch("^(((.).).* (%w*))$", function(ma, mb, mc, md){ a=ma; b=mb; c=mc; d=md;})
- sqt.ok(a == "âlo alo" && b == "âl" && c == "â" && d == "alo" && e == null)
- a = b = c = d = e =null
- "0123456789".gmatch("(.+(.?)())", function(ma, mb, mc){ a=ma; b=mb; c=mc;})
- sqt.ok(a == "0123456789" && b == "" && c == 10 && d == null)
- //print("+")
- sqt.ok("ülo ülo".gsub("ü", "x") == "xlo xlo")
- sqt.ok("alo úlo ".gsub(" +$", "") == "alo úlo") // trim
- sqt.ok(" alo alo ".gsub("^%s*(.-)%s*$", "%1") == "alo alo") // double trim
- sqt.ok("alo alo \n 123\n ".gsub("%s+", " ") == "alo alo 123 ")
- t = "abç d"
- a = t.gsub("(.)", "%1@")
- sqt.ok("@"+ a == t.gsub("", "@"))
- a = "abçd".gsub("(.)", "%0@", 2)
- sqt.ok(a == "a@b@çd")
- sqt.ok("alo alo".gsub("()[al]", "%1") == "12o 56o")
- sqt.ok("abc=xyz".gsub("(%w*)(%p)(%w+)", "%3%2%1-%0") ==
- "xyz=abc-abc=xyz")
- sqt.ok("abc".gsub("%w", "%1%0") == "aabbcc")
- sqt.ok("abc".gsub("%w+", "%0%1") == "abcabc")
- sqt.ok("áéí".gsub("$", "\0óú") == "áéí\0óú")
- sqt.ok("".gsub("^", "r") == "r")
- sqt.ok("".gsub("$", "r") == "r")
- //print("+")
- sqt.ok("um (dois) tres (quatro)".gsub("(%(%w+%))", function(m) {return m.toupper();}) ==
- "um (DOIS) tres (QUATRO)")
- {
- local function setglobal (n,v) {table_rawset(globals, n, v) }
- "ga=roberto,groberto=ga".gsub("(%w+)=(%w%w*)", setglobal)
- sqt.ok(ga=="roberto" && groberto=="ga")
- }
- f = function (a,b) {return a.gsub(".",b);}
- sqt.ok("trocar tudo em |teste|b| é |beleza|al|".gsub("|([^|]*)|([^|]*)|", f) ==
- "trocar tudo em bbbbb é alalalalalal")
- local function dostring (s) {return (loadstring(s)() || "").tostring();}
- sqt.ok("alo $a<-1$ novamente $return a$".gsub("$([^$]*)%$", dostring) ==
- "alo novamente 1")
- x = "$x<-\"alo\".gsub(\".\", function(m){return m.toupper()})$ assim vai para $return x$".gsub(
- "$([^$]*)%$", dostring)
- sqt.ok(x == " assim vai para ALO")
- t = []
- s = "a alo jose joao"
- r = s.gsub("()(%w+)()", function (a,w,b)
- {
- sqt.ok(w.len() == b-a);
- //print(b-a)
- t.append(b-a);
- return w;
- })
- sqt.ok(s == r && t[0] == 1 && t[1] == 3 && t[2] == 4 && t[3] == 4)
- function isbalanced (s)
- {
- return s.gsub("%b()", "").find_lua("[()]") < 0
- }
- sqt.ok(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
- sqt.ok(! isbalanced("(9 ((8) 7) a b (\0 c) a"))
- sqt.ok("alo 'oi' alo".gsub("%b''", "\"") == "alo \" alo")
- t = ["apple", "orange", "lime"]
- local tn = 0;
- sqt.ok("x and x and x".gsub("x", function (m) {return t[tn++];})
- == "apple and orange and lime")
- t = []
- "first second word".gsub("%w%w*", function (w) {t.append(w); return w;})
- sqt.ok(t[0] == "first" && t[1] == "second" && t[2] == "word" && t.len() == 3)
- t = []
- sqt.ok("first second word".gsub("%w+",
- function (w) {t.append(w); return w;}, 2) == "first second word")
- sqt.ok(t[0] == "first" && t[1] == "second" && t.len() == 2)
- function mygsub(str, p, fnc)
- {
- try {
- return str.gsub(p, fnc);
- } catch(e) {
- //print(e);
- return false;
- }
- }
- //print("testing invalid patterns")
- sqt.ok(! mygsub("alo", "(.", print))
- sqt.ok(! mygsub("alo", ".)", print))
- sqt.ok(! mygsub("alo", "(.", {}))
- sqt.ok(! mygsub("alo", "(.)", "%2"))
- sqt.ok(! mygsub("alo", "(%1)", "a"))
- sqt.ok(! mygsub("alo", "(%0)", "a"))
- // big strings
- a = "a".rep(300000)
- sqt.ok(a.find_lua("^a*.?$") >= 0)
- sqt.ok(a.find_lua("^a*.?b$") < 0)
- sqt.ok(a.find_lua("^a-.?$") >= 0)
- // deep nest of gsubs
- local rev;
- rev = function(s)
- {
- return s.gsub("(.)(.+)", function (c,s1) {return rev(s1) + c;})
- }
- x = "012345".rep(8) // using 9 and above thow max_natcalls
- sqt.ok(rev(rev(x)) == x)
- // gsub with tables
- sqt.ok("alo alo".gsub(".", {}) == "alo alo")
- sqt.ok("alo alo".gsub("(.)", {a="AA", l=""}) == "AAo AAo")
- sqt.ok("alo alo".gsub("(.).", {a="AA", l="K"}) == "AAo AAo")
- //sqt.ok("alo alo".gsub("((.)(.?))", {al="AA", o=false}) == "AAo AAo")
- sqt.ok("alo alo".gsub("().", [2,5,6]) == "256 alo")
- t = {}; table_setdelegate(t, {_get = function (s) {return s.toupper();}})
- sqt.ok("a alo b hi".gsub("%w%w+", t) == "a ALO b HI")
- // tests for gmatch
- //sqt.ok(string.gfind == string.gmatch)
- a = 0
- "abcde".gmatch("()", function(sp) {sqt.ok(sp == a); a=sp+1; return true;})
- sqt.ok(a==5)
- t = []
- "first second word".gmatch("(%w+)", function(w) {t.append(w); return true;})
- sqt.ok(t[0] == "first" && t[1] == "second" && t[2] == "word")
- t = [2, 5, 8]
- "xuxx uu ppar r".gmatch ("()(.)%2", function(sp, ac) {
- sqt.ok(sp == t.remove(0))
- return true
- })
- sqt.ok(t.len() == 0)
- t = {}
- "13 14 10 = 11, 15= 16, 22=23".gmatch("(%d+)%s*=%s*(%d+)",
- function(i,j){ t[i] <- j; return true;})
- a = 0
- foreach( k,v in t) {sqt.ok(k.tointeger()+1 == v.tointeger()); a=a+1;}
- sqt.ok(a == 3)
- // tests for `%f' (`frontiers')
- sqt.ok("aaa aa a aaa a".gsub("%f[%w]a", "x") == "xaa xa x xaa x")
- sqt.ok("[[]] [][] [[[[".gsub("%f[[].", "x") == "x[]] x]x] x[[[")
- sqt.ok("01abc45de3".gsub("%f[%d]", ".") == ".01abc.45de.3")
- sqt.ok("01abc45 de3x".gsub("%f[%D]%w", ".") == "01.bc45 de3.")
- sqt.ok("function".gsub("%f[\x01-\xff]%w", ".") == ".unction")
- sqt.ok("function".gsub("%f[^\x01-\xff]", ".") == "function.")
- local ma = []
- " alo aalo allo".find_lua("%f[%S].-%f[%s].-%f[%S]", ma)
- //print(ma.len(), ma[0], ma[1])
- sqt.ok(ma[0] == 1 && ma[1] == 5)
- local k = " alo aalo allo".match("%f[%S](.-%f[%s].-%f[%S])")
- sqt.ok(k == "alo ")
- a = [0, 4, 8, 13, 16]
- "alo alo th02 is 1hat".gmatch("()%f[%w%d]",
- function(sp) {
- local asp = a.remove(0);
- //print("==", asp, sp);
- sqt.ok(asp == sp);
- return true;
- })
- sqt.ok(a.len() == 0)
- local src_str = "abcdef";
- local re_str = "c()";
- sqt.ok(src_str.match(re_str) == 3);
- re = regexp(re_str);
- sqt.ok(re.match(src_str) == 3);
- });
- sqt.run("regexp", function(){
- /*
- Adapted from
- http://www.theiling.de/cnc/date/2017-12-19.html
- http://www.theiling.de/cnc/regexp/test.c
- */
- const RE_E_OK = 1;
- const RE_E_FAIL = 2;
- const RE_E_SYNTAX = 3;
- const RE_E_NOTIMPL = 3;
- local function re_match(re_str, test_str)
- {
- local result;
- try {
- local re = regexp(re_str);
- result = (re.match(test_str) != null) ? RE_E_OK : RE_E_FAIL;
- } catch(e) {result = RE_E_SYNTAX;}
- return result;
- }
- /*
- print(re_match("aaa", "aaa"));
- print(re_match("aaa", "aab"));
- print(re_match("(a|[(])+", "a(aa((a"));
- print("bac".match("[\\^a]*"));
- */
- sqt.ok(re_match("aaa", "aaa") == RE_E_OK);
- sqt.ok(re_match("aaa", "aab") == RE_E_FAIL);
- sqt.ok(re_match("aaa", "aa") == RE_E_FAIL);
- sqt.ok(re_match("aa", "aaa") == RE_E_OK);
- sqt.ok(re_match("a..", "aaa") == RE_E_OK);
- sqt.ok(re_match("aaa", "a..") == RE_E_FAIL);
- sqt.ok(re_match("a{}", "") == RE_E_SYNTAX);
- sqt.ok(re_match("(aaa)", "aaa") == RE_E_OK);
- sqt.ok(re_match("(aa)a", "aaa") == RE_E_OK);
- sqt.ok(re_match("(aaa", "aaa") == RE_E_SYNTAX);
- sqt.ok(re_match("a+", "") == RE_E_FAIL);
- sqt.ok(re_match("a+", "aaa") == RE_E_OK);
- sqt.ok(re_match(".+", "aaa") == RE_E_OK);
- sqt.ok(re_match("(a)+", "aaa") == RE_E_OK);
- sqt.ok(re_match("(aa)+", "aaaa") == RE_E_OK);
- //sqt.ok(re_match("(aa)+", "aaa") == RE_E_FAIL);
- sqt.ok(re_match("(aa(b)+)+", "aabaabb") == RE_E_OK);
- //sqt.ok(re_match("(aa(b)+)+", "aaabaabb") == RE_E_FAIL);
- //sqt.ok(re_match("(aa(b)+)+", "aaaabb") == RE_E_FAIL);
- sqt.ok(re_match("a+b", "aaaaaaaaaaaaaaaab") == RE_E_OK);
- sqt.ok(re_match("a+b", "aaaaaaaaaaaaaaaa") == RE_E_FAIL);
- sqt.ok(re_match("a?", "a") == RE_E_OK);
- sqt.ok(re_match("a?", "aa") == RE_E_OK);
- sqt.ok(re_match("a?b", "b") == RE_E_OK);
- //sqt.ok(re_match("a?", "") == RE_E_OK);
- //sqt.ok(re_match("(a?)?", "") == RE_E_OK);
- //sqt.ok(re_match("a*", "") == RE_E_OK);
- //sqt.ok(re_match("(a?)+", "") == RE_E_OK);
- //sqt.ok(re_match("?", "") == RE_E_SYNTAX);
- sqt.ok(re_match("a*b", "b") == RE_E_OK);
- //sqt.ok(re_match("(a+)?", "") == RE_E_OK);
- sqt.ok(re_match("a+b", "b") == RE_E_FAIL);
- sqt.ok(re_match("a+b", "ab") == RE_E_OK);
- sqt.ok(re_match("a+b", "aab") == RE_E_OK);
- sqt.ok(re_match("a+b", "aaab") == RE_E_OK);
- sqt.ok(re_match("a{2}b", "ab") == RE_E_FAIL);
- sqt.ok(re_match("a{2}b", "aab") == RE_E_OK);
- sqt.ok(re_match("a{2}b", "aaab") == RE_E_OK);
- sqt.ok(re_match("a{2,}b", "aaab") == RE_E_OK);
- sqt.ok(re_match("a{2,}b", "aab") == RE_E_OK);
- sqt.ok(re_match("a{2,}b", "ab") == RE_E_FAIL);
- sqt.ok(re_match("a*b", "aaab") == RE_E_OK);
- sqt.ok(re_match("a{1}", "a") == RE_E_OK);
- sqt.ok(re_match("a{2}", "a") == RE_E_FAIL);
- sqt.ok(re_match("a{2}", "aa") == RE_E_OK);
- sqt.ok(re_match("a{2}", "aaa") == RE_E_OK);
- sqt.ok(re_match("a{3}", "aa") == RE_E_FAIL);
- sqt.ok(re_match("a{3}", "aaa") == RE_E_OK);
- sqt.ok(re_match("a{3}", "aaaa") == RE_E_OK);
- sqt.ok(re_match("a{2,4}", "a") == RE_E_FAIL);
- sqt.ok(re_match("a{2,4}", "aa") == RE_E_OK);
- sqt.ok(re_match("a{2,4}", "aaa") == RE_E_OK);
- sqt.ok(re_match("a{2,4}", "aaaa") == RE_E_OK);
- sqt.ok(re_match("a{2,4}", "aaaaa") == RE_E_OK);
- sqt.ok(re_match("ab{2,4}", "a") == RE_E_FAIL);
- sqt.ok(re_match("ab{2,4}", "ab") == RE_E_FAIL);
- sqt.ok(re_match("ab{2,4}", "abb") == RE_E_OK);
- sqt.ok(re_match("ab{2,4}", "abbb") == RE_E_OK);
- sqt.ok(re_match("ab{2,4}", "abbbb") == RE_E_OK);
- sqt.ok(re_match("ab{2,4}", "abbbbb") == RE_E_OK);
- sqt.ok(re_match("ab{2,4}", "aabbbb") == RE_E_OK);
- sqt.ok(re_match("a{,2}b{2,4}", "aabbbb") == RE_E_SYNTAX);
- sqt.ok(re_match("a{0,2}b{2,4}", "aabbbb") == RE_E_OK);
- sqt.ok(re_match("a)", "") == RE_E_SYNTAX);
- //sqt.ok(re_match("(a+b+)?", "") == RE_E_OK);
- sqt.ok(re_match("(a{2}b+)*c", "aabc") == RE_E_OK);
- //sqt.ok(re_match("(a{2}b+)*c", "c") == RE_E_OK);
- sqt.ok(re_match("(a{2}b+)*", "aab") == RE_E_OK);
- //sqt.ok(re_match("(a{2}b+)*", "") == RE_E_OK);
- sqt.ok(re_match("(a{2}b+)?", "aab") == RE_E_OK);
- //sqt.ok(re_match("(a{2}b+)?", "") == RE_E_OK);
- sqt.ok(re_match("(ab){2,4}", "a") == RE_E_FAIL);
- sqt.ok(re_match("(ab){2,4}", "b") == RE_E_FAIL);
- sqt.ok(re_match("(ab){2,4}", "") == RE_E_FAIL);
- sqt.ok(re_match("(ab){2,4}", "ab") == RE_E_FAIL);
- sqt.ok(re_match("(ab){2,4}", "abab") == RE_E_OK);
- sqt.ok(re_match("(ab){2,4}", "ababab") == RE_E_OK);
- sqt.ok(re_match("(ab){2,4}", "abababab") == RE_E_OK);
- sqt.ok(re_match("(ab){2,4}", "ababababab") == RE_E_OK);
- sqt.ok(re_match("ab", "ab") == RE_E_OK);
- sqt.ok(re_match("ab", "abab") == RE_E_OK);
- sqt.ok(re_match("(ab)", "ab") == RE_E_OK);
- sqt.ok(re_match("(ab)", "abab") == RE_E_OK);
- sqt.ok(re_match("(ab){1}", "ab") == RE_E_OK);
- sqt.ok(re_match("(ab){1}", "abab") == RE_E_OK);
- sqt.ok(re_match("(ab){1,1}", "ab") == RE_E_OK);
- sqt.ok(re_match("(ab){1,1}", "abab") == RE_E_OK);
- //sqt.ok(re_match("(ab){1}{1,1}", "ab") == RE_E_OK);
- sqt.ok(re_match("(ab){1}{1,1}", "abab") == RE_E_FAIL);
- sqt.ok(re_match("((ab){2,4}c)+", "") == RE_E_FAIL);
- //sqt.ok(re_match("(((ab){2,4}c)+)?", "") == RE_E_OK);
- //sqt.ok(re_match("((ab){2,4}c)+?", "") == RE_E_OK);
- sqt.ok(re_match("((ab){2,4}c)+", "ababc") == RE_E_OK);
- sqt.ok(re_match("((ab){2,4}c)+", "ababcabababc") == RE_E_OK);
- //sqt.ok(re_match("((ab){2,4}c)+?", "ababcabababc") == RE_E_OK);
- //sqt.ok(re_match("((ab){2,4}c)+?", "") == RE_E_OK);
- sqt.ok(re_match("a{0,1}", "a") == RE_E_OK);
- sqt.ok(re_match("a{1,1}", "a") == RE_E_OK);
- sqt.ok(re_match("a{0,}", "a") == RE_E_OK);
- sqt.ok(re_match("a{,}", "a") == RE_E_SYNTAX);
- sqt.ok(re_match("a{,}", "a") == RE_E_SYNTAX);
- sqt.ok(re_match("a{,g}", "a") == RE_E_SYNTAX);
- sqt.ok(re_match("a{,1g}", "a") == RE_E_SYNTAX);
- sqt.ok(re_match("a{0,g}", "a") == RE_E_SYNTAX);
- sqt.ok(re_match("a{0,1g}", "a") == RE_E_SYNTAX);
- //sqt.ok(re_match("a{1,0}", "a") == RE_E_SYNTAX);
- //sqt.ok(re_match("a{1,0}", "a") == RE_E_SYNTAX);
- //sqt.ok(re_match("a{0}", "a") == RE_E_FAIL);
- //sqt.ok(re_match("a{0,0}", "a") == RE_E_FAIL);
- //sqt.ok(re_match("a++", "aaa") == RE_E_OK);
- //sqt.ok(re_match("a??", "") == RE_E_OK);
- //sqt.ok(re_match("a**", "") == RE_E_OK);
- //sqt.ok(re_match("a?*", "") == RE_E_OK);
- //sqt.ok(re_match("a*?", "") == RE_E_OK);
- sqt.ok(re_match("a?", "a") == RE_E_OK);
- //sqt.ok(re_match("a??", "a") == RE_E_OK);
- //sqt.ok(re_match("a**", "a") == RE_E_OK);
- //sqt.ok(re_match("a?*", "a") == RE_E_OK);
- //sqt.ok(re_match("a*?", "a") == RE_E_OK);
- sqt.ok(re_match("a?", "aa") == RE_E_OK);
- //sqt.ok(re_match("a??", "aa") == RE_E_FAIL);
- //sqt.ok(re_match("a**", "aa") == RE_E_OK);
- //sqt.ok(re_match("a?*", "aa") == RE_E_OK);
- //sqt.ok(re_match("a*?", "aa") == RE_E_OK);
- //sqt.ok(re_match("a+?", "") == RE_E_OK);
- //sqt.ok(re_match("a?+", "") == RE_E_OK);
- //sqt.ok(re_match("a+?b", "b") == RE_E_OK);
- sqt.ok(re_match("a\\rb", "a\rb") == RE_E_OK);
- sqt.ok(re_match("a+\n+b+", "aa\n\nbb") == RE_E_OK);
- sqt.ok(re_match("a+\\n+b+", "aa\n\nbb") == RE_E_OK);
- //sqt.ok(re_match("a+\\5+b+", "aa\n\nbb") == RE_E_SYNTAX);
- sqt.ok(re_match("a+\\(+b+", "aa((bb") == RE_E_OK);
- sqt.ok(re_match("[a]+", "a") == RE_E_OK);
- sqt.ok(re_match("[a]+", "aa") == RE_E_OK);
- sqt.ok(re_match("[a]+", "aaa") == RE_E_OK);
- sqt.ok(re_match("[ba]+", "a") == RE_E_OK);
- sqt.ok(re_match("[ba]+", "ab") == RE_E_OK);
- sqt.ok(re_match("[ba]+", "baaa") == RE_E_OK);
- sqt.ok(re_match("[ba]+", "c") == RE_E_FAIL);
- sqt.ok(re_match("[\\n]+", "\n") == RE_E_OK);
- sqt.ok(re_match("[\\n]+", "\n\n") == RE_E_OK);
- sqt.ok(re_match("[\\n]+", "\n\n\n") == RE_E_OK);
- sqt.ok(re_match("[a-c]+", "aaa") == RE_E_OK);
- sqt.ok(re_match("[a-c]+", "ccc") == RE_E_OK);
- sqt.ok(re_match("[a-c]+", "bbb") == RE_E_OK);
- sqt.ok(re_match("[a", "bbb") == RE_E_SYNTAX);
- sqt.ok(re_match("[a-", "bbb") == RE_E_SYNTAX);
- sqt.ok(re_match("[a-b", "bbb") == RE_E_SYNTAX);
- sqt.ok(re_match("[a-\\b", "bbb") == RE_E_NOTIMPL);
- //sqt.ok(re_match("a\\bb", "bbb") == RE_E_NOTIMPL);
- sqt.ok(re_match("[a-\\n", "bbb") == RE_E_NOTIMPL);
- sqt.ok(re_match("[\\n-a", "bbb") == RE_E_NOTIMPL);
- //sqt.ok(re_match("[-]", "bbb") == RE_E_NOTIMPL);
- //sqt.ok(re_match("[a-b-c]", "bbb") == RE_E_NOTIMPL);
- sqt.ok(re_match("[a--]", "bbb") == RE_E_NOTIMPL);
- //sqt.ok(re_match("[a-z-[bc]]", "bbb") == RE_E_NOTIMPL);
- sqt.ok(re_match("0[xX][\\da-fA-F]{1,4}", "0x12a6") == RE_E_OK);
- //sqt.ok(re_match("0[xX][\\da-fA-F]{1,4}", "0x12ag") == RE_E_FAIL);
- sqt.ok(re_match("(a|c)+", "aa") == RE_E_OK);
- sqt.ok(re_match("(a|c)+", "cc") == RE_E_OK);
- sqt.ok(re_match("(a|c)+", "cccc") == RE_E_OK);
- sqt.ok(re_match("(a|c)+", "aaaa") == RE_E_OK);
- sqt.ok(re_match("(a|c)+", "ca") == RE_E_OK);
- sqt.ok(re_match("(a|c)+", "caca") == RE_E_OK);
- sqt.ok(re_match("(a|b|c|d)+", "aa") == RE_E_OK);
- sqt.ok(re_match("(a|b|c|d)+", "bb") == RE_E_OK);
- sqt.ok(re_match("(a|b|c|d)+", "cc") == RE_E_OK);
- sqt.ok(re_match("(a|b|c|d)+", "dd") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "aa") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "bb") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "cc") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "abc") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "cba") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "cccc") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "aa") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "aaaa") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "ca") == RE_E_OK);
- sqt.ok(re_match("(a|b|c)+", "caca") == RE_E_OK);
- sqt.ok(re_match("(ab|cd|ed)+", "abcded") == RE_E_OK);
- //sqt.ok(re_match("(?:ab)", "a") == RE_E_SYNTAX);
- //sqt.ok(re_match("[^a]*", "") == RE_E_OK);
- sqt.ok(re_match("[^a]+", "") == RE_E_FAIL);
- sqt.ok(re_match("[^a]*", "b") == RE_E_OK);
- sqt.ok(re_match("[^a]*", "bcbd") == RE_E_OK);
- //sqt.ok(re_match("[^a]*", "a") == RE_E_FAIL);
- sqt.ok(re_match("[^a]*", "bac") == RE_E_OK);
- sqt.ok(re_match("[^ab]*", "bac") == RE_E_OK);
- sqt.ok(re_match("[^ab]*", "cded") == RE_E_OK);
- sqt.ok(re_match("[\\^a]*", "bac") == RE_E_OK);
- sqt.ok(re_match("[\\^a]*", "a") == RE_E_OK);
- sqt.ok(re_match("[\\^a]*", "a^") == RE_E_OK);
- sqt.ok(re_match("(a|[\\(])+", "a(aa((a") == RE_E_OK);
- sqt.ok(re_match("(a|[\\[])+", "a[aa[[a") == RE_E_OK);
- sqt.ok(re_match("(a|[(])+", "a(aa((a") == RE_E_OK);
- sqt.ok(re_match("(a|[[])+", "a(aa((a") == RE_E_OK);
- sqt.ok(re_match("\\D*", "bac") == RE_E_OK);
- sqt.ok(re_match("\\D*", "b0c") == RE_E_OK);
- sqt.ok(re_match("\\S*", "bac") == RE_E_OK);
- sqt.ok(re_match("\\S*", "b c") == RE_E_OK);
- sqt.ok(re_match(".", "") == RE_E_FAIL);
- sqt.ok(re_match("a.b", "acb") == RE_E_OK);
- sqt.ok(re_match("a.b", "a\nb") == RE_E_OK);
- });
- sqt.run("table", function(){
- local t = table_new(10);
- local t2 = obj_clone(t);
- sqt.ok((type(t2) == "table") && (t != t2));
- sqt.ok(table_len(t) == 0);
- table_rawset(t, "one", 1);
- sqt.ok(table_len(t) == 1 && (table_get(t, "one") == 1));
- table_set(t, "one", 10);
- sqt.ok((table_get(t, "one") == 10));
- table_rawset(t, "two", 2);
- sqt.ok(table_len(t) == 2 && (table_get(t, "two") == 2));
- local ary = table_toarray(t);
- sqt.ok(type(ary) == "array");
- sqt.ok(ary.len() == table_len(t));
- sqt.ok(type(ary[0]) == "array");
- sqt.ok(type(ary[1]) == "array");
- sqt.ok(ary[0][1] == table_rawget(t, ary[0][0]));
- sqt.ok(ary[1][1] == table_rawget(t, ary[1][0]));
- table_rawdelete(t, "one");
- sqt.ok(table_len(t) == 1 && (table_get(t, "two") == 2));
- sqt.ok(table_rawin(t, "two"));
- table_clear(t);
- sqt.ok(table_len(t) == 0);
- sqt.ok({ abc = "abc" } != { abc = "abc" });
- });
- sqt.run("array", function(){
- local t = array(0);
- local t2 = obj_clone(t);
- sqt.ok((type(t2) == "array") && (t != t2));
- sqt.ok(t.len() == 0);
- t.append(1);
- sqt.ok(t.len() == 1 && (t.get(0) == 1));
- t.clear();
- sqt.ok(t.len() == 0);
- sqt.ok([ 0, 1, 2 ] != [ 0, 1, 2 ]);
- t = ["one"];
- sqt.ok(t.concat(",") == "one");
- t = ["one", "two"];
- sqt.ok(t.concat(", ") == "one, two");
- sqt.ok(t.len() == 2);
- sqt.ok(t.capacity() == 2);
- t.reserve(100);
- sqt.ok(t.len() == 2);
- sqt.ok(t.capacity() == 100);
- t.clear();
- sqt.ok(t.len() == 0);
- sqt.ok(t.capacity() == 100);
- t.extend([1,2,3,4,5,6,7,8,9,10]);
- sqt.ok(t.len() == 10);
- sqt.ok(t.capacity() == 100);
- t.minsize(5);
- sqt.ok(t.len() == 10);
- sqt.ok(t.capacity() == 100);
- t.resize(5);
- sqt.ok(t.len() == 5);
- sqt.ok(t.capacity() == t.len());
- if(table_rawget(globals, "array_float32", false))
- {
- local function testArray(arr, sz, val)
- {
- local val2 = val * 2;
- local val3 = val * 3;
- arr.append(val2);
- arr.append(val);
- arr.append(val3);
- sqt.ok(arr.len() == 3 && (arr.get(1) == val));
- arr.sort();
- sqt.ok((arr[0] == val) && (arr[1] == val2) && (arr[2] == val3));
- arr.clear();
- sqt.ok(arr.len() == 0);
- sqt.ok(arr.sizeofelm() == sz);
- local asize = 20;
- arr.resize(asize);
- for(local i=0; i < asize; ++i) arr[i] = i+asize;
- local arr2 = obj_clone(arr);
- for(local i=0; i < asize; ++i) sqt.ok(arr[i] == arr2[i]);
- sqt.ok(arr.sizeofelm() == arr2.sizeofelm());
- local arr3 = arr2.slice(2, 18);
- sqt.ok(arr3.sizeofelm() == arr2.sizeofelm());
- sqt.ok(arr3[0] == arr2[2]);
- sqt.ok(arr3.top() == arr2[17]);
- try { arr.resize(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
- try { arr.reserve(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
- }
- if(_intsize_ == 8)
- {
- t = array_int64(0);
- testArray(t, 8, 1);
- }
- if(_floatsize_ == 8)
- {
- t = array_float64(0);
- testArray(t, 8, 1.0);
- }
- t = array_float32(0);
- testArray(t, 4, 1.0);
- t = array_int32(0);
- testArray(t, 4, 1);
- t = array_int16(0);
- testArray(t, 2, 1);
- t = array_int8(0);
- testArray(t, 1, 1);
- }
- try { t.resize(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
- try { t.reserve(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
- });
- // core/string/index_of_start.wren
- sqt.run("string", function(){
- sqt.ok( ("a".toupper() + "A".tolower()) == "Aa");
- sqt.ok( ("à ".sl_upper() + "À".sl_lower()) == "Àà ");
- sqt.ok(type("s") == "string");
- sqt.ok("a" + "b" == "ab");
- local str = "abcde";
- sqt.ok(str.slice(0, 0) == "");
- sqt.ok(str.slice(1, 1) == "");
- sqt.ok(str.slice(1, 3) == "bc");
- sqt.ok(str.slice(1, 2) == "b");
- sqt.ok(str.slice(2) == "cde");
- sqt.ok(str.slice(2, 5) == "cde");
- //sqt.ok(str.slice(3, 1) == "dcb");
- //sqt.ok(str.slice(3, 1) == "dc");
- sqt.ok(str.slice(3, 3) == "");
- sqt.ok(str.slice(-5, -2) == "abc");
- //sqt.ok(str.slice(-3, -5) == "cba");
- //sqt.ok(str.slice(-3, -6) == "cba");
- sqt.ok(str.slice(-5, 3) == "abc");
- sqt.ok(str.slice(-3, 5) == "cde");
- //sqt.ok(str.slice(-2, 1) == "dcb");
- //sqt.ok(str.slice(-2, 0) == "dcb");
- sqt.ok(str.slice(1, -2) == "bc");
- sqt.ok(str.slice(2, -1) == "cd");
- //sqt.ok(str.slice(4, -5) == "edcba");
- //sqt.ok(str.slice(3, -6) == "dcba");
- sqt.ok("".slice(0, 0) == "");
- //sqt.ok("".slice(0, -1) == "");
- sqt.ok("abc".slice(3, 3) == "");
- //sqt.ok("abc".slice(3, -1) == "");
- sqt.ok("søméஃthîng".slice(0, 3) == "sø");
- sqt.ok("søméஃthîng".slice(3, 10) == "méஃt");
- sqt.ok("søméஃthîng".slice(3, 9) == "méஃ");
- sqt.ok("søméஃthîng".slice(3, 6) == "mé");
- sqt.ok("".tostring() == "");
- sqt.ok("blah".tostring() == "blah");
- sqt.ok("a\0b\0c".tostring() == "a\0b\0c");
- sqt.ok("a\0b\0c".tostring() != "a");
- sqt.ok("abcd"[0] == 'a');
- sqt.ok("abcd"[1] == 'b');
- sqt.ok("abcd"[2] == 'c');
- sqt.ok("abcd"[3] == 'd');
- sqt.ok("abcd"[-4] == 'a');
- sqt.ok("abcd"[-3] == 'b');
- sqt.ok("abcd"[-2] == 'c');
- sqt.ok("abcd"[-1] == 'd');
- sqt.ok("abcd"[1] == 'b');
- sqt.ok("søméஃthîng"[0] == 's');
- //sqt.ok("søméஃthîng"[1] == 'ø');
- sqt.ok("søméஃthîng"[3] == 'm');
- //sqt.ok("søméஃthîng"[6] == 'ஃ');
- sqt.ok("søméஃthîng"[10] == 'h');
- sqt.ok("søméஃthîng"[-1] == 'g');
- sqt.ok("søméஃthîng"[-2] == 'n');
- //sqt.ok("søméஃthîng"[-4] == 'î');
- sqt.ok("søméஃthîng"[2] == '\xb8');
- sqt.ok("søméஃthîng"[7] == '\xae');
- sqt.ok("søméஃthîng"[8] == '\x83');
- sqt.ok("søméஃ"[-1] == '\x83');
- sqt.ok("søméஃ"[-2] == '\xae');
- sqt.ok("a\0b\0c"[0] == 'a');
- sqt.ok("a\0b\0c"[1] == '\0');
- sqt.ok("a\0b\0c"[2] == 'b');
- sqt.ok("a\0b\0c"[3] == '\0');
- sqt.ok("a\0b\0c"[4] == 'c');
- sqt.ok("\xef\xf7"[0] == '\xef');
- sqt.ok("\xef\xf7"[1] == '\xf7');
- //sqt.ok("something".split("meth") == ["so", "ing"]);
- //sqt.ok("something".split("some") == ["thing"]);
- //sqt.ok("something".split("math") == ["something"]);
- //sqt.ok("somethingsomething".split("meth") == ["so", "ingso", "ing"]);
- //sqt.ok("abc abc abc".split(" ") == ["abc", "abc", "abc"]);
- //sqt.ok("abcabcabc".split("abc") == [, , , ]);
- //sqt.ok("søméthîng".split("meth") == ["søméthîng"]);
- //sqt.ok("a\0b\0c".split("\0") == ["a", "b", "c"]);
- sqt.ok("something".replace("some", "no") == "nothing");
- sqt.ok("something".replace("thing", "one") == "someone");
- sqt.ok("something".replace("math", "ton") == "something");
- sqt.ok("somethingsomething".replace("some", "no") == "nothingnothing");
- sqt.ok("abc abc abc".replace(" ", "") == "abcabcabc");
- sqt.ok("søméthîng".replace("meth", "ton") == "søméthîng");
- sqt.ok("a\0b\0c".replace("\0", "") == "abc");
- sqt.ok("a\0b\0c".replace("b", "") == "a\0\0c");
- sqt.ok(!"s" == false);
- sqt.ok(!"" == false);
- sqt.ok("a string".len() == 8);
- //sqt.ok("søméஃthîng".len() == 10);
- sqt.ok("\xefok\xf7".len() == 4);
- sqt.ok("abcd".startswith("cd") == false);
- sqt.ok("abcd".startswith("a") == true);
- sqt.ok("abcd".startswith("abcd") == true);
- sqt.ok("abcd".startswith("abcde") == false);
- sqt.ok("abcd".startswith("") == true);
- sqt.ok("søméthîng".startswith("some") == false);
- sqt.ok("abcd".endswith("cd") == true);
- sqt.ok("abcd".endswith("abcde") == false);
- sqt.ok("abcd".endswith("abcd") == true);
- sqt.ok("abcd".endswith("f") == false);
- sqt.ok("abcd".endswith("") == true);
- sqt.ok("søméthîng".endswith("thing") == false);
- sqt.ok("vålue" != "value");
- sqt.ok("vålue" == "vålue");
- sqt.ok("a\0b\0c" != "a");
- sqt.ok("a\0b\0c" != "abc");
- sqt.ok("a\0b\0c" == "a\0b\0c");
- sqt.ok("abcd".indexOf("") == 0);
- sqt.ok("abcd".indexOf("cd") == 2);
- sqt.ok("abcd".indexOf("a") == 0);
- sqt.ok("abcd".indexOf("abcd") == 0);
- sqt.ok("abcd".indexOf("abcde") == -1);
- sqt.ok("abab".indexOf("ab") == 0);
- sqt.ok("abcdefabcdefg".indexOf("defg") == 9);
- sqt.ok("abcdabcdabcd".indexOf("dab") == 3);
- sqt.ok("abcdabcdabcdabcd".indexOf("dabcdabc") == 3);
- sqt.ok("abcdefg".indexOf("abcdef!") == -1);
- sqt.ok("søméஃthîng".indexOf("e") == -1);
- sqt.ok("søméஃthîng".indexOf("m") == 3);
- sqt.ok("søméஃthîng".indexOf("thî") == 9);
- //sqt.ok("a\0b\0c".indexOf("\0") == 1);
- sqt.ok("a\0b\0c".indexOf("a") == 0);
- //sqt.ok("a\0b\0c".indexOf("b\0c") == 2);
- //sqt.ok("a\0b\0c".indexOf("a\0b\0c\0d") == -1);
- sqt.ok("a\0b\0a\0b".indexOf("a\0b") == 0);
- sqt.ok("abcd".indexOf("") == 0);
- sqt.ok("aaaaa".indexOf("aaaa") == 0);
- sqt.ok("abcd".indexOf("cd") == 2);
- //sqt.ok("here as well as here".indexOf("here") == 16);
- //sqt.ok("a\0b\0c".indexOf("\0") == 1);
- sqt.ok("a\0b\0c".indexOf("a") == 0);
- //sqt.ok("a\0b\0c".indexOf("a\0b\0c\0d") == -1);
- sqt.ok("a\0b\0a\0b".indexOf("a\0b") == 0);
- local base_str = "one two"
- str = "one \
- two";
- sqt.ok(base_str == str);
- str = "one "
- "two";
- sqt.ok(base_str == str);
- sqt.ok(@"foo
- bar" == "foo\nbar");
- sqt.ok("foo \
- bar" == "foo bar");
- sqt.ok("foo "
- "bar" == "foo bar");
- sqt.ok(R"(foo
- bar)" == "foo\nbar");
- sqt.ok([==[foo
- bar]==] == "foo\nbar");
- sqt.ok(("(" + 0x7fffffff + ")") == "(2147483647)");
- sqt.ok(("(" + -0x80000000 + ")") == "(-2147483648)");
- //C/C++ wchar
- sqt.ok(L"fake wchar" == "fake wchar");
- sqt.ok(L'\n' == '\n');
- //utf8
- sqt.ok("aà eèiìoòuù".isvalidutf8());
- sqt.ok("aà eèiìoòuù".utf8Len() == 10);
- sqt.ok("aà eèiìoòuù".len() == 15);
- sqt.ok('\u0031' == 49);
- sqt.ok('\u0531' == 1329);
- });
- sqt.run("number", function(){
- sqt.ok(0.0f == 0.0); //C/C++ notation
- //adapetd from pike https://github.com/pikelang/Pike
- sqt.ok(1e1 == 10.0);
- sqt.ok(1E1 == 10.0);
- sqt.ok(1e+1 == 10.0);
- sqt.ok(1.1e1 == 11.0);
- sqt.ok(1e-1 == 0.1);
- sqt.ok('\x20' == 32);
- sqt.ok("\x20" == "\040");
- //sqt.ok("\d32" == "\x20");
- sqt.ok('Å' == "Å"[0]);
- //sqt.ok('\7777' == "\7777"[0]);
- //sqt.ok("\77777777" == "\77777777"[0]);
- sqt.ok("\x10000" == "\x10000");
- sqt.ok(0x80000000-0x80000000 == 0);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok(0xf0000000-0xf0000000 == 0);
- #endif
- sqt.ok(0x80000001-0x80000000 == 1);
- sqt.ok(0x80000000-0x80000001 == -1);
- sqt.ok(-2147483648*-1 == -2147483648/-1);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok(0x8000000000000000-0x8000000000000000 == 0);
- //sqt.ok(0xf000000000000000-0xf000000000000000 == 0);
- sqt.ok(0x8000000000000001-0x8000000000000000 == 1);
- sqt.ok(0x8000000000000000-0x8000000000000001 == -1);
- sqt.ok(-9223372036854775808*-1 == -9223372036854775808/-1);
- #endif
- // https://github.com/pikelang/Pike
- sqt.ok(15 == "0x0F".tointeger(16));
- sqt.ok(15 == "F".tointeger(16));
- sqt.ok(15 == "017".tointeger(8));
- sqt.ok(15 == "17".tointeger(8));
- sqt.ok(15 == "1111".tointeger(2));
- sqt.ok(15 == "00001111".tointeger(2));
- sqt.ok("0.5".tofloat() == 0.5);
- sqt.ok("0.5" == (0.5).tostring());
- sqt.ok("9.99".tofloat() == 9.99);
- sqt.ok("9.99" == (9.99).tostring());
- sqt.ok("12".tointeger() == 12);
- sqt.ok("12" == (12).tostring());
- sqt.ok("12.0".tofloat() == 12.0);
- //sqt.ok("12.0" == (12.0).tostring());
- sqt.ok("12.0e+10".tofloat() == 12.0e+10);
- sqt.ok("-122131242342.0e-10".tofloat() == -122131242342.0e-10);
- //sqt.ok("-122131242342.0e-10" == (-122131242342.0e-10).tostring());
- //sqt.ok(".5".tofloat() == .5);
- sqt.ok("122138760e-2".tofloat() == 122138760e-2);
- //sqt.ok("122138760e-2" == (122138760e-2).tostring());
- // signed and unsigned integer
- sqt.ok( 1 == 1 );
- sqt.ok(-1 == -1);
- //sqt.ok(+1 == +1);
- // signed and unsigned float
- sqt.ok( 1.0 == 1.0 );
- sqt.ok(-1.0 == -1.0);
- // binary
- //sqt.ok( 128 == 0b10000000 );
- //sqt.ok( 128 == 0B10000000 );
- // octal
- //sqt.ok( 8 == 0o10 );
- //sqt.ok( 8 == 0O10 );
- //sqt.ok( 8 == 0_10 );
- // hex
- sqt.ok( 255 == 0xff );
- sqt.ok( 255 == 0Xff );
- // decimal
- //sqt.ok( 999 == 0d999 );
- //sqt.ok( 999 == 0D999 );
- // decimal seperator
- //sqt.ok( 10000000 == 10_000_000 );
- //sqt.ok( 10 == 1_0 );
- // integer with exponent
- sqt.ok( 10.0 == 1e1 );
- sqt.ok(0.1 == 1e-1);
- sqt.ok( 10.0 == 1e+1 );
- // float with exponent
- sqt.ok( 10.0 == 1.0e1 );
- sqt.ok(0.1 == 1.0e-1);
- sqt.ok( 10.0 == 1.0e+1 );
- auto nstr = format("%7.1e", 68.514);
- sqt.ok( (nstr == "6.9e+01") || (nstr == "6.9e+001"));
- sqt.ok(format("%4d %4d %4d %4d %d %#x %#X", 6, 34, 16923, -12, -1, 14, 12) == " 6 34 16923 -12 -1 0xe 0XC");
- sqt.ok((3.123456788 + 0.000000001) == 3.123456789);
- sqt.fequal((3.123456789 + 1), 4.123456789);
- sqt.ok((3.123456790 - 0.000000001) == 3.123456789);
- sqt.ok((5.123456789 - 1) == 4.123456789);
- sqt.ok((3.125 * 3.125) == 9.765625);
- sqt.ok((3.125 * 1) == 3.125);
- sqt.ok((3.123456789 / 3.123456789) == 1.0);
- sqt.ok((3.123456789 / 1) == 3.123456789);
- sqt.ok(math.abs(123) == 123);
- sqt.ok(math.abs(-123) == 123);
- sqt.ok(math.abs(0) == 0);
- sqt.ok(math.abs(-0) == 0);
- sqt.ok(math.fabs(-0.12) == 0.12);
- sqt.ok(math.fabs(12.34) == 12.34);
- //print("math.acos(0)", math.acos(0))
- sqt.fequal(math.acos(0), 1.5707963267949);
- sqt.ok(math.acos(1) == 0);
- sqt.fequal(math.acos(-1), 3.1415926535898);
- sqt.ok(math.asin(0) == 0);
- sqt.fequal(math.asin(1), 1.5707963267949);
- sqt.fequal(math.asin(-1), -1.5707963267949);
- sqt.ok(math.atan(0) == 0);
- sqt.fequal(math.atan(1), 0.78539816339745);
- sqt.ok(math.atan2(0, 0) == 0);
- sqt.ok(math.atan2(0, 1) == 0);
- sqt.fequal(math.atan2(1, 0), 1.5707963267949);
- sqt.ok(math.ceil(123) == 123);
- sqt.ok(math.ceil(-123) == -123);
- sqt.ok(math.ceil(0) == 0);
- sqt.ok(math.ceil(-0) == -0);
- sqt.ok(math.ceil(0.123) == 1);
- sqt.ok(math.ceil(12.3) == 13);
- sqt.ok(math.ceil(-0.123) == -0);
- sqt.ok(math.ceil(-12.3) == -12);
- sqt.ok(math.floor(123) == 123);
- sqt.ok(math.floor(-123) == -123);
- sqt.ok(math.floor(0) == 0);
- sqt.ok(math.floor(-0) == -0);
- sqt.ok(math.floor(0.123) == 0);
- sqt.ok(math.floor(12.3) == 12);
- sqt.ok(math.floor(-0.123) == -1);
- sqt.ok(math.floor(-12.3) == -13);
- sqt.ok(math.tan(0) == 0);
- sqt.fequal(math.tan(math.PI / 4), 1.0);
- sqt.fequal(math.tan(-math.PI / 4), -1.0);
- sqt.ok(math.cos(0) == 1);
- sqt.ok(math.cos(math.PI) == -1);
- sqt.ok(math.cos(2 * math.PI) == 1);
- sqt.ok(math.abs(math.cos(math.PI / 2)) < 1.0e-16);
- sqt.ok(math.sin(0) == 0);
- sqt.ok(math.sin(math.PI / 2) == 1);
- sqt.ok(math.abs(math.sin(math.PI)) < 1.0e-15);
- sqt.ok(math.abs(math.sin(2 * math.PI)) < 1.0e-15);
- sqt.fequal(math.log(3), 1.0986122886681);
- sqt.fequal(math.log(100), 4.6051701859881);
- sqt.ok(math.isnan(math.log(-1)));
- sqt.ok(math.pow(2,4) == 16);
- sqt.ok(math.pow(2, 10) == 1024);
- sqt.ok(math.pow(1, 0) == 1);
- sqt.ok(math.sqrt(4) == 2);
- sqt.ok(math.sqrt(1000000) == 1000);
- sqt.ok(math.sqrt(1) == 1);
- sqt.ok(math.sqrt(-0) == -0);
- sqt.ok(math.sqrt(0) == 0);
- sqt.fequal(math.sqrt(2), 1.4142135623731);
- sqt.ok(math.isnan(math.sqrt(-4)) == true);
- /*
- sqt.ok(123.truncate == 123);
- sqt.ok((-123).truncate == -123);
- sqt.ok(0.truncate == 0);
- sqt.ok((-0).truncate == -0);
- sqt.ok(0.123.truncate == 0);
- sqt.ok(12.3.truncate == 12);
- sqt.ok((-0.123).truncate == -0);
- sqt.ok((-12.3).truncate == -12);
- sqt.ok((12345678901234.5).truncate == 12345678901234);
- sqt.ok((-12345678901234.5).truncate == -12345678901234);
- */
- /*
- sqt.ok(123.sign == 1);
- sqt.ok((-123).sign == -1);
- sqt.ok(0.sign == 0);
- sqt.ok((-0).sign == 0);
- sqt.ok(0.123.sign == 1);
- sqt.ok((-0.123).sign == -1);
- */
- /*
- sqt.ok(123.round == 123);
- sqt.ok((-123).round == -123);
- sqt.ok(0.round == 0);
- sqt.ok((-0).round == -0);
- sqt.ok(0.123.round == 0);
- sqt.ok(12.3.round == 12);
- sqt.ok((-0.123).round == -0);
- sqt.ok((-12.3).round == -12);
- */
- sqt.ok(123 % 1 == 0);
- sqt.ok((-123) % 1 == -0);
- sqt.ok(0 % 1 == 0);
- sqt.ok((-0) % 1 == -0);
- sqt.ok((0.123) % 1.0 == 0.123);
- sqt.ok((-0.123) % 1.0 == -0.123);
- sqt.fequal((12.3) % 1.0, 0.3);
- sqt.fequal((-12.3) % 1.0, -0.3);
- sqt.fequal((1.23456789012345) % 1.0, 0.23456789012345);
- sqt.fequal((-1.23456789012345) % 1.0, -0.23456789012345);
- sqt.ok((0.000000000000000000000000000000000000000001) % 1.0 == 1e-42);
- sqt.ok((-0.000000000000000000000000000000000000000001) % 1.0 == -1e-42);
- sqt.ok((1.000000000000000000000000000000000000000001) % 1.0 == 0);
- sqt.ok((-1.000000000000000000000000000000000000000001) % 1.0 == -0);
- sqt.ok("123".tointeger() == 123);
- sqt.ok("-123".tointeger() == -123);
- sqt.ok("-0".tointeger() == -0);
- sqt.ok("12.34".tofloat() == 12.34);
- sqt.ok("-0.0001".tofloat() == -0.0001 == true);
- sqt.ok(" 12 ".tointeger() == 12);
- try{"test1".tointeger(), sqt.ok(0);}catch(e){sqt.ok(1);};
- try{"test1".tofloat(), sqt.ok(0);}catch(e){sqt.ok(1);};
- //sqt.ok("".tointeger() == null);
- //sqt.ok("prefix1.2".tofloat() == null);
- //sqt.ok("1.2suffix".tofloat() == null);
- sqt.ok("1.2suffix".tofloat() == 1.2); // ??????
- sqt.ok("1.2suffix".tointeger() == 1); // ?????
- sqt.ok((123).tostring() == "123");
- sqt.ok((-123).tostring() == "-123");
- //sqt.ok((-0).tostring() == "-0");
- //sqt.ok((-0.0).tostring() == "-0.0");
- sqt.ok((12.34).tostring() == "12.34");
- sqt.ok((-0.0001).tostring() == "-0.0001");
- sqt.ok(type(123) == "integer");
- sqt.ok(type(123.0) != "integer");
- sqt.ok(type(0) == "integer");
- sqt.ok(type(1.0000001) != "integer");
- sqt.ok(type(-2.3) != "integer");
- //sqt.ok(type(0/0).isInteger == false);
- //sqt.ok(type(1/0).isInteger == false);
- //sqt.fequal(math.HUGE, 1.7976931348623e+308);
- //sqt.ok(Num.smallest == 2.2250738585072e-308);
- sqt.ok(1 < 2);
- sqt.ok(!(2 < 2));
- sqt.ok(!(2 < 1));
- sqt.ok(1 <= 2);
- sqt.ok(2 <= 2);
- sqt.ok(!(2 <= 1));
- sqt.ok(!(1 > 2));
- sqt.ok(!(2 > 2));
- sqt.ok(2 > 1);
- sqt.ok(!(1 >= 2));
- sqt.ok(2 >= 2);
- sqt.ok(2 >= 1);
- sqt.ok(!(0 < -0));
- sqt.ok(!(-0 < 0));
- sqt.ok(!(0 > -0));
- sqt.ok(!(-0 > 0));
- sqt.ok(0 <= -0);
- sqt.ok(-0 <= 0);
- sqt.ok(0 >= -0);
- sqt.ok(-0 >= 0);
- sqt.ok((8 / 2) == 4);
- sqt.fequal((12.34 / -0.4), -30.85);
- //sqt.ok(!math.isfinite(3 / 0)); // == infinity);
- //sqt.ok(!math.isfinite(-3 / 0)); // == -infinity);
- //sqt.ok(math.isnan(0 / 0));
- //sqt.ok(math.isnan(-0 / 0));
- //sqt.ok(!math.isfinite(3 / -0)); // == -infinity);
- //sqt.ok(!math.isfinite(-3 / -0)); // == infinity);
- //sqt.ok(math.isnan(0 / -0));
- //sqt.ok(math.isnan(-0 / -0));
- sqt.ok(123 == 123);
- sqt.ok(123 != 124);
- sqt.ok(-3 != 3);
- sqt.ok(0 == -0);
- sqt.ok(0.0 == -0.0);
- sqt.ok(123 != "123");
- sqt.ok(1 == true);
- sqt.ok(0 == false);
- sqt.ok(1 !== true);
- sqt.ok(0 !== false);
- sqt.ok((0 & 0) == 0);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok((0xaaaaaaaa & 0x55555555) == 0);
- sqt.ok((0xf0f0f0f0 & 0x3c3c3c3c) == 808464432);
- sqt.ok((0xffffffff & 0xffffffff) == 4294967295);
- #endif
- try{1 & false, sqt.ok(0)} catch(e) {sqt.ok(1);} // expect runtime error: Right operand must be a number.
- sqt.ok((0 << 0) == 0);
- sqt.ok((1 << 0) == 1);
- sqt.ok((0 << 1) == 0);
- sqt.ok((1 << 1) == 2);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok((0xffffffff << 0) == 4294967295);
- #endif
- sqt.ok((0 >> 0) == 0);
- sqt.ok((1 >> 0) == 1);
- sqt.ok((0 >> 1) == 0);
- sqt.ok((1 >> 1) == 0);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok((0xaaaaaaaa << 1) == 5726623060);
- sqt.ok((0xf0f0f0f0 << 1) == 8084644320);
- sqt.ok((0xaaaaaaaa >> 1) == 1431655765);
- sqt.ok((0xf0f0f0f0 >> 1) == 2021161080);
- sqt.ok((0xffffffff >> 1) == 2147483647);
- #else
- sqt.ok((0xaaaaaaaa << 1) == 1431655764);
- sqt.ok((0xaaaaaaaa >> 1) == -715827883);
- //sqt.ok((0xf0f0f0f0 << 1) == -505290272);
- //sqt.ok((0xf0f0f0f0 >> 1) == -126322568);
- //sqt.ok((0xffffffff >> 1) == -1);
- #endif
- sqt.ok((0 ^ 0) == 0);
- sqt.ok((1 ^ 1) == 0);
- sqt.ok((0 ^ 1) == 1);
- sqt.ok((1 ^ 0) == 1);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok((0xaaaaaaaa ^ 0x55555555) == 4294967295);
- sqt.ok((0xf0f0f0f0 ^ 0x3c3c3c3c) == 3435973836);
- sqt.ok((0xffffffff ^ 0xffffffff) == 0);
- #endif
- //sqt.ok((~0) == 4294967295);
- //sqt.ok((~1) == 4294967294);
- //sqt.ok((~23) == 4294967272);
- //sqt.ok((~0xffffffff) == 0);
- //sqt.ok((~1.23) == 4294967294);
- //sqt.ok((~0.00123) == 4294967295);
- //sqt.ok((~345.67) == 4294966950);
- sqt.ok((0 | 0) == 0);
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok((0xaaaaaaaa | 0x55555555) == 4294967295);
- sqt.ok((0xcccccccc | 0x66666666) == 4008636142);
- sqt.ok((0xffffffff | 0xffffffff) == 4294967295);
- #endif
- local a = 3;
- sqt.ok((5 - 3) == 2);
- sqt.fequal((3.1 - 0.24), 2.86);
- sqt.ok((3 - 2 - 1) == 0);
- sqt.ok(-a == -3);
- sqt.ok((1 + 2) == 3);
- sqt.ok((12.34 + 0.13) == 12.47);
- sqt.ok((3 + 5 + 2) == 10);
- sqt.ok((5 % 3) == 2);
- sqt.ok((10 % 5) == 0);
- sqt.ok((-4 % 3) == -1);
- sqt.ok((4 % -3) == 1);
- sqt.ok((-4 % -3) == -1);
- sqt.ok((-4.2 % 3.1) == -1.1);
- sqt.ok((4.2 % -3.1) == 1.1);
- sqt.ok((-4.2 % -3.1) == -1.1);
- sqt.ok((13 % 7 % 4) == 2);
- sqt.ok((13 + 1 % 7) == 14);
- sqt.ok((5 * 3) == 15);
- sqt.ok((12.34 * 0.3) == 3.702);
- //10000000000000004 < 32BITS
- //10000000000000002 < 64BITS
- //print(format("%.17g", 1e16 + 2.9999) , "10000000000000002");
- //if(_intsize_ == 8)
- #ifdef __SQUILU_INT_SZ8__
- sqt.ok(format("%.17g", 1e16 + 2.9999) == "10000000000000002");
- #endif
- //else sqt.ok(format("%.17g", 1e16 + 2.9999) == "10000000000000004");
- });
- sqt.run("enum", function(){
- enum e1 {one=1, two};
- sqt.ok(e1.one == 1);
- sqt.ok(e1.two == 2);
- enum e2 {one=-1, two, three};
- sqt.ok(e2.one == -1);
- sqt.ok(e2.two == 0);
- sqt.ok(e2.three == 1);
- enum e3 {one=-1, two, three, nine=9, ten};
- sqt.ok(e3.one == -1);
- sqt.ok(e3.two == 0);
- sqt.ok(e3.three == 1);
- sqt.ok(e3.nine == 9);
- sqt.ok(e3.ten == 10);
- });
- sqt.run("constants", function(){
- const ONE = 1;
- const STR = "string";
- const $CTCC;
- sqt.ok(ONE == 1);
- sqt.ok(STR == "string");
- sqt.ok(CTCC == "CTCC");
- sqt.ok($ONE == 1);
- sqt.ok($STR == "string");
- sqt.ok($CTCC == "CTCC");
- //print(dostring("return $CTC == \"CTC\";"));
- });
- sqt.run("class", function(){
- class Comparable {
- constructor(n)
- {
- name = n;
- }
- function _typeof() {return "Comparable";};
- function _cmp(other)
- {
- if(name<other.name) return -1;
- if(name>other.name) return 1;
- return 0;
- }
- static function st() {return "st";};
- name = null;
- id = 0;
- static count = 0;
- }
- local a = Comparable("Alberto");
- local b = Comparable("Wouter");
- local c = Comparable("Alberto");
- sqt.ok(a < b);
- sqt.ok(b > a);
- sqt.ok(a.id == 0);
- sqt.ok(Comparable.count == 0);
- sqt.ok(a.count == 0);
- sqt.ok(Comparable.st() == "st");
- sqt.ok(a.st() == "st");
- sqt.ok(typeof(a) == "Comparable");
- sqt.ok( c == a );
- sqt.ok( c !== a );
- });
- sqt.run("globals", function(){
- sqt.ok( obj_clone(3) == 3 );
- sqt.ok( obj_clone(3.4) == 3.4 );
- sqt.ok( obj_clone("str") == "str" );
- });
- sqt.run("coroutines", function(){
- local state1, state2, state3;
- state1 = function()
- {
- ::suspend("state1");
- return state2();
- }
- state2 = function()
- {
- ::suspend("state2");
- return state3();
- }
- state3 = function()
- {
- ::suspend("state3");
- return state1();
- }
- local statethread = ::newthread(state1)
- ::table_len(globals)
- //the above dummy global call with "::" prefix
- //after a statement ending without ';' terminator
- //is here to check a bug regression on the compiler
- sqt.ok(statethread.call() == "state1");
- for(local i = 0, j=1; i < 10000; i++)
- {
- switch(j++)
- {
- case 0: sqt.ok(statethread.wakeup() == "state1"); break;
- case 1: sqt.ok(statethread.wakeup() == "state2"); break;
- case 2:
- sqt.ok(statethread.wakeup() == "state3");
- j = 0; //restart
- break;
- }
- }
- });
- sqt.run("C/C++ syntax", function(){
- //string line continuation
- sqt.ok("foo \
- bar" == "foo bar");
- //adjacent strings
- sqt.ok("foo "
- "bar" == "foo bar");
- //C++ multi line string
- sqt.ok(R"(foo
- bar)" == "foo\nbar");
- #ifndef ZEROX
- class Dad
- {
- int_t _n;
- public:
- Dad(int_t n){ _n = n;}
- void print() {printf("%d\n", _n);}
- int_t getN() {return _n;}
- protected:
- void onlyForFriends(){}
- private:
- void onlyForUs(){}
- };
- auto dad = Dad(6);
- sqt.ok(dad.getN() == 6);
- #endif
- //SquiLu has several predefined syntax alias for "local"
- // int8_t, int16_t, int32_t, int64_t, float_t, double_t, string_t, void, void_ptr_t
- extern int_t eadd(int_t a, int_t b);
- //int_t add(int_t a, int_t b);
- int_t a = 4;
- //SquiLu accpet "auto" as an alias for "local"
- auto b = 5;
- int_t add(const int_t a, const int_t b) {return a+b;}
- sqt.ok(add(a,b) == 9);
- //SquiLu has limited single pass preprocessor capabilities
- #ifdef __SQUILU__
- //here squilu definitions /code
- //we can use typedef to create type aliases
- typedef T T;
- const double_t pi = 3.14;
- sqt.ok(pi == 3.14);
- #else
- //here C/C++ definitions/code
- #endif
- //SquiLu only accepts the template definition
- //but doesn't do anything with it right now
- //although class/struct/function definitions work
- //because dynamic type values are the default
- template<typename T>
- T tadd(const T a, const T b) {return a+b;}
- sqt.ok(tadd(a,b) == 9);
- class A
- {
- int_t _a;
- public:
- A(const int_t a){ _a = a;}
- virtual ~A() {/*ignored in squilu*/}
- virtual void addIt(const int_t n) {_a += n;}
- virtual int_t getIt() {return _a;}
- };
- auto ca = A(4); //RAII with direct constructor call
- sqt.ok(ca.getIt() == 4);
- ca.addIt(5);
- sqt.ok(ca.getIt() == 9);
- class B :public A
- {
- public:
- B(const int_t a) {_a = a;}
- int_t getItPlus(const int_t p) {return _a + p;}
- };
- auto cb = B(4); //RAII with direct constructor call
- sqt.ok(cb.getIt() == 4);
- cb.addIt(5);
- sqt.ok(cb.getIt() == 9);
- sqt.ok(ca.getIt() == cb.getIt());
- sqt.ok(cb.getItPlus(9) == 18);
- struct S
- {
- int_t _a;
- S(const int_t a){ _a = a;}
- virtual ~S() {/*ignored in squilu*/}
- virtual void addIt(const int_t n) {_a += n;}
- virtual int_t getIt() {return _a;}
- };
- auto sa = S(4); //RAII with direct constructor call
- sqt.ok(sa.getIt() == 4);
- sa.addIt(5);
- sqt.ok(sa.getIt() == 9);
- template<typename T>
- class Ktpl
- {
- T _a;
- public:
- Ktpl(const T a){ _a = a;}
- virtual ~Ktpl() {/*ignored in squilu*/}
- virtual void addIt(const T n) {_a += n;}
- virtual T getIt() {return _a;}
- T getItPlus(const T n);
- };
- //T Ktpl::getItPlus(const T n) {return _a + n;}
- auto ki = Ktpl<int_t>(4); //RAII with direct constructor call
- sqt.ok(ki.getIt() == 4);
- ki.addIt(5);
- sqt.ok(ki.getIt() == 9);
- auto kf = Ktpl<float_t>(4); //RAII with direct constructor call
- sqt.ok(kf.getIt() == 4);
- kf.addIt(5);
- sqt.ok(kf.getIt() == 9);
- Ktpl<double_t> kd = Ktpl<double_t>(4); //RAII with direct constructor call
- sqt.ok(kd.getIt() == 4);
- kd.addIt(5);
- sqt.ok(kd.getIt() == 9);
- /*
- typedef Ktpl<int64_t> Ktpl_i64;
- auto ki64 = Ktpl_i64(4); //RAII with direct constructor call
- sqt.ok(ki64.getIt() == 4);
- ki64.addIt(5);
- sqt.ok(ki64.getIt() == 9);
- */
- auto max_loop = 10;
- for(auto i=max_loop; i >= 0; --i) sqt.ok(max_loop-- == i);
- //SquiLu only accept the reference parameter syntax
- //for int/float/boolean it doesn't do anything, they are still passed by value
- //instances are passed by reference allways
- int_t addByRef(const int_t &a, const int_t &b) {return a + b;}
- sqt.ok(addByRef(a, b) == addByRef(b, a));
- //SquiLu do not accept '*' pointer notation
- //but with a dummy typedef we can simulate it
- #ifdef __SQUILU__
- typedef char_ptr_t char_ptr_t;
- #else
- typedef char* char_ptr_t;
- #endif
- char_ptr_t strcopy(const char_ptr_t str) {return str;} //only for syntax test
- sqt.ok(strcopy("squilu") == "squilu");
- });
- sqt.run("Javascript syntax", function(){
- //SquiLu accpet "var/let" as an alias for "local"
- var a = 20;
- let b = 30;
- let add = function(a, b) {return a+b;}
- sqt.ok( add(a,b) == 50 );
- });
- sqt.run("Typescript syntax", function(){
- //SquiLu accpet "var/let" as an alias for "local"
- let Greeting1 : string = "Hello, ";
- function greeter(person: string) : string {
- return Greeting1 + person;
- }
- let user = "Jane User";
- sqt.ok(greeter(user) == Greeting1 + user);
- let notSure: any = 4;
- notSure = "maybe a string instead";
- notSure = false; // okay, definitely a boolean
- sqt.ok(!notSure);
- let a : number = 20;
- let b : number = 30;
- let add = function(a : number, b : number) : number {return a+b;}
- sqt.ok( add(a,b) == 50 );
- });
- return sqt.results(); //show results
|