From 4b4f1a19647ef71c3cb1eaa2cac4da7cfb12ef17 Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Wed, 26 Apr 2017 11:18:45 +0200 Subject: [test-suite] strings.lua (complete) --- tests/test-suite/strings.js | 237 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 237 insertions(+) diff --git a/tests/test-suite/strings.js b/tests/test-suite/strings.js index 8686171..5bc2690 100644 --- a/tests/test-suite/strings.js +++ b/tests/test-suite/strings.js @@ -559,3 +559,240 @@ test('testing large numbers for format', function (t) { }, "Lua program ran without error"); }); + + +test("testing 'format %a %A'", function (t) { + 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 + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(checkerror + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("testing errors in format", function (t) { + 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) + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(checkerror + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +test("testing table.concat", function (t) { + 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) == "") + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(checkerror + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); + + +// TODO: os.setlocale NYI +if (false) { + test("testing locale", function (t) { + 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 + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(checkerror + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + + }); +} + + +test("testing bug in Lua 5.3.2: 'gmatch' iterator does not work across coroutines", function (t) { + let luaCode = ` + do + local f = string.gmatch("1 2 3 4 5", "%d+") + assert(f() == "1") + co = coroutine.wrap(f) + assert(co() == "2") + end + `, L; + + t.plan(2); + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + linit.luaL_openlibs(L); + + lauxlib.luaL_loadstring(L, lua.to_luastring(checkerror + luaCode)); + + }, "Lua program loaded without error"); + + t.doesNotThrow(function () { + + lapi.lua_call(L, 0, -1); + + }, "Lua program ran without error"); + +}); -- cgit v1.2.3-54-g00ecf