github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/testdata/lua-5.3.3-tests/constructs.lua (about)

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