Welcome to mirror list, hosted at ThFree Co, Russian Federation.

permute_spec.lua « spec - github.com/stevedonovan/Penlight.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 49143c0790954a3f29a96c92dca4dcaa18e9d60c (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
local permute = require("pl.permute")
local tcopy = require("pl.tablex").copy
local utils = require("pl.utils")

describe("pl.permute", function()

  describe("order_iter", function()

    it("returns all order combinations", function()
      local result = {}
      for list in permute.order_iter({"one", "two", "three"}) do
        result[#result+1] = tcopy(list)
      end
      assert.same({
        [1] = {
           [1] = 'two',
           [2] = 'three',
           [3] = 'one' },
         [2] = {
           [1] = 'three',
           [2] = 'two',
           [3] = 'one' },
         [3] = {
           [1] = 'three',
           [2] = 'one',
           [3] = 'two' },
         [4] = {
           [1] = 'one',
           [2] = 'three',
           [3] = 'two' },
         [5] = {
           [1] = 'two',
           [2] = 'one',
           [3] = 'three' },
         [6] = {
           [1] = 'one',
           [2] = 'two',
           [3] = 'three' } }, result)
    end)


    it("returns nil on empty list", function()
      local result = {}
      for list in permute.order_iter({}) do
        result[#result+1] = tcopy(list)
      end
      assert.equal(0, #result)
    end)

  end)



  describe("order_table", function()

    it("returns all order combinations", function()
      local result = permute.order_table({"one", "two", "three"})
      assert.same({
        [1] = {
           [1] = 'two',
           [2] = 'three',
           [3] = 'one' },
         [2] = {
           [1] = 'three',
           [2] = 'two',
           [3] = 'one' },
         [3] = {
           [1] = 'three',
           [2] = 'one',
           [3] = 'two' },
         [4] = {
           [1] = 'one',
           [2] = 'three',
           [3] = 'two' },
         [5] = {
           [1] = 'two',
           [2] = 'one',
           [3] = 'three' },
         [6] = {
           [1] = 'one',
           [2] = 'two',
           [3] = 'three' } }, result)
    end)


    it("returns empty table on empty input list", function()
      local result = permute.order_table({})
      assert.same({}, result)
    end)

  end)



  describe("list_iter", function()

    it("returns all combinations from sub-lists", function()
      local result = {}
      local strs = {"one", "two", "three"}
      local ints = { 1,2,3 }
      local bools = { true, false }
      for count, str, int, bool in permute.list_iter(strs, ints, bools) do
        result[#result+1] = {count, str, int, bool}
      end
      assert.same({
        [1] = {1, 'one', 1, true },
        [2] = {2, 'two', 1, true },
        [3] = {3, 'three', 1, true },
        [4] = {4, 'one', 2, true },
        [5] = {5, 'two', 2, true },
        [6] = {6, 'three', 2, true },
        [7] = {7, 'one', 3, true },
        [8] = {8, 'two', 3, true },
        [9] = {9, 'three', 3, true },
        [10] = {10, 'one', 1, false },
        [11] = {11, 'two', 1, false },
        [12] = {12, 'three', 1, false },
        [13] = {13, 'one', 2, false },
        [14] = {14, 'two', 2, false },
        [15] = {15, 'three', 2, false },
        [16] = {16, 'one', 3, false },
        [17] = {17, 'two', 3, false },
        [18] = {18, 'three', 3, false },
      }, result)
    end)


    it("is nil-safe, given 'n' is set", function()
      local result = {}
      local bools = utils.pack(nil, true, false)
      local strs = utils.pack("one", "two", nil)
      for count, bool, str in permute.list_iter(bools, strs) do
        result[#result+1] = {count, bool, str}
      end
      assert.same({
        [1] = {1, nil, 'one' },
        [2] = {2, true, 'one' },
        [3] = {3, false, 'one' },
        [4] = {4, nil, 'two' },
        [5] = {5, true, 'two' },
        [6] = {6, false, 'two' },
        [7] = {7, nil, nil },
        [8] = {8, true, nil },
        [9] = {9, false, nil },
      }, result)
    end)


    it("returns nil on empty list", function()
      local count = 0
      for list in permute.list_iter({}) do
        count = count + 1
      end
      assert.equal(0, count)
    end)

  end)



  describe("list_table", function()

    it("returns all combinations from sub-lists", function()
      local strs = {"one", "two", "three"}
      local ints = { 1,2,3 }
      local bools = { true, false }
      assert.same({
        [1] = {'one', 1, true, n = 3 },
        [2] = {'two', 1, true, n = 3 },
        [3] = {'three', 1, true, n = 3 },
        [4] = {'one', 2, true, n = 3 },
        [5] = {'two', 2, true, n = 3 },
        [6] = {'three', 2, true, n = 3 },
        [7] = {'one', 3, true, n = 3 },
        [8] = {'two', 3, true, n = 3 },
        [9] = {'three', 3, true, n = 3 },
        [10] = {'one', 1, false, n = 3 },
        [11] = {'two', 1, false, n = 3 },
        [12] = {'three', 1, false, n = 3 },
        [13] = {'one', 2, false, n = 3 },
        [14] = {'two', 2, false, n = 3 },
        [15] = {'three', 2, false, n = 3 },
        [16] = {'one', 3, false, n = 3 },
        [17] = {'two', 3, false, n = 3 },
        [18] = {'three', 3, false, n = 3 },
      }, permute.list_table(strs, ints, bools))
    end)


    it("is nil-safe, given 'n' is set", function()
      local bools = utils.pack(nil, true, false)
      local strs = utils.pack("one", "two", nil)
      assert.same({
        [1] = {nil, 'one', n = 2 },
        [2] = {true, 'one', n = 2 },
        [3] = {false, 'one', n = 2 },
        [4] = {nil, 'two', n = 2 },
        [5] = {true, 'two', n = 2 },
        [6] = {false, 'two', n = 2 },
        [7] = {nil, nil, n = 2 },
        [8] = {true, nil, n = 2 },
        [9] = {false, nil, n = 2 },
      }, permute.list_table(bools, strs))
    end)


    it("returns nil on empty list", function()
      assert.same({}, permute.list_table({}))
    end)

  end)

end)