lua
[Ported from github.com/lua/lua] Cast in 'rl_readline_name'
Brought by drakestapleton
| 1 | -- $Id: testes/sort.lua $ |
| 2 | -- See Copyright Notice in file lua.h |
| 3 | |
| 4 | print "testing (parts of) table library" |
| 5 | |
| 6 | local maxI = math.maxinteger |
| 7 | local minI = math.mininteger |
| 8 | |
| 9 | |
| 10 | local function checkerror (msg, f, ...) |
| 11 | local s, err = pcall(f, ...) |
| 12 | assert(not s and string.find(err, msg)) |
| 13 | end |
| 14 | |
| 15 | |
| 16 | do print "testing 'table.create'" |
| 17 | local N = 10000 |
| 18 | collectgarbage() |
| 19 | local m = collectgarbage("count") * 1024 |
| 20 | local t = table.create(N) |
| 21 | local memdiff = collectgarbage("count") * 1024 - m |
| 22 | assert(memdiff > N * 4) |
| 23 | for i = 1, 20 do |
| 24 | assert(#t == i - 1) |
| 25 | t[i] = 0 |
| 26 | end |
| 27 | for i = 1, 20 do t[#t + 1] = i * 10 end |
| 28 | assert(#t == 40 and t[39] == 190) |
| 29 | assert(not T or T.querytab(t) == N) |
| 30 | t = nil |
| 31 | collectgarbage() |
| 32 | m = collectgarbage("count") * 1024 |
| 33 | t = table.create(0, 1024) |
| 34 | memdiff = collectgarbage("count") * 1024 - m |
| 35 | assert(memdiff > 1024 * 12) |
| 36 | assert(not T or select(2, T.querytab(t)) == 1024) |
| 37 | |
| 38 | local maxint1 = 1 << (string.packsize("i") * 8 - 1) |
| 39 | checkerror("out of range", table.create, maxint1) |
| 40 | checkerror("out of range", table.create, 0, maxint1) |
| 41 | checkerror("table overflow", table.create, 0, maxint1 - 1) |
| 42 | end |
| 43 | |
| 44 | |
| 45 | print "testing unpack" |
| 46 | |
| 47 | local unpack = table.unpack |
| 48 | |
| 49 | |
| 50 | checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4) |
| 51 | |
| 52 | local x,y,z,a,n |
| 53 | a = {}; local lim = _soft and 200 or 2000 |
| 54 | for i=1, lim do a[i]=i end |
| 55 | assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim) |
| 56 | x = unpack(a) |
| 57 | assert(x == 1) |
| 58 | x = {unpack(a)} |
| 59 | assert(#x == lim and x[1] == 1 and x[lim] == lim) |
| 60 | x = {unpack(a, lim-2)} |
| 61 | assert(#x == 3 and x[1] == lim-2 and x[3] == lim) |
| 62 | x = {unpack(a, 10, 6)} |
| 63 | assert(next(x) == nil) -- no elements |
| 64 | x = {unpack(a, 11, 10)} |
| 65 | assert(next(x) == nil) -- no elements |
| 66 | x,y = unpack(a, 10, 10) |
| 67 | assert(x == 10 and y == nil) |
| 68 | x,y,z = unpack(a, 10, 11) |
| 69 | assert(x == 10 and y == 11 and z == nil) |
| 70 | a,x = unpack{1} |
| 71 | assert(a==1 and x==nil) |
| 72 | a,x = unpack({1,2}, 1, 1) |
| 73 | assert(a==1 and x==nil) |
| 74 | |
| 75 | |
| 76 | do -- unpack with non-tables |
| 77 | local debug = require"debug" |
| 78 | local oldmt = debug.getmetatable(0) |
| 79 | local str = "hello" |
| 80 | debug.setmetatable(0, |
| 81 | { __len = function () return #str end, |
| 82 | __index = function (_, i) return string.sub(str, i, i) end}) |
| 83 | assert(table.concat({table.unpack(0)}) == str) |
| 84 | debug.setmetatable(0, oldmt) -- restore original metatable for numbers |
| 85 | end |
| 86 | |
| 87 | |
| 88 | do |
| 89 | local maxi = (1 << 31) - 1 -- maximum value for an int (usually) |
| 90 | local mini = -(1 << 31) -- minimum value for an int (usually) |
| 91 | checkerror("too many results", unpack, {}, 0, maxi) |
| 92 | checkerror("too many results", unpack, {}, 1, maxi) |
| 93 | checkerror("too many results", unpack, {}, 0, maxI) |
| 94 | checkerror("too many results", unpack, {}, 1, maxI) |
| 95 | checkerror("too many results", unpack, {}, mini, maxi) |
| 96 | checkerror("too many results", unpack, {}, -maxi, maxi) |
| 97 | checkerror("too many results", unpack, {}, minI, maxI) |
| 98 | unpack({}, maxi, 0) |
| 99 | unpack({}, maxi, 1) |
| 100 | unpack({}, maxI, minI) |
| 101 | pcall(unpack, {}, 1, maxi + 1) |
| 102 | local a, b = unpack({[maxi] = 20}, maxi, maxi) |
| 103 | assert(a == 20 and b == nil) |
| 104 | a, b = unpack({[maxi] = 20}, maxi - 1, maxi) |
| 105 | assert(a == nil and b == 20) |
| 106 | local t = {[maxI - 1] = 12, [maxI] = 23} |
| 107 | a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23) |
| 108 | a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil) |
| 109 | a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil) |
| 110 | t = {[minI] = 12.3, [minI + 1] = 23.5} |
| 111 | a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5) |
| 112 | a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil) |
| 113 | a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil) |
| 114 | end |
| 115 | |
| 116 | do -- length is not an integer |
| 117 | local t = setmetatable({}, {__len = function () return 'abc' end}) |
| 118 | assert(#t == 'abc') |
| 119 | checkerror("object length is not an integer", table.insert, t, 1) |
| 120 | end |
| 121 | |
| 122 | print "testing pack" |
| 123 | |
| 124 | a = table.pack() |
| 125 | assert(a[1] == undef and a.n == 0) |
| 126 | |
| 127 | a = table.pack(table) |
| 128 | assert(a[1] == table and a.n == 1) |
| 129 | |
| 130 | a = table.pack(nil, nil, nil, nil) |
| 131 | assert(a[1] == nil and a.n == 4) |
| 132 | |
| 133 | |
| 134 | -- testing move |
| 135 | do |
| 136 | |
| 137 | checkerror("table expected", table.move, 1, 2, 3, 4) |
| 138 | |
| 139 | local function eqT (a, b) |
| 140 | for k, v in pairs(a) do assert(b[k] == v) end |
| 141 | for k, v in pairs(b) do assert(a[k] == v) end |
| 142 | end |
| 143 | |
| 144 | local a = table.move({10,20,30}, 1, 3, 2) -- move forward |
| 145 | eqT(a, {10,10,20,30}) |
| 146 | |
| 147 | -- move forward with overlap of 1 |
| 148 | a = table.move({10, 20, 30}, 1, 3, 3) |
| 149 | eqT(a, {10, 20, 10, 20, 30}) |
| 150 | |
| 151 | -- moving to the same table (not being explicit about it) |
| 152 | a = {10, 20, 30, 40} |
| 153 | table.move(a, 1, 4, 2, a) |
| 154 | eqT(a, {10, 10, 20, 30, 40}) |
| 155 | |
| 156 | a = table.move({10,20,30}, 2, 3, 1) -- move backward |
| 157 | eqT(a, {20,30,30}) |
| 158 | |
| 159 | a = {} -- move to new table |
| 160 | assert(table.move({10,20,30}, 1, 3, 1, a) == a) |
| 161 | eqT(a, {10,20,30}) |
| 162 | |
| 163 | a = {} |
| 164 | assert(table.move({10,20,30}, 1, 0, 3, a) == a) -- empty move (no move) |
| 165 | eqT(a, {}) |
| 166 | |
| 167 | a = table.move({10,20,30}, 1, 10, 1) -- move to the same place |
| 168 | eqT(a, {10,20,30}) |
| 169 | |
| 170 | -- moving on the fringes |
| 171 | a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3}, |
| 172 | maxI - 2, maxI, -10, {}) |
| 173 | eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) |
| 174 | |
| 175 | a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3}, |
| 176 | minI, minI + 2, -10, {}) |
| 177 | eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3}) |
| 178 | |
| 179 | a = table.move({45}, 1, 1, maxI) |
| 180 | eqT(a, {45, [maxI] = 45}) |
| 181 | |
| 182 | a = table.move({[maxI] = 100}, maxI, maxI, minI) |
| 183 | eqT(a, {[minI] = 100, [maxI] = 100}) |
| 184 | |
| 185 | a = table.move({[minI] = 100}, minI, minI, maxI) |
| 186 | eqT(a, {[minI] = 100, [maxI] = 100}) |
| 187 | |
| 188 | a = setmetatable({}, { |
| 189 | __index = function (_,k) return k * 10 end, |
| 190 | __newindex = error}) |
| 191 | local b = table.move(a, 1, 10, 3, {}) |
| 192 | eqT(a, {}) |
| 193 | eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100}) |
| 194 | |
| 195 | b = setmetatable({""}, { |
| 196 | __index = error, |
| 197 | __newindex = function (t,k,v) |
| 198 | t[1] = string.format("%s(%d,%d)", t[1], k, v) |
| 199 | end}) |
| 200 | table.move(a, 10, 13, 3, b) |
| 201 | assert(b[1] == "(3,100)(4,110)(5,120)(6,130)") |
| 202 | local stat, msg = pcall(table.move, b, 10, 13, 3, b) |
| 203 | assert(not stat and msg == b) |
| 204 | end |
| 205 | |
| 206 | do |
| 207 | -- for very long moves, just check initial accesses and interrupt |
| 208 | -- move with an error |
| 209 | local function checkmove (f, e, t, x, y) |
| 210 | local pos1, pos2 |
| 211 | local a = setmetatable({}, { |
| 212 | __index = function (_,k) pos1 = k end, |
| 213 | __newindex = function (_,k) pos2 = k; error() end, }) |
| 214 | local st, msg = pcall(table.move, a, f, e, t) |
| 215 | assert(not st and pos1 == x and pos2 == y) |
| 216 | end |
| 217 | checkmove(1, maxI, 0, 1, 0) |
| 218 | checkmove(0, maxI - 1, 1, maxI - 1, maxI) |
| 219 | checkmove(minI, -2, -5, -2, maxI - 6) |
| 220 | checkmove(minI + 1, -1, -2, -1, maxI - 3) |
| 221 | checkmove(minI, -2, 0, minI, 0) -- non overlapping |
| 222 | checkmove(minI + 1, -1, 1, minI + 1, 1) -- non overlapping |
| 223 | end |
| 224 | |
| 225 | checkerror("too many", table.move, {}, 0, maxI, 1) |
| 226 | checkerror("too many", table.move, {}, -1, maxI - 1, 1) |
| 227 | checkerror("too many", table.move, {}, minI, -1, 1) |
| 228 | checkerror("too many", table.move, {}, minI, maxI, 1) |
| 229 | checkerror("wrap around", table.move, {}, 1, maxI, 2) |
| 230 | checkerror("wrap around", table.move, {}, 1, 2, maxI) |
| 231 | checkerror("wrap around", table.move, {}, minI, -2, 2) |
| 232 | |
| 233 | |
| 234 | print"testing sort" |
| 235 | |
| 236 | |
| 237 | -- strange lengths |
| 238 | local a = setmetatable({}, {__len = function () return -1 end}) |
| 239 | assert(#a == -1) |
| 240 | table.sort(a, error) -- should not compare anything |
| 241 | a = setmetatable({}, {__len = function () return maxI end}) |
| 242 | checkerror("too big", table.sort, a) |
| 243 | |
| 244 | -- test checks for invalid order functions |
| 245 | local function check (t) |
| 246 | local function f(a, b) assert(a and b); return true end |
| 247 | checkerror("invalid order function", table.sort, t, f) |
| 248 | end |
| 249 | |
| 250 | check{1,2,3,4} |
| 251 | check{1,2,3,4,5} |
| 252 | check{1,2,3,4,5,6} |
| 253 | |
| 254 | |
| 255 | function check (a, f) |
| 256 | f = f or function (x,y) return x<y end; |
| 257 | for n = #a, 2, -1 do |
| 258 | assert(not f(a[n], a[n-1])) |
| 259 | end |
| 260 | end |
| 261 | |
| 262 | a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", |
| 263 | "Oct", "Nov", "Dec"} |
| 264 | |
| 265 | table.sort(a) |
| 266 | check(a) |
| 267 | |
| 268 | local function perm (s, n) |
| 269 | n = n or #s |
| 270 | if n == 1 then |
| 271 | local t = {unpack(s)} |
| 272 | table.sort(t) |
| 273 | check(t) |
| 274 | else |
| 275 | for i = 1, n do |
| 276 | s[i], s[n] = s[n], s[i] |
| 277 | perm(s, n - 1) |
| 278 | s[i], s[n] = s[n], s[i] |
| 279 | end |
| 280 | end |
| 281 | end |
| 282 | |
| 283 | perm{} |
| 284 | perm{1} |
| 285 | perm{1,2} |
| 286 | perm{1,2,3} |
| 287 | perm{1,2,3,4} |
| 288 | perm{2,2,3,4} |
| 289 | perm{1,2,3,4,5} |
| 290 | perm{1,2,3,3,5} |
| 291 | perm{1,2,3,4,5,6} |
| 292 | perm{2,2,3,3,5,6} |
| 293 | |
| 294 | local function timesort (a, n, func, msg, pre) |
| 295 | local x = os.clock() |
| 296 | table.sort(a, func) |
| 297 | x = (os.clock() - x) * 1000 |
| 298 | pre = pre or "" |
| 299 | print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x)) |
| 300 | check(a, func) |
| 301 | end |
| 302 | |
| 303 | local limit = 50000 |
| 304 | if _soft then limit = 5000 end |
| 305 | |
| 306 | a = {} |
| 307 | for i=1,limit do |
| 308 | a[i] = math.random() |
| 309 | end |
| 310 | |
| 311 | timesort(a, limit, nil, "random") |
| 312 | |
| 313 | timesort(a, limit, nil, "sorted", "re-") |
| 314 | |
| 315 | a = {} |
| 316 | for i=1,limit do |
| 317 | a[i] = math.random() |
| 318 | end |
| 319 | |
| 320 | local x = os.clock(); local i = 0 |
| 321 | table.sort(a, function(x,y) i=i+1; return y<x end) |
| 322 | x = (os.clock() - x) * 1000 |
| 323 | print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons", |
| 324 | limit, x, i)) |
| 325 | check(a, function(x,y) return y<x end) |
| 326 | |
| 327 | |
| 328 | table.sort{} -- empty array |
| 329 | |
| 330 | for i=1,limit do a[i] = false end |
| 331 | timesort(a, limit, function(x,y) return nil end, "equal") |
| 332 | |
| 333 | for i,v in pairs(a) do assert(v == false) end |
| 334 | |
| 335 | AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"} |
| 336 | table.sort(AA) |
| 337 | check(AA) |
| 338 | |
| 339 | table.sort(AA, function (x, y) |
| 340 | load(string.format("AA[%q] = ''", x), "")() |
| 341 | collectgarbage() |
| 342 | return x<y |
| 343 | end) |
| 344 | |
| 345 | _G.AA = nil |
| 346 | |
| 347 | local tt = {__lt = function (a,b) return a.val < b.val end} |
| 348 | a = {} |
| 349 | for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end |
| 350 | table.sort(a) |
| 351 | check(a, tt.__lt) |
| 352 | check(a) |
| 353 | |
| 354 | print"OK" |