squilu-test.nut 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. #ifndef __SQUILU__
  2. #include <stdio.h>
  3. #endif
  4. //the above is only for syntax check in SquiLu
  5. #include "minctest.nut"
  6. local sqt = minctest();
  7. #ifdef __SQUILU_INT_SZ8__
  8. print("__SQUILU_INT_SZ8__ defined");
  9. #endif
  10. #ifdef __SQUILU_INT_SZ4__
  11. print("__SQUILU_INT_SZ4__ defined");
  12. #endif
  13. #ifdef __SQUILU_FLOAT_SZ8__
  14. print("__SQUILU_FLOAT_SZ8__ defined");
  15. #endif
  16. #ifdef __SQUILU_FLOAT_SZ4__
  17. print("__SQUILU_FLOAT_SZ4__ defined");
  18. #endif
  19. #ifdef __SQUILU_PTR_SZ8__
  20. print("__SQUILU_PTR_SZ8__ defined");
  21. #endif
  22. #ifdef __SQUILU_PTR_SZ4__
  23. print("__SQUILU_PTR_SZ4__ defined");
  24. #endif
  25. print("_versionnumber_", _versionnumber_);
  26. print("_version_", _version_);
  27. print("_charsize_", _charsize_);
  28. print("_intsize_", _intsize_);
  29. print("_floatsize_", _floatsize_);
  30. local Is32Bits = _intsize_ == 4;
  31. //print("Is32Bits", Is32Bits);
  32. local globals = getroottable();
  33. sqt.run("closures", function(){
  34. local A = 0, B = {g=10}
  35. local f = function(x)
  36. {
  37. local a = []
  38. for(local i=1; i < 1000; ++i)
  39. {
  40. local y = 0
  41. {
  42. a.append(function () {++B.g; y += x; return y+A;});
  43. }
  44. }
  45. local dummy = function () {return a[A];}
  46. collectgarbage()
  47. A = 1;
  48. sqt.ok(dummy() == a[1]);
  49. A = 0;
  50. //print("a[0]", a[0]())
  51. sqt.ok(a[0]() == x)
  52. sqt.ok(a[2]() == x)
  53. collectgarbage()
  54. sqt.ok(B.g == 12)
  55. return a
  56. }
  57. local a = f(10)
  58. // testing closures with 'for' control variable
  59. a = []
  60. for(local i=0; i < 10; ++i)
  61. {
  62. local lv = i;
  63. a.append({set = function(x){ lv=x; }, get = function (){ return lv; }})
  64. if( i == 2 ) break;
  65. }
  66. sqt.ok(a.len() == 3)
  67. sqt.ok(a[0].get() == 0)
  68. a[0].set(10)
  69. sqt.ok(a[0].get() == 10)
  70. sqt.ok(a[1].get() == 1)
  71. a[1].set('a')
  72. sqt.ok(a[1].get() == 'a')
  73. //a[2].set(2)
  74. sqt.ok(a[2].get() == 2)
  75. a = []
  76. foreach( i, k in ['a', 'b'])
  77. {
  78. local li = i
  79. local lk = k
  80. a.append( {set = function(x, y) {li=x; lk=y},
  81. get = function () {return [li, lk];}} )
  82. if( i == 2 ) break;
  83. }
  84. a[0].set(10, 20)
  85. local rs = a[1].get()
  86. sqt.ok(rs[0] == 1 && rs[1] == 'b')
  87. rs = a[0].get()
  88. sqt.ok(rs[0] == 10 && rs[1] == 20)
  89. a[1].set('a', 'b')
  90. rs = a[1].get()
  91. sqt.ok(rs[0] == 'a' && rs[1] == 'b')
  92. // testing closures with 'for' control variable x break
  93. for(local i=1; i <= 3; ++i)
  94. {
  95. local li = i
  96. f = function () { return li;}
  97. break
  98. }
  99. sqt.ok(f() == 1)
  100. foreach( k, v in ["a", "b"])
  101. {
  102. local lk = k, lv = v
  103. f = function () {return [lk, lv]; }
  104. break
  105. }
  106. sqt.ok(([f()])[0][0] == 0)
  107. sqt.ok(([f()])[0][1] == "a")
  108. // testing closure x break x return x errors
  109. local b
  110. f = function (x)
  111. {
  112. local first = 1
  113. while( 1 ) {
  114. if( x == 3 && ! first ) return
  115. local la = "xuxu"
  116. b = function (op, y=0) {
  117. if( op == "set" )
  118. la = x+y
  119. else
  120. return la
  121. }
  122. if( x == 1 ) { break }
  123. else if( x == 2 ) return
  124. else if( x != 3 ) throw("x != 3")
  125. first = null
  126. }
  127. }
  128. for(local i=1; i <= 3; ++i) {
  129. f(i)
  130. sqt.ok(b("get") == "xuxu")
  131. b("set", 10); sqt.ok(b("get") == 10+i)
  132. b = null
  133. }
  134. //pcall(f, 4);
  135. try{ f(4);}catch(e){}
  136. sqt.ok(b("get") == "xuxu")
  137. b("set", 10); sqt.ok(b("get") == 14)
  138. // testing multi-level closure
  139. local w
  140. f = function(x)
  141. {
  142. return function (y)
  143. {
  144. return function (z) {return w+x+y+z;}
  145. }
  146. }
  147. local y = f(10)
  148. w = 1.345
  149. sqt.ok(y(20)(30) == 60+w)
  150. // test for correctly closing upvalues in tail calls of vararg functions
  151. local function t ()
  152. {
  153. local function c(a,b) {sqt.ok(a=="test" && b=="OK") }
  154. local function v(f, ...) {c("test", f() != 1 && "FAILED" || "OK") }
  155. local lx = 1
  156. return v(function() {return lx;})
  157. }
  158. t()
  159. });
  160. sqt.run("calls", function(){
  161. // get the opportunity to test "type" too ;)
  162. local Klass = class {};
  163. local aklass = Klass();
  164. sqt.ok(type(1<2) == "bool")
  165. sqt.ok(type(true) == "bool" && type(false) == "bool")
  166. sqt.ok(type(null) == "null")
  167. sqt.ok(type(-3) == "integer")
  168. sqt.ok(type(-3.14) == "float")
  169. sqt.ok(type("x") == "string")
  170. sqt.ok(type({}) == "table")
  171. sqt.ok(type(type) == "function")
  172. sqt.ok(type(Klass) == "class")
  173. sqt.ok(type(aklass) == "instance")
  174. sqt.ok(type(sqt.ok) == type(print))
  175. local f = null
  176. f = function (x) {return a.x(x);}
  177. sqt.ok(type(f) == "function")
  178. // testing local-function recursion
  179. local fact = false
  180. {
  181. local res = 1
  182. local function lfact (n)
  183. {
  184. if( n==0 ) return res
  185. else return n*lfact(n-1)
  186. }
  187. sqt.ok(lfact(5) == 120)
  188. }
  189. sqt.ok(fact == false)
  190. // testing declarations
  191. local a = {i = 10}
  192. local self = 20
  193. a.x <- function(x) {return x+this.i;}
  194. a.y <- function(x) {return x+self;}
  195. sqt.ok(a.x(1)+10 == a.y(1))
  196. a.t <- {i=-100}
  197. a["t"].x <- function (a,b) {return this.i+a+b;}
  198. sqt.ok(a.t.x(2,3) == -95)
  199. {
  200. local la = {x=0}
  201. la.add <- function(x) {this.x = this.x+x; la.y <- 20; return this; }
  202. sqt.ok(la.add(10).add(20).add(30).x == 60 && la.y == 20)
  203. }
  204. a = {b={c={}}}
  205. a.b.c.f1 <- function(x) {return x+1;}
  206. a.b.c.f2 <- function(x,y) {this[x] <- y;}
  207. sqt.ok(a.b.c.f1(4) == 5)
  208. a.b.c.f2("k", 12); sqt.ok(a.b.c.k == 12)
  209. local t = null // 'declare' t
  210. f = function(a,b,c=null, e=null) {local d = 'a'; t=[a,b,c,d];}
  211. f( // this line change must be valid
  212. 1,2)
  213. sqt.ok(t[0] == 1 && t[1] == 2 && t[2] == null && t[3] == 'a')
  214. f(1,2, // this one too
  215. 3,4)
  216. sqt.ok(t[0] == 1 && t[1] == 2 && t[2] == 3 && t[3] == 'a')
  217. function fat(x)
  218. {
  219. if( x <= 1 ) return 1
  220. else return x*loadstring("return fat(" +( x-1) + ")")()
  221. }
  222. sqt.ok(loadstring( "loadstring( \"sqt.ok(fat(6)==720)\" )()" ))
  223. a = loadstring("return [fat(5), 3]")
  224. a = a()
  225. sqt.ok(a[0] == 120 && a[1] == 3)
  226. // fixed-point operator
  227. local Y = function (le)
  228. {
  229. local function la (f)
  230. {
  231. return le(function (x) {return f(f)(x);})
  232. }
  233. return la(la)
  234. }
  235. // non-recursive factorial
  236. local F = function (f)
  237. {
  238. return function (n)
  239. {
  240. if( n == 0 ) return 1
  241. else return n*f(n-1)
  242. }
  243. }
  244. fat = Y(F)
  245. sqt.ok(fat(0) == 1 && fat(4) == 24 && Y(F)(5)==5*Y(F)(4))
  246. local function g (z)
  247. {
  248. local function lf (a,b,c,d)
  249. {
  250. return function (x,y) {return a+b+c+d+a+x+y+z;}
  251. }
  252. return lf(z,z+1,z+2,z+3)
  253. }
  254. f = g(10)
  255. sqt.ok(f(9, 16) == 10+11+12+13+10+9+16+10)
  256. Y, F, f = null
  257. try { assert(true); sqt.ok(true);} catch(e) {sqt.ok(false);};
  258. try { assert(true, "opt msg"); sqt.ok(true);} catch(e) {sqt.ok(false);};
  259. try { assert(false); sqt.ok(false);} catch(e) {sqt.ok(true);};
  260. try { assert(false, "opt msg"); sqt.ok(false);} catch(e) {sqt.ok(e == "opt msg");};
  261. local function bugRecursionLocal(str, num=666, car=777){
  262. if(str == "recurse") return bugRecursionLocal("recurring with recurse", 5);
  263. return str + num;
  264. }
  265. sqt.ok(bugRecursionLocal("dad") == "dad666");
  266. sqt.ok(bugRecursionLocal("recurse") == "recurring with recurse5");
  267. local function f1(){ return "f1";}
  268. local function f2(){ return "f2";}
  269. local function f3(f=f2){ return "f3" + f();}
  270. sqt.ok(f3() == "f3f2");
  271. local function f4(f=f2){ return "f4" + type(f);}
  272. sqt.ok(f4() == "f4function");
  273. local ary = ["blue"]
  274. local function f5(s, f=ary){ return "f5" + s + type(f);}
  275. sqt.ok(f5("ded") == "f5dedarray");
  276. local ncount = 0;
  277. local function nested(p=88, q=66)
  278. {
  279. ++ncount;
  280. local result = ncount + ":" + p + ":" + q;
  281. local function ni(x=99)
  282. {
  283. result += ":" + x;
  284. }
  285. ni();
  286. if(p==88) result += "::" + nested(22);
  287. return result;
  288. }
  289. sqt.ok(nested() == "1:88:66:99::2:22:66:99");
  290. sqt.ok(nested("n") == "3:n:66:99");
  291. });
  292. sqt.run("sort", function(){
  293. local function check (a, f=null)
  294. {
  295. f = f || function (x,y) {return x<y;};
  296. for(local n=a.len()-1; n > 2; --n) sqt.ok(! f(a[n], a[n-1]))
  297. }
  298. local a = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  299. "Oct", "Nov", "Dec"]
  300. a.sort()
  301. check(a)
  302. limit <- 30000
  303. if( table_rawget(globals, "_soft", false) ) limit = 5000
  304. a = []
  305. for(local i=0; i < limit; ++i) a.append(math.random())
  306. local x = os.clock()
  307. a.sort()
  308. //print(format("Sorting %d elements in %.2f sec.", limit, os.clock()-x))
  309. check(a)
  310. x = os.clock()
  311. a.sort()
  312. //print(format("Re-sorting %d elements in %.2f sec.", limit, os.clock()-x))
  313. check(a)
  314. a = []
  315. for(local i=0; i < limit; ++i) a.append(math.random())
  316. x = os.clock();
  317. local li=0
  318. a.sort(function(x,y) {li=li+1; return y<=>x;})
  319. //print(format("Invert-sorting other %d elements in %.2f sec., with %i comparisons",
  320. // limit, os.clock()-x, li))
  321. check(a, function(x,y) {return y<x;})
  322. a = []
  323. a.sort() // empty array
  324. for(local i=0; i < limit; ++i) a.append(false)
  325. x = os.clock();
  326. a.sort(function(x,y) {return 0;})
  327. //print(format("Sorting %d equal elements in %.2f sec.", limit, os.clock()-x))
  328. check(a, function(x,y) {return 0;})
  329. foreach( i,v in a) sqt.ok(! v || i=='n' && v==limit)
  330. a = ["álo", "\0first :-)", "alo", "then this one", "45", "and a new"]
  331. a.sort()
  332. check(a)
  333. });
  334. sqt.run("pattern matching", function(){
  335. local re = regexp(@"^[^\n]+");
  336. sqt.ok(re.match("abc\ndef") == "abc");
  337. sqt.ok("abc\ndef".match("^[^\n]+") == "abc");
  338. re = regexp(@"abc..def");
  339. sqt.ok(re.match("abc\u00b5def") == "abc\u00b5def");
  340. sqt.ok("abc\u00b5def".match("abc..def") == "abc\u00b5def");
  341. re = regexp("ab\u00b5\u2704."); //need not use @ to expand \uXXXX
  342. sqt.ok(re.match("ab\u00b5\u2704x") == "ab\u00b5\u2704x");
  343. sqt.ok("ab\u00b5\u2704x".match("ab\u00b5\u2704.") == "ab\u00b5\u2704x");
  344. sqt.ok("aBcaBc" == "abcabc".gsub("b", "B"));
  345. sqt.ok("aBcaBc" == "abcabc".gsub("b", function(w){return w.toupper(); }));
  346. sqt.ok("$a$a$" == "#a#a#".gsub("#", "$"));
  347. sqt.ok("$a$a$" == "#a#a#".gsub("#", function(w){return "$"; }));
  348. sqt.ok("$$a$$" == "##a##".gsub("##", "$$"));
  349. sqt.ok("$$a$$" == "##a##".gsub("##", function(w){ return "$$"; }));
  350. sqt.ok("A" == "a".gsub("a", "A"));
  351. sqt.ok("A" == "a".gsub("a", function(w){return w.toupper(); }));
  352. //sqt.ok("<a><><>" == "a".gsub("a", "<%0><%1><%2>"));
  353. sqt.ok(".h.e.l.l.o." == "hello".gsub("", "."));
  354. local a, b,c, d, e, f, r, s, t, x
  355. f = function (s, p)
  356. {
  357. local res = []
  358. if( s.find_lua(p, res) >= 0) return s.slice(res[0], res[1])
  359. }
  360. function f1(s, p)
  361. {
  362. p = p.gsub("%%([0-9])", function (s) {return "%" + (s.tointeger()+1);})
  363. p = p.gsub("^(^?)", "%1()", 1)
  364. p = p.gsub("($?)$", "()%1", 1)
  365. local tl = []
  366. s.gmatch(p, function(...){foreach(elm in vargv) tl.append(elm); return true;})
  367. return s.slice(tl[0], tl[tl.len()-1] )
  368. }
  369. a = []
  370. b = "".find_lua("", a) // empty patterns are tricky
  371. sqt.ok(a[0] == 0 && a[1] == 0);
  372. b = "alo".find_lua("", a)
  373. sqt.ok(a[0] == 0 && a[1] == 0)
  374. b = "a\0o a\0o a\0o".find_lua("a", a, 0) // first position
  375. sqt.ok(a[0] == 0 && a[1] == 1)
  376. b = "a\0o a\0o a\0o".find_lua("a\0o", a, 1) // starts in the midle
  377. sqt.ok(a[0] == 4 && a[1] == 7)
  378. b = "a\0o a\0o a\0o".find_lua("a\0o", a, 8) // starts in the midle
  379. sqt.ok(a[0] == 8 && a[1] == 11)
  380. b = "a\0a\0a\0a\0\0ab".find_lua("\0ab", a, 1); // finds at the end
  381. sqt.ok(a[0] == 8 && a[1] == 11);
  382. b = "a\0a\0a\0a\0\0ab".find_lua("b", a) // last position
  383. //print(a[0], a[1])
  384. sqt.ok(a[0] == 10 && a[1] == 11)
  385. sqt.ok("a\0a\0a\0a\0\0ab".find_lua("b\0") < 0) // check ending
  386. sqt.ok("".find_lua("\0") < 0)
  387. sqt.ok("alo123alo".find_lua("12") == 3)
  388. sqt.ok("alo123alo".find_lua("^12") < 0)
  389. sqt.ok(f("aloALO", "%l*") == "alo")
  390. sqt.ok(f("aLo_ALO", "%a*") == "aLo")
  391. sqt.ok(f("aaab", "a*") == "aaa");
  392. sqt.ok(f("aaa", "^.*$") == "aaa");
  393. sqt.ok(f("aaa", "b*") == "");
  394. sqt.ok(f("aaa", "ab*a") == "aa")
  395. sqt.ok(f("aba", "ab*a") == "aba")
  396. sqt.ok(f("aaab", "a+") == "aaa")
  397. sqt.ok(f("aaa", "^.+$") == "aaa")
  398. sqt.ok(f("aaa", "b+") == null)
  399. sqt.ok(f("aaa", "ab+a") == null)
  400. sqt.ok(f("aba", "ab+a") == "aba")
  401. sqt.ok(f("a$a", ".$") == "a")
  402. sqt.ok(f("a$a", ".%$") == "a$")
  403. sqt.ok(f("a$a", ".$.") == "a$a")
  404. sqt.ok(f("a$a", "$$") == null)
  405. sqt.ok(f("a$b", "a$") == null)
  406. sqt.ok(f("a$a", "$") == "")
  407. sqt.ok(f("", "b*") == "")
  408. sqt.ok(f("aaa", "bb*") == null)
  409. sqt.ok(f("aaab", "a-") == "")
  410. sqt.ok(f("aaa", "^.-$") == "aaa")
  411. sqt.ok(f("aabaaabaaabaaaba", "b.*b") == "baaabaaabaaab")
  412. sqt.ok(f("aabaaabaaabaaaba", "b.-b") == "baaab")
  413. sqt.ok(f("alo xo", ".o$") == "xo")
  414. sqt.ok(f(" \n isto é assim", "%S%S*") == "isto")
  415. sqt.ok(f(" \n isto é assim", "%S*$") == "assim")
  416. sqt.ok(f(" \n isto é assim", "[a-z]*$") == "assim")
  417. sqt.ok(f("um caracter ? extra", "[^%sa-z]") == "?")
  418. sqt.ok(f("", "a?") == "")
  419. sqt.ok(f("á", "á?") == "á")
  420. sqt.ok(f("ábl", "á?b?l?") == "ábl")
  421. sqt.ok(f(" ábl", "á?b?l?") == "")
  422. sqt.ok(f("aa", "^aa?a?a") == "aa")
  423. sqt.ok(f("]]]áb", "[^]]") == "á")
  424. sqt.ok(f("0alo alo", "%x*") == "0a")
  425. sqt.ok(f("alo alo", "%C+") == "alo alo")
  426. //print("+")
  427. sqt.ok(f1("alo alx 123 b\0o b\0o", "(..*) %1") == "b\0o b\0o")
  428. sqt.ok(f1("axz123= 4= 4 34", "(.+)=(.*)=%2 %1") == "3= 4= 4 3")
  429. sqt.ok(f1("=======", "^(=*)=%1$") == "=======")
  430. sqt.ok("==========".match("^([=]*)=%1$") == null)
  431. local function range (i, j)
  432. {
  433. local res_len = j-i
  434. local res = array(res_len+1)
  435. local count = 0;
  436. for(local idx = i; idx <= j; ++idx) res[count++] = idx;
  437. return res
  438. }
  439. local ar = range(0, 255)
  440. //print(ar.len())
  441. ar.insert(0, "a");
  442. local abc = str_from_chars.acall(ar);
  443. //print("abc.len()", abc.len() , 256);
  444. sqt.ok(abc.len() == 256)
  445. function strset (p)
  446. {
  447. local res = {s=""}
  448. abc.gsub(p, function (c) {res.s = res.s + c;})
  449. return res.s
  450. }
  451. sqt.ok(strset("[\xC8-\xD2]").len() == 11)
  452. sqt.ok(strset("[a-z]") == "abcdefghijklmnopqrstuvwxyz")
  453. sqt.ok(strset("[a-z%d]") == strset("[%da-uu-z]"))
  454. sqt.ok(strset("[a-]") == "-a")
  455. sqt.ok(strset("[^%W]") == strset("[%w]"))
  456. sqt.ok(strset("[]%%]") == "%]")
  457. sqt.ok(strset("[a%-z]") == "-az")
  458. sqt.ok(strset("[%^%[%-a%]%-b]") == "-[]^ab")
  459. sqt.ok(strset("%Z") == strset("[\x01-\xff]"))
  460. sqt.ok(strset(".") == strset("[\x01-\xff%z]"))
  461. //print("+");
  462. sqt.ok("alo xyzK".match("(%w+)K") == "xyz")
  463. sqt.ok("254 K".match("(%d*)K") == "")
  464. sqt.ok("alo ".match("(%w*)$") == "")
  465. sqt.ok("alo ".match("(%w+)$") == null)
  466. sqt.ok("(álo)".find_lua("%(á") == 0)
  467. a = b = c = d = e =null
  468. "âlo alo".gmatch("^(((.).).* (%w*))$", function(ma, mb, mc, md){ a=ma; b=mb; c=mc; d=md;})
  469. sqt.ok(a == "âlo alo" && b == "âl" && c == "â" && d == "alo" && e == null)
  470. a = b = c = d = e =null
  471. "0123456789".gmatch("(.+(.?)())", function(ma, mb, mc){ a=ma; b=mb; c=mc;})
  472. sqt.ok(a == "0123456789" && b == "" && c == 10 && d == null)
  473. //print("+")
  474. sqt.ok("ülo ülo".gsub("ü", "x") == "xlo xlo")
  475. sqt.ok("alo úlo ".gsub(" +$", "") == "alo úlo") // trim
  476. sqt.ok(" alo alo ".gsub("^%s*(.-)%s*$", "%1") == "alo alo") // double trim
  477. sqt.ok("alo alo \n 123\n ".gsub("%s+", " ") == "alo alo 123 ")
  478. t = "abç d"
  479. a = t.gsub("(.)", "%1@")
  480. sqt.ok("@"+ a == t.gsub("", "@"))
  481. a = "abçd".gsub("(.)", "%0@", 2)
  482. sqt.ok(a == "a@b@çd")
  483. sqt.ok("alo alo".gsub("()[al]", "%1") == "12o 56o")
  484. sqt.ok("abc=xyz".gsub("(%w*)(%p)(%w+)", "%3%2%1-%0") ==
  485. "xyz=abc-abc=xyz")
  486. sqt.ok("abc".gsub("%w", "%1%0") == "aabbcc")
  487. sqt.ok("abc".gsub("%w+", "%0%1") == "abcabc")
  488. sqt.ok("áéí".gsub("$", "\0óú") == "áéí\0óú")
  489. sqt.ok("".gsub("^", "r") == "r")
  490. sqt.ok("".gsub("$", "r") == "r")
  491. //print("+")
  492. sqt.ok("um (dois) tres (quatro)".gsub("(%(%w+%))", function(m) {return m.toupper();}) ==
  493. "um (DOIS) tres (QUATRO)")
  494. {
  495. local function setglobal (n,v) {table_rawset(globals, n, v) }
  496. "ga=roberto,groberto=ga".gsub("(%w+)=(%w%w*)", setglobal)
  497. sqt.ok(ga=="roberto" && groberto=="ga")
  498. }
  499. f = function (a,b) {return a.gsub(".",b);}
  500. sqt.ok("trocar tudo em |teste|b| é |beleza|al|".gsub("|([^|]*)|([^|]*)|", f) ==
  501. "trocar tudo em bbbbb é alalalalalal")
  502. local function dostring (s) {return (loadstring(s)() || "").tostring();}
  503. sqt.ok("alo $a<-1$ novamente $return a$".gsub("$([^$]*)%$", dostring) ==
  504. "alo novamente 1")
  505. x = "$x<-\"alo\".gsub(\".\", function(m){return m.toupper()})$ assim vai para $return x$".gsub(
  506. "$([^$]*)%$", dostring)
  507. sqt.ok(x == " assim vai para ALO")
  508. t = []
  509. s = "a alo jose joao"
  510. r = s.gsub("()(%w+)()", function (a,w,b)
  511. {
  512. sqt.ok(w.len() == b-a);
  513. //print(b-a)
  514. t.append(b-a);
  515. return w;
  516. })
  517. sqt.ok(s == r && t[0] == 1 && t[1] == 3 && t[2] == 4 && t[3] == 4)
  518. function isbalanced (s)
  519. {
  520. return s.gsub("%b()", "").find_lua("[()]") < 0
  521. }
  522. sqt.ok(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a"))
  523. sqt.ok(! isbalanced("(9 ((8) 7) a b (\0 c) a"))
  524. sqt.ok("alo 'oi' alo".gsub("%b''", "\"") == "alo \" alo")
  525. t = ["apple", "orange", "lime"]
  526. local tn = 0;
  527. sqt.ok("x and x and x".gsub("x", function (m) {return t[tn++];})
  528. == "apple and orange and lime")
  529. t = []
  530. "first second word".gsub("%w%w*", function (w) {t.append(w); return w;})
  531. sqt.ok(t[0] == "first" && t[1] == "second" && t[2] == "word" && t.len() == 3)
  532. t = []
  533. sqt.ok("first second word".gsub("%w+",
  534. function (w) {t.append(w); return w;}, 2) == "first second word")
  535. sqt.ok(t[0] == "first" && t[1] == "second" && t.len() == 2)
  536. function mygsub(str, p, fnc)
  537. {
  538. try {
  539. return str.gsub(p, fnc);
  540. } catch(e) {
  541. //print(e);
  542. return false;
  543. }
  544. }
  545. //print("testing invalid patterns")
  546. sqt.ok(! mygsub("alo", "(.", print))
  547. sqt.ok(! mygsub("alo", ".)", print))
  548. sqt.ok(! mygsub("alo", "(.", {}))
  549. sqt.ok(! mygsub("alo", "(.)", "%2"))
  550. sqt.ok(! mygsub("alo", "(%1)", "a"))
  551. sqt.ok(! mygsub("alo", "(%0)", "a"))
  552. // big strings
  553. a = "a".rep(300000)
  554. sqt.ok(a.find_lua("^a*.?$") >= 0)
  555. sqt.ok(a.find_lua("^a*.?b$") < 0)
  556. sqt.ok(a.find_lua("^a-.?$") >= 0)
  557. // deep nest of gsubs
  558. local rev;
  559. rev = function(s)
  560. {
  561. return s.gsub("(.)(.+)", function (c,s1) {return rev(s1) + c;})
  562. }
  563. x = "012345".rep(8) // using 9 and above thow max_natcalls
  564. sqt.ok(rev(rev(x)) == x)
  565. // gsub with tables
  566. sqt.ok("alo alo".gsub(".", {}) == "alo alo")
  567. sqt.ok("alo alo".gsub("(.)", {a="AA", l=""}) == "AAo AAo")
  568. sqt.ok("alo alo".gsub("(.).", {a="AA", l="K"}) == "AAo AAo")
  569. //sqt.ok("alo alo".gsub("((.)(.?))", {al="AA", o=false}) == "AAo AAo")
  570. sqt.ok("alo alo".gsub("().", [2,5,6]) == "256 alo")
  571. t = {}; table_setdelegate(t, {_get = function (s) {return s.toupper();}})
  572. sqt.ok("a alo b hi".gsub("%w%w+", t) == "a ALO b HI")
  573. // tests for gmatch
  574. //sqt.ok(string.gfind == string.gmatch)
  575. a = 0
  576. "abcde".gmatch("()", function(sp) {sqt.ok(sp == a); a=sp+1; return true;})
  577. sqt.ok(a==5)
  578. t = []
  579. "first second word".gmatch("(%w+)", function(w) {t.append(w); return true;})
  580. sqt.ok(t[0] == "first" && t[1] == "second" && t[2] == "word")
  581. t = [2, 5, 8]
  582. "xuxx uu ppar r".gmatch ("()(.)%2", function(sp, ac) {
  583. sqt.ok(sp == t.remove(0))
  584. return true
  585. })
  586. sqt.ok(t.len() == 0)
  587. t = {}
  588. "13 14 10 = 11, 15= 16, 22=23".gmatch("(%d+)%s*=%s*(%d+)",
  589. function(i,j){ t[i] <- j; return true;})
  590. a = 0
  591. foreach( k,v in t) {sqt.ok(k.tointeger()+1 == v.tointeger()); a=a+1;}
  592. sqt.ok(a == 3)
  593. // tests for `%f' (`frontiers')
  594. sqt.ok("aaa aa a aaa a".gsub("%f[%w]a", "x") == "xaa xa x xaa x")
  595. sqt.ok("[[]] [][] [[[[".gsub("%f[[].", "x") == "x[]] x]x] x[[[")
  596. sqt.ok("01abc45de3".gsub("%f[%d]", ".") == ".01abc.45de.3")
  597. sqt.ok("01abc45 de3x".gsub("%f[%D]%w", ".") == "01.bc45 de3.")
  598. sqt.ok("function".gsub("%f[\x01-\xff]%w", ".") == ".unction")
  599. sqt.ok("function".gsub("%f[^\x01-\xff]", ".") == "function.")
  600. local ma = []
  601. " alo aalo allo".find_lua("%f[%S].-%f[%s].-%f[%S]", ma)
  602. //print(ma.len(), ma[0], ma[1])
  603. sqt.ok(ma[0] == 1 && ma[1] == 5)
  604. local k = " alo aalo allo".match("%f[%S](.-%f[%s].-%f[%S])")
  605. sqt.ok(k == "alo ")
  606. a = [0, 4, 8, 13, 16]
  607. "alo alo th02 is 1hat".gmatch("()%f[%w%d]",
  608. function(sp) {
  609. local asp = a.remove(0);
  610. //print("==", asp, sp);
  611. sqt.ok(asp == sp);
  612. return true;
  613. })
  614. sqt.ok(a.len() == 0)
  615. local src_str = "abcdef";
  616. local re_str = "c()";
  617. sqt.ok(src_str.match(re_str) == 3);
  618. re = regexp(re_str);
  619. sqt.ok(re.match(src_str) == 3);
  620. });
  621. sqt.run("regexp", function(){
  622. /*
  623. Adapted from
  624. http://www.theiling.de/cnc/date/2017-12-19.html
  625. http://www.theiling.de/cnc/regexp/test.c
  626. */
  627. const RE_E_OK = 1;
  628. const RE_E_FAIL = 2;
  629. const RE_E_SYNTAX = 3;
  630. const RE_E_NOTIMPL = 3;
  631. local function re_match(re_str, test_str)
  632. {
  633. local result;
  634. try {
  635. local re = regexp(re_str);
  636. result = (re.match(test_str) != null) ? RE_E_OK : RE_E_FAIL;
  637. } catch(e) {result = RE_E_SYNTAX;}
  638. return result;
  639. }
  640. /*
  641. print(re_match("aaa", "aaa"));
  642. print(re_match("aaa", "aab"));
  643. print(re_match("(a|[(])+", "a(aa((a"));
  644. print("bac".match("[\\^a]*"));
  645. */
  646. sqt.ok(re_match("aaa", "aaa") == RE_E_OK);
  647. sqt.ok(re_match("aaa", "aab") == RE_E_FAIL);
  648. sqt.ok(re_match("aaa", "aa") == RE_E_FAIL);
  649. sqt.ok(re_match("aa", "aaa") == RE_E_OK);
  650. sqt.ok(re_match("a..", "aaa") == RE_E_OK);
  651. sqt.ok(re_match("aaa", "a..") == RE_E_FAIL);
  652. sqt.ok(re_match("a{}", "") == RE_E_SYNTAX);
  653. sqt.ok(re_match("(aaa)", "aaa") == RE_E_OK);
  654. sqt.ok(re_match("(aa)a", "aaa") == RE_E_OK);
  655. sqt.ok(re_match("(aaa", "aaa") == RE_E_SYNTAX);
  656. sqt.ok(re_match("a+", "") == RE_E_FAIL);
  657. sqt.ok(re_match("a+", "aaa") == RE_E_OK);
  658. sqt.ok(re_match(".+", "aaa") == RE_E_OK);
  659. sqt.ok(re_match("(a)+", "aaa") == RE_E_OK);
  660. sqt.ok(re_match("(aa)+", "aaaa") == RE_E_OK);
  661. //sqt.ok(re_match("(aa)+", "aaa") == RE_E_FAIL);
  662. sqt.ok(re_match("(aa(b)+)+", "aabaabb") == RE_E_OK);
  663. //sqt.ok(re_match("(aa(b)+)+", "aaabaabb") == RE_E_FAIL);
  664. //sqt.ok(re_match("(aa(b)+)+", "aaaabb") == RE_E_FAIL);
  665. sqt.ok(re_match("a+b", "aaaaaaaaaaaaaaaab") == RE_E_OK);
  666. sqt.ok(re_match("a+b", "aaaaaaaaaaaaaaaa") == RE_E_FAIL);
  667. sqt.ok(re_match("a?", "a") == RE_E_OK);
  668. sqt.ok(re_match("a?", "aa") == RE_E_OK);
  669. sqt.ok(re_match("a?b", "b") == RE_E_OK);
  670. //sqt.ok(re_match("a?", "") == RE_E_OK);
  671. //sqt.ok(re_match("(a?)?", "") == RE_E_OK);
  672. //sqt.ok(re_match("a*", "") == RE_E_OK);
  673. //sqt.ok(re_match("(a?)+", "") == RE_E_OK);
  674. //sqt.ok(re_match("?", "") == RE_E_SYNTAX);
  675. sqt.ok(re_match("a*b", "b") == RE_E_OK);
  676. //sqt.ok(re_match("(a+)?", "") == RE_E_OK);
  677. sqt.ok(re_match("a+b", "b") == RE_E_FAIL);
  678. sqt.ok(re_match("a+b", "ab") == RE_E_OK);
  679. sqt.ok(re_match("a+b", "aab") == RE_E_OK);
  680. sqt.ok(re_match("a+b", "aaab") == RE_E_OK);
  681. sqt.ok(re_match("a{2}b", "ab") == RE_E_FAIL);
  682. sqt.ok(re_match("a{2}b", "aab") == RE_E_OK);
  683. sqt.ok(re_match("a{2}b", "aaab") == RE_E_OK);
  684. sqt.ok(re_match("a{2,}b", "aaab") == RE_E_OK);
  685. sqt.ok(re_match("a{2,}b", "aab") == RE_E_OK);
  686. sqt.ok(re_match("a{2,}b", "ab") == RE_E_FAIL);
  687. sqt.ok(re_match("a*b", "aaab") == RE_E_OK);
  688. sqt.ok(re_match("a{1}", "a") == RE_E_OK);
  689. sqt.ok(re_match("a{2}", "a") == RE_E_FAIL);
  690. sqt.ok(re_match("a{2}", "aa") == RE_E_OK);
  691. sqt.ok(re_match("a{2}", "aaa") == RE_E_OK);
  692. sqt.ok(re_match("a{3}", "aa") == RE_E_FAIL);
  693. sqt.ok(re_match("a{3}", "aaa") == RE_E_OK);
  694. sqt.ok(re_match("a{3}", "aaaa") == RE_E_OK);
  695. sqt.ok(re_match("a{2,4}", "a") == RE_E_FAIL);
  696. sqt.ok(re_match("a{2,4}", "aa") == RE_E_OK);
  697. sqt.ok(re_match("a{2,4}", "aaa") == RE_E_OK);
  698. sqt.ok(re_match("a{2,4}", "aaaa") == RE_E_OK);
  699. sqt.ok(re_match("a{2,4}", "aaaaa") == RE_E_OK);
  700. sqt.ok(re_match("ab{2,4}", "a") == RE_E_FAIL);
  701. sqt.ok(re_match("ab{2,4}", "ab") == RE_E_FAIL);
  702. sqt.ok(re_match("ab{2,4}", "abb") == RE_E_OK);
  703. sqt.ok(re_match("ab{2,4}", "abbb") == RE_E_OK);
  704. sqt.ok(re_match("ab{2,4}", "abbbb") == RE_E_OK);
  705. sqt.ok(re_match("ab{2,4}", "abbbbb") == RE_E_OK);
  706. sqt.ok(re_match("ab{2,4}", "aabbbb") == RE_E_OK);
  707. sqt.ok(re_match("a{,2}b{2,4}", "aabbbb") == RE_E_SYNTAX);
  708. sqt.ok(re_match("a{0,2}b{2,4}", "aabbbb") == RE_E_OK);
  709. sqt.ok(re_match("a)", "") == RE_E_SYNTAX);
  710. //sqt.ok(re_match("(a+b+)?", "") == RE_E_OK);
  711. sqt.ok(re_match("(a{2}b+)*c", "aabc") == RE_E_OK);
  712. //sqt.ok(re_match("(a{2}b+)*c", "c") == RE_E_OK);
  713. sqt.ok(re_match("(a{2}b+)*", "aab") == RE_E_OK);
  714. //sqt.ok(re_match("(a{2}b+)*", "") == RE_E_OK);
  715. sqt.ok(re_match("(a{2}b+)?", "aab") == RE_E_OK);
  716. //sqt.ok(re_match("(a{2}b+)?", "") == RE_E_OK);
  717. sqt.ok(re_match("(ab){2,4}", "a") == RE_E_FAIL);
  718. sqt.ok(re_match("(ab){2,4}", "b") == RE_E_FAIL);
  719. sqt.ok(re_match("(ab){2,4}", "") == RE_E_FAIL);
  720. sqt.ok(re_match("(ab){2,4}", "ab") == RE_E_FAIL);
  721. sqt.ok(re_match("(ab){2,4}", "abab") == RE_E_OK);
  722. sqt.ok(re_match("(ab){2,4}", "ababab") == RE_E_OK);
  723. sqt.ok(re_match("(ab){2,4}", "abababab") == RE_E_OK);
  724. sqt.ok(re_match("(ab){2,4}", "ababababab") == RE_E_OK);
  725. sqt.ok(re_match("ab", "ab") == RE_E_OK);
  726. sqt.ok(re_match("ab", "abab") == RE_E_OK);
  727. sqt.ok(re_match("(ab)", "ab") == RE_E_OK);
  728. sqt.ok(re_match("(ab)", "abab") == RE_E_OK);
  729. sqt.ok(re_match("(ab){1}", "ab") == RE_E_OK);
  730. sqt.ok(re_match("(ab){1}", "abab") == RE_E_OK);
  731. sqt.ok(re_match("(ab){1,1}", "ab") == RE_E_OK);
  732. sqt.ok(re_match("(ab){1,1}", "abab") == RE_E_OK);
  733. //sqt.ok(re_match("(ab){1}{1,1}", "ab") == RE_E_OK);
  734. sqt.ok(re_match("(ab){1}{1,1}", "abab") == RE_E_FAIL);
  735. sqt.ok(re_match("((ab){2,4}c)+", "") == RE_E_FAIL);
  736. //sqt.ok(re_match("(((ab){2,4}c)+)?", "") == RE_E_OK);
  737. //sqt.ok(re_match("((ab){2,4}c)+?", "") == RE_E_OK);
  738. sqt.ok(re_match("((ab){2,4}c)+", "ababc") == RE_E_OK);
  739. sqt.ok(re_match("((ab){2,4}c)+", "ababcabababc") == RE_E_OK);
  740. //sqt.ok(re_match("((ab){2,4}c)+?", "ababcabababc") == RE_E_OK);
  741. //sqt.ok(re_match("((ab){2,4}c)+?", "") == RE_E_OK);
  742. sqt.ok(re_match("a{0,1}", "a") == RE_E_OK);
  743. sqt.ok(re_match("a{1,1}", "a") == RE_E_OK);
  744. sqt.ok(re_match("a{0,}", "a") == RE_E_OK);
  745. sqt.ok(re_match("a{,}", "a") == RE_E_SYNTAX);
  746. sqt.ok(re_match("a{,}", "a") == RE_E_SYNTAX);
  747. sqt.ok(re_match("a{,g}", "a") == RE_E_SYNTAX);
  748. sqt.ok(re_match("a{,1g}", "a") == RE_E_SYNTAX);
  749. sqt.ok(re_match("a{0,g}", "a") == RE_E_SYNTAX);
  750. sqt.ok(re_match("a{0,1g}", "a") == RE_E_SYNTAX);
  751. //sqt.ok(re_match("a{1,0}", "a") == RE_E_SYNTAX);
  752. //sqt.ok(re_match("a{1,0}", "a") == RE_E_SYNTAX);
  753. //sqt.ok(re_match("a{0}", "a") == RE_E_FAIL);
  754. //sqt.ok(re_match("a{0,0}", "a") == RE_E_FAIL);
  755. //sqt.ok(re_match("a++", "aaa") == RE_E_OK);
  756. //sqt.ok(re_match("a??", "") == RE_E_OK);
  757. //sqt.ok(re_match("a**", "") == RE_E_OK);
  758. //sqt.ok(re_match("a?*", "") == RE_E_OK);
  759. //sqt.ok(re_match("a*?", "") == RE_E_OK);
  760. sqt.ok(re_match("a?", "a") == RE_E_OK);
  761. //sqt.ok(re_match("a??", "a") == RE_E_OK);
  762. //sqt.ok(re_match("a**", "a") == RE_E_OK);
  763. //sqt.ok(re_match("a?*", "a") == RE_E_OK);
  764. //sqt.ok(re_match("a*?", "a") == RE_E_OK);
  765. sqt.ok(re_match("a?", "aa") == RE_E_OK);
  766. //sqt.ok(re_match("a??", "aa") == RE_E_FAIL);
  767. //sqt.ok(re_match("a**", "aa") == RE_E_OK);
  768. //sqt.ok(re_match("a?*", "aa") == RE_E_OK);
  769. //sqt.ok(re_match("a*?", "aa") == RE_E_OK);
  770. //sqt.ok(re_match("a+?", "") == RE_E_OK);
  771. //sqt.ok(re_match("a?+", "") == RE_E_OK);
  772. //sqt.ok(re_match("a+?b", "b") == RE_E_OK);
  773. sqt.ok(re_match("a\\rb", "a\rb") == RE_E_OK);
  774. sqt.ok(re_match("a+\n+b+", "aa\n\nbb") == RE_E_OK);
  775. sqt.ok(re_match("a+\\n+b+", "aa\n\nbb") == RE_E_OK);
  776. //sqt.ok(re_match("a+\\5+b+", "aa\n\nbb") == RE_E_SYNTAX);
  777. sqt.ok(re_match("a+\\(+b+", "aa((bb") == RE_E_OK);
  778. sqt.ok(re_match("[a]+", "a") == RE_E_OK);
  779. sqt.ok(re_match("[a]+", "aa") == RE_E_OK);
  780. sqt.ok(re_match("[a]+", "aaa") == RE_E_OK);
  781. sqt.ok(re_match("[ba]+", "a") == RE_E_OK);
  782. sqt.ok(re_match("[ba]+", "ab") == RE_E_OK);
  783. sqt.ok(re_match("[ba]+", "baaa") == RE_E_OK);
  784. sqt.ok(re_match("[ba]+", "c") == RE_E_FAIL);
  785. sqt.ok(re_match("[\\n]+", "\n") == RE_E_OK);
  786. sqt.ok(re_match("[\\n]+", "\n\n") == RE_E_OK);
  787. sqt.ok(re_match("[\\n]+", "\n\n\n") == RE_E_OK);
  788. sqt.ok(re_match("[a-c]+", "aaa") == RE_E_OK);
  789. sqt.ok(re_match("[a-c]+", "ccc") == RE_E_OK);
  790. sqt.ok(re_match("[a-c]+", "bbb") == RE_E_OK);
  791. sqt.ok(re_match("[a", "bbb") == RE_E_SYNTAX);
  792. sqt.ok(re_match("[a-", "bbb") == RE_E_SYNTAX);
  793. sqt.ok(re_match("[a-b", "bbb") == RE_E_SYNTAX);
  794. sqt.ok(re_match("[a-\\b", "bbb") == RE_E_NOTIMPL);
  795. //sqt.ok(re_match("a\\bb", "bbb") == RE_E_NOTIMPL);
  796. sqt.ok(re_match("[a-\\n", "bbb") == RE_E_NOTIMPL);
  797. sqt.ok(re_match("[\\n-a", "bbb") == RE_E_NOTIMPL);
  798. //sqt.ok(re_match("[-]", "bbb") == RE_E_NOTIMPL);
  799. //sqt.ok(re_match("[a-b-c]", "bbb") == RE_E_NOTIMPL);
  800. sqt.ok(re_match("[a--]", "bbb") == RE_E_NOTIMPL);
  801. //sqt.ok(re_match("[a-z-[bc]]", "bbb") == RE_E_NOTIMPL);
  802. sqt.ok(re_match("0[xX][\\da-fA-F]{1,4}", "0x12a6") == RE_E_OK);
  803. //sqt.ok(re_match("0[xX][\\da-fA-F]{1,4}", "0x12ag") == RE_E_FAIL);
  804. sqt.ok(re_match("(a|c)+", "aa") == RE_E_OK);
  805. sqt.ok(re_match("(a|c)+", "cc") == RE_E_OK);
  806. sqt.ok(re_match("(a|c)+", "cccc") == RE_E_OK);
  807. sqt.ok(re_match("(a|c)+", "aaaa") == RE_E_OK);
  808. sqt.ok(re_match("(a|c)+", "ca") == RE_E_OK);
  809. sqt.ok(re_match("(a|c)+", "caca") == RE_E_OK);
  810. sqt.ok(re_match("(a|b|c|d)+", "aa") == RE_E_OK);
  811. sqt.ok(re_match("(a|b|c|d)+", "bb") == RE_E_OK);
  812. sqt.ok(re_match("(a|b|c|d)+", "cc") == RE_E_OK);
  813. sqt.ok(re_match("(a|b|c|d)+", "dd") == RE_E_OK);
  814. sqt.ok(re_match("(a|b|c)+", "aa") == RE_E_OK);
  815. sqt.ok(re_match("(a|b|c)+", "bb") == RE_E_OK);
  816. sqt.ok(re_match("(a|b|c)+", "cc") == RE_E_OK);
  817. sqt.ok(re_match("(a|b|c)+", "abc") == RE_E_OK);
  818. sqt.ok(re_match("(a|b|c)+", "cba") == RE_E_OK);
  819. sqt.ok(re_match("(a|b|c)+", "cccc") == RE_E_OK);
  820. sqt.ok(re_match("(a|b|c)+", "aa") == RE_E_OK);
  821. sqt.ok(re_match("(a|b|c)+", "aaaa") == RE_E_OK);
  822. sqt.ok(re_match("(a|b|c)+", "ca") == RE_E_OK);
  823. sqt.ok(re_match("(a|b|c)+", "caca") == RE_E_OK);
  824. sqt.ok(re_match("(ab|cd|ed)+", "abcded") == RE_E_OK);
  825. //sqt.ok(re_match("(?:ab)", "a") == RE_E_SYNTAX);
  826. //sqt.ok(re_match("[^a]*", "") == RE_E_OK);
  827. sqt.ok(re_match("[^a]+", "") == RE_E_FAIL);
  828. sqt.ok(re_match("[^a]*", "b") == RE_E_OK);
  829. sqt.ok(re_match("[^a]*", "bcbd") == RE_E_OK);
  830. //sqt.ok(re_match("[^a]*", "a") == RE_E_FAIL);
  831. sqt.ok(re_match("[^a]*", "bac") == RE_E_OK);
  832. sqt.ok(re_match("[^ab]*", "bac") == RE_E_OK);
  833. sqt.ok(re_match("[^ab]*", "cded") == RE_E_OK);
  834. sqt.ok(re_match("[\\^a]*", "bac") == RE_E_OK);
  835. sqt.ok(re_match("[\\^a]*", "a") == RE_E_OK);
  836. sqt.ok(re_match("[\\^a]*", "a^") == RE_E_OK);
  837. sqt.ok(re_match("(a|[\\(])+", "a(aa((a") == RE_E_OK);
  838. sqt.ok(re_match("(a|[\\[])+", "a[aa[[a") == RE_E_OK);
  839. sqt.ok(re_match("(a|[(])+", "a(aa((a") == RE_E_OK);
  840. sqt.ok(re_match("(a|[[])+", "a(aa((a") == RE_E_OK);
  841. sqt.ok(re_match("\\D*", "bac") == RE_E_OK);
  842. sqt.ok(re_match("\\D*", "b0c") == RE_E_OK);
  843. sqt.ok(re_match("\\S*", "bac") == RE_E_OK);
  844. sqt.ok(re_match("\\S*", "b c") == RE_E_OK);
  845. sqt.ok(re_match(".", "") == RE_E_FAIL);
  846. sqt.ok(re_match("a.b", "acb") == RE_E_OK);
  847. sqt.ok(re_match("a.b", "a\nb") == RE_E_OK);
  848. });
  849. sqt.run("table", function(){
  850. local t = table_new(10);
  851. local t2 = obj_clone(t);
  852. sqt.ok((type(t2) == "table") && (t != t2));
  853. sqt.ok(table_len(t) == 0);
  854. table_rawset(t, "one", 1);
  855. sqt.ok(table_len(t) == 1 && (table_get(t, "one") == 1));
  856. table_set(t, "one", 10);
  857. sqt.ok((table_get(t, "one") == 10));
  858. table_rawset(t, "two", 2);
  859. sqt.ok(table_len(t) == 2 && (table_get(t, "two") == 2));
  860. local ary = table_toarray(t);
  861. sqt.ok(type(ary) == "array");
  862. sqt.ok(ary.len() == table_len(t));
  863. sqt.ok(type(ary[0]) == "array");
  864. sqt.ok(type(ary[1]) == "array");
  865. sqt.ok(ary[0][1] == table_rawget(t, ary[0][0]));
  866. sqt.ok(ary[1][1] == table_rawget(t, ary[1][0]));
  867. table_rawdelete(t, "one");
  868. sqt.ok(table_len(t) == 1 && (table_get(t, "two") == 2));
  869. sqt.ok(table_rawin(t, "two"));
  870. table_clear(t);
  871. sqt.ok(table_len(t) == 0);
  872. sqt.ok({ abc = "abc" } != { abc = "abc" });
  873. });
  874. sqt.run("array", function(){
  875. local t = array(0);
  876. local t2 = obj_clone(t);
  877. sqt.ok((type(t2) == "array") && (t != t2));
  878. sqt.ok(t.len() == 0);
  879. t.append(1);
  880. sqt.ok(t.len() == 1 && (t.get(0) == 1));
  881. t.clear();
  882. sqt.ok(t.len() == 0);
  883. sqt.ok([ 0, 1, 2 ] != [ 0, 1, 2 ]);
  884. t = ["one"];
  885. sqt.ok(t.concat(",") == "one");
  886. t = ["one", "two"];
  887. sqt.ok(t.concat(", ") == "one, two");
  888. sqt.ok(t.len() == 2);
  889. sqt.ok(t.capacity() == 2);
  890. t.reserve(100);
  891. sqt.ok(t.len() == 2);
  892. sqt.ok(t.capacity() == 100);
  893. t.clear();
  894. sqt.ok(t.len() == 0);
  895. sqt.ok(t.capacity() == 100);
  896. t.extend([1,2,3,4,5,6,7,8,9,10]);
  897. sqt.ok(t.len() == 10);
  898. sqt.ok(t.capacity() == 100);
  899. t.minsize(5);
  900. sqt.ok(t.len() == 10);
  901. sqt.ok(t.capacity() == 100);
  902. t.resize(5);
  903. sqt.ok(t.len() == 5);
  904. sqt.ok(t.capacity() == t.len());
  905. if(table_rawget(globals, "array_float32", false))
  906. {
  907. local function testArray(arr, sz, val)
  908. {
  909. local val2 = val * 2;
  910. local val3 = val * 3;
  911. arr.append(val2);
  912. arr.append(val);
  913. arr.append(val3);
  914. sqt.ok(arr.len() == 3 && (arr.get(1) == val));
  915. arr.sort();
  916. sqt.ok((arr[0] == val) && (arr[1] == val2) && (arr[2] == val3));
  917. arr.clear();
  918. sqt.ok(arr.len() == 0);
  919. sqt.ok(arr.sizeofelm() == sz);
  920. local asize = 20;
  921. arr.resize(asize);
  922. for(local i=0; i < asize; ++i) arr[i] = i+asize;
  923. local arr2 = obj_clone(arr);
  924. for(local i=0; i < asize; ++i) sqt.ok(arr[i] == arr2[i]);
  925. sqt.ok(arr.sizeofelm() == arr2.sizeofelm());
  926. local arr3 = arr2.slice(2, 18);
  927. sqt.ok(arr3.sizeofelm() == arr2.sizeofelm());
  928. sqt.ok(arr3[0] == arr2[2]);
  929. sqt.ok(arr3.top() == arr2[17]);
  930. try { arr.resize(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
  931. try { arr.reserve(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
  932. }
  933. if(_intsize_ == 8)
  934. {
  935. t = array_int64(0);
  936. testArray(t, 8, 1);
  937. }
  938. if(_floatsize_ == 8)
  939. {
  940. t = array_float64(0);
  941. testArray(t, 8, 1.0);
  942. }
  943. t = array_float32(0);
  944. testArray(t, 4, 1.0);
  945. t = array_int32(0);
  946. testArray(t, 4, 1);
  947. t = array_int16(0);
  948. testArray(t, 2, 1);
  949. t = array_int8(0);
  950. testArray(t, 1, 1);
  951. }
  952. try { t.resize(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
  953. try { t.reserve(-5); sqt.ok(false);} catch(e) {sqt.ok(true);};
  954. });
  955. // core/string/index_of_start.wren
  956. sqt.run("string", function(){
  957. sqt.ok( ("a".toupper() + "A".tolower()) == "Aa");
  958. sqt.ok( ("à".sl_upper() + "À".sl_lower()) == "Àà");
  959. sqt.ok(type("s") == "string");
  960. sqt.ok("a" + "b" == "ab");
  961. local str = "abcde";
  962. sqt.ok(str.slice(0, 0) == "");
  963. sqt.ok(str.slice(1, 1) == "");
  964. sqt.ok(str.slice(1, 3) == "bc");
  965. sqt.ok(str.slice(1, 2) == "b");
  966. sqt.ok(str.slice(2) == "cde");
  967. sqt.ok(str.slice(2, 5) == "cde");
  968. //sqt.ok(str.slice(3, 1) == "dcb");
  969. //sqt.ok(str.slice(3, 1) == "dc");
  970. sqt.ok(str.slice(3, 3) == "");
  971. sqt.ok(str.slice(-5, -2) == "abc");
  972. //sqt.ok(str.slice(-3, -5) == "cba");
  973. //sqt.ok(str.slice(-3, -6) == "cba");
  974. sqt.ok(str.slice(-5, 3) == "abc");
  975. sqt.ok(str.slice(-3, 5) == "cde");
  976. //sqt.ok(str.slice(-2, 1) == "dcb");
  977. //sqt.ok(str.slice(-2, 0) == "dcb");
  978. sqt.ok(str.slice(1, -2) == "bc");
  979. sqt.ok(str.slice(2, -1) == "cd");
  980. //sqt.ok(str.slice(4, -5) == "edcba");
  981. //sqt.ok(str.slice(3, -6) == "dcba");
  982. sqt.ok("".slice(0, 0) == "");
  983. //sqt.ok("".slice(0, -1) == "");
  984. sqt.ok("abc".slice(3, 3) == "");
  985. //sqt.ok("abc".slice(3, -1) == "");
  986. sqt.ok("søméஃthîng".slice(0, 3) == "sø");
  987. sqt.ok("søméஃthîng".slice(3, 10) == "méஃt");
  988. sqt.ok("søméஃthîng".slice(3, 9) == "méஃ");
  989. sqt.ok("søméஃthîng".slice(3, 6) == "mé");
  990. sqt.ok("".tostring() == "");
  991. sqt.ok("blah".tostring() == "blah");
  992. sqt.ok("a\0b\0c".tostring() == "a\0b\0c");
  993. sqt.ok("a\0b\0c".tostring() != "a");
  994. sqt.ok("abcd"[0] == 'a');
  995. sqt.ok("abcd"[1] == 'b');
  996. sqt.ok("abcd"[2] == 'c');
  997. sqt.ok("abcd"[3] == 'd');
  998. sqt.ok("abcd"[-4] == 'a');
  999. sqt.ok("abcd"[-3] == 'b');
  1000. sqt.ok("abcd"[-2] == 'c');
  1001. sqt.ok("abcd"[-1] == 'd');
  1002. sqt.ok("abcd"[1] == 'b');
  1003. sqt.ok("søméஃthîng"[0] == 's');
  1004. //sqt.ok("søméஃthîng"[1] == 'ø');
  1005. sqt.ok("søméஃthîng"[3] == 'm');
  1006. //sqt.ok("søméஃthîng"[6] == 'ஃ');
  1007. sqt.ok("søméஃthîng"[10] == 'h');
  1008. sqt.ok("søméஃthîng"[-1] == 'g');
  1009. sqt.ok("søméஃthîng"[-2] == 'n');
  1010. //sqt.ok("søméஃthîng"[-4] == 'î');
  1011. sqt.ok("søméஃthîng"[2] == '\xb8');
  1012. sqt.ok("søméஃthîng"[7] == '\xae');
  1013. sqt.ok("søméஃthîng"[8] == '\x83');
  1014. sqt.ok("søméஃ"[-1] == '\x83');
  1015. sqt.ok("søméஃ"[-2] == '\xae');
  1016. sqt.ok("a\0b\0c"[0] == 'a');
  1017. sqt.ok("a\0b\0c"[1] == '\0');
  1018. sqt.ok("a\0b\0c"[2] == 'b');
  1019. sqt.ok("a\0b\0c"[3] == '\0');
  1020. sqt.ok("a\0b\0c"[4] == 'c');
  1021. sqt.ok("\xef\xf7"[0] == '\xef');
  1022. sqt.ok("\xef\xf7"[1] == '\xf7');
  1023. //sqt.ok("something".split("meth") == ["so", "ing"]);
  1024. //sqt.ok("something".split("some") == ["thing"]);
  1025. //sqt.ok("something".split("math") == ["something"]);
  1026. //sqt.ok("somethingsomething".split("meth") == ["so", "ingso", "ing"]);
  1027. //sqt.ok("abc abc abc".split(" ") == ["abc", "abc", "abc"]);
  1028. //sqt.ok("abcabcabc".split("abc") == [, , , ]);
  1029. //sqt.ok("søméthîng".split("meth") == ["søméthîng"]);
  1030. //sqt.ok("a\0b\0c".split("\0") == ["a", "b", "c"]);
  1031. sqt.ok("something".replace("some", "no") == "nothing");
  1032. sqt.ok("something".replace("thing", "one") == "someone");
  1033. sqt.ok("something".replace("math", "ton") == "something");
  1034. sqt.ok("somethingsomething".replace("some", "no") == "nothingnothing");
  1035. sqt.ok("abc abc abc".replace(" ", "") == "abcabcabc");
  1036. sqt.ok("søméthîng".replace("meth", "ton") == "søméthîng");
  1037. sqt.ok("a\0b\0c".replace("\0", "") == "abc");
  1038. sqt.ok("a\0b\0c".replace("b", "") == "a\0\0c");
  1039. sqt.ok(!"s" == false);
  1040. sqt.ok(!"" == false);
  1041. sqt.ok("a string".len() == 8);
  1042. //sqt.ok("søméஃthîng".len() == 10);
  1043. sqt.ok("\xefok\xf7".len() == 4);
  1044. sqt.ok("abcd".startswith("cd") == false);
  1045. sqt.ok("abcd".startswith("a") == true);
  1046. sqt.ok("abcd".startswith("abcd") == true);
  1047. sqt.ok("abcd".startswith("abcde") == false);
  1048. sqt.ok("abcd".startswith("") == true);
  1049. sqt.ok("søméthîng".startswith("some") == false);
  1050. sqt.ok("abcd".endswith("cd") == true);
  1051. sqt.ok("abcd".endswith("abcde") == false);
  1052. sqt.ok("abcd".endswith("abcd") == true);
  1053. sqt.ok("abcd".endswith("f") == false);
  1054. sqt.ok("abcd".endswith("") == true);
  1055. sqt.ok("søméthîng".endswith("thing") == false);
  1056. sqt.ok("vålue" != "value");
  1057. sqt.ok("vålue" == "vålue");
  1058. sqt.ok("a\0b\0c" != "a");
  1059. sqt.ok("a\0b\0c" != "abc");
  1060. sqt.ok("a\0b\0c" == "a\0b\0c");
  1061. sqt.ok("abcd".indexOf("") == 0);
  1062. sqt.ok("abcd".indexOf("cd") == 2);
  1063. sqt.ok("abcd".indexOf("a") == 0);
  1064. sqt.ok("abcd".indexOf("abcd") == 0);
  1065. sqt.ok("abcd".indexOf("abcde") == -1);
  1066. sqt.ok("abab".indexOf("ab") == 0);
  1067. sqt.ok("abcdefabcdefg".indexOf("defg") == 9);
  1068. sqt.ok("abcdabcdabcd".indexOf("dab") == 3);
  1069. sqt.ok("abcdabcdabcdabcd".indexOf("dabcdabc") == 3);
  1070. sqt.ok("abcdefg".indexOf("abcdef!") == -1);
  1071. sqt.ok("søméஃthîng".indexOf("e") == -1);
  1072. sqt.ok("søméஃthîng".indexOf("m") == 3);
  1073. sqt.ok("søméஃthîng".indexOf("thî") == 9);
  1074. //sqt.ok("a\0b\0c".indexOf("\0") == 1);
  1075. sqt.ok("a\0b\0c".indexOf("a") == 0);
  1076. //sqt.ok("a\0b\0c".indexOf("b\0c") == 2);
  1077. //sqt.ok("a\0b\0c".indexOf("a\0b\0c\0d") == -1);
  1078. sqt.ok("a\0b\0a\0b".indexOf("a\0b") == 0);
  1079. sqt.ok("abcd".indexOf("") == 0);
  1080. sqt.ok("aaaaa".indexOf("aaaa") == 0);
  1081. sqt.ok("abcd".indexOf("cd") == 2);
  1082. //sqt.ok("here as well as here".indexOf("here") == 16);
  1083. //sqt.ok("a\0b\0c".indexOf("\0") == 1);
  1084. sqt.ok("a\0b\0c".indexOf("a") == 0);
  1085. //sqt.ok("a\0b\0c".indexOf("a\0b\0c\0d") == -1);
  1086. sqt.ok("a\0b\0a\0b".indexOf("a\0b") == 0);
  1087. local base_str = "one two"
  1088. str = "one \
  1089. two";
  1090. sqt.ok(base_str == str);
  1091. str = "one "
  1092. "two";
  1093. sqt.ok(base_str == str);
  1094. sqt.ok(@"foo
  1095. bar" == "foo\nbar");
  1096. sqt.ok("foo \
  1097. bar" == "foo bar");
  1098. sqt.ok("foo "
  1099. "bar" == "foo bar");
  1100. sqt.ok(R"(foo
  1101. bar)" == "foo\nbar");
  1102. sqt.ok([==[foo
  1103. bar]==] == "foo\nbar");
  1104. sqt.ok(("(" + 0x7fffffff + ")") == "(2147483647)");
  1105. sqt.ok(("(" + -0x80000000 + ")") == "(-2147483648)");
  1106. //C/C++ wchar
  1107. sqt.ok(L"fake wchar" == "fake wchar");
  1108. sqt.ok(L'\n' == '\n');
  1109. //utf8
  1110. sqt.ok("aàeèiìoòuù".isvalidutf8());
  1111. sqt.ok("aàeèiìoòuù".utf8Len() == 10);
  1112. sqt.ok("aàeèiìoòuù".len() == 15);
  1113. sqt.ok('\u0031' == 49);
  1114. sqt.ok('\u0531' == 1329);
  1115. });
  1116. sqt.run("number", function(){
  1117. sqt.ok(0.0f == 0.0); //C/C++ notation
  1118. //adapetd from pike https://github.com/pikelang/Pike
  1119. sqt.ok(1e1 == 10.0);
  1120. sqt.ok(1E1 == 10.0);
  1121. sqt.ok(1e+1 == 10.0);
  1122. sqt.ok(1.1e1 == 11.0);
  1123. sqt.ok(1e-1 == 0.1);
  1124. sqt.ok('\x20' == 32);
  1125. sqt.ok("\x20" == "\040");
  1126. //sqt.ok("\d32" == "\x20");
  1127. sqt.ok('Å' == "Å"[0]);
  1128. //sqt.ok('\7777' == "\7777"[0]);
  1129. //sqt.ok("\77777777" == "\77777777"[0]);
  1130. sqt.ok("\x10000" == "\x10000");
  1131. sqt.ok(0x80000000-0x80000000 == 0);
  1132. #ifdef __SQUILU_INT_SZ8__
  1133. sqt.ok(0xf0000000-0xf0000000 == 0);
  1134. #endif
  1135. sqt.ok(0x80000001-0x80000000 == 1);
  1136. sqt.ok(0x80000000-0x80000001 == -1);
  1137. sqt.ok(-2147483648*-1 == -2147483648/-1);
  1138. #ifdef __SQUILU_INT_SZ8__
  1139. sqt.ok(0x8000000000000000-0x8000000000000000 == 0);
  1140. //sqt.ok(0xf000000000000000-0xf000000000000000 == 0);
  1141. sqt.ok(0x8000000000000001-0x8000000000000000 == 1);
  1142. sqt.ok(0x8000000000000000-0x8000000000000001 == -1);
  1143. sqt.ok(-9223372036854775808*-1 == -9223372036854775808/-1);
  1144. #endif
  1145. // https://github.com/pikelang/Pike
  1146. sqt.ok(15 == "0x0F".tointeger(16));
  1147. sqt.ok(15 == "F".tointeger(16));
  1148. sqt.ok(15 == "017".tointeger(8));
  1149. sqt.ok(15 == "17".tointeger(8));
  1150. sqt.ok(15 == "1111".tointeger(2));
  1151. sqt.ok(15 == "00001111".tointeger(2));
  1152. sqt.ok("0.5".tofloat() == 0.5);
  1153. sqt.ok("0.5" == (0.5).tostring());
  1154. sqt.ok("9.99".tofloat() == 9.99);
  1155. sqt.ok("9.99" == (9.99).tostring());
  1156. sqt.ok("12".tointeger() == 12);
  1157. sqt.ok("12" == (12).tostring());
  1158. sqt.ok("12.0".tofloat() == 12.0);
  1159. //sqt.ok("12.0" == (12.0).tostring());
  1160. sqt.ok("12.0e+10".tofloat() == 12.0e+10);
  1161. sqt.ok("-122131242342.0e-10".tofloat() == -122131242342.0e-10);
  1162. //sqt.ok("-122131242342.0e-10" == (-122131242342.0e-10).tostring());
  1163. //sqt.ok(".5".tofloat() == .5);
  1164. sqt.ok("122138760e-2".tofloat() == 122138760e-2);
  1165. //sqt.ok("122138760e-2" == (122138760e-2).tostring());
  1166. // signed and unsigned integer
  1167. sqt.ok( 1 == 1 );
  1168. sqt.ok(-1 == -1);
  1169. //sqt.ok(+1 == +1);
  1170. // signed and unsigned float
  1171. sqt.ok( 1.0 == 1.0 );
  1172. sqt.ok(-1.0 == -1.0);
  1173. // binary
  1174. //sqt.ok( 128 == 0b10000000 );
  1175. //sqt.ok( 128 == 0B10000000 );
  1176. // octal
  1177. //sqt.ok( 8 == 0o10 );
  1178. //sqt.ok( 8 == 0O10 );
  1179. //sqt.ok( 8 == 0_10 );
  1180. // hex
  1181. sqt.ok( 255 == 0xff );
  1182. sqt.ok( 255 == 0Xff );
  1183. // decimal
  1184. //sqt.ok( 999 == 0d999 );
  1185. //sqt.ok( 999 == 0D999 );
  1186. // decimal seperator
  1187. //sqt.ok( 10000000 == 10_000_000 );
  1188. //sqt.ok( 10 == 1_0 );
  1189. // integer with exponent
  1190. sqt.ok( 10.0 == 1e1 );
  1191. sqt.ok(0.1 == 1e-1);
  1192. sqt.ok( 10.0 == 1e+1 );
  1193. // float with exponent
  1194. sqt.ok( 10.0 == 1.0e1 );
  1195. sqt.ok(0.1 == 1.0e-1);
  1196. sqt.ok( 10.0 == 1.0e+1 );
  1197. auto nstr = format("%7.1e", 68.514);
  1198. sqt.ok( (nstr == "6.9e+01") || (nstr == "6.9e+001"));
  1199. sqt.ok(format("%4d %4d %4d %4d %d %#x %#X", 6, 34, 16923, -12, -1, 14, 12) == " 6 34 16923 -12 -1 0xe 0XC");
  1200. sqt.ok((3.123456788 + 0.000000001) == 3.123456789);
  1201. sqt.fequal((3.123456789 + 1), 4.123456789);
  1202. sqt.ok((3.123456790 - 0.000000001) == 3.123456789);
  1203. sqt.ok((5.123456789 - 1) == 4.123456789);
  1204. sqt.ok((3.125 * 3.125) == 9.765625);
  1205. sqt.ok((3.125 * 1) == 3.125);
  1206. sqt.ok((3.123456789 / 3.123456789) == 1.0);
  1207. sqt.ok((3.123456789 / 1) == 3.123456789);
  1208. sqt.ok(math.abs(123) == 123);
  1209. sqt.ok(math.abs(-123) == 123);
  1210. sqt.ok(math.abs(0) == 0);
  1211. sqt.ok(math.abs(-0) == 0);
  1212. sqt.ok(math.fabs(-0.12) == 0.12);
  1213. sqt.ok(math.fabs(12.34) == 12.34);
  1214. //print("math.acos(0)", math.acos(0))
  1215. sqt.fequal(math.acos(0), 1.5707963267949);
  1216. sqt.ok(math.acos(1) == 0);
  1217. sqt.fequal(math.acos(-1), 3.1415926535898);
  1218. sqt.ok(math.asin(0) == 0);
  1219. sqt.fequal(math.asin(1), 1.5707963267949);
  1220. sqt.fequal(math.asin(-1), -1.5707963267949);
  1221. sqt.ok(math.atan(0) == 0);
  1222. sqt.fequal(math.atan(1), 0.78539816339745);
  1223. sqt.ok(math.atan2(0, 0) == 0);
  1224. sqt.ok(math.atan2(0, 1) == 0);
  1225. sqt.fequal(math.atan2(1, 0), 1.5707963267949);
  1226. sqt.ok(math.ceil(123) == 123);
  1227. sqt.ok(math.ceil(-123) == -123);
  1228. sqt.ok(math.ceil(0) == 0);
  1229. sqt.ok(math.ceil(-0) == -0);
  1230. sqt.ok(math.ceil(0.123) == 1);
  1231. sqt.ok(math.ceil(12.3) == 13);
  1232. sqt.ok(math.ceil(-0.123) == -0);
  1233. sqt.ok(math.ceil(-12.3) == -12);
  1234. sqt.ok(math.floor(123) == 123);
  1235. sqt.ok(math.floor(-123) == -123);
  1236. sqt.ok(math.floor(0) == 0);
  1237. sqt.ok(math.floor(-0) == -0);
  1238. sqt.ok(math.floor(0.123) == 0);
  1239. sqt.ok(math.floor(12.3) == 12);
  1240. sqt.ok(math.floor(-0.123) == -1);
  1241. sqt.ok(math.floor(-12.3) == -13);
  1242. sqt.ok(math.tan(0) == 0);
  1243. sqt.fequal(math.tan(math.PI / 4), 1.0);
  1244. sqt.fequal(math.tan(-math.PI / 4), -1.0);
  1245. sqt.ok(math.cos(0) == 1);
  1246. sqt.ok(math.cos(math.PI) == -1);
  1247. sqt.ok(math.cos(2 * math.PI) == 1);
  1248. sqt.ok(math.abs(math.cos(math.PI / 2)) < 1.0e-16);
  1249. sqt.ok(math.sin(0) == 0);
  1250. sqt.ok(math.sin(math.PI / 2) == 1);
  1251. sqt.ok(math.abs(math.sin(math.PI)) < 1.0e-15);
  1252. sqt.ok(math.abs(math.sin(2 * math.PI)) < 1.0e-15);
  1253. sqt.fequal(math.log(3), 1.0986122886681);
  1254. sqt.fequal(math.log(100), 4.6051701859881);
  1255. sqt.ok(math.isnan(math.log(-1)));
  1256. sqt.ok(math.pow(2,4) == 16);
  1257. sqt.ok(math.pow(2, 10) == 1024);
  1258. sqt.ok(math.pow(1, 0) == 1);
  1259. sqt.ok(math.sqrt(4) == 2);
  1260. sqt.ok(math.sqrt(1000000) == 1000);
  1261. sqt.ok(math.sqrt(1) == 1);
  1262. sqt.ok(math.sqrt(-0) == -0);
  1263. sqt.ok(math.sqrt(0) == 0);
  1264. sqt.fequal(math.sqrt(2), 1.4142135623731);
  1265. sqt.ok(math.isnan(math.sqrt(-4)) == true);
  1266. /*
  1267. sqt.ok(123.truncate == 123);
  1268. sqt.ok((-123).truncate == -123);
  1269. sqt.ok(0.truncate == 0);
  1270. sqt.ok((-0).truncate == -0);
  1271. sqt.ok(0.123.truncate == 0);
  1272. sqt.ok(12.3.truncate == 12);
  1273. sqt.ok((-0.123).truncate == -0);
  1274. sqt.ok((-12.3).truncate == -12);
  1275. sqt.ok((12345678901234.5).truncate == 12345678901234);
  1276. sqt.ok((-12345678901234.5).truncate == -12345678901234);
  1277. */
  1278. /*
  1279. sqt.ok(123.sign == 1);
  1280. sqt.ok((-123).sign == -1);
  1281. sqt.ok(0.sign == 0);
  1282. sqt.ok((-0).sign == 0);
  1283. sqt.ok(0.123.sign == 1);
  1284. sqt.ok((-0.123).sign == -1);
  1285. */
  1286. /*
  1287. sqt.ok(123.round == 123);
  1288. sqt.ok((-123).round == -123);
  1289. sqt.ok(0.round == 0);
  1290. sqt.ok((-0).round == -0);
  1291. sqt.ok(0.123.round == 0);
  1292. sqt.ok(12.3.round == 12);
  1293. sqt.ok((-0.123).round == -0);
  1294. sqt.ok((-12.3).round == -12);
  1295. */
  1296. sqt.ok(123 % 1 == 0);
  1297. sqt.ok((-123) % 1 == -0);
  1298. sqt.ok(0 % 1 == 0);
  1299. sqt.ok((-0) % 1 == -0);
  1300. sqt.ok((0.123) % 1.0 == 0.123);
  1301. sqt.ok((-0.123) % 1.0 == -0.123);
  1302. sqt.fequal((12.3) % 1.0, 0.3);
  1303. sqt.fequal((-12.3) % 1.0, -0.3);
  1304. sqt.fequal((1.23456789012345) % 1.0, 0.23456789012345);
  1305. sqt.fequal((-1.23456789012345) % 1.0, -0.23456789012345);
  1306. sqt.ok((0.000000000000000000000000000000000000000001) % 1.0 == 1e-42);
  1307. sqt.ok((-0.000000000000000000000000000000000000000001) % 1.0 == -1e-42);
  1308. sqt.ok((1.000000000000000000000000000000000000000001) % 1.0 == 0);
  1309. sqt.ok((-1.000000000000000000000000000000000000000001) % 1.0 == -0);
  1310. sqt.ok("123".tointeger() == 123);
  1311. sqt.ok("-123".tointeger() == -123);
  1312. sqt.ok("-0".tointeger() == -0);
  1313. sqt.ok("12.34".tofloat() == 12.34);
  1314. sqt.ok("-0.0001".tofloat() == -0.0001 == true);
  1315. sqt.ok(" 12 ".tointeger() == 12);
  1316. try{"test1".tointeger(), sqt.ok(0);}catch(e){sqt.ok(1);};
  1317. try{"test1".tofloat(), sqt.ok(0);}catch(e){sqt.ok(1);};
  1318. //sqt.ok("".tointeger() == null);
  1319. //sqt.ok("prefix1.2".tofloat() == null);
  1320. //sqt.ok("1.2suffix".tofloat() == null);
  1321. sqt.ok("1.2suffix".tofloat() == 1.2); // ??????
  1322. sqt.ok("1.2suffix".tointeger() == 1); // ?????
  1323. sqt.ok((123).tostring() == "123");
  1324. sqt.ok((-123).tostring() == "-123");
  1325. //sqt.ok((-0).tostring() == "-0");
  1326. //sqt.ok((-0.0).tostring() == "-0.0");
  1327. sqt.ok((12.34).tostring() == "12.34");
  1328. sqt.ok((-0.0001).tostring() == "-0.0001");
  1329. sqt.ok(type(123) == "integer");
  1330. sqt.ok(type(123.0) != "integer");
  1331. sqt.ok(type(0) == "integer");
  1332. sqt.ok(type(1.0000001) != "integer");
  1333. sqt.ok(type(-2.3) != "integer");
  1334. //sqt.ok(type(0/0).isInteger == false);
  1335. //sqt.ok(type(1/0).isInteger == false);
  1336. //sqt.fequal(math.HUGE, 1.7976931348623e+308);
  1337. //sqt.ok(Num.smallest == 2.2250738585072e-308);
  1338. sqt.ok(1 < 2);
  1339. sqt.ok(!(2 < 2));
  1340. sqt.ok(!(2 < 1));
  1341. sqt.ok(1 <= 2);
  1342. sqt.ok(2 <= 2);
  1343. sqt.ok(!(2 <= 1));
  1344. sqt.ok(!(1 > 2));
  1345. sqt.ok(!(2 > 2));
  1346. sqt.ok(2 > 1);
  1347. sqt.ok(!(1 >= 2));
  1348. sqt.ok(2 >= 2);
  1349. sqt.ok(2 >= 1);
  1350. sqt.ok(!(0 < -0));
  1351. sqt.ok(!(-0 < 0));
  1352. sqt.ok(!(0 > -0));
  1353. sqt.ok(!(-0 > 0));
  1354. sqt.ok(0 <= -0);
  1355. sqt.ok(-0 <= 0);
  1356. sqt.ok(0 >= -0);
  1357. sqt.ok(-0 >= 0);
  1358. sqt.ok((8 / 2) == 4);
  1359. sqt.fequal((12.34 / -0.4), -30.85);
  1360. //sqt.ok(!math.isfinite(3 / 0)); // == infinity);
  1361. //sqt.ok(!math.isfinite(-3 / 0)); // == -infinity);
  1362. //sqt.ok(math.isnan(0 / 0));
  1363. //sqt.ok(math.isnan(-0 / 0));
  1364. //sqt.ok(!math.isfinite(3 / -0)); // == -infinity);
  1365. //sqt.ok(!math.isfinite(-3 / -0)); // == infinity);
  1366. //sqt.ok(math.isnan(0 / -0));
  1367. //sqt.ok(math.isnan(-0 / -0));
  1368. sqt.ok(123 == 123);
  1369. sqt.ok(123 != 124);
  1370. sqt.ok(-3 != 3);
  1371. sqt.ok(0 == -0);
  1372. sqt.ok(0.0 == -0.0);
  1373. sqt.ok(123 != "123");
  1374. sqt.ok(1 == true);
  1375. sqt.ok(0 == false);
  1376. sqt.ok(1 !== true);
  1377. sqt.ok(0 !== false);
  1378. sqt.ok((0 & 0) == 0);
  1379. #ifdef __SQUILU_INT_SZ8__
  1380. sqt.ok((0xaaaaaaaa & 0x55555555) == 0);
  1381. sqt.ok((0xf0f0f0f0 & 0x3c3c3c3c) == 808464432);
  1382. sqt.ok((0xffffffff & 0xffffffff) == 4294967295);
  1383. #endif
  1384. try{1 & false, sqt.ok(0)} catch(e) {sqt.ok(1);} // expect runtime error: Right operand must be a number.
  1385. sqt.ok((0 << 0) == 0);
  1386. sqt.ok((1 << 0) == 1);
  1387. sqt.ok((0 << 1) == 0);
  1388. sqt.ok((1 << 1) == 2);
  1389. #ifdef __SQUILU_INT_SZ8__
  1390. sqt.ok((0xffffffff << 0) == 4294967295);
  1391. #endif
  1392. sqt.ok((0 >> 0) == 0);
  1393. sqt.ok((1 >> 0) == 1);
  1394. sqt.ok((0 >> 1) == 0);
  1395. sqt.ok((1 >> 1) == 0);
  1396. #ifdef __SQUILU_INT_SZ8__
  1397. sqt.ok((0xaaaaaaaa << 1) == 5726623060);
  1398. sqt.ok((0xf0f0f0f0 << 1) == 8084644320);
  1399. sqt.ok((0xaaaaaaaa >> 1) == 1431655765);
  1400. sqt.ok((0xf0f0f0f0 >> 1) == 2021161080);
  1401. sqt.ok((0xffffffff >> 1) == 2147483647);
  1402. #else
  1403. sqt.ok((0xaaaaaaaa << 1) == 1431655764);
  1404. sqt.ok((0xaaaaaaaa >> 1) == -715827883);
  1405. //sqt.ok((0xf0f0f0f0 << 1) == -505290272);
  1406. //sqt.ok((0xf0f0f0f0 >> 1) == -126322568);
  1407. //sqt.ok((0xffffffff >> 1) == -1);
  1408. #endif
  1409. sqt.ok((0 ^ 0) == 0);
  1410. sqt.ok((1 ^ 1) == 0);
  1411. sqt.ok((0 ^ 1) == 1);
  1412. sqt.ok((1 ^ 0) == 1);
  1413. #ifdef __SQUILU_INT_SZ8__
  1414. sqt.ok((0xaaaaaaaa ^ 0x55555555) == 4294967295);
  1415. sqt.ok((0xf0f0f0f0 ^ 0x3c3c3c3c) == 3435973836);
  1416. sqt.ok((0xffffffff ^ 0xffffffff) == 0);
  1417. #endif
  1418. //sqt.ok((~0) == 4294967295);
  1419. //sqt.ok((~1) == 4294967294);
  1420. //sqt.ok((~23) == 4294967272);
  1421. //sqt.ok((~0xffffffff) == 0);
  1422. //sqt.ok((~1.23) == 4294967294);
  1423. //sqt.ok((~0.00123) == 4294967295);
  1424. //sqt.ok((~345.67) == 4294966950);
  1425. sqt.ok((0 | 0) == 0);
  1426. #ifdef __SQUILU_INT_SZ8__
  1427. sqt.ok((0xaaaaaaaa | 0x55555555) == 4294967295);
  1428. sqt.ok((0xcccccccc | 0x66666666) == 4008636142);
  1429. sqt.ok((0xffffffff | 0xffffffff) == 4294967295);
  1430. #endif
  1431. local a = 3;
  1432. sqt.ok((5 - 3) == 2);
  1433. sqt.fequal((3.1 - 0.24), 2.86);
  1434. sqt.ok((3 - 2 - 1) == 0);
  1435. sqt.ok(-a == -3);
  1436. sqt.ok((1 + 2) == 3);
  1437. sqt.ok((12.34 + 0.13) == 12.47);
  1438. sqt.ok((3 + 5 + 2) == 10);
  1439. sqt.ok((5 % 3) == 2);
  1440. sqt.ok((10 % 5) == 0);
  1441. sqt.ok((-4 % 3) == -1);
  1442. sqt.ok((4 % -3) == 1);
  1443. sqt.ok((-4 % -3) == -1);
  1444. sqt.ok((-4.2 % 3.1) == -1.1);
  1445. sqt.ok((4.2 % -3.1) == 1.1);
  1446. sqt.ok((-4.2 % -3.1) == -1.1);
  1447. sqt.ok((13 % 7 % 4) == 2);
  1448. sqt.ok((13 + 1 % 7) == 14);
  1449. sqt.ok((5 * 3) == 15);
  1450. sqt.ok((12.34 * 0.3) == 3.702);
  1451. //10000000000000004 < 32BITS
  1452. //10000000000000002 < 64BITS
  1453. //print(format("%.17g", 1e16 + 2.9999) , "10000000000000002");
  1454. //if(_intsize_ == 8)
  1455. #ifdef __SQUILU_INT_SZ8__
  1456. sqt.ok(format("%.17g", 1e16 + 2.9999) == "10000000000000002");
  1457. #endif
  1458. //else sqt.ok(format("%.17g", 1e16 + 2.9999) == "10000000000000004");
  1459. });
  1460. sqt.run("enum", function(){
  1461. enum e1 {one=1, two};
  1462. sqt.ok(e1.one == 1);
  1463. sqt.ok(e1.two == 2);
  1464. enum e2 {one=-1, two, three};
  1465. sqt.ok(e2.one == -1);
  1466. sqt.ok(e2.two == 0);
  1467. sqt.ok(e2.three == 1);
  1468. enum e3 {one=-1, two, three, nine=9, ten};
  1469. sqt.ok(e3.one == -1);
  1470. sqt.ok(e3.two == 0);
  1471. sqt.ok(e3.three == 1);
  1472. sqt.ok(e3.nine == 9);
  1473. sqt.ok(e3.ten == 10);
  1474. });
  1475. sqt.run("constants", function(){
  1476. const ONE = 1;
  1477. const STR = "string";
  1478. const $CTCC;
  1479. sqt.ok(ONE == 1);
  1480. sqt.ok(STR == "string");
  1481. sqt.ok(CTCC == "CTCC");
  1482. sqt.ok($ONE == 1);
  1483. sqt.ok($STR == "string");
  1484. sqt.ok($CTCC == "CTCC");
  1485. //print(dostring("return $CTC == \"CTC\";"));
  1486. });
  1487. sqt.run("class", function(){
  1488. class Comparable {
  1489. constructor(n)
  1490. {
  1491. name = n;
  1492. }
  1493. function _typeof() {return "Comparable";};
  1494. function _cmp(other)
  1495. {
  1496. if(name<other.name) return -1;
  1497. if(name>other.name) return 1;
  1498. return 0;
  1499. }
  1500. static function st() {return "st";};
  1501. name = null;
  1502. id = 0;
  1503. static count = 0;
  1504. }
  1505. local a = Comparable("Alberto");
  1506. local b = Comparable("Wouter");
  1507. local c = Comparable("Alberto");
  1508. sqt.ok(a < b);
  1509. sqt.ok(b > a);
  1510. sqt.ok(a.id == 0);
  1511. sqt.ok(Comparable.count == 0);
  1512. sqt.ok(a.count == 0);
  1513. sqt.ok(Comparable.st() == "st");
  1514. sqt.ok(a.st() == "st");
  1515. sqt.ok(typeof(a) == "Comparable");
  1516. sqt.ok( c == a );
  1517. sqt.ok( c !== a );
  1518. });
  1519. sqt.run("globals", function(){
  1520. sqt.ok( obj_clone(3) == 3 );
  1521. sqt.ok( obj_clone(3.4) == 3.4 );
  1522. sqt.ok( obj_clone("str") == "str" );
  1523. });
  1524. sqt.run("coroutines", function(){
  1525. local state1, state2, state3;
  1526. state1 = function()
  1527. {
  1528. ::suspend("state1");
  1529. return state2();
  1530. }
  1531. state2 = function()
  1532. {
  1533. ::suspend("state2");
  1534. return state3();
  1535. }
  1536. state3 = function()
  1537. {
  1538. ::suspend("state3");
  1539. return state1();
  1540. }
  1541. local statethread = ::newthread(state1)
  1542. ::table_len(globals)
  1543. //the above dummy global call with "::" prefix
  1544. //after a statement ending without ';' terminator
  1545. //is here to check a bug regression on the compiler
  1546. sqt.ok(statethread.call() == "state1");
  1547. for(local i = 0, j=1; i < 10000; i++)
  1548. {
  1549. switch(j++)
  1550. {
  1551. case 0: sqt.ok(statethread.wakeup() == "state1"); break;
  1552. case 1: sqt.ok(statethread.wakeup() == "state2"); break;
  1553. case 2:
  1554. sqt.ok(statethread.wakeup() == "state3");
  1555. j = 0; //restart
  1556. break;
  1557. }
  1558. }
  1559. });
  1560. sqt.run("C/C++ syntax", function(){
  1561. //string line continuation
  1562. sqt.ok("foo \
  1563. bar" == "foo bar");
  1564. //adjacent strings
  1565. sqt.ok("foo "
  1566. "bar" == "foo bar");
  1567. //C++ multi line string
  1568. sqt.ok(R"(foo
  1569. bar)" == "foo\nbar");
  1570. #ifndef ZEROX
  1571. class Dad
  1572. {
  1573. int_t _n;
  1574. public:
  1575. Dad(int_t n){ _n = n;}
  1576. void print() {printf("%d\n", _n);}
  1577. int_t getN() {return _n;}
  1578. protected:
  1579. void onlyForFriends(){}
  1580. private:
  1581. void onlyForUs(){}
  1582. };
  1583. auto dad = Dad(6);
  1584. sqt.ok(dad.getN() == 6);
  1585. #endif
  1586. //SquiLu has several predefined syntax alias for "local"
  1587. // int8_t, int16_t, int32_t, int64_t, float_t, double_t, string_t, void, void_ptr_t
  1588. extern int_t eadd(int_t a, int_t b);
  1589. //int_t add(int_t a, int_t b);
  1590. int_t a = 4;
  1591. //SquiLu accpet "auto" as an alias for "local"
  1592. auto b = 5;
  1593. int_t add(const int_t a, const int_t b) {return a+b;}
  1594. sqt.ok(add(a,b) == 9);
  1595. //SquiLu has limited single pass preprocessor capabilities
  1596. #ifdef __SQUILU__
  1597. //here squilu definitions /code
  1598. //we can use typedef to create type aliases
  1599. typedef T T;
  1600. const double_t pi = 3.14;
  1601. sqt.ok(pi == 3.14);
  1602. #else
  1603. //here C/C++ definitions/code
  1604. #endif
  1605. //SquiLu only accepts the template definition
  1606. //but doesn't do anything with it right now
  1607. //although class/struct/function definitions work
  1608. //because dynamic type values are the default
  1609. template<typename T>
  1610. T tadd(const T a, const T b) {return a+b;}
  1611. sqt.ok(tadd(a,b) == 9);
  1612. class A
  1613. {
  1614. int_t _a;
  1615. public:
  1616. A(const int_t a){ _a = a;}
  1617. virtual ~A() {/*ignored in squilu*/}
  1618. virtual void addIt(const int_t n) {_a += n;}
  1619. virtual int_t getIt() {return _a;}
  1620. };
  1621. auto ca = A(4); //RAII with direct constructor call
  1622. sqt.ok(ca.getIt() == 4);
  1623. ca.addIt(5);
  1624. sqt.ok(ca.getIt() == 9);
  1625. class B :public A
  1626. {
  1627. public:
  1628. B(const int_t a) {_a = a;}
  1629. int_t getItPlus(const int_t p) {return _a + p;}
  1630. };
  1631. auto cb = B(4); //RAII with direct constructor call
  1632. sqt.ok(cb.getIt() == 4);
  1633. cb.addIt(5);
  1634. sqt.ok(cb.getIt() == 9);
  1635. sqt.ok(ca.getIt() == cb.getIt());
  1636. sqt.ok(cb.getItPlus(9) == 18);
  1637. struct S
  1638. {
  1639. int_t _a;
  1640. S(const int_t a){ _a = a;}
  1641. virtual ~S() {/*ignored in squilu*/}
  1642. virtual void addIt(const int_t n) {_a += n;}
  1643. virtual int_t getIt() {return _a;}
  1644. };
  1645. auto sa = S(4); //RAII with direct constructor call
  1646. sqt.ok(sa.getIt() == 4);
  1647. sa.addIt(5);
  1648. sqt.ok(sa.getIt() == 9);
  1649. template<typename T>
  1650. class Ktpl
  1651. {
  1652. T _a;
  1653. public:
  1654. Ktpl(const T a){ _a = a;}
  1655. virtual ~Ktpl() {/*ignored in squilu*/}
  1656. virtual void addIt(const T n) {_a += n;}
  1657. virtual T getIt() {return _a;}
  1658. T getItPlus(const T n);
  1659. };
  1660. //T Ktpl::getItPlus(const T n) {return _a + n;}
  1661. auto ki = Ktpl<int_t>(4); //RAII with direct constructor call
  1662. sqt.ok(ki.getIt() == 4);
  1663. ki.addIt(5);
  1664. sqt.ok(ki.getIt() == 9);
  1665. auto kf = Ktpl<float_t>(4); //RAII with direct constructor call
  1666. sqt.ok(kf.getIt() == 4);
  1667. kf.addIt(5);
  1668. sqt.ok(kf.getIt() == 9);
  1669. Ktpl<double_t> kd = Ktpl<double_t>(4); //RAII with direct constructor call
  1670. sqt.ok(kd.getIt() == 4);
  1671. kd.addIt(5);
  1672. sqt.ok(kd.getIt() == 9);
  1673. /*
  1674. typedef Ktpl<int64_t> Ktpl_i64;
  1675. auto ki64 = Ktpl_i64(4); //RAII with direct constructor call
  1676. sqt.ok(ki64.getIt() == 4);
  1677. ki64.addIt(5);
  1678. sqt.ok(ki64.getIt() == 9);
  1679. */
  1680. auto max_loop = 10;
  1681. for(auto i=max_loop; i >= 0; --i) sqt.ok(max_loop-- == i);
  1682. //SquiLu only accept the reference parameter syntax
  1683. //for int/float/boolean it doesn't do anything, they are still passed by value
  1684. //instances are passed by reference allways
  1685. int_t addByRef(const int_t &a, const int_t &b) {return a + b;}
  1686. sqt.ok(addByRef(a, b) == addByRef(b, a));
  1687. //SquiLu do not accept '*' pointer notation
  1688. //but with a dummy typedef we can simulate it
  1689. #ifdef __SQUILU__
  1690. typedef char_ptr_t char_ptr_t;
  1691. #else
  1692. typedef char* char_ptr_t;
  1693. #endif
  1694. char_ptr_t strcopy(const char_ptr_t str) {return str;} //only for syntax test
  1695. sqt.ok(strcopy("squilu") == "squilu");
  1696. });
  1697. sqt.run("Javascript syntax", function(){
  1698. //SquiLu accpet "var/let" as an alias for "local"
  1699. var a = 20;
  1700. let b = 30;
  1701. let add = function(a, b) {return a+b;}
  1702. sqt.ok( add(a,b) == 50 );
  1703. });
  1704. sqt.run("Typescript syntax", function(){
  1705. //SquiLu accpet "var/let" as an alias for "local"
  1706. let Greeting1 : string = "Hello, ";
  1707. function greeter(person: string) : string {
  1708. return Greeting1 + person;
  1709. }
  1710. let user = "Jane User";
  1711. sqt.ok(greeter(user) == Greeting1 + user);
  1712. let notSure: any = 4;
  1713. notSure = "maybe a string instead";
  1714. notSure = false; // okay, definitely a boolean
  1715. sqt.ok(!notSure);
  1716. let a : number = 20;
  1717. let b : number = 30;
  1718. let add = function(a : number, b : number) : number {return a+b;}
  1719. sqt.ok( add(a,b) == 50 );
  1720. });
  1721. return sqt.results(); //show results