diff --git a/ngcp/utils.lua b/ngcp/utils.lua index 281d4ad..cad5991 100644 --- a/ngcp/utils.lua +++ b/ngcp/utils.lua @@ -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 end -function table.shift(t, positions) +function table.shift(t, position) local k, v 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) table.insert(res, v) end diff --git a/tests/utils.lua b/tests/utils.lua index b8f28bd..0815d8a 100644 --- a/tests/utils.lua +++ b/tests/utils.lua @@ -83,9 +83,22 @@ TestUtils = {} --class end function TestUtils:test_table_shift2() - assertEquals(self.simple_list, {1,2,3}) - table.shift(self.simple_list, 4) - assertEquals(self.simple_list, {2,3,1}) + local tmp = table.deepcopy(self.simple_list) + assertEquals(tmp, {1,2,3}) + 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 function TestUtils:test_table_tostring()