From 65d022bf3d7330a3d8dc69c08e07a81f25b8ba4b Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 14 Apr 2018 14:53:11 +1000 Subject: test/test-suite: Move ported PUC-Rio test suite to jest --- test/test-suite/strings.test.js | 577 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 577 insertions(+) create mode 100644 test/test-suite/strings.test.js (limited to 'test/test-suite/strings.test.js') diff --git a/test/test-suite/strings.test.js b/test/test-suite/strings.test.js new file mode 100644 index 0000000..7003b89 --- /dev/null +++ b/test/test-suite/strings.test.js @@ -0,0 +1,577 @@ +"use strict"; + +const lua = require('../../src/lua.js'); +const lauxlib = require('../../src/lauxlib.js'); +const lualib = require('../../src/lualib.js'); +const {to_luastring} = require("../../src/fengaricore.js"); + +const checkerror = ` + local maxi, mini = math.maxinteger, math.mininteger + + local function checkerror (msg, f, ...) + local s, err = pcall(f, ...) + assert(not s and string.find(err, msg)) + end +`; + +test('[test-suite] strings: string comparisons', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert('alo' < 'alo1') + assert('' < 'a') + assert('alo\\0alo' < 'alo\\0b') + assert('alo\\0alo\\0\\0' > 'alo\\0alo\\0') + assert('alo' < 'alo\\0') + assert('alo\\0' > 'alo') + assert('\\0' < '\\1') + assert('\\0\\0' < '\\0\\1') + assert('\\1\\0a\\0a' <= '\\1\\0a\\0a') + assert(not ('\\1\\0a\\0b' <= '\\1\\0a\\0a')) + assert('\\0\\0\\0' < '\\0\\0\\0\\0') + assert(not('\\0\\0\\0\\0' < '\\0\\0\\0')) + assert('\\0\\0\\0' <= '\\0\\0\\0\\0') + assert(not('\\0\\0\\0\\0' <= '\\0\\0\\0')) + assert('\\0\\0\\0' <= '\\0\\0\\0') + assert('\\0\\0\\0' >= '\\0\\0\\0') + assert(not ('\\0\\0b' < '\\0\\0a\\0')) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: string.sub', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert('alo' < 'alo1') + assert('' < 'a') + assert('alo\\0alo' < 'alo\\0b') + assert('alo\\0alo\\0\\0' > 'alo\\0alo\\0') + assert('alo' < 'alo\\0') + assert('alo\\0' > 'alo') + assert('\\0' < '\\1') + assert('\\0\\0' < '\\0\\1') + assert('\\1\\0a\\0a' <= '\\1\\0a\\0a') + assert(not ('\\1\\0a\\0b' <= '\\1\\0a\\0a')) + assert('\\0\\0\\0' < '\\0\\0\\0\\0') + assert(not('\\0\\0\\0\\0' < '\\0\\0\\0')) + assert('\\0\\0\\0' <= '\\0\\0\\0\\0') + assert(not('\\0\\0\\0\\0' <= '\\0\\0\\0')) + assert('\\0\\0\\0' <= '\\0\\0\\0') + assert('\\0\\0\\0' >= '\\0\\0\\0') + assert(not ('\\0\\0b' < '\\0\\0a\\0')) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: string.find', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(string.find("123456789", "345") == 3) + a,b = string.find("123456789", "345") + assert(string.sub("123456789", a, b) == "345") + assert(string.find("1234567890123456789", "345", 3) == 3) + assert(string.find("1234567890123456789", "345", 4) == 13) + assert(string.find("1234567890123456789", "346", 4) == nil) + assert(string.find("1234567890123456789", ".45", -9) == 13) + assert(string.find("abcdefg", "\\0", 5, 1) == nil) + assert(string.find("", "") == 1) + assert(string.find("", "", 1) == 1) + assert(not string.find("", "", 2)) + assert(string.find('', 'aaa', 1) == nil) + assert(('alo(.)alo'):find('(.)', 1, 1) == 4) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: string.len and #', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(string.len("") == 0) + assert(string.len("\\0\\0\\0") == 3) + assert(string.len("1234567890") == 10) + + assert(#"" == 0) + assert(#"\\0\\0\\0" == 3) + assert(#"1234567890" == 10) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: string.byte/string.char', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(string.byte("a") == 97) + assert(string.byte("\\xe4") > 127) + assert(string.byte(string.char(255)) == 255) + assert(string.byte(string.char(0)) == 0) + assert(string.byte("\\0") == 0) + assert(string.byte("\\0\\0alo\\0x", -1) == string.byte('x')) + assert(string.byte("ba", 2) == 97) + assert(string.byte("\\n\\n", 2, -1) == 10) + assert(string.byte("\\n\\n", 2, 2) == 10) + assert(string.byte("") == nil) + assert(string.byte("hi", -3) == nil) + assert(string.byte("hi", 3) == nil) + assert(string.byte("hi", 9, 10) == nil) + assert(string.byte("hi", 2, 1) == nil) + assert(string.char() == "") + assert(string.char(0, 255, 0) == "\\0\\255\\0") + assert(string.char(0, string.byte("\\xe4"), 0) == "\\0\\xe4\\0") + assert(string.char(string.byte("\\xe4l\\0óu", 1, -1)) == "\\xe4l\\0óu") + assert(string.char(string.byte("\\xe4l\\0óu", 1, 0)) == "") + assert(string.char(string.byte("\\xe4l\\0óu", -10, 100)) == "\\xe4l\\0óu") + + assert(string.upper("ab\\0c") == "AB\\0C") + assert(string.lower("\\0ABCc%$") == "\\0abcc%$") + assert(string.rep('teste', 0) == '') + assert(string.rep('tés\\00tê', 2) == 'tés\\0têtés\\000tê') + assert(string.rep('', 10) == '') + + if string.packsize("i") == 4 then + -- result length would be 2^31 (int overflow) + checkerror("too large", string.rep, 'aa', (1 << 30)) + checkerror("too large", string.rep, 'a', (1 << 30), ',') + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: repetitions with separator', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(string.rep('teste', 0, 'xuxu') == '') + assert(string.rep('teste', 1, 'xuxu') == 'teste') + assert(string.rep('\\1\\0\\1', 2, '\\0\\0') == '\\1\\0\\1\\0\\0\\1\\0\\1') + assert(string.rep('', 10, '.') == string.rep('.', 9)) + assert(not pcall(string.rep, "aa", maxi // 2 + 10)) + assert(not pcall(string.rep, "", maxi // 2 + 10, "aa")) + + assert(string.reverse"" == "") + assert(string.reverse"\\0\\1\\2\\3" == "\\3\\2\\1\\0") + assert(string.reverse"\\0001234" == "4321\\0") + + for i=0,30 do assert(string.len(string.rep('a', i)) == i) end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: tostring', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(type(tostring(nil)) == 'string') + assert(type(tostring(12)) == 'string') + assert(string.find(tostring{}, 'table:')) + assert(string.find(tostring(print), 'function:')) + assert(#tostring('\\0') == 1) + assert(tostring(true) == "true") + assert(tostring(false) == "false") + assert(tostring(-1203) == "-1203") + assert(tostring(1203.125) == "1203.125") + assert(tostring(-0.5) == "-0.5") + assert(tostring(-32767) == "-32767") + if math.tointeger(2147483647) then -- no overflow? (32 bits) + assert(tostring(-2147483647) == "-2147483647") + end + if math.tointeger(4611686018427387904) then -- no overflow? (64 bits) + assert(tostring(4611686018427387904) == "4611686018427387904") + assert(tostring(-4611686018427387904) == "-4611686018427387904") + end + + if tostring(0.0) == "0.0" then -- "standard" coercion float->string + assert('' .. 12 == '12' and 12.0 .. '' == '12.0') + assert(tostring(-1203 + 0.0) == "-1203.0") + else -- compatible coercion + assert(tostring(0.0) == "0") + assert('' .. 12 == '12' and 12.0 .. '' == '12') + assert(tostring(-1203 + 0.0) == "-1203") + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: string.format', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + x = '"ílo"\\n\\\\' + assert(string.format('%q%s', x, x) == '"\\\\"ílo\\\\"\\\\\\n\\\\\\\\""ílo"\\n\\\\') + assert(string.format('%q', "\\0") == [["\\0"]]) + assert(load(string.format('return %q', x))() == x) + x = "\\0\\1\\0023\\5\\0009" + assert(load(string.format('return %q', x))() == x) + assert(string.format("\\0%c\\0%c%x\\0", string.byte("\\xe4"), string.byte("b"), 140) == + "\\0\\xe4\\0b8c\\0") + assert(string.format('') == "") + assert(string.format("%c",34)..string.format("%c",48)..string.format("%c",90)..string.format("%c",100) == + string.format("%c%c%c%c", 34, 48, 90, 100)) + assert(string.format("%s\\0 is not \\0%s", 'not be', 'be') == 'not be\\0 is not \\0be') + assert(string.format("%%%d %010d", 10, 23) == "%10 0000000023") + assert(tonumber(string.format("%f", 10.3)) == 10.3) + x = string.format('"%-50s"', 'a') + assert(#x == 52) + assert(string.sub(x, 1, 4) == '"a ') + + assert(string.format("-%.20s.20s", string.rep("%", 2000)) == + "-"..string.rep("%", 20)..".20s") + assert(string.format('"-%20s.20s"', string.rep("%", 2000)) == + string.format("%q", "-"..string.rep("%", 2000)..".20s")) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: %q', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + do + local function checkQ (v) + local s = string.format("%q", v) + local nv = load("return " .. s)() + assert(v == nv and math.type(v) == math.type(nv)) + end + checkQ("\\0\\0\\1\\255\\u{234}") + checkQ(math.maxinteger) + checkQ(math.mininteger) + checkQ(math.pi) + checkQ(0.1) + checkQ(true) + checkQ(nil) + checkQ(false) + checkerror("no literal", string.format, "%q", {}) + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: embedded zeros error', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(string.format("\\0%s\\0", "\\0\\0\\1") == "\\0\\0\\0\\1\\0") + checkerror("contains zeros", string.format, "%10s", "\\0") + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: format x tostring', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(string.format("%s %s", nil, true) == "nil true") + assert(string.format("%s %.4s", false, true) == "false true") + assert(string.format("%.3s %.3s", false, true) == "fal tru") + local m = setmetatable({}, {__tostring = function () return "hello" end, + __name = "hi"}) + assert(string.format("%s %.10s", m, m) == "hello hello") + getmetatable(m).__tostring = nil -- will use '__name' from now on + assert(string.format("%.4s", m) == "hi: ") + + getmetatable(m).__tostring = function () return {} end + checkerror("'__tostring' must return a string", tostring, m) + + + assert(string.format("%x", 0.0) == "0") + assert(string.format("%02x", 0.0) == "00") + -- assert(string.format("%08X", 0xFFFFFFFF) == "FFFFFFFF") + assert(string.format("%+08d", 31501) == "+0031501") + assert(string.format("%+08d", -30927) == "-0030927") + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: longest number that can be formatted', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + do + local i = 1 + local j = 10000 + while i + 1 < j do -- binary search for maximum finite float + local m = (i + j) // 2 + if 10^m < math.huge then i = m else j = m end + end + assert(10^i < math.huge and 10^j == math.huge) + local s = string.format('%.20f', -(10^i)) -- TODO: %.99f not possible with sprintf.js + -- assert(string.len(s) >= i + 101) + assert(tonumber(s) == -(10^i)) + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test('[test-suite] strings: large numbers for format', () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + do -- assume at least 32 bits + local max, min = 0x7fffffff, -0x80000000 -- "large" for 32 bits + -- assert(string.sub(string.format("%8x", -1), -8) == "ffffffff") + assert(string.format("%x", max) == "7fffffff") + assert(string.sub(string.format("%x", min), -8) == "80000000") + assert(string.format("%d", max) == "2147483647") + assert(string.format("%d", min) == "-2147483648") + assert(string.format("%u", 0xffffffff) == "4294967295") + assert(string.format("%o", 0xABCD) == "125715") + + max, min = 0x7fffffffffffffff, -0x8000000000000000 + if max > 2.0^53 then -- only for 64 bits + assert(string.format("%x", (2^52 | 0) - 1) == "fffffffffffff") + assert(string.format("0x%8X", 0x8f000003) == "0x8F000003") + assert(string.format("%d", 2^53) == "9007199254740992") + assert(string.format("%i", -2^53) == "-9007199254740992") + assert(string.format("%x", max) == "7fffffffffffffff") + assert(string.format("%x", min) == "8000000000000000") + assert(string.format("%d", max) == "9223372036854775807") + assert(string.format("%d", min) == "-9223372036854775808") + assert(string.format("%u", ~(-1 << 64)) == "18446744073709551615") + assert(tostring(1234567890123) == '1234567890123') + end + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test("[test-suite] strings: 'format %a %A'", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + do + local function matchhexa (n) + local s = string.format("%a", n) + -- result matches ISO C requirements + assert(string.find(s, "^%-?0x[1-9a-f]%.?[0-9a-f]*p[-+]?%d+$")) + assert(tonumber(s) == n) -- and has full precision + s = string.format("%A", n) + assert(string.find(s, "^%-?0X[1-9A-F]%.?[0-9A-F]*P[-+]?%d+$")) + assert(tonumber(s) == n) + end + for _, n in ipairs{0.1, -0.1, 1/3, -1/3, 1e30, -1e30, + -45/247, 1, -1, 2, -2, 3e-20, -3e-20} do + matchhexa(n) + end + + assert(string.find(string.format("%A", 0.0), "^0X0%.?0?P%+?0$")) + assert(string.find(string.format("%a", -0.0), "^%-0x0%.?0?p%+?0$")) + + if not _port then -- test inf, -inf, NaN, and -0.0 + assert(string.find(string.format("%a", 1/0), "^inf")) + assert(string.find(string.format("%A", -1/0), "^%-INF")) + assert(string.find(string.format("%a", 0/0), "^%-?nan")) + assert(string.find(string.format("%a", -0.0), "^%-0x0")) + end + + if not pcall(string.format, "%.3a", 0) then + (Message or print)("\\n >>> modifiers for format '%a' not available <<<\\n") + else + assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) + assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) + end + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test("[test-suite] strings: errors in format", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local function check (fmt, msg) + checkerror(msg, string.format, fmt, 10) + end + + local aux = string.rep('0', 600) + check("%100.3d", "too long") + check("%1"..aux..".3d", "too long") + check("%1.100d", "too long") + check("%10.1"..aux.."004d", "too long") + check("%t", "invalid option") + check("%"..aux.."d", "repeated flags") + check("%d %d", "no value") + + + assert(load("return 1\\n--comment without ending EOL")() == 1) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test("[test-suite] strings: table.concat", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + checkerror("table expected", table.concat, 3) + assert(table.concat{} == "") + assert(table.concat({}, 'x') == "") + assert(table.concat({'\\0', '\\0\\1', '\\0\\1\\2'}, '.\\0.') == "\\0.\\0.\\0\\1.\\0.\\0\\1\\2") + local a = {}; for i=1,300 do a[i] = "xuxu" end + assert(table.concat(a, "123").."123" == string.rep("xuxu123", 300)) + assert(table.concat(a, "b", 20, 20) == "xuxu") + assert(table.concat(a, "", 20, 21) == "xuxuxuxu") + assert(table.concat(a, "x", 22, 21) == "") + assert(table.concat(a, "3", 299) == "xuxu3xuxu") + assert(table.concat({}, "x", maxi, maxi - 1) == "") + assert(table.concat({}, "x", mini + 1, mini) == "") + assert(table.concat({}, "x", maxi, mini) == "") + assert(table.concat({[maxi] = "alo"}, "x", maxi, maxi) == "alo") + assert(table.concat({[maxi] = "alo", [maxi - 1] = "y"}, "-", maxi - 1, maxi) + == "y-alo") + + assert(not pcall(table.concat, {"a", "b", {}})) + + a = {"a","b","c"} + assert(table.concat(a, ",", 1, 0) == "") + assert(table.concat(a, ",", 1, 1) == "a") + assert(table.concat(a, ",", 1, 2) == "a,b") + assert(table.concat(a, ",", 2) == "b,c") + assert(table.concat(a, ",", 3) == "c") + assert(table.concat(a, ",", 4) == "") + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +// TODO: os.setlocale NYI +test.skip("[test-suite] strings: locale", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + if not _port then + + local locales = { "ptb", "pt_BR.iso88591", "ISO-8859-1" } + local function trylocale (w) + for i = 1, #locales do + if os.setlocale(locales[i], w) then + print(string.format("'%s' locale set to '%s'", w, locales[i])) + return locales[i] + end + end + print(string.format("'%s' locale not found", w)) + return false + end + + if trylocale("collate") then + assert("alo" < "álo" and "álo" < "amo") + end + + if trylocale("ctype") then + assert(string.gsub("áéíóú", "%a", "x") == "xxxxx") + assert(string.gsub("áÁéÉ", "%l", "x") == "xÁxÉ") + assert(string.gsub("áÁéÉ", "%u", "x") == "áxéx") + assert(string.upper"áÁé{xuxu}ção" == "ÁÁÉ{XUXU}ÇÃO") + end + + os.setlocale("C") + assert(os.setlocale() == 'C') + assert(os.setlocale(nil, "numeric") == 'C') + + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test("[test-suite] strings: bug in Lua 5.3.2: 'gmatch' iterator does not work across coroutines", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + do + local f = string.gmatch("1 2 3 4 5", "%d+") + assert(f() == "1") + co = coroutine.wrap(f) + assert(co() == "2") + end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(checkerror + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); -- cgit v1.2.3-70-g09d2 From e9986b9189e2358f9c1a005412a792b7795643d2 Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sat, 14 Apr 2018 15:03:54 +1000 Subject: test/test-suite: Remove print calls --- test/lbaselib.test.js | 1 - test/test-suite/api.test.js | 2 +- test/test-suite/attrib.test.js | 2 +- test/test-suite/code.test.js | 2 +- test/test-suite/constructs.test.js | 2 +- test/test-suite/db.test.js | 3 +- test/test-suite/math.test.js | 124 +++++++++++++++++++------------------ test/test-suite/sort.test.js | 6 +- test/test-suite/strings.test.js | 2 +- test/test-suite/tpack.test.js | 16 ++--- test/test-suite/vararg.test.js | 2 +- 11 files changed, 81 insertions(+), 81 deletions(-) (limited to 'test/test-suite/strings.test.js') diff --git a/test/lbaselib.test.js b/test/lbaselib.test.js index 259de36..dc30a98 100644 --- a/test/lbaselib.test.js +++ b/test/lbaselib.test.js @@ -28,7 +28,6 @@ test('setmetatable, getmetatable', () => { let luaCode = ` local mt = { __index = function () - print("hello") return "hello" end } diff --git a/test/test-suite/api.test.js b/test/test-suite/api.test.js index 152ddcf..5a5e10e 100644 --- a/test/test-suite/api.test.js +++ b/test/test-suite/api.test.js @@ -686,7 +686,7 @@ test("[test-suite] api: test errors in non protected threads", () => { if not _soft then checkerrnopro("pushnum 3; call 0 0", "attempt to call") - print"testing stack overflow in unprotected thread" + -- print"testing stack overflow in unprotected thread" function f () f() end checkerrnopro("getglobal 'f'; call 0 0;", "stack overflow") end diff --git a/test/test-suite/attrib.test.js b/test/test-suite/attrib.test.js index 596c82c..bb07e62 100644 --- a/test/test-suite/attrib.test.js +++ b/test/test-suite/attrib.test.js @@ -23,7 +23,7 @@ test("[test-suite] attrib: testing require", () => { assert(type(package.preload) == "table") assert(type(package.config) == "string") - print("package config: "..string.gsub(package.config, "\\n", "|")) + -- print("package config: "..string.gsub(package.config, "\\n", "|")) do -- create a path with 'max' templates, diff --git a/test/test-suite/code.test.js b/test/test-suite/code.test.js index d37030c..377dd63 100644 --- a/test/test-suite/code.test.js +++ b/test/test-suite/code.test.js @@ -45,7 +45,7 @@ const prefix = ` local arg = {...} local c = T.listcode(f) for i=1, #arg do - print(arg[i], c[i]) + -- print(arg[i], c[i]) assert(string.find(c[i], '- '..arg[i]..' *%d')) end assert(c[#arg+2] == nil) diff --git a/test/test-suite/constructs.test.js b/test/test-suite/constructs.test.js index de0648d..e79778a 100644 --- a/test/test-suite/constructs.test.js +++ b/test/test-suite/constructs.test.js @@ -332,7 +332,7 @@ test.skip('[test-suite] constructs: huge loops, upvalue', () => { IX = false assert(p() == v[2] and IX == not not v[2]) i = i + 1 - if i % 60000 == 0 then print('+') end + -- if i % 60000 == 0 then print('+') end end end `; diff --git a/test/test-suite/db.test.js b/test/test-suite/db.test.js index 38330db..89ec66d 100644 --- a/test/test-suite/db.test.js +++ b/test/test-suite/db.test.js @@ -598,8 +598,7 @@ test("[test-suite] db: tests for tail calls", () => { local tail = debug.getinfo(2) assert(tail.func == g1 and tail.istailcall == true) assert(debug.getinfo(3, "S").what == "main") - print"+" - end + end end function g(x) return f(x) end diff --git a/test/test-suite/math.test.js b/test/test-suite/math.test.js index c649225..9cf4e21 100644 --- a/test/test-suite/math.test.js +++ b/test/test-suite/math.test.js @@ -80,8 +80,8 @@ test("[test-suite] math: number of bits in the mantissa of a floating-point numb local x = 2.0^floatbits assert(x > x - 1.0 and x == x + 1.0) - print(string.format("%d-bit integers, %d-bit (mantissa) floats", - intbits, floatbits)) + -- print(string.format("%d-bit integers, %d-bit (mantissa) floats", + -- intbits, floatbits)) end assert(math.type(0) == "integer" and math.type(0.0) == "float" @@ -304,66 +304,69 @@ test("[test-suite] math: order between floats and integers", () => { assert(minint <= -2^(intbits - 1)) assert(-2^(intbits - 1) <= minint) end + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); - if floatbits < intbits then - print("testing order (floats cannot represent all integers)") - local fmax = 2^floatbits - local ifmax = fmax | 0 - assert(fmax < ifmax + 1) - assert(fmax - 1 < ifmax) - assert(-(fmax - 1) > -ifmax) - assert(not (fmax <= ifmax - 1)) - assert(-fmax > -(ifmax + 1)) - assert(not (-fmax >= -(ifmax - 1))) - - assert(fmax/2 - 0.5 < ifmax//2) - assert(-(fmax/2 - 0.5) > -ifmax//2) - - assert(maxint < 2^intbits) - assert(minint > -2^intbits) - assert(maxint <= 2^intbits) - assert(minint >= -2^intbits) - else - print("testing order (floats can represent all integers)") - assert(maxint < maxint + 1.0) - assert(maxint < maxint + 0.5) - assert(maxint - 1.0 < maxint) - assert(maxint - 0.5 < maxint) - assert(not (maxint + 0.0 < maxint)) - assert(maxint + 0.0 <= maxint) - assert(not (maxint < maxint + 0.0)) - assert(maxint + 0.0 <= maxint) - assert(maxint <= maxint + 0.0) - assert(not (maxint + 1.0 <= maxint)) - assert(not (maxint + 0.5 <= maxint)) - assert(not (maxint <= maxint - 1.0)) - assert(not (maxint <= maxint - 0.5)) - - assert(minint < minint + 1.0) - assert(minint < minint + 0.5) - assert(minint <= minint + 0.5) - assert(minint - 1.0 < minint) - assert(minint - 1.0 <= minint) - assert(not (minint + 0.0 < minint)) - assert(not (minint + 0.5 < minint)) - assert(not (minint < minint + 0.0)) - assert(minint + 0.0 <= minint) - assert(minint <= minint + 0.0) - assert(not (minint + 1.0 <= minint)) - assert(not (minint + 0.5 <= minint)) - assert(not (minint <= minint - 1.0)) - end - do - local NaN = 0/0 - assert(not (NaN < 0)) - assert(not (NaN > minint)) - assert(not (NaN <= -9)) - assert(not (NaN <= maxint)) - assert(not (NaN < maxint)) - assert(not (minint <= NaN)) - assert(not (minint < NaN)) - end +test("[test-suite] math: testing order (floats can represent all integers)", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + assert(floatbits >= intbits) + + assert(maxint < maxint + 1.0) + assert(maxint < maxint + 0.5) + assert(maxint - 1.0 < maxint) + assert(maxint - 0.5 < maxint) + assert(not (maxint + 0.0 < maxint)) + assert(maxint + 0.0 <= maxint) + assert(not (maxint < maxint + 0.0)) + assert(maxint + 0.0 <= maxint) + assert(maxint <= maxint + 0.0) + assert(not (maxint + 1.0 <= maxint)) + assert(not (maxint + 0.5 <= maxint)) + assert(not (maxint <= maxint - 1.0)) + assert(not (maxint <= maxint - 0.5)) + + assert(minint < minint + 1.0) + assert(minint < minint + 0.5) + assert(minint <= minint + 0.5) + assert(minint - 1.0 < minint) + assert(minint - 1.0 <= minint) + assert(not (minint + 0.0 < minint)) + assert(not (minint + 0.5 < minint)) + assert(not (minint < minint + 0.0)) + assert(minint + 0.0 <= minint) + assert(minint <= minint + 0.0) + assert(not (minint + 1.0 <= minint)) + assert(not (minint + 0.5 <= minint)) + assert(not (minint <= minint - 1.0)) + `; + lualib.luaL_openlibs(L); + if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) + throw new SyntaxError(lua.lua_tojsstring(L, -1)); + lua.lua_call(L, 0, 0); +}); + + +test("[test-suite] math: NaN order", () => { + let L = lauxlib.luaL_newstate(); + if (!L) throw Error("failed to create lua state"); + + let luaCode = ` + local NaN = 0/0 + assert(not (NaN < 0)) + assert(not (NaN > minint)) + assert(not (NaN <= -9)) + assert(not (NaN <= maxint)) + assert(not (NaN < maxint)) + assert(not (minint <= NaN)) + assert(not (minint < NaN)) `; lualib.luaL_openlibs(L); if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) @@ -988,7 +991,6 @@ test("[test-suite] math: testing -0 and NaN", () => { let luaCode = ` do - print("testing -0 and NaN") local mz, z = -0.0, 0.0 assert(mz == z) assert(1/mz < 0 and 0 < 1/z) diff --git a/test/test-suite/sort.test.js b/test/test-suite/sort.test.js index 154db03..cc33c0c 100644 --- a/test/test-suite/sort.test.js +++ b/test/test-suite/sort.test.js @@ -22,7 +22,7 @@ const prefix = ` table.sort(a, func) x = (os.clock() - x) * 1000 pre = pre or "" - print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x)) + -- print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x)) check(a, func) end @@ -397,8 +397,8 @@ test("[test-suite] sort: Invert-sorting", () => { x = os.clock(); i=0 table.sort(a, function(x,y) i=i+1; return y { end if not pcall(string.format, "%.3a", 0) then - (Message or print)("\\n >>> modifiers for format '%a' not available <<<\\n") + -- (Message or print)("\\n >>> modifiers for format '%a' not available <<<\\n") else assert(string.find(string.format("%+.2A", 12), "^%+0X%x%.%x0P%+?%d$")) assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) diff --git a/test/test-suite/tpack.test.js b/test/test-suite/tpack.test.js index 132956e..3b97d3a 100644 --- a/test/test-suite/tpack.test.js +++ b/test/test-suite/tpack.test.js @@ -38,14 +38,14 @@ test("[test-suite] tpack: maximum size for integers", () => { assert(1 <= sizeshort and sizeshort <= sizeint and sizeint <= sizelong and sizefloat <= sizedouble) - print("platform:") - print(string.format( - "\\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\\n\\z - \\tlua Integer %d, lua Number %d", - sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, - sizeLI, sizenumber)) - print("\\t" .. (little and "little" or "big") .. " endian") - print("\\talignment: " .. align) + -- print("platform:") + -- print(string.format( + -- "\\tshort %d, int %d, long %d, size_t %d, float %d, double %d,\\n\\z + -- \\tlua Integer %d, lua Number %d", + -- sizeshort, sizeint, sizelong, sizesize_t, sizefloat, sizedouble, + -- sizeLI, sizenumber)) + -- print("\\t" .. (little and "little" or "big") .. " endian") + -- print("\\talignment: " .. align) `; lualib.luaL_openlibs(L); if (lauxlib.luaL_loadstring(L, to_luastring(prefix + luaCode)) === lua.LUA_ERRSYNTAX) diff --git a/test/test-suite/vararg.test.js b/test/test-suite/vararg.test.js index 78439f1..0538541 100644 --- a/test/test-suite/vararg.test.js +++ b/test/test-suite/vararg.test.js @@ -45,7 +45,7 @@ test("[test-suite] vararg: testing vararg", () => { assert(a[1] == b and a[2] == c and a.n == 2) a = vararg(call(call, {c12, {1,2}})) assert(a.n == 2 and a[1] == 55 and a[2] == 2) - a = call(print, {'+'}) + a = call(function()end, {'+'}) assert(a == nil) local t = {1, 10} -- cgit v1.2.3-70-g09d2