constructs.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. -- $Id: testes/constructs.lua $
  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. do -- testing operators with diffent kinds of constants
  44. -- operands to consider:
  45. -- * fit in register
  46. -- * constant doesn't fit in register
  47. -- * floats with integral values
  48. local operand = {3, 100, 5.0, -10, -5.0, 10000, -10000}
  49. local operator = {"+", "-", "*", "/", "//", "%", "^",
  50. "&", "|", "^", "<<", ">>",
  51. "==", "~=", "<", ">", "<=", ">=",}
  52. for _, op in ipairs(operator) do
  53. local f = assert(load(string.format([[return function (x,y)
  54. return x %s y
  55. end]], op)))();
  56. for _, o1 in ipairs(operand) do
  57. for _, o2 in ipairs(operand) do
  58. local gab = f(o1, o2)
  59. _ENV.XX = o1
  60. code = string.format("return XX %s %s", op, o2)
  61. res = assert(load(code))()
  62. assert(res == gab)
  63. _ENV.XX = o2
  64. local code = string.format("return (%s) %s XX", o1, op)
  65. local res = assert(load(code))()
  66. assert(res == gab)
  67. code = string.format("return (%s) %s %s", o1, op, o2)
  68. res = assert(load(code))()
  69. assert(res == gab)
  70. end
  71. end
  72. end
  73. end
  74. -- silly loops
  75. repeat until 1; repeat until true;
  76. while false do end; while nil do end;
  77. do -- test old bug (first name could not be an `upvalue')
  78. local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
  79. end
  80. function f (i)
  81. if type(i) ~= 'number' then return i,'jojo'; end;
  82. if i > 0 then return i, f(i-1); end;
  83. end
  84. x = {f(3), f(5), f(10);};
  85. assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1);
  86. assert(x[nil] == nil)
  87. x = {f'alo', f'xixi', nil};
  88. assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil);
  89. x = {f'alo'..'xixi'};
  90. assert(x[1] == 'aloxixi')
  91. x = {f{}}
  92. assert(x[2] == 'jojo' and type(x[1]) == 'table')
  93. local f = function (i)
  94. if i < 10 then return 'a';
  95. elseif i < 20 then return 'b';
  96. elseif i < 30 then return 'c';
  97. end;
  98. end
  99. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
  100. for i=1,1000 do break; end;
  101. n=100;
  102. i=3;
  103. t = {};
  104. a=nil
  105. while not a do
  106. a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
  107. end
  108. assert(a == n*(n+1)/2 and i==3);
  109. assert(t[1] and t[n] and not t[0] and not t[n+1])
  110. function f(b)
  111. local x = 1;
  112. repeat
  113. local a;
  114. if b==1 then local b=1; x=10; break
  115. elseif b==2 then x=20; break;
  116. elseif b==3 then x=30;
  117. else local a,b,c,d=math.sin(1); x=x+1;
  118. end
  119. until x>=12;
  120. return x;
  121. end;
  122. assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12)
  123. local f = function (i)
  124. if i < 10 then return 'a'
  125. elseif i < 20 then return 'b'
  126. elseif i < 30 then return 'c'
  127. else return 8
  128. end
  129. end
  130. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8)
  131. local a, b = nil, 23
  132. x = {f(100)*2+3 or a, a or b+2}
  133. assert(x[1] == 19 and x[2] == 25)
  134. x = {f=2+3 or a, a = b+2}
  135. assert(x.f == 5 and x.a == 25)
  136. a={y=1}
  137. x = {a.y}
  138. assert(x[1] == 1)
  139. function f(i)
  140. while 1 do
  141. if i>0 then i=i-1;
  142. else return; end;
  143. end;
  144. end;
  145. function g(i)
  146. while 1 do
  147. if i>0 then i=i-1
  148. else return end
  149. end
  150. end
  151. f(10); g(10);
  152. do
  153. function f () return 1,2,3; end
  154. local a, b, c = f();
  155. assert(a==1 and b==2 and c==3)
  156. a, b, c = (f());
  157. assert(a==1 and b==nil and c==nil)
  158. end
  159. local a,b = 3 and f();
  160. assert(a==1 and b==nil)
  161. function g() f(); return; end;
  162. assert(g() == nil)
  163. function g() return nil or f() end
  164. a,b = g()
  165. assert(a==1 and b==nil)
  166. print'+';
  167. do -- testing constants
  168. local prog <const> = [[local x <XXX> = 10]]
  169. checkload(prog, "unknown attribute 'XXX'")
  170. checkload([[local xxx <const> = 20; xxx = 10]],
  171. ":1: attempt to assign to const variable 'xxx'")
  172. checkload([[
  173. local xx;
  174. local xxx <const> = 20;
  175. local yyy;
  176. local function foo ()
  177. local abc = xx + yyy + xxx;
  178. return function () return function () xxx = yyy end end
  179. end
  180. ]], ":6: attempt to assign to const variable 'xxx'")
  181. checkload([[
  182. local x <close> = nil
  183. x = io.open()
  184. ]], ":2: attempt to assign to const variable 'x'")
  185. end
  186. f = [[
  187. return function ( a , b , c , d , e )
  188. local x = a >= b or c or ( d and e ) or nil
  189. return x
  190. end , { a = 1 , b = 2 >= 1 , } or { 1 };
  191. ]]
  192. f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes
  193. f,a = load(f)();
  194. assert(a.a == 1 and a.b)
  195. function g (a,b,c,d,e)
  196. if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
  197. end
  198. function h (a,b,c,d,e)
  199. while (a>=b or c or (d and e) or nil) do return 1; end;
  200. return 0;
  201. end;
  202. assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1)
  203. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  204. assert(f(1,2,'a')
  205. ~= -- force SETLINE before nil
  206. nil, "")
  207. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  208. assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and
  209. h(1,2,nil,1,'x') == 1)
  210. assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and
  211. h(1,2,nil,nil,'x') == 0)
  212. assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and
  213. h(1,2,nil,1,nil) == 0)
  214. assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true)
  215. x = 2<3 and not 3; assert(x==false)
  216. x = 2<1 or (2>1 and 'a'); assert(x=='a')
  217. do
  218. local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2
  219. assert(a==2)
  220. end
  221. function F(a)
  222. assert(debug.getinfo(1, "n").name == 'F')
  223. return a,2,3
  224. end
  225. a,b = F(1)~=nil; assert(a == true and b == nil);
  226. a,b = F(nil)==nil; assert(a == true and b == nil)
  227. ----------------------------------------------------------------
  228. ------------------------------------------------------------------
  229. -- sometimes will be 0, sometimes will not...
  230. _ENV.GLOB1 = math.random(0, 1)
  231. -- basic expressions with their respective values
  232. local basiccases = {
  233. {"nil", nil},
  234. {"false", false},
  235. {"true", true},
  236. {"10", 10},
  237. {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
  238. }
  239. local prog
  240. if _ENV.GLOB1 == 0 then
  241. basiccases[2][1] = "F" -- constant false
  242. prog = [[
  243. local F <const> = false
  244. if %s then IX = true end
  245. return %s
  246. ]]
  247. else
  248. basiccases[4][1] = "k10" -- constant 10
  249. prog = [[
  250. local k10 <const> = 10
  251. if %s then IX = true end
  252. return %s
  253. ]]
  254. end
  255. print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
  256. -- operators with their respective values
  257. local binops <const> = {
  258. {" and ", function (a,b) if not a then return a else return b end end},
  259. {" or ", function (a,b) if a then return a else return b end end},
  260. }
  261. local cases <const> = {}
  262. -- creates all combinations of '(cases[i] op cases[n-i])' plus
  263. -- 'not(cases[i] op cases[n-i])' (syntax + value)
  264. local function createcases (n)
  265. local res = {}
  266. for i = 1, n - 1 do
  267. for _, v1 in ipairs(cases[i]) do
  268. for _, v2 in ipairs(cases[n - i]) do
  269. for _, op in ipairs(binops) do
  270. local t = {
  271. "(" .. v1[1] .. op[1] .. v2[1] .. ")",
  272. op[2](v1[2], v2[2])
  273. }
  274. res[#res + 1] = t
  275. res[#res + 1] = {"not" .. t[1], not t[2]}
  276. end
  277. end
  278. end
  279. end
  280. return res
  281. end
  282. -- do not do too many combinations for soft tests
  283. local level = _soft and 3 or 4
  284. cases[1] = basiccases
  285. for i = 2, level do cases[i] = createcases(i) end
  286. print("+")
  287. local i = 0
  288. for n = 1, level do
  289. for _, v in pairs(cases[n]) do
  290. local s = v[1]
  291. local p = load(string.format(prog, s, s), "")
  292. IX = false
  293. assert(p() == v[2] and IX == not not v[2])
  294. i = i + 1
  295. if i % 60000 == 0 then print('+') end
  296. end
  297. end
  298. ------------------------------------------------------------------
  299. -- testing some syntax errors (chosen through 'gcov')
  300. checkload("for x do", "expected")
  301. checkload("x:call", "expected")
  302. print'OK'