-- 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 sort" function checksort(t, f, ...) assert(#t == select('#', ...)) local copy = table.clone(t) table.sort(copy, f) for i=1,#t do assert(copy[i] == select(i, ...)) end end -- basic edge cases checksort({}, nil) checksort({1}, nil, 1) -- small inputs checksort({1, 2}, nil, 1, 2) checksort({2, 1}, nil, 1, 2) checksort({1, 2, 3}, nil, 1, 2, 3) checksort({2, 1, 3}, nil, 1, 2, 3) checksort({1, 3, 2}, nil, 1, 2, 3) checksort({3, 2, 1}, nil, 1, 2, 3) checksort({3, 1, 2}, nil, 1, 2, 3) -- "large" input checksort({3, 8, 1, 7, 10, 2, 5, 4, 9, 6}, nil, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10) checksort({"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}, nil, "Apr", "Aug", "Dec", "Feb", "Jan", "Jul", "Jun", "Mar", "May", "Nov", "Oct", "Sep") -- duplicates checksort({3, 1, 1, 7, 1, 3, 5, 1, 9, 3}, nil, 1, 1, 1, 1, 3, 3, 3, 5, 7, 9) -- predicates checksort({3, 8, 1, 7, 10, 2, 5, 4, 9, 6}, function (a, b) return a > b end, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1) -- can't sort readonly tables assert(pcall(table.sort, table.freeze({2, 1})) == false) -- first argument must be a table, second argument must be nil or function assert(pcall(table.sort) == false) assert(pcall(table.sort, "abc") == false) assert(pcall(table.sort, {}, 42) == false) assert(pcall(table.sort, {}, {}) == false) -- legacy Lua tests function check (a, f) f = f or function (x,y) return x