constructs.lua 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. print "testing syntax"
  2. -- testing priorities
  3. assert(2^3^2 == 2^(3^2));
  4. assert(2^3*4 == (2^3)*4);
  5. assert(2^-2 == 1/4 and -2^- -2 == - - -4);
  6. assert(not nil and 2 and not(2>3 or 3<2));
  7. assert(-3-1-5 == 0+0-9);
  8. assert(-2^2 == -4 and (-2)^2 == 4 and 2*2-3-1 == 0);
  9. assert(2*1+3/3 == 3 and 1+2 .. 3*1 == "33");
  10. assert(not(2+1 > 3*1) and "a".."b" > "a");
  11. assert(not ((true or false) and nil))
  12. assert( true or false and nil)
  13. local a,b = 1,nil;
  14. assert(-(1 or 2) == -1 and (1 and 2)+(-1.25 or -4) == 0.75);
  15. x = ((b or a)+1 == 2 and (10 or a)+1 == 11); assert(x);
  16. x = (((2<3) or 1) == true and (2<3 and 4) == 4); assert(x);
  17. x,y=1,2;
  18. assert((x>y) and x or y == 2);
  19. x,y=2,1;
  20. assert((x>y) and x or y == 2);
  21. assert(1234567890 == tonumber('1234567890') and 1234567890+1 == 1234567891)
  22. -- silly loops
  23. repeat until 1; repeat until true;
  24. while false do end; while nil do end;
  25. do -- test old bug (first name could not be an `upvalue')
  26. local a; function f(x) x={a=1}; x={x=1}; x={G=1} end
  27. end
  28. function f (i)
  29. if type(i) ~= 'number' then return i,'jojo'; end;
  30. if i > 0 then return i, f(i-1); end;
  31. end
  32. x = {f(3), f(5), f(10);};
  33. assert(x[1] == 3 and x[2] == 5 and x[3] == 10 and x[4] == 9 and x[12] == 1);
  34. assert(x[nil] == nil)
  35. x = {f'alo', f'xixi', nil};
  36. assert(x[1] == 'alo' and x[2] == 'xixi' and x[3] == nil);
  37. x = {f'alo'..'xixi'};
  38. assert(x[1] == 'aloxixi')
  39. x = {f{}}
  40. assert(x[2] == 'jojo' and type(x[1]) == 'table')
  41. local f = function (i)
  42. if i < 10 then return 'a';
  43. elseif i < 20 then return 'b';
  44. elseif i < 30 then return 'c';
  45. end;
  46. end
  47. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == nil)
  48. for i=1,1000 do break; end;
  49. n=100;
  50. i=3;
  51. t = {};
  52. a=nil
  53. while not a do
  54. a=0; for i=1,n do for i=i,1,-1 do a=a+1; t[i]=1; end; end;
  55. end
  56. assert(a == n*(n+1)/2 and i==3);
  57. assert(t[1] and t[n] and not t[0] and not t[n+1])
  58. function f(b)
  59. local x = 1;
  60. repeat
  61. local a;
  62. if b==1 then local b=1; x=10; break
  63. elseif b==2 then x=20; break;
  64. elseif b==3 then x=30;
  65. else local a,b,c,d=math.sin(1); x=x+1;
  66. end
  67. until x>=12;
  68. return x;
  69. end;
  70. assert(f(1) == 10 and f(2) == 20 and f(3) == 30 and f(4)==12)
  71. local f = function (i)
  72. if i < 10 then return 'a'
  73. elseif i < 20 then return 'b'
  74. elseif i < 30 then return 'c'
  75. else return 8
  76. end
  77. end
  78. assert(f(3) == 'a' and f(12) == 'b' and f(26) == 'c' and f(100) == 8)
  79. local a, b = nil, 23
  80. x = {f(100)*2+3 or a, a or b+2}
  81. assert(x[1] == 19 and x[2] == 25)
  82. x = {f=2+3 or a, a = b+2}
  83. assert(x.f == 5 and x.a == 25)
  84. a={y=1}
  85. x = {a.y}
  86. assert(x[1] == 1)
  87. function f(i)
  88. while 1 do
  89. if i>0 then i=i-1;
  90. else return; end;
  91. end;
  92. end;
  93. function g(i)
  94. while 1 do
  95. if i>0 then i=i-1
  96. else return end
  97. end
  98. end
  99. f(10); g(10);
  100. do
  101. function f () return 1,2,3; end
  102. local a, b, c = f();
  103. assert(a==1 and b==2 and c==3)
  104. a, b, c = (f());
  105. assert(a==1 and b==nil and c==nil)
  106. end
  107. local a,b = 3 and f();
  108. assert(a==1 and b==nil)
  109. function g() f(); return; end;
  110. assert(g() == nil)
  111. function g() return nil or f() end
  112. a,b = g()
  113. assert(a==1 and b==nil)
  114. print'+';
  115. f = [[
  116. return function ( a , b , c , d , e )
  117. local x = a >= b or c or ( d and e ) or nil
  118. return x
  119. end , { a = 1 , b = 2 >= 1 , } or { 1 };
  120. ]]
  121. f = string.gsub(f, "%s+", "\n"); -- force a SETLINE between opcodes
  122. f,a = loadstring(f)();
  123. assert(a.a == 1 and a.b)
  124. function g (a,b,c,d,e)
  125. if not (a>=b or c or d and e or nil) then return 0; else return 1; end;
  126. end
  127. function h (a,b,c,d,e)
  128. while (a>=b or c or (d and e) or nil) do return 1; end;
  129. return 0;
  130. end;
  131. assert(f(2,1) == true and g(2,1) == 1 and h(2,1) == 1)
  132. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  133. assert(f(1,2,'a')
  134. ~= -- force SETLINE before nil
  135. nil, "")
  136. assert(f(1,2,'a') == 'a' and g(1,2,'a') == 1 and h(1,2,'a') == 1)
  137. assert(f(1,2,nil,1,'x') == 'x' and g(1,2,nil,1,'x') == 1 and
  138. h(1,2,nil,1,'x') == 1)
  139. assert(f(1,2,nil,nil,'x') == nil and g(1,2,nil,nil,'x') == 0 and
  140. h(1,2,nil,nil,'x') == 0)
  141. assert(f(1,2,nil,1,nil) == nil and g(1,2,nil,1,nil) == 0 and
  142. h(1,2,nil,1,nil) == 0)
  143. assert(1 and 2<3 == true and 2<3 and 'a'<'b' == true)
  144. x = 2<3 and not 3; assert(x==false)
  145. x = 2<1 or (2>1 and 'a'); assert(x=='a')
  146. do
  147. local a; if nil then a=1; else a=2; end; -- this nil comes as PUSHNIL 2
  148. assert(a==2)
  149. end
  150. function F(a)
  151. assert(debug.getinfo(1, "n").name == 'F')
  152. return a,2,3
  153. end
  154. a,b = F(1)~=nil; assert(a == true and b == nil);
  155. a,b = F(nil)==nil; assert(a == true and b == nil)
  156. ----------------------------------------------------------------
  157. -- creates all combinations of
  158. -- [not] ([not] arg op [not] (arg op [not] arg ))
  159. -- and tests each one
  160. function ID(x) return x end
  161. function f(t, i)
  162. local b = t.n
  163. local res = math.mod(math.floor(i/c), b)+1
  164. c = c*b
  165. return t[res]
  166. end
  167. local arg = {" ( 1 < 2 ) ", " ( 1 >= 2 ) ", " F ( ) ", " nil "; n=4}
  168. local op = {" and ", " or ", " == ", " ~= "; n=4}
  169. local neg = {" ", " not "; n=2}
  170. local i = 0
  171. repeat
  172. c = 1
  173. local s = f(neg, i)..'ID('..f(neg, i)..f(arg, i)..f(op, i)..
  174. f(neg, i)..'ID('..f(arg, i)..f(op, i)..f(neg, i)..f(arg, i)..'))'
  175. local s1 = string.gsub(s, 'ID', '')
  176. K,X,NX,WX1,WX2 = nil
  177. s = string.format([[
  178. local a = %s
  179. local b = not %s
  180. K = b
  181. local xxx;
  182. if %s then X = a else X = b end
  183. if %s then NX = b else NX = a end
  184. while %s do WX1 = a; break end
  185. while %s do WX2 = a; break end
  186. repeat if (%s) then break end; assert(b) until not(%s)
  187. ]], s1, s, s1, s, s1, s, s1, s, s)
  188. assert(loadstring(s))()
  189. assert(X and not NX and not WX1 == K and not WX2 == K)
  190. if math.mod(i,4000) == 0 then print('+') end
  191. i = i+1
  192. until i==c
  193. print'OK'