constructs.lua 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. ;;print "testing syntax";;
  2. local debug = require "debug"
  3. -- testing semicollons
  4. do ;;; end
  5. ; do ; a = 3; assert(a == 3) end;
  6. ;
  7. -- testing priorities
  8. assert(2^3^2 == 2^(3^2));
  9. assert(2^3*4 == (2^3)*4);
  10. assert(2^-2 == 1/4 and -2^- -2 == - - -4);
  11. assert(not nil and 2 and not(2>3 or 3<2));
  12. assert(-3-1-5 == 0+0-9);
  13. assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0);
  14. assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
  15. assert(not(2+1 > 3*1) and "a".."b" > "a");
  16. assert(not ((true or false) and nil))
  17. assert( true or false and nil)
  18. -- old bug
  19. assert((((1 or false) and true) or false) == true)
  20. assert((((nil and true) or false) and true) == false)
  21. local a,b = 1,nil;
  22. assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
  23. x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
  24. x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
  25. x,y=1,2;
  26. assert((x>y) and x or y == 2);
  27. x,y=2,1;
  28. assert((x>y) and x or y == 2);
  29. assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
  30. -- silly loops
  31. repeat until 1; repeat until true;
  32. while false do end; while nil do end;
  33. do -- test old bug (first name could not be an `upvalue')
  34. local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
  35. end
  36. function f (i)
  37. if type(i) ~= 'number' then return i,'jojo'; end;
  38. if i > 0 then return i, f(i-1); end;
  39. end
  40. x = {f(3), f(5), f(10);};
  41. assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1);
  42. assert(x[nil] == nil)
  43. x = {f'alo', f'xixi', nil};
  44. assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil);
  45. x = {f'alo'..'xixi'};
  46. assert(x[1] == 'aloxixi')
  47. x = {f{}}
  48. assert(x[2] == 'jojo' and type(x[1]) == 'table')
  49. local f = function (i)
  50. if i < 10 then return 'a';
  51. elseif i < 20 then return 'b';
  52. elseif i < 30 then return 'c';
  53. end;
  54. end
  55. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
  56. for i=1,1000 do break; end;
  57. n=100;
  58. i=3;
  59. t = {};
  60. a=nil
  61. while not a do
  62. a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
  63. end
  64. assert(a == n*(n+1)/2 and i==3);
  65. assert(t[1] and t[n] and not t[0] and not t[n+1])
  66. function f(b)
  67. local x = 1;
  68. repeat
  69. local a;
  70. if b==1 then local b=1; x=10; break
  71. elseif b==2 then x=20; break;
  72. elseif b==3 then x=30;
  73. else local a,b,c,d=math.sin(1); x=x+1;
  74. end
  75. until x>=12;
  76. return x;
  77. end;
  78. assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12)
  79. local f = function (i)
  80. if i < 10 then return 'a'
  81. elseif i < 20 then return 'b'
  82. elseif i < 30 then return 'c'
  83. else return 8
  84. end
  85. end
  86. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8)
  87. local a, b = nil, 23
  88. x = {f(100)*2+3 or a, a or b+2}
  89. assert(x[1] == 19 and x[2] == 25)
  90. x = {f=2+3 or a, a = b+2}
  91. assert(x.f == 5 and x.a == 25)
  92. a={y=1}
  93. x = {a.y}
  94. assert(x[1] == 1)
  95. function f(i)
  96. while 1 do
  97. if i>0 then i=i-1;
  98. else return; end;
  99. end;
  100. end;
  101. function g(i)
  102. while 1 do
  103. if i>0 then i=i-1
  104. else return end
  105. end
  106. end
  107. f(10); g(10);
  108. do
  109. function f () return 1,2,3; end
  110. local a, b, c = f();
  111. assert(a==1 and b==2 and c==3)
  112. a, b, c = (f());
  113. assert(a==1 and b==nil and c==nil)
  114. end
  115. local a,b = 3 and f();
  116. assert(a==1 and b==nil)
  117. function g() f(); return; end;
  118. assert(g() == nil)
  119. function g() return nil or f() end
  120. a,b = g()
  121. assert(a==1 and b==nil)
  122. print'+';
  123. f = [[
  124. return function ( a , b , c , d , e )
  125. local x = a >= b or c or ( d and e ) or nil
  126. return x
  127. end , { a = 1 , b = 2 >= 1 , } or { 1 };
  128. ]]
  129. f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes
  130. f,a = load(f)();
  131. assert(a.a == 1 and a.b)
  132. function g (a,b,c,d,e)
  133. if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
  134. end
  135. function h (a,b,c,d,e)
  136. while (a>=b or c or (d and e) or nil) do return 1; end;
  137. return 0;
  138. end;
  139. assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1)
  140. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  141. assert(f(1,2,'a')
  142. ~= -- force SETLINE before nil
  143. nil, "")
  144. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  145. assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and
  146. h(1,2,nil,1,'x') == 1)
  147. assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and
  148. h(1,2,nil,nil,'x') == 0)
  149. assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and
  150. h(1,2,nil,1,nil) == 0)
  151. assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true)
  152. x = 2<3 and not 3; assert(x==false)
  153. x = 2<1 or (2>1 and 'a'); assert(x=='a')
  154. do
  155. local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2
  156. assert(a==2)
  157. end
  158. function F(a)
  159. assert(debug.getinfo(1, "n").name == 'F')
  160. return a,2,3
  161. end
  162. a,b = F(1)~=nil; assert(a == true and b == nil);
  163. a,b = F(nil)==nil; assert(a == true and b == nil)
  164. ----------------------------------------------------------------
  165. -- creates all combinations of
  166. -- [not] ([not] arg op [not] (arg op [not] arg ))
  167. -- and tests each one
  168. function ID(x) return x end
  169. function f(t, i)
  170. local b = t.n
  171. local res = math.fmod(math.floor(i/c), b)+1
  172. c = c*b
  173. return t[res]
  174. end
  175. local arg = {" ( 1 < 2 ) ", " ( 1 >= 2 ) ", " F ( ) ", " nil "; n=4}
  176. local op = {" and ", " or ", " == ", " ~= "; n=4}
  177. local neg = {" ", " not "; n=2}
  178. local i = 0
  179. repeat
  180. c = 1
  181. local s = f(neg, i)..'ID('..f(neg, i)..f(arg, i)..f(op, i)..
  182. f(neg, i)..'ID('..f(arg, i)..f(op, i)..f(neg, i)..f(arg, i)..'))'
  183. local s1 = string.gsub(s, 'ID', '')
  184. K,X,NX,WX1,WX2 = nil
  185. s = string.format([[
  186. local a = %s
  187. local b = not %s
  188. K = b
  189. local xxx;
  190. if %s then X = a else X = b end
  191. if %s then NX = b else NX = a end
  192. while %s do WX1 = a; break end
  193. while %s do WX2 = a; break end
  194. repeat if (%s) then break end; assert(b) until not(%s)
  195. ]], s1, s, s1, s, s1, s, s1, s, s)
  196. assert(load(s))()
  197. assert(X and not NX and not WX1 == K and not WX2 == K)
  198. if math.fmod(i,4000) == 0 then print('+') end
  199. i = i+1
  200. until i==c
  201. print '+'
  202. ------------------------------------------------------------------
  203. print 'testing short-circuit optimizations'
  204. _ENV.GLOB1 = 1
  205. _ENV.GLOB2 = 2
  206. local basiccases = {
  207. {"nil", nil},
  208. {"false", false},
  209. {"true", true},
  210. {"10", 10},
  211. {"(_ENV.GLOB1 < _ENV.GLOB2)", true},
  212. {"(_ENV.GLOB2 < _ENV.GLOB1)", false},
  213. }
  214. local binops = {
  215. {" and ", function (a,b) if not a then return a else return b end end},
  216. {" or ", function (a,b) if a then return a else return b end end},
  217. }
  218. local mem = {basiccases} -- for memoization
  219. local function allcases (n)
  220. if mem[n] then return mem[n] end
  221. local res = {}
  222. -- include all smaller cases
  223. for _, v in ipairs(allcases(n - 1)) do
  224. res[#res + 1] = v
  225. end
  226. for i = 1, n - 1 do
  227. for _, v1 in ipairs(allcases(i)) do
  228. for _, v2 in ipairs(allcases(n - i)) do
  229. for _, op in ipairs(binops) do
  230. res[#res + 1] = {
  231. "(" .. v1[1] .. op[1] .. v2[1] .. ")",
  232. op[2](v1[2], v2[2])
  233. }
  234. end
  235. end
  236. end
  237. print('+')
  238. end
  239. mem[n] = res -- memoize
  240. return res
  241. end
  242. -- do not do too many combinations for soft tests
  243. local level = _soft and 3 or 4
  244. for _, v in pairs(allcases(level)) do
  245. local res = load("return " .. v[1])()
  246. assert(res == v[2])
  247. end
  248. ------------------------------------------------------------------
  249. print'OK'