mirror of
https://github.com/luau-lang/luau.git
synced 2024-11-15 14:25:44 +08:00
107 lines
2.8 KiB
Lua
107 lines
2.8 KiB
Lua
|
-- This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
|
||
|
-- This file is based on Lua 5.x tests -- https://github.com/lua/lua/tree/master/testes
|
||
|
print("testing assignments, logical operators, and constructors")
|
||
|
|
||
|
local unpack = table.unpack
|
||
|
|
||
|
local res, res2 = 27
|
||
|
|
||
|
a, b = 1, 2+3
|
||
|
assert(a==1 and b==5)
|
||
|
a={}
|
||
|
function f() return 10, 11, 12 end
|
||
|
a.x, b, a[1] = 1, 2, f()
|
||
|
assert(a.x==1 and b==2 and a[1]==10)
|
||
|
a[f()], b, a[f()+3] = f(), a, 'x'
|
||
|
assert(a[10] == 10 and b == a and a[13] == 'x')
|
||
|
|
||
|
do
|
||
|
local f = function (n) local x = {}; for i=1,n do x[i]=i end;
|
||
|
return unpack(x) end;
|
||
|
local a,b,c
|
||
|
a,b = 0, f(1)
|
||
|
assert(a == 0 and b == 1)
|
||
|
A,b = 0, f(1)
|
||
|
assert(A == 0 and b == 1)
|
||
|
a,b,c = 0,5,f(4)
|
||
|
assert(a==0 and b==5 and c==1)
|
||
|
a,b,c = 0,5,f(0)
|
||
|
assert(a==0 and b==5 and c==nil)
|
||
|
end
|
||
|
|
||
|
|
||
|
a, b, c, d = 1 and nil, 1 or nil, (1 and (nil or 1)), 6
|
||
|
assert(not a and b and c and d==6)
|
||
|
|
||
|
d = 20
|
||
|
a, b, c, d = f()
|
||
|
assert(a==10 and b==11 and c==12 and d==nil)
|
||
|
a,b = f(), 1, 2, 3, f()
|
||
|
assert(a==10 and b==1)
|
||
|
|
||
|
assert(a<b == false and a>b == true)
|
||
|
assert((10 and 2) == 2)
|
||
|
assert((10 or 2) == 10)
|
||
|
assert((10 or assert(nil)) == 10)
|
||
|
assert(not (nil and assert(nil)))
|
||
|
assert((nil or "alo") == "alo")
|
||
|
assert((nil and 10) == nil)
|
||
|
assert((false and 10) == false)
|
||
|
assert((true or 10) == true)
|
||
|
assert((false or 10) == 10)
|
||
|
assert(false ~= nil)
|
||
|
assert(nil ~= false)
|
||
|
assert(not nil == true)
|
||
|
assert(not not nil == false)
|
||
|
assert(not not 1 == true)
|
||
|
assert(not not a == true)
|
||
|
assert(not not (6 or nil) == true)
|
||
|
assert(not not (nil and 56) == false)
|
||
|
assert(not not (nil and true) == false)
|
||
|
print('+')
|
||
|
|
||
|
a = {}
|
||
|
a[true] = 20
|
||
|
a[false] = 10
|
||
|
assert(a[1<2] == 20 and a[1>2] == 10)
|
||
|
|
||
|
function f(a) return a end
|
||
|
|
||
|
local a = {}
|
||
|
for i=3000,-3000,-1 do a[i] = i; end
|
||
|
a[10e30] = "alo"; a[true] = 10; a[false] = 20
|
||
|
assert(a[10e30] == 'alo' and a[not 1] == 20 and a[10<20] == 10)
|
||
|
for i=3000,-3000,-1 do assert(a[i] == i); end
|
||
|
a[print] = assert
|
||
|
a[f] = print
|
||
|
a[a] = a
|
||
|
assert(a[a][a][a][a][print] == assert)
|
||
|
a[print](a[a[f]] == a[print])
|
||
|
a = nil
|
||
|
|
||
|
a = {10,9,8,7,6,5,4,3,2; [-3]='a', [f]=print, a='a', b='ab'}
|
||
|
a, a.x, a.y = a, a[-3]
|
||
|
assert(a[1]==10 and a[-3]==a.a and a[f]==print and a.x=='a' and not a.y)
|
||
|
a[1], f(a)[2], b, c = {['alo']=assert}, 10, a[1], a[f], 6, 10, 23, f(a), 2
|
||
|
a[1].alo(a[2]==10 and b==10 and c==print)
|
||
|
|
||
|
a[2^31] = 10; a[2^31+1] = 11; a[-2^31] = 12;
|
||
|
a[2^32] = 13; a[-2^32] = 14; a[2^32+1] = 15; a[10^33] = 16;
|
||
|
|
||
|
assert(a[2^31] == 10 and a[2^31+1] == 11 and a[-2^31] == 12 and
|
||
|
a[2^32] == 13 and a[-2^32] == 14 and a[2^32+1] == 15 and
|
||
|
a[10^33] == 16)
|
||
|
|
||
|
a = nil
|
||
|
|
||
|
|
||
|
do
|
||
|
local a,i,j,b
|
||
|
a = {'a', 'b'}; i=1; j=2; b=a
|
||
|
i, a[i], a, j, a[j], a[i+j] = j, i, i, b, j, i
|
||
|
assert(i == 2 and b[1] == 1 and a == 1 and j == b and b[2] == 2 and
|
||
|
b[3] == 1)
|
||
|
end
|
||
|
|
||
|
return('OK')
|