303 lines
13 KiB
Luau
303 lines
13 KiB
Luau
-- compat tests: the Luau semantics the library relies on, checked under both runtimes. Under luau these pin down what
|
|
-- Luau does; under LuaJIT they check compat/luajit.lua (bit32, buffer, table / math / string / utf8 additions, typeof)
|
|
-- and tools/build.py's lowering (generalized iteration, if-expression chains, continue, compound assignment, string
|
|
-- interpolation, floor division, number literals).
|
|
local T = require("@tests/T")
|
|
|
|
local t = T.suite("compat")
|
|
|
|
-- ------------------------------------------------------------------------------------------------ bit32
|
|
do
|
|
t:eq(bit32.band(0xFFFFFFFF, 0x0F0F0F0F), 0x0F0F0F0F, "band unsigned")
|
|
t:eq(bit32.band(-1), 0xFFFFFFFF, "band(-1)")
|
|
t:eq(bit32.band(), 0xFFFFFFFF, "band()")
|
|
t:eq(bit32.band(2 ^ 32 + 3, 7), 3, "band reduces mod 2^32")
|
|
t:eq(bit32.band(0xFF, 0xF0, 0x3C), 0x30, "band 3 args")
|
|
t:eq(bit32.band(0xFF, 0xF0, 0x3C, 0x20, 0x20), 0x20, "band 5 args")
|
|
t:eq(bit32.bor(0x80000000, 1), 0x80000001, "bor high bit")
|
|
t:eq(bit32.bor(), 0, "bor()")
|
|
t:eq(bit32.bor(1, 2, 4, 8, 16), 31, "bor 5 args")
|
|
t:eq(bit32.bxor(0xFFFFFFFF, 0x12345678), 0xEDCBA987, "bxor")
|
|
t:eq(bit32.bxor(1, 2, 4, 8, 16, 32, 64, 128, 256, 512), 1023, "bxor 10 args")
|
|
t:eq(bit32.bnot(0), 0xFFFFFFFF, "bnot")
|
|
t:eq(bit32.bnot(0xFFFFFFFF), 0, "bnot all")
|
|
t:eq(bit32.lshift(1, 31), 0x80000000, "lshift 31")
|
|
t:eq(bit32.lshift(1, 32), 0, "lshift 32")
|
|
t:eq(bit32.lshift(0xFFFFFFFF, 4), 0xFFFFFFF0, "lshift drops high bits")
|
|
t:eq(bit32.lshift(1, -1), 0, "lshift negative")
|
|
t:eq(bit32.lshift(8, -2), 2, "lshift negative = rshift")
|
|
t:eq(bit32.rshift(0x80000000, 31), 1, "rshift logical")
|
|
t:eq(bit32.rshift(0xFFFFFFFF, 0), 0xFFFFFFFF, "rshift 0 stays unsigned")
|
|
t:eq(bit32.rshift(2, -1), 4, "rshift negative = lshift")
|
|
t:eq(bit32.rshift(5, 40), 0, "rshift 40")
|
|
t:eq(bit32.arshift(0x80000000, 4), 0xF8000000, "arshift sign fill")
|
|
t:eq(bit32.arshift(0x80000000, 40), 0xFFFFFFFF, "arshift >= 32")
|
|
t:eq(bit32.lrotate(0x80000001, 1), 3, "lrotate")
|
|
t:eq(bit32.rrotate(3, 1), 0x80000001, "rrotate")
|
|
t:eq(bit32.extract(0xF0, 4, 4), 15, "extract")
|
|
t:eq(bit32.extract(0x80000000, 31), 1, "extract 1 bit")
|
|
t:eq(bit32.replace(0, 5, 4, 3), 80, "replace")
|
|
t:eq(bit32.countlz(1), 31, "countlz")
|
|
t:eq(bit32.countlz(0), 32, "countlz 0")
|
|
t:eq(bit32.countrz(8), 3, "countrz")
|
|
t:eq(bit32.countrz(0), 32, "countrz 0")
|
|
t:eq(bit32.byteswap(0x01020304), 0x04030201, "byteswap")
|
|
t:eq(bit32.byteswap(0x000000FF), 0xFF000000, "byteswap high")
|
|
t:check(bit32.btest(6, 2) and not bit32.btest(4, 2), "btest")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ buffer
|
|
do
|
|
local b = buffer.create(16)
|
|
t:eq(buffer.len(b), 16, "len")
|
|
t:eq(buffer.readu32(b, 12), 0, "zero-filled")
|
|
buffer.writeu32(b, 0, 0xDEADBEEF)
|
|
t:eq(buffer.readu32(b, 0), 0xDEADBEEF, "u32 round trip")
|
|
t:eq(buffer.readu8(b, 0), 0xEF, "little-endian")
|
|
t:eq(buffer.readu16(b, 2), 0xDEAD, "u16")
|
|
t:eq(buffer.readi8(b, 3), -34, "i8 sign")
|
|
t:eq(buffer.readi16(b, 2), 0xDEAD - 65536, "i16 sign")
|
|
t:eq(buffer.readi32(b, 0), 0xDEADBEEF - 2 ^ 32, "i32 sign")
|
|
buffer.writeu8(b, 0, 300)
|
|
t:eq(buffer.readu8(b, 0), 44, "writeu8 wraps")
|
|
buffer.writeu8(b, 0, -1)
|
|
t:eq(buffer.readu8(b, 0), 255, "writeu8 -1")
|
|
buffer.writeu8(b, 0, 3.7)
|
|
t:eq(buffer.readu8(b, 0), 3, "writeu8 truncates")
|
|
buffer.writeu8(b, 0, 255 + 255)
|
|
t:eq(buffer.readu8(b, 0), 254, "writeu8 of a sum")
|
|
buffer.writeu32(b, 4, -1)
|
|
t:eq(buffer.readu32(b, 4), 0xFFFFFFFF, "writeu32 -1")
|
|
buffer.writeu32(b, 4, 2 ^ 32 + 5)
|
|
t:eq(buffer.readu32(b, 4), 5, "writeu32 wraps")
|
|
buffer.writei16(b, 8, 40000)
|
|
t:eq(buffer.readi16(b, 8), -25536, "writei16 wraps")
|
|
buffer.writeu16(b, 8, -2)
|
|
t:eq(buffer.readu16(b, 8), 65534, "writeu16 -2")
|
|
buffer.writei32(b, 8, -5)
|
|
t:eq(buffer.readi32(b, 8), -5, "i32 round trip")
|
|
buffer.writef32(b, 0, 1.5)
|
|
t:eq(buffer.readf32(b, 0), 1.5, "f32")
|
|
buffer.writef32(b, 0, 0.1)
|
|
t:check(math.abs(buffer.readf32(b, 0) - 0.1) < 1e-8 and buffer.readf32(b, 0) ~= 0.1, "f32 rounds")
|
|
buffer.writef64(b, 8, math.pi)
|
|
t:eq(buffer.readf64(b, 8), math.pi, "f64")
|
|
local nan = buffer.readf32(buffer.fromstring("\0\0\192\127"), 0)
|
|
t:check(nan ~= nan, "f32 NaN")
|
|
-- out of bounds throws (both runtimes), and nothing is written
|
|
t:check(not pcall(buffer.readu8, b, 16), "readu8 past the end throws")
|
|
t:check(not pcall(buffer.readu8, b, -1), "readu8 at -1 throws")
|
|
t:check(not pcall(buffer.readu32, b, 13), "readu32 overlapping the end throws")
|
|
t:check(not pcall(buffer.writeu16, b, 15, 0), "writeu16 overlapping the end throws")
|
|
t:check(not pcall(buffer.readstring, b, 10, 7), "readstring past the end throws")
|
|
t:check(not pcall(buffer.copy, b, 10, b, 0, 7), "copy past the end throws")
|
|
t:check(not pcall(buffer.fill, b, 10, 0, 7), "fill past the end throws")
|
|
-- strings, NULs, copy, fill
|
|
local s = buffer.fromstring("a\0b\255c")
|
|
t:eq(buffer.len(s), 5, "fromstring with NUL")
|
|
t:eq(buffer.tostring(s), "a\0b\255c", "tostring with NUL")
|
|
t:eq(buffer.readstring(s, 1, 3), "\0b\255", "readstring")
|
|
buffer.writestring(b, 0, "hello world")
|
|
t:eq(buffer.readstring(b, 0, 11), "hello world", "writestring")
|
|
buffer.writestring(b, 0, "HELLO", 2)
|
|
t:eq(buffer.readstring(b, 0, 5), "HEllo", "writestring count")
|
|
buffer.copy(b, 2, b, 0, 6) -- overlapping (memmove)
|
|
t:eq(buffer.readstring(b, 0, 8), "HEHEllo ", "copy overlapping")
|
|
local c = buffer.create(4)
|
|
buffer.copy(c, 1, s, 2)
|
|
t:eq(buffer.readstring(c, 1, 3), "b\255c", "copy default count")
|
|
buffer.fill(c, 0, 0x41)
|
|
t:eq(buffer.tostring(c), "AAAA", "fill to the end")
|
|
buffer.fill(c, 1, 0x142, 2)
|
|
t:eq(buffer.tostring(c), "ABBA", "fill value wraps")
|
|
t:eq(buffer.len(buffer.create(0)), 0, "empty buffer")
|
|
t:eq(buffer.tostring(buffer.create(0)), "", "empty tostring")
|
|
-- readbits / writebits
|
|
local bb = buffer.create(8)
|
|
buffer.writeu32(bb, 0, 0xDEADBEEF)
|
|
t:eq(buffer.readbits(bb, 4, 12), 3054, "readbits")
|
|
t:eq(buffer.readbits(bb, 0, 32), 0xDEADBEEF, "readbits 32")
|
|
t:eq(buffer.readbits(bb, 0, 0), 0, "readbits 0")
|
|
buffer.writebits(bb, 3, 7, 0x7F)
|
|
t:eq(buffer.readu32(bb, 0), 3735928831, "writebits")
|
|
buffer.writebits(bb, 30, 20, 0xABCDE)
|
|
t:eq(buffer.readbits(bb, 30, 20), 0xABCDE, "writebits across bytes")
|
|
t:check(not pcall(buffer.readbits, bb, 60, 5), "readbits past the end throws")
|
|
-- big buffers (LuaJIT: calloc'd memory)
|
|
local big = buffer.create(1000000)
|
|
buffer.writeu32(big, 999996, 0x01020304)
|
|
t:eq(buffer.readu32(big, 999996), 0x01020304, "big buffer")
|
|
t:eq(buffer.readu8(big, 500000), 0, "big buffer zero-filled")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ typeof / type
|
|
do
|
|
local b = buffer.create(1)
|
|
t:eq(typeof(b), "buffer", "typeof buffer")
|
|
t:eq(type(b), "buffer", "type buffer")
|
|
t:eq(typeof({}), "table", "typeof table")
|
|
t:eq(type(1), "number", "type number")
|
|
t:eq(typeof(nil), "nil", "typeof nil")
|
|
t:eq(typeof("s"), "string", "typeof string")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ table / math / string
|
|
do
|
|
local c = table.create(3, 7)
|
|
t:eq(#c, 3, "table.create size")
|
|
t:eq(c[3], 7, "table.create value")
|
|
t:eq(#table.create(5), 0, "table.create without value")
|
|
t:eq(table.find({ 5, 6, 7 }, 6), 2, "table.find")
|
|
t:eq(table.find({ 5, 6, 7 }, 8), nil, "table.find missing")
|
|
t:eq(table.find({ 5, 6, 5 }, 5, 2), 3, "table.find init")
|
|
local x = { 1, 2, a = 3 }
|
|
table.clear(x)
|
|
t:check(next(x) == nil, "table.clear")
|
|
local f = table.freeze({ 1 })
|
|
t:check(table.isfrozen(f) and not table.isfrozen({}), "freeze / isfrozen")
|
|
local cl = table.clone({ 1, 2, k = "v" })
|
|
t:check(cl[2] == 2 and cl.k == "v", "table.clone")
|
|
t:eq(table.unpack({ 1, 2, 3 }, 2, 3), 2, "table.unpack")
|
|
t:eq(select("#", table.unpack({ 1, 2, 3 })), 3, "table.unpack count")
|
|
local m = table.move({ 1, 2, 3 }, 1, 3, 2)
|
|
t:check(m[1] == 1 and m[2] == 1 and m[3] == 2 and m[4] == 3, "table.move overlapping")
|
|
t:eq(math.clamp(5, 0, 3), 3, "clamp high")
|
|
t:eq(math.clamp(-1, 0, 3), 0, "clamp low")
|
|
t:eq(math.sign(-3), -1, "sign")
|
|
t:eq(math.sign(0), 0, "sign 0")
|
|
t:eq(math.round(2.5), 3, "round half up")
|
|
t:eq(math.round(-2.5), -3, "round half away from zero")
|
|
t:eq(math.round(2.4), 2, "round down")
|
|
t:check(type(math.noise(0.5, 0.3, 0.1)) == "number", "noise is a number")
|
|
local parts = string.split("a,b,,c", ",")
|
|
t:check(#parts == 4 and parts[1] == "a" and parts[3] == "" and parts[4] == "c", "string.split")
|
|
t:eq(#string.split("", ","), 1, "split empty")
|
|
t:eq(#("x y z"):split(" "), 3, "split as a method")
|
|
t:eq(string.format("%5.1f|%d|%x|%s", 3.14159, 42, 255, "s"), " 3.1|42|ff|s", "string.format")
|
|
t:eq(math.floor(-3.5), -4, "floor")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ utf8
|
|
do
|
|
t:eq(utf8.char(72, 228, 8364, 128512), "H\xC3\xA4\xE2\x82\xAC\xF0\x9F\x98\x80", "utf8.char")
|
|
t:eq(utf8.len("h\xC3\xA4ll\xE2\x82\xAC"), 5, "utf8.len")
|
|
local n, bad = utf8.len("a\xFFb")
|
|
t:check(n == nil and bad == 2, "utf8.len invalid")
|
|
t:eq(utf8.codepoint("\xC3\xA4"), 228, "utf8.codepoint")
|
|
t:eq(utf8.offset("h\xC3\xA4ll", 3), 4, "utf8.offset")
|
|
local cps = {}
|
|
for _, c in utf8.codes("a\xC3\xA4\xE2\x82\xAC") do
|
|
table.insert(cps, c)
|
|
end
|
|
t:check(#cps == 3 and cps[2] == 228 and cps[3] == 8364, "utf8.codes")
|
|
t:check(string.find("\xC3\xA4", utf8.charpattern) == 1, "utf8.charpattern")
|
|
end
|
|
|
|
-- ------------------------------------------------------------------------------------------------ syntax lowering
|
|
do
|
|
-- generalized iteration: arrays (in order), dictionaries, mixed, iterator functions, nested
|
|
local order = {}
|
|
for i, v in { 10, 20, 30 } do
|
|
table.insert(order, i * 100 + v)
|
|
end
|
|
t:check(order[1] == 110 and order[2] == 220 and order[3] == 330, "for over an array, in order")
|
|
local sum = 0
|
|
for k, v in { a = 1, b = 2, c = 3 } do
|
|
sum += v
|
|
t:check(type(k) == "string", "dict keys")
|
|
end
|
|
t:eq(sum, 6, "for over a dictionary")
|
|
local count = 0
|
|
for _ in { 1, 2, x = 3 } do
|
|
count += 1
|
|
end
|
|
t:eq(count, 3, "for over a mixed table visits every key")
|
|
local zero = { [0] = 5, 6, 7 }
|
|
local zsum = 0
|
|
for _, v in zero do
|
|
zsum += v
|
|
end
|
|
t:eq(zsum, 18, "for over a table with key 0 visits it (LuaJIT keeps t[0] in the array part)")
|
|
local w = {}
|
|
for word in string.gmatch("a b c", "%a") do
|
|
table.insert(w, word)
|
|
end
|
|
t:eq(#w, 3, "for over an iterator function")
|
|
local pc = 0
|
|
for _, _ in pairs({ 1, 2 }) do
|
|
pc += 1
|
|
end
|
|
t:eq(pc, 2, "pairs untouched")
|
|
local empty = 0
|
|
for _ in {} do
|
|
empty += 1
|
|
end
|
|
t:eq(empty, 0, "for over an empty table")
|
|
-- if-expressions: chains of elseif (darklua 0.19 reverses them; build.py rewrites them)
|
|
local function grade(i: number): string
|
|
return if i <= 144 then "a" elseif i <= 256 then "b" elseif i <= 280 then "c" else "d"
|
|
end
|
|
t:check(grade(1) == "a" and grade(200) == "b" and grade(270) == "c" and grade(285) == "d", "if-expression elseif chain")
|
|
local function n3(i: number): number
|
|
return if i < 0 then -1 elseif i == 0 then 0 elseif i < 10 then 1 elseif i < 100 then 2 else 3
|
|
end
|
|
t:check(n3(-5) == -1 and n3(0) == 0 and n3(5) == 1 and n3(50) == 2 and n3(500) == 3, "numeric elseif chain")
|
|
local function falsy(c: boolean): any
|
|
return if c then nil else false
|
|
end
|
|
t:check(falsy(true) == nil and falsy(false) == false, "if-expression with falsy branches")
|
|
local function nested(a: boolean, b: boolean): number
|
|
return if a then (if b then 1 else 2) else if b then 3 else 4
|
|
end
|
|
t:check(nested(true, true) == 1 and nested(true, false) == 2 and nested(false, true) == 3 and nested(false, false) == 4, "nested if-expressions")
|
|
local v = 5
|
|
local arith = if v > 3 then v * 2 else v - 1
|
|
t:eq(arith, 10, "if-expression arithmetic")
|
|
-- continue, compound assignment, interpolation, floor division, literals
|
|
local odd = 0
|
|
for i = 1, 10 do
|
|
if i % 2 == 0 then
|
|
continue
|
|
end
|
|
odd += i
|
|
end
|
|
t:eq(odd, 25, "continue")
|
|
local s = "a"
|
|
s ..= "b"
|
|
local n = 7
|
|
n //= 2
|
|
n *= 3
|
|
n -= 1
|
|
n ^= 2
|
|
t:check(s == "ab" and n == 64, "compound assignment")
|
|
t:eq(`x={n} y={1 + 2}`, "x=64 y=3", "string interpolation")
|
|
t:eq(-7 // 2, -4, "floor division of a negative")
|
|
t:eq(7.5 // 2, 3, "floor division of a fraction")
|
|
t:eq(-7 % 3, 2, "modulo of a negative")
|
|
t:eq(0b1010 + 1_000 + 0xFF_FF, 10 + 1000 + 65535, "number literals")
|
|
t:eq(0x7FFFFFFF + 1, 2147483648, "no integer overflow")
|
|
end
|
|
|
|
do -- float reads: NaNs with a payload come back as the canonical NaN (LuaJIT: never an aliased object value)
|
|
local b = buffer.create(16)
|
|
buffer.writeu32(b, 0, 0x7fc12345)
|
|
buffer.writeu32(b, 4, 0x7f800000)
|
|
buffer.writeu32(b, 8, 0xdeadbeef)
|
|
buffer.writeu32(b, 12, 0xfff12345)
|
|
local a, i, c = buffer.readf32(b, 0), buffer.readf32(b, 4), buffer.readf64(b, 8)
|
|
t:eq(type(a), "number", "f32 NaN payload is a number")
|
|
t:check(a ~= a, "f32 NaN payload reads as NaN")
|
|
t:eq(i, math.huge, "f32 +inf")
|
|
t:eq(type(c), "number", "f64 NaN payload is a number")
|
|
t:check(c ~= c, "f64 NaN payload reads as NaN")
|
|
buffer.writeu32(b, 8, 0)
|
|
buffer.writeu32(b, 12, 0xfff00000)
|
|
t:eq(buffer.readf64(b, 8), -math.huge, "f64 -inf")
|
|
buffer.writef32(b, 0, 1.5)
|
|
buffer.writef64(b, 8, -2.25)
|
|
t:eq(buffer.readf32(b, 0), 1.5, "f32 normal")
|
|
t:eq(buffer.readf64(b, 8), -2.25, "f64 normal")
|
|
end
|
|
|
|
t:done()
|