sort.lua 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. print "testing (parts of) table library"
  2. print "testing unpack"
  3. local unpack = table.unpack
  4. local x,y,z,a,n
  5. a = {}; lim = 2000
  6. for i=1, lim do a[i]=i end
  7. assert(select(lim, unpack(a)) == lim and select('#', unpack(a)) == lim)
  8. x = unpack(a)
  9. assert(x == 1)
  10. x = {unpack(a)}
  11. assert(#x == lim and x[1] == 1 and x[lim] == lim)
  12. x = {unpack(a, lim-2)}
  13. assert(#x == 3 and x[1] == lim-2 and x[3] == lim)
  14. x = {unpack(a, 10, 6)}
  15. assert(next(x) == nil) -- no elements
  16. x = {unpack(a, 11, 10)}
  17. assert(next(x) == nil) -- no elements
  18. x,y = unpack(a, 10, 10)
  19. assert(x == 10 and y == nil)
  20. x,y,z = unpack(a, 10, 11)
  21. assert(x == 10 and y == 11 and z == nil)
  22. a,x = unpack{1}
  23. assert(a==1 and x==nil)
  24. a,x = unpack({1,2}, 1, 1)
  25. assert(a==1 and x==nil)
  26. if not _no32 then
  27. assert(not pcall(unpack, {}, 0, 2^31-1))
  28. assert(not pcall(unpack, {}, 1, 2^31-1))
  29. assert(not pcall(unpack, {}, -(2^31), 2^31-1))
  30. assert(not pcall(unpack, {}, -(2^31 - 1), 2^31-1))
  31. assert(pcall(unpack, {}, 2^31-1, 0))
  32. assert(pcall(unpack, {}, 2^31-1, 1))
  33. pcall(unpack, {}, 1, 2^31)
  34. a, b = unpack({[2^31-1] = 20}, 2^31-1, 2^31-1)
  35. assert(a == 20 and b == nil)
  36. a, b = unpack({[2^31-1] = 20}, 2^31-2, 2^31-1)
  37. assert(a == nil and b == 20)
  38. end
  39. print "testing pack"
  40. a = table.pack()
  41. assert(a[1] == nil and a.n == 0)
  42. a = table.pack(table)
  43. assert(a[1] == table and a.n == 1)
  44. a = table.pack(nil, nil, nil, nil)
  45. assert(a[1] == nil and a.n == 4)
  46. print"testing sort"
  47. -- test checks for invalid order functions
  48. local function check (t)
  49. local function f(a, b) assert(a and b); return true end
  50. local s, e = pcall(table.sort, t, f)
  51. assert(not s and e:find("invalid order function"))
  52. end
  53. check{1,2,3,4}
  54. check{1,2,3,4,5}
  55. check{1,2,3,4,5,6}
  56. function check (a, f)
  57. f = f or function (x,y) return x<y end;
  58. for n = #a, 2, -1 do
  59. assert(not f(a[n], a[n-1]))
  60. end
  61. end
  62. a = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
  63. "Oct", "Nov", "Dec"}
  64. table.sort(a)
  65. check(a)
  66. function perm (s, n)
  67. n = n or #s
  68. if n == 1 then
  69. local t = {unpack(s)}
  70. table.sort(t)
  71. check(t)
  72. else
  73. for i = 1, n do
  74. s[i], s[n] = s[n], s[i]
  75. perm(s, n - 1)
  76. s[i], s[n] = s[n], s[i]
  77. end
  78. end
  79. end
  80. perm{}
  81. perm{1}
  82. perm{1,2}
  83. perm{1,2,3}
  84. perm{1,2,3,4}
  85. perm{2,2,3,4}
  86. perm{1,2,3,4,5}
  87. perm{1,2,3,3,5}
  88. perm{1,2,3,4,5,6}
  89. perm{2,2,3,3,5,6}
  90. limit = 30000
  91. if _soft then limit = 5000 end
  92. a = {}
  93. for i=1,limit do
  94. a[i] = math.random()
  95. end
  96. local x = os.clock()
  97. table.sort(a)
  98. print(string.format("Sorting %d elements in %.2f sec.", limit, os.clock()-x))
  99. check(a)
  100. x = os.clock()
  101. table.sort(a)
  102. print(string.format("Re-sorting %d elements in %.2f sec.", limit, os.clock()-x))
  103. check(a)
  104. a = {}
  105. for i=1,limit do
  106. a[i] = math.random()
  107. end
  108. x = os.clock(); i=0
  109. table.sort(a, function(x,y) i=i+1; return y<x end)
  110. print(string.format("Invert-sorting other %d elements in %.2f sec., with %i comparisons",
  111. limit, os.clock()-x, i))
  112. check(a, function(x,y) return y<x end)
  113. table.sort{} -- empty array
  114. for i=1,limit do a[i] = false end
  115. x = os.clock();
  116. table.sort(a, function(x,y) return nil end)
  117. print(string.format("Sorting %d equal elements in %.2f sec.", limit, os.clock()-x))
  118. check(a, function(x,y) return nil end)
  119. for i,v in pairs(a) do assert(not v or i=='n' and v==limit) end
  120. A = {"álo", "\0first :-)", "alo", "then this one", "45", "and a new"}
  121. table.sort(A)
  122. check(A)
  123. table.sort(A, function (x, y)
  124. load(string.format("A[%q] = ''", x))()
  125. collectgarbage()
  126. return x<y
  127. end)
  128. tt = {__lt = function (a,b) return a.val < b.val end}
  129. a = {}
  130. for i=1,10 do a[i] = {val=math.random(100)}; setmetatable(a[i], tt); end
  131. table.sort(a)
  132. check(a, tt.__lt)
  133. check(a)
  134. print"OK"