From 72e49b26d32423d66b68fb45e9aace9c503a1a5c Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Tue, 11 Apr 2017 08:05:25 +0200 Subject: Array.prototype.sort sort by Unicode code points by default WTF JS ! --- src/ltable.js | 6 ++-- tests/single.lua | 87 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 2 deletions(-) diff --git a/src/ltable.js b/src/ltable.js index 025efe0..039dd6d 100644 --- a/src/ltable.js +++ b/src/ltable.js @@ -14,7 +14,9 @@ const TValue = lobject.TValue; Table.prototype.ordered_intindexes = function() { return [...this.value.keys()] .filter(e => typeof e === 'number' && e % 1 === 0) // Only integer indexes - .sort(); + .sort(function (a, b) { + return a > b ? 1 : -1; + }); }; Table.prototype.ordered_indexes = function() { @@ -73,4 +75,4 @@ Table.prototype.luaH_next = function(L, keyI) { } return 0; -}; \ No newline at end of file +}; diff --git a/tests/single.lua b/tests/single.lua index c167196..fd06c3a 100644 --- a/tests/single.lua +++ b/tests/single.lua @@ -285,3 +285,90 @@ do print("testing 'format %a %A'") assert(string.find(string.format("%.4A", -12), "^%-0X%x%.%x000P%+?%d$")) end end + +-- errors in format + +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) + +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) == "") + +-- TODO: when os.setlocale is implemented +if false and 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 + + +-- bug in Lua 5.3.2 +-- 'gmatch' iterator does not work across coroutines +do + local f = string.gmatch("1 2 3 4 5", "%d+") + assert(f() == "1") + co = coroutine.wrap(f) + assert(co() == "2") +end + +print('OK') -- cgit v1.2.3-70-g09d2