constructs.lua 7.3 KB

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