MT#7679 fix table.shift()

mr3.4.1
Victor Seva 11 years ago
parent c4ab43e6b1
commit 20222e73cc

@ -142,10 +142,13 @@ range = function (i, to, inc)
return function () if i == to then return nil end i = i + inc return i, i end return function () if i == to then return nil end i = i + inc return i, i end
end end
function table.shift(t, positions) function table.shift(t, position)
local k, v local k, v
local res = {} local res = {}
for k in range(1, positions % #t) do local p = position % #t
if p == 0 then return end
for k in range(1, p) do
v = table.remove(t, k-#res) v = table.remove(t, k-#res)
table.insert(res, v) table.insert(res, v)
end end

@ -83,9 +83,22 @@ TestUtils = {} --class
end end
function TestUtils:test_table_shift2() function TestUtils:test_table_shift2()
assertEquals(self.simple_list, {1,2,3}) local tmp = table.deepcopy(self.simple_list)
table.shift(self.simple_list, 4) assertEquals(tmp, {1,2,3})
assertEquals(self.simple_list, {2,3,1}) table.shift(tmp, 0)
assertEquals(tmp, {1,2,3})
tmp = table.deepcopy(self.simple_list)
table.shift(tmp, 1)
assertEquals(tmp, {2,3,1})
tmp = table.deepcopy(self.simple_list)
table.shift(tmp, 2)
assertEquals(tmp, {3,1,2})
tmp = table.deepcopy(self.simple_list)
table.shift(tmp, 3)
assertEquals(tmp, {1,2,3})
tmp = table.deepcopy(self.simple_list)
table.shift(tmp, 4)
assertEquals(tmp, {2,3,1})
end end
function TestUtils:test_table_tostring() function TestUtils:test_table_tostring()

Loading…
Cancel
Save