constructs.lua 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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. do -- bug since 5.4.0
  81. -- create code with a table using more than 256 constants
  82. local code = {"local x = {"}
  83. for i = 1, 257 do
  84. code[#code + 1] = i .. ".1,"
  85. end
  86. code[#code + 1] = "};"
  87. code = table.concat(code)
  88. -- add "ret" to the end of that code and checks that
  89. -- it produces the expected value "val"
  90. local function check (ret, val)
  91. local code = code .. ret
  92. code = load(code)
  93. assert(code() == val)
  94. end
  95. check("return (1 ~ (2 or 3))", 1 ~ 2)
  96. check("return (1 | (2 or 3))", 1 | 2)
  97. check("return (1 + (2 or 3))", 1 + 2)
  98. check("return (1 << (2 or 3))", 1 << 2)
  99. end
  100. function f (i)
  101. if type(i) ~= 'number' then return i,'jojo'; end;
  102. if i > 0 then return i, f(i-1); end;
  103. end
  104. x = {f(3), f(5), f(10);};
  105. assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1);
  106. assert(x[nil] == nil)
  107. x = {f'alo', f'xixi', nil};
  108. assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil);
  109. x = {f'alo'..'xixi'};
  110. assert(x[1] == 'aloxixi')
  111. x = {f{}}
  112. assert(x[2] == 'jojo' and type(x[1]) == 'table')
  113. local f = function (i)
  114. if i < 10 then return 'a';
  115. elseif i < 20 then return 'b';
  116. elseif i < 30 then return 'c';
  117. end;
  118. end
  119. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
  120. for i=1,1000 do break; end;
  121. n=100;
  122. i=3;
  123. t = {};
  124. a=nil
  125. while not a do
  126. a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
  127. end
  128. assert(a == n*(n+1)/2 and i==3);
  129. assert(t[1] and t[n] and not t[0] and not t[n+1])
  130. function f(b)
  131. local x = 1;
  132. repeat
  133. local a;
  134. if b==1 then local b=1; x=10; break
  135. elseif b==2 then x=20; break;
  136. elseif b==3 then x=30;
  137. else local a,b,c,d=math.sin(1); x=x+1;
  138. end
  139. until x>=12;
  140. return x;
  141. end;
  142. assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12)
  143. local f = function (i)
  144. if i < 10 then return 'a'
  145. elseif i < 20 then return 'b'
  146. elseif i < 30 then return 'c'
  147. else return 8
  148. end
  149. end
  150. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8)
  151. local a, b = nil, 23
  152. x = {f(100)*2+3 or a, a or b+2}
  153. assert(x[1] == 19 and x[2] == 25)
  154. x = {f=2+3 or a, a = b+2}
  155. assert(x.f == 5 and x.a == 25)
  156. a={y=1}
  157. x = {a.y}
  158. assert(x[1] == 1)
  159. function f(i)
  160. while 1 do
  161. if i>0 then i=i-1;
  162. else return; end;
  163. end;
  164. end;
  165. function g(i)
  166. while 1 do
  167. if i>0 then i=i-1
  168. else return end
  169. end
  170. end
  171. f(10); g(10);
  172. do
  173. function f () return 1,2,3; end
  174. local a, b, c = f();
  175. assert(a==1 and b==2 and c==3)
  176. a, b, c = (f());
  177. assert(a==1 and b==nil and c==nil)
  178. end
  179. local a,b = 3 and f();
  180. assert(a==1 and b==nil)
  181. function g() f(); return; end;
  182. assert(g() == nil)
  183. function g() return nil or f() end
  184. a,b = g()
  185. assert(a==1 and b==nil)
  186. print'+';
  187. do -- testing constants
  188. local prog <const> = [[local x <XXX> = 10]]
  189. checkload(prog, "unknown attribute 'XXX'")
  190. checkload([[local xxx <const> = 20; xxx = 10]],
  191. ":1: attempt to assign to const variable 'xxx'")
  192. checkload([[
  193. local xx;
  194. local xxx <const> = 20;
  195. local yyy;
  196. local function foo ()
  197. local abc = xx + yyy + xxx;
  198. return function () return function () xxx = yyy end end
  199. end
  200. ]], ":6: attempt to assign to const variable 'xxx'")
  201. checkload([[
  202. local x <close> = nil
  203. x = io.open()
  204. ]], ":2: attempt to assign to const variable 'x'")
  205. end
  206. f = [[
  207. return function ( a , b , c , d , e )
  208. local x = a >= b or c or ( d and e ) or nil
  209. return x
  210. end , { a = 1 , b = 2 >= 1 , } or { 1 };
  211. ]]
  212. f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes
  213. f,a = load(f)();
  214. assert(a.a == 1 and a.b)
  215. function g (a,b,c,d,e)
  216. if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
  217. end
  218. function h (a,b,c,d,e)
  219. while (a>=b or c or (d and e) or nil) do return 1; end;
  220. return 0;
  221. end;
  222. assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1)
  223. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  224. assert(f(1,2,'a')
  225. ~= -- force SETLINE before nil
  226. nil, "")
  227. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  228. assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and
  229. h(1,2,nil,1,'x') == 1)
  230. assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and
  231. h(1,2,nil,nil,'x') == 0)
  232. assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and
  233. h(1,2,nil,1,nil) == 0)
  234. assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true)
  235. x = 2<3 and not 3; assert(x==false)
  236. x = 2<1 or (2>1 and 'a'); assert(x=='a')
  237. do
  238. local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2
  239. assert(a==2)
  240. end
  241. function F(a)
  242. assert(debug.getinfo(1, "n").name == 'F')
  243. return a,2,3
  244. end
  245. a,b = F(1)~=nil; assert(a == true and b == nil);
  246. a,b = F(nil)==nil; assert(a == true and b == nil)
  247. ----------------------------------------------------------------
  248. ------------------------------------------------------------------
  249. -- sometimes will be 0, sometimes will not...
  250. _ENV.GLOB1 = math.random(0, 1)
  251. -- basic expressions with their respective values
  252. local basiccases = {
  253. {"nil", nil},
  254. {"false", false},
  255. {"true", true},
  256. {"10", 10},
  257. {"(0==_ENV.GLOB1)", 0 == _ENV.GLOB1},
  258. }
  259. local prog
  260. if _ENV.GLOB1 == 0 then
  261. basiccases[2][1] = "F" -- constant false
  262. prog = [[
  263. local F <const> = false
  264. if %s then IX = true end
  265. return %s
  266. ]]
  267. else
  268. basiccases[4][1] = "k10" -- constant 10
  269. prog = [[
  270. local k10 <const> = 10
  271. if %s then IX = true end
  272. return %s
  273. ]]
  274. end
  275. print('testing short-circuit optimizations (' .. _ENV.GLOB1 .. ')')
  276. -- operators with their respective values
  277. local binops <const> = {
  278. {" and ", function (a,b) if not a then return a else return b end end},
  279. {" or ", function (a,b) if a then return a else return b end end},
  280. }
  281. local cases <const> = {}
  282. -- creates all combinations of '(cases[i] op cases[n-i])' plus
  283. -- 'not(cases[i] op cases[n-i])' (syntax + value)
  284. local function createcases (n)
  285. local res = {}
  286. for i = 1, n - 1 do
  287. for _, v1 in ipairs(cases[i]) do
  288. for _, v2 in ipairs(cases[n - i]) do
  289. for _, op in ipairs(binops) do
  290. local t = {
  291. "(" .. v1[1] .. op[1] .. v2[1] .. ")",
  292. op[2](v1[2], v2[2])
  293. }
  294. res[#res + 1] = t
  295. res[#res + 1] = {"not" .. t[1], not t[2]}
  296. end
  297. end
  298. end
  299. end
  300. return res
  301. end
  302. -- do not do too many combinations for soft tests
  303. local level = _soft and 3 or 4
  304. cases[1] = basiccases
  305. for i = 2, level do cases[i] = createcases(i) end
  306. print("+")
  307. local i = 0
  308. for n = 1, level do
  309. for _, v in pairs(cases[n]) do
  310. local s = v[1]
  311. local p = load(string.format(prog, s, s), "")
  312. IX = false
  313. assert(p() == v[2] and IX == not not v[2])
  314. i = i + 1
  315. if i % 60000 == 0 then print('+') end
  316. end
  317. end
  318. ------------------------------------------------------------------
  319. -- testing some syntax errors (chosen through 'gcov')
  320. checkload("for x do", "expected")
  321. checkload("x:call", "expected")
  322. print'OK'