File: mutable_spec.cr

package info (click to toggle)
crystal 1.14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 24,384 kB
  • sloc: javascript: 6,400; sh: 695; makefile: 269; ansic: 121; python: 105; cpp: 77; xml: 32
file content (409 lines) | stat: -rw-r--r-- 13,799 bytes parent folder | download | duplicates (2)
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
require "spec"

private class SafeIndexableMutable
  include Indexable::Mutable(Int32)

  getter size

  # prevents `@values` from being interpretable as a raw slice of elements
  @values : Array(Array(Int32))

  def initialize(@size : Int32, *, offset = 0)
    @values = Array.new(size) { |i| [i + offset] }
  end

  def unsafe_fetch(index)
    raise IndexError.new unless 0 <= index < size
    @values[index][0]
  end

  def unsafe_put(index, value : Int32)
    raise IndexError.new unless 0 <= index < size
    @values[index] = [value]
  end
end

private class Foo
end

private class SafeIndexableMutableFoo
  include Indexable::Mutable(Foo)

  getter size

  @values : Array(Array(Foo))

  def initialize(@size : Int32)
    @values = Array.new(size) { [Foo.new] }
  end

  def unsafe_fetch(index)
    raise IndexError.new unless 0 <= index < size
    @values[index][0]
  end

  def unsafe_put(index, value : Foo)
    raise IndexError.new unless 0 <= index < size
    @values[index] = [value]
  end
end

describe Indexable::Mutable do
  # General note: code that tests `#method!` must not rely on the results of
  # `#method`, as otherwise those tests would trivially pass if the latter were
  # implemented as `to_a.method!`

  describe "#[]=" do
    it "sets the value at the given index" do
      coll = SafeIndexableMutable.new(5)
      (coll[2] = 123).should eq(123)
      coll[2].should eq(123)

      coll = SafeIndexableMutableFoo.new(5)
      foo = Foo.new
      (coll[2] = foo).should be(foo)
      coll[2].should be(foo)
    end

    it "wraps negative indices" do
      coll = SafeIndexableMutable.new(5)
      (coll[-2] = 123).should eq(123)
      coll[3].should eq(123)

      (coll[-5] = 456).should eq(456)
      coll[0].should eq(456)
    end

    it "raises on out-of-bound indices" do
      expect_raises(IndexError) { SafeIndexableMutable.new(5)[5] = 0 }
      expect_raises(IndexError) { SafeIndexableMutable.new(5)[-6] = 0 }
    end
  end

  describe "#update" do
    it "updates the value at the given index with the block" do
      coll = SafeIndexableMutable.new(5)
      coll.update(2) { |x| x + 7 }.should eq(9)
      coll[2].should eq(9)

      coll = SafeIndexableMutableFoo.new(5)
      foo = Foo.new
      coll.update(2) { foo }.should be(foo)
      coll[2].should be(foo)
    end

    it "wraps negative indices" do
      coll = SafeIndexableMutable.new(5)
      coll.update(-2) { |x| x + 7 }.should eq(10)
      coll[3].should eq(10)

      coll.update(-5) { |x| x + 14 }.should eq(14)
      coll[0].should eq(14)
    end

    it "raises on out-of-bound indices" do
      expect_raises(IndexError) { SafeIndexableMutable.new(5).update(5, &.itself) }
      expect_raises(IndexError) { SafeIndexableMutable.new(5).update(-6, &.itself) }
    end
  end

  describe "#swap" do
    it "exchanges the values at two indices" do
      coll = SafeIndexableMutable.new(12, offset: 50)
      coll.swap(2, 7).should be(coll)
      coll[2].should eq(57)
      coll[7].should eq(52)

      coll = SafeIndexableMutableFoo.new(12)
      foo2 = coll[2]
      foo7 = coll[7]
      coll.swap(2, 7).should be(coll)
      coll[2].should be(foo7)
      coll[7].should be(foo2)
    end

    it "wraps negative indices" do
      coll = SafeIndexableMutable.new(12, offset: 50)
      coll.swap(-3, -7).should be(coll)
      coll[9].should eq(55)
      coll[5].should eq(59)
    end

    it "raises on out-of-bound indices" do
      expect_raises(IndexError) { SafeIndexableMutable.new(5).swap(5, 5) }
      expect_raises(IndexError) { SafeIndexableMutable.new(5).swap(-6, -6) }
    end
  end

  describe "#reverse!" do
    it "reverses the order of all elements in place" do
      coll = SafeIndexableMutable.new(5)
      coll.reverse!.should be(coll)
      coll.to_a.should eq([4, 3, 2, 1, 0])

      coll = SafeIndexableMutable.new(8)
      coll.reverse!.should be(coll)
      coll.to_a.should eq([7, 6, 5, 4, 3, 2, 1, 0])

      coll = SafeIndexableMutableFoo.new(5)
      foos = coll.to_a
      coll.reverse!.should be(coll)
      coll.to_a.should eq([foos[4], foos[3], foos[2], foos[1], foos[0]])

      coll = SafeIndexableMutableFoo.new(8)
      foos = coll.to_a
      coll.reverse!.should be(coll)
      coll.to_a.should eq([foos[7], foos[6], foos[5], foos[4], foos[3], foos[2], foos[1], foos[0]])
    end
  end

  describe "#fill" do
    context "without block" do
      it "sets all elements to the same value" do
        coll = SafeIndexableMutable.new(5)
        coll.fill(4).should be(coll)
        coll.to_a.should eq([4, 4, 4, 4, 4])

        coll = SafeIndexableMutableFoo.new(5)
        foo = Foo.new
        coll.fill(foo).should be(coll)
        coll.to_a.should eq([foo, foo, foo, foo, foo])
      end
    end

    context "without block, with start + count" do
      it "sets a subrange of elements to the same value" do
        coll = SafeIndexableMutable.new(5)
        coll.fill(7, 2, 2).should be(coll)
        coll.to_a.should eq([0, 1, 7, 7, 4])

        coll = SafeIndexableMutableFoo.new(5)
        foos = coll.to_a
        foo = Foo.new
        coll.fill(foo, -4, 2).should be(coll)
        coll.to_a.should eq([foos[0], foo, foo, foos[3], foos[4]])
      end

      it "sets zero elements" do
        coll = SafeIndexableMutable.new(5)
        coll.fill(7, 1, 0).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, 5, 0).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, -3, 0).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, -5, 0).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, 4, -1).to_a.should eq([0, 1, 2, 3, 4])

        coll = SafeIndexableMutableFoo.new(5)
        foo = Foo.new
        coll.fill(foo, 1, 0).should_not contain(foo)
        coll.fill(foo, 5, 0).should_not contain(foo)
        coll.fill(foo, -3, 0).should_not contain(foo)
        coll.fill(foo, -5, 0).should_not contain(foo)
        coll.fill(foo, 4, -1).should_not contain(foo)
      end

      it "raises on out of bound start index" do
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(7, 6, 1) }
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(7, -6, 1) }
      end
    end

    context "without block, with range" do
      it "sets a subrange of elements to the same value" do
        coll = SafeIndexableMutable.new(5)
        coll.fill(7, 2..3).should be(coll)
        coll.to_a.should eq([0, 1, 7, 7, 4])

        coll = SafeIndexableMutableFoo.new(5)
        foos = coll.to_a
        foo = Foo.new
        coll.fill(foo, -4...-2).should be(coll)
        coll.to_a.should eq([foos[0], foo, foo, foos[3], foos[4]])
      end

      it "sets zero elements" do
        coll = SafeIndexableMutable.new(5)
        coll.fill(7, 1..0).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, 5...5).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, -3...1).to_a.should eq([0, 1, 2, 3, 4])
        coll.fill(7, -5..-6).to_a.should eq([0, 1, 2, 3, 4])

        coll = SafeIndexableMutableFoo.new(5)
        foo = Foo.new
        coll.fill(foo, 1..0).should_not contain(foo)
        coll.fill(foo, 5...5).should_not contain(foo)
        coll.fill(foo, -3...1).should_not contain(foo)
        coll.fill(foo, -5..-6).should_not contain(foo)
      end

      it "raises on out of bound start index" do
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(7, 6..7) }
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(7, -6..-1) }
      end
    end

    context "with block" do
      it "yields index to the block and sets all elements" do
        coll = SafeIndexableMutable.new(5, offset: 10)
        coll.fill { |i| i * i }.should be(coll)
        coll.to_a.should eq([0, 1, 4, 9, 16])

        coll = SafeIndexableMutableFoo.new(5)
        foo = Foo.new
        coll.fill { foo }.should be(coll)
        coll.to_a.should eq([foo, foo, foo, foo, foo])
      end
    end

    context "with block + offset" do
      it "yields index plus offset to the block and sets all elements" do
        coll = SafeIndexableMutable.new(5, offset: 10)
        coll.fill(offset: 7) { |i| i * i }.should be(coll)
        coll.to_a.should eq([49, 64, 81, 100, 121])

        coll = SafeIndexableMutableFoo.new(5)
        foos = coll.to_a
        coll.fill(offset: -2) { |i| foos[i] }.should be(coll)
        coll.to_a.should eq([foos[-2], foos[-1], foos[0], foos[1], foos[2]])
      end
    end

    context "with block + start + count" do
      it "yields index to the block and sets elements in a subrange" do
        coll = SafeIndexableMutable.new(5, offset: 10)
        coll.fill(2, 2) { |i| i + 7 }.should be(coll)
        coll.to_a.should eq([10, 11, 9, 10, 14])

        coll = SafeIndexableMutableFoo.new(5)
        foos = coll.to_a
        foo = Foo.new
        coll.fill(-4, 2) { foo }.should be(coll)
        coll.to_a.should eq([foos[0], foo, foo, foos[3], foos[4]])
      end

      it "sets zero elements" do
        coll = SafeIndexableMutable.new(5, offset: 10)
        coll.fill(1, 0) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(5, 0) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(-3, 0) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(-5, 0) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(4, -1) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])

        coll = SafeIndexableMutableFoo.new(5)
        foo = Foo.new
        coll.fill(1, 0) { foo }.should_not contain(foo)
        coll.fill(5, 0) { foo }.should_not contain(foo)
        coll.fill(-3, 0) { foo }.should_not contain(foo)
        coll.fill(-5, 0) { foo }.should_not contain(foo)
        coll.fill(4, -1) { foo }.should_not contain(foo)
      end

      it "raises on out of bound start index" do
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(6, 1, &.itself) }
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(-6, 1, &.itself) }
      end
    end

    context "with block + range" do
      it "yields index to the block and sets elements in a subrange" do
        coll = SafeIndexableMutable.new(5, offset: 10)
        coll.fill(2..3) { |i| i + 7 }.should be(coll)
        coll.to_a.should eq([10, 11, 9, 10, 14])

        coll = SafeIndexableMutableFoo.new(5)
        foos = coll.to_a
        foo = Foo.new
        coll.fill(-4...-2) { foo }.should be(coll)
        coll.to_a.should eq([foos[0], foo, foo, foos[3], foos[4]])
      end

      it "sets zero elements" do
        coll = SafeIndexableMutable.new(5, offset: 10)
        coll.fill(1..0) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(5...5) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(-3...1) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])
        coll.fill(-5..-6) { |i| i + 7 }.to_a.should eq([10, 11, 12, 13, 14])

        coll = SafeIndexableMutableFoo.new(5)
        foo = Foo.new
        coll.fill(1..0) { foo }.should_not contain(foo)
        coll.fill(5...5) { foo }.should_not contain(foo)
        coll.fill(-3...1) { foo }.should_not contain(foo)
        coll.fill(-5..-6) { foo }.should_not contain(foo)
      end

      it "raises on out of bound start index" do
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(6..7, &.itself) }
        expect_raises(IndexError) { SafeIndexableMutable.new(5).fill(-6..-1, &.itself) }
      end
    end
  end

  describe "#map!" do
    it "replaces each element with the block value" do
      coll = SafeIndexableMutable.new(5, offset: 2)
      coll.map! { |i| 10 - i }
      coll.to_a.should eq([8, 7, 6, 5, 4])

      coll = SafeIndexableMutableFoo.new(5)
      foos = coll.to_a
      coll.map! &.itself
      coll.to_a.should eq(foos)
    end
  end

  describe "#map_with_index!" do
    context "without offset" do
      it "yields each element and index to the block" do
        coll = SafeIndexableMutable.new(5)
        coll[2] = 4
        coll[4] = 7
        coll.map_with_index! { |v, i| v * 100 + i }
        coll.to_a.should eq([0, 101, 402, 303, 704])
      end
    end

    context "with offset" do
      it "yields each element and index plus offset to the block" do
        coll = SafeIndexableMutable.new(5)
        coll[2] = 4
        coll[4] = 7
        coll.map_with_index!(offset: 4) { |v, i| v * 100 + i }
        coll.to_a.should eq([4, 105, 406, 307, 708])
      end
    end
  end

  describe "#shuffle!" do
    it "randomizes the order of all elements" do
      coll = SafeIndexableMutable.new(5)
      coll.shuffle!(Random.new(42)).should be(coll)
      coll.to_a.should eq([2, 1, 3, 4, 0])

      coll = SafeIndexableMutableFoo.new(5)
      foos = coll.to_a
      coll.shuffle!(Random.new(42)).should be(coll)
      coll.to_a.should eq([foos[2], foos[1], foos[3], foos[4], foos[0]])
    end
  end

  describe "#rotate!" do
    it "left-shifts all elements" do
      coll = SafeIndexableMutable.new(5)
      coll.rotate!(2).should be(coll)
      coll.to_a.should eq([2, 3, 4, 0, 1])

      coll = SafeIndexableMutableFoo.new(5)
      foos = coll.to_a
      coll.rotate!(3).should be(coll)
      coll.to_a.should eq([foos[3], foos[4], foos[0], foos[1], foos[2]])

      SafeIndexableMutable.new(6).rotate!(2).to_a.should eq([2, 3, 4, 5, 0, 1])
      SafeIndexableMutable.new(6).rotate!(0).to_a.should eq([0, 1, 2, 3, 4, 5])
      SafeIndexableMutable.new(6).rotate!(12).to_a.should eq([0, 1, 2, 3, 4, 5])
      SafeIndexableMutable.new(6).rotate!(-1).to_a.should eq([5, 0, 1, 2, 3, 4])
      SafeIndexableMutable.new(10).rotate!(6).to_a.should eq([6, 7, 8, 9, 0, 1, 2, 3, 4, 5])
      SafeIndexableMutable.new(10).rotate!(12345678).to_a.should eq([8, 9, 0, 1, 2, 3, 4, 5, 6, 7])
      SafeIndexableMutable.new(10).rotate!(-1234567).to_a.should eq([3, 4, 5, 6, 7, 8, 9, 0, 1, 2])
    end
  end
end