Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/lua/blob/master/testes/sort.lua # lua [Ported from github.com/lua/lua] Cast in 'rl_readline_name' Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `lua / testes / sort.lua` 354 lines ยท 9625 bytes Raw Blame History 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