1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
local utils = require("pl.utils")
describe("pl.utils", function ()
describe("npairs", function ()
local npairs = utils.npairs
it("start index defaults to 1", function()
local t1 = { 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, nil, 2) do t2[i] = v end
assert.are.same({ 1, 2 }, t2)
end)
it("end index defaults to `t.n`", function()
local t1 = { n = 2, 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1) do t2[i] = v end
assert.are.same({1, 2}, t2)
end)
it("step size defaults to 1", function()
local t1 = { 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1) do t2[i] = v end
assert.are.same({1, 2, 3}, t2)
end)
it("step size cannot be 0", function()
local t1 = { 1, 2, 3 }
assert.has.error(function()
npairs(t1, nil, nil, 0)
end, "iterator step-size cannot be 0")
end)
it("end index defaults to `#t` if there is no `t.n`", function()
local t1 = { 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1) do t2[i] = v end
assert.are.same({1, 2, 3}, t2)
end)
it("returns nothing if start index is beyond end index", function()
local t1 = { 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, 5, 3) do t2[i] = v end
assert.are.same({}, t2)
end)
it("returns nothing if start index is beyond end index, with negative step size", function()
local t1 = { 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, 3, 1, -1) do t2[#t2+1] = v end
assert.are.same({ 3, 2, 1}, t2)
end)
it("returns 1 key/value if end == start index", function()
local t1 = { 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, 2, 2) do t2[i] = v end
assert.are.same({ [2] = 2 }, t2)
end)
it("returns negative to positive ranges", function()
local t1 = { [-5] = -5, [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, -4, 1) do t2[i] = v end
assert.are.same({ [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1 }, t2)
end)
it("returns nil values with the range", function()
local t1 = { n = 3 }
local t2 = {}
for i, v in npairs(t1) do t2[i] = tostring(v) end
assert.are.same({ "nil", "nil", "nil" }, t2)
end)
it("honours positive step size", function()
local t1 = { [-5] = -5, [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, -4, 1, 2) do t2[#t2+1] = v end
assert.are.same({ -4, -2, 0}, t2)
end)
it("honours negative step size", function()
local t1 = { [-5] = -5, [-4] = -4, [-3] = -3, [-2] = -2, [-1] = -1, [0] = 0, 1, 2, 3 }
local t2 = {}
for i, v in npairs(t1, 0, -5, -2) do t2[#t2+1] = v end
assert.are.same({ 0, -2, -4 }, t2)
end)
end)
end)
|