sort.lua 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. -- $Id: testes/sort.lua $
  2. -- See Copyright Notice in file all.lua
  3. print "testing (parts of) table library"
  4. print "testing unpack"
  5. local unpack = table.unpack
  6. local maxI = math.maxinteger
  7. local minI = math.mininteger
  8. local function checkerror (msg, f, ...)
  9. local s, err = pcall(f, ...)
  10. assert(not s and string.find(err, msg))
  11. end
  12. checkerror("wrong number of arguments", table.insert, {}, 2, 3, 4)
  13. local x,y,z,a,n
  14. a = {}; local lim = _soft and 200 or 2000
  15. for i=1, lim do a[i]=i end
  16. assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
  17. x = unpack(a)
  18. assert(x == 1)
  19. x = {unpack(a)}
  20. assert(#x == lim and x[1] == 1 and x[lim] == lim)
  21. x = {unpack(a, lim-2)}
  22. assert(#x == 3 and x[1] == lim-2 and x[3] == lim)
  23. x = {unpack(a, 10, 6)}
  24. assert(next(x) == nil) -- no elements
  25. x = {unpack(a, 11, 10)}
  26. assert(next(x) == nil) -- no elements
  27. x,y = unpack(a, 10, 10)
  28. assert(x == 10 and y == nil)
  29. x,y,z = unpack(a, 10, 11)
  30. assert(x == 10 and y == 11 and z == nil)
  31. a,x = unpack{1}
  32. assert(a==1 and x==nil)
  33. a,x = unpack({1,2}, 1, 1)
  34. assert(a==1 and x==nil)
  35. do
  36. local maxi = (1 << 31) - 1 -- maximum value for an int (usually)
  37. local mini = -(1 << 31) -- minimum value for an int (usually)
  38. checkerror("too many results", unpack, {}, 0, maxi)
  39. checkerror("too many results", unpack, {}, 1, maxi)
  40. checkerror("too many results", unpack, {}, 0, maxI)
  41. checkerror("too many results", unpack, {}, 1, maxI)
  42. checkerror("too many results", unpack, {}, mini, maxi)
  43. checkerror("too many results", unpack, {}, -maxi, maxi)
  44. checkerror("too many results", unpack, {}, minI, maxI)
  45. unpack({}, maxi, 0)
  46. unpack({}, maxi, 1)
  47. unpack({}, maxI, minI)
  48. pcall(unpack, {}, 1, maxi + 1)
  49. local a, b = unpack({[maxi] = 20}, maxi, maxi)
  50. assert(a == 20 and b == nil)
  51. a, b = unpack({[maxi] = 20}, maxi - 1, maxi)
  52. assert(a == nil and b == 20)
  53. local t = {[maxI - 1] = 12, [maxI] = 23}
  54. a, b = unpack(t, maxI - 1, maxI); assert(a == 12 and b == 23)
  55. a, b = unpack(t, maxI, maxI); assert(a == 23 and b == nil)
  56. a, b = unpack(t, maxI, maxI - 1); assert(a == nil and b == nil)
  57. t = {[minI] = 12.3, [minI + 1] = 23.5}
  58. a, b = unpack(t, minI, minI + 1); assert(a == 12.3 and b == 23.5)
  59. a, b = unpack(t, minI, minI); assert(a == 12.3 and b == nil)
  60. a, b = unpack(t, minI + 1, minI); assert(a == nil and b == nil)
  61. end
  62. do -- length is not an integer
  63. local t = setmetatable({}, {__len = function () return 'abc' end})
  64. assert(#t == 'abc')
  65. checkerror("object length is not an integer", table.insert, t, 1)
  66. end
  67. print "testing pack"
  68. a = table.pack()
  69. assert(a[1] == undef and a.n == 0)
  70. a = table.pack(table)
  71. assert(a[1] == table and a.n == 1)
  72. a = table.pack(nil, nil, nil, nil)
  73. assert(a[1] == nil and a.n == 4)
  74. -- testing move
  75. do
  76. checkerror("table expected", table.move, 1, 2, 3, 4)
  77. local function eqT (a, b)
  78. for k, v in pairs(a) do assert(b[k] == v) end
  79. for k, v in pairs(b) do assert(a[k] == v) end
  80. end
  81. local a = table.move({10,20,30}, 1, 3, 2) -- move forward
  82. eqT(a, {10,10,20,30})
  83. -- move forward with overlap of 1
  84. a = table.move({10, 20, 30}, 1, 3, 3)
  85. eqT(a, {10, 20, 10, 20, 30})
  86. -- moving to the same table (not being explicit about it)
  87. a = {10, 20, 30, 40}
  88. table.move(a, 1, 4, 2, a)
  89. eqT(a, {10, 10, 20, 30, 40})
  90. a = table.move({10,20,30}, 2, 3, 1) -- move backward
  91. eqT(a, {20,30,30})
  92. a = {} -- move to new table
  93. assert(table.move({10,20,30}, 1, 3, 1, a) == a)
  94. eqT(a, {10,20,30})
  95. a = {}
  96. assert(table.move({10,20,30}, 1, 0, 3, a) == a) -- empty move (no move)
  97. eqT(a, {})
  98. a = table.move({10,20,30}, 1, 10, 1) -- move to the same place
  99. eqT(a, {10,20,30})
  100. -- moving on the fringes
  101. a = table.move({[maxI - 2] = 1, [maxI - 1] = 2, [maxI] = 3},
  102. maxI - 2, maxI, -10, {})
  103. eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3})
  104. a = table.move({[minI] = 1, [minI + 1] = 2, [minI + 2] = 3},
  105. minI, minI + 2, -10, {})
  106. eqT(a, {[-10] = 1, [-9] = 2, [-8] = 3})
  107. a = table.move({45}, 1, 1, maxI)
  108. eqT(a, {45, [maxI] = 45})
  109. a = table.move({[maxI] = 100}, maxI, maxI, minI)
  110. eqT(a, {[minI] = 100, [maxI] = 100})
  111. a = table.move({[minI] = 100}, minI, minI, maxI)
  112. eqT(a, {[minI] = 100, [maxI] = 100})
  113. a = setmetatable({}, {
  114. __index = function (_,k) return k * 10 end,
  115. __newindex = error})
  116. local b = table.move(a, 1, 10, 3, {})
  117. eqT(a, {})
  118. eqT(b, {nil,nil,10,20,30,40,50,60,70,80,90,100})
  119. b = setmetatable({""}, {
  120. __index = error,
  121. __newindex = function (t,k,v)
  122. t[1] = string.format("%s(%d,%d)", t[1], k, v)
  123. end})
  124. table.move(a, 10, 13, 3, b)
  125. assert(b[1] == "(3,100)(4,110)(5,120)(6,130)")
  126. local stat, msg = pcall(table.move, b, 10, 13, 3, b)
  127. assert(not stat and msg == b)
  128. end
  129. do
  130. -- for very long moves, just check initial accesses and interrupt
  131. -- move with an error
  132. local function checkmove (f, e, t, x, y)
  133. local pos1, pos2
  134. local a = setmetatable({}, {
  135. __index = function (_,k) pos1 = k end,
  136. __newindex = function (_,k) pos2 = k; error() end, })
  137. local st, msg = pcall(table.move, a, f, e, t)
  138. assert(not st and not msg and pos1 == x and pos2 == y)
  139. end
  140. checkmove(1, maxI, 0, 1, 0)
  141. checkmove(0, maxI - 1, 1, maxI - 1, maxI)
  142. checkmove(minI, -2, -5, -2, maxI - 6)
  143. checkmove(minI + 1, -1, -2, -1, maxI - 3)
  144. checkmove(minI, -2, 0, minI, 0) -- non overlapping
  145. checkmove(minI + 1, -1, 1, minI + 1, 1) -- non overlapping
  146. end
  147. checkerror("too many", table.move, {}, 0, maxI, 1)
  148. checkerror("too many", table.move, {}, -1, maxI - 1, 1)
  149. checkerror("too many", table.move, {}, minI, -1, 1)
  150. checkerror("too many", table.move, {}, minI, maxI, 1)
  151. checkerror("wrap around", table.move, {}, 1, maxI, 2)
  152. checkerror("wrap around", table.move, {}, 1, 2, maxI)
  153. checkerror("wrap around", table.move, {}, minI, -2, 2)
  154. print"testing sort"
  155. -- strange lengths
  156. local a = setmetatable({}, {__len = function () return -1 end})
  157. assert(#a == -1)
  158. table.sort(a, error) -- should not compare anything
  159. a = setmetatable({}, {__len = function () return maxI end})
  160. checkerror("too big", table.sort, a)
  161. -- test checks for invalid order functions
  162. local function check (t)
  163. local function f(a, b) assert(a and b); return true end
  164. checkerror("invalid order function", table.sort, t, f)
  165. end
  166. check{1,2,3,4}
  167. check{1,2,3,4,5}
  168. check{1,2,3,4,5,6}
  169. function check (a, f)
  170. f = f or function (x,y) return x<y end;
  171. for n = #a, 2, -1 do
  172. assert(not f(a[n], a[n-1]))
  173. end
  174. end
  175. a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  176. "Oct", "Nov", "Dec"}
  177. table.sort(a)
  178. check(a)
  179. local function perm (s, n)
  180. n = n or #s
  181. if n == 1 then
  182. local t = {unpack(s)}
  183. table.sort(t)
  184. check(t)
  185. else
  186. for i = 1, n do
  187. s[i], s[n] = s[n], s[i]
  188. perm(s, n - 1)
  189. s[i], s[n] = s[n], s[i]
  190. end
  191. end
  192. end
  193. perm{}
  194. perm{1}
  195. perm{1,2}
  196. perm{1,2,3}
  197. perm{1,2,3,4}
  198. perm{2,2,3,4}
  199. perm{1,2,3,4,5}
  200. perm{1,2,3,3,5}
  201. perm{1,2,3,4,5,6}
  202. perm{2,2,3,3,5,6}
  203. local function timesort (a, n, func, msg, pre)
  204. local x = os.clock()
  205. table.sort(a, func)
  206. x = (os.clock() - x) * 1000
  207. pre = pre or ""
  208. print(string.format("%ssorting %d %s elements in %.2f msec.", pre, n, msg, x))
  209. check(a, func)
  210. end
  211. local limit = 50000
  212. if _soft then limit = 5000 end
  213. a = {}
  214. for i=1,limit do
  215. a[i] = math.random()
  216. end
  217. timesort(a, limit, nil, "random")
  218. timesort(a, limit, nil, "sorted", "re-")
  219. a = {}
  220. for i=1,limit do
  221. a[i] = math.random()
  222. end
  223. local x = os.clock(); local i = 0
  224. table.sort(a, function(x,y) i=i+1; return y<x end)
  225. x = (os.clock() - x) * 1000
  226. print(string.format("Invert-sorting other %d elements in %.2f msec., with %i comparisons",
  227. limit, x, i))
  228. check(a, function(x,y) return y<x end)
  229. table.sort{} -- empty array
  230. for i=1,limit do a[i] = false end
  231. timesort(a, limit, function(x,y) return nil end, "equal")
  232. for i,v in pairs(a) do assert(v == false) end
  233. AA = {"\xE1lo", "\0first :-)", "alo", "then this one", "45", "and a new"}
  234. table.sort(AA)
  235. check(AA)
  236. table.sort(AA, function (x, y)
  237. load(string.format("AA[%q] = ''", x), "")()
  238. collectgarbage()
  239. return x<y
  240. end)
  241. _G.AA = nil
  242. local tt = {__lt = function (a,b) return a.val < b.val end}
  243. a = {}
  244. for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
  245. table.sort(a)
  246. check(a, tt.__lt)
  247. check(a)
  248. print"OK"