constructs.lua 7.1 KB

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