-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
Copy pathindexable_spec.cr
898 lines (756 loc) · 27.4 KB
/
indexable_spec.cr
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
require "spec"
require "spec/helpers/iterate"
module OtherInterface; end
private record Three do
include OtherInterface
end
private record Four do
include OtherInterface
end
private struct InterfaceIndexable
include Indexable(OtherInterface)
def size
2
end
def unsafe_fetch(index : Int) : OtherInterface
case index
when 0 then Three.new
when 1 then Four.new
else
raise ""
end
end
end
private class SafeIndexable
include Indexable(Int32)
property size
def initialize(@size : Int32, @offset = 0_i32)
end
def unsafe_fetch(index) : Int32
raise IndexError.new unless 0 <= index < size
(index + @offset).to_i
end
end
private class SafeNestedIndexable
include Indexable(Indexable(Int32))
property size
def initialize(@size : Int32, @inner_size : Int32)
end
def unsafe_fetch(index)
raise IndexError.new unless 0 <= index < size
SafeIndexable.new(@inner_size)
end
end
private class SafeStringIndexable
include Indexable(String)
property size
def initialize(@size : Int32)
end
def unsafe_fetch(index) : String
raise IndexError.new unless 0 <= index < size
index.to_s
end
end
private class SafeMixedIndexable
include Indexable(String | Int32)
property size
def initialize(@size : Int32)
end
def unsafe_fetch(index) : String | Int32
raise IndexError.new unless 0 <= index < size
index.to_s
end
end
private class SafeRecursiveIndexable
include Indexable(SafeRecursiveIndexable | Int32)
property size
def initialize(@size : Int32)
end
def unsafe_fetch(index) : SafeRecursiveIndexable | Int32
raise IndexError.new unless 0 <= index < size
if (index % 2) == 0
SafeRecursiveIndexable.new(index)
else
index
end
end
end
describe Indexable do
describe "#index" do
it "does index with big negative offset" do
indexable = SafeIndexable.new(3)
indexable.index(0, -100).should be_nil
end
it "does index with big offset" do
indexable = SafeIndexable.new(3)
indexable.index(0, 100).should be_nil
end
it "offset type" do
indexable = SafeIndexable.new(3)
indexable.index(1, 0_i64).should eq 1
indexable.index(1, 0_i64).should be_a(Int64)
end
end
describe "#index!" do
it "offset type" do
indexable = SafeIndexable.new(3)
indexable.index!(1, 0_i64).should eq 1
indexable.index!(1, 0_i64).should be_a(Int64)
end
it "raises if no element is found" do
indexable = SafeIndexable.new(3)
expect_raises(Enumerable::NotFoundError) { indexable.index!(0, -100) }
expect_raises(Enumerable::NotFoundError) { indexable.index!(0, -4) }
expect_raises(Enumerable::NotFoundError) { indexable.index!(0, 1) }
expect_raises(Enumerable::NotFoundError) { indexable.index!(0, 3) }
expect_raises(Enumerable::NotFoundError) { indexable.index!(0, 100) }
expect_raises(Enumerable::NotFoundError) { indexable.index!(-4) { true } }
expect_raises(Enumerable::NotFoundError) { indexable.index!(3) { true } }
expect_raises(Enumerable::NotFoundError) { indexable.index!(2) { false } }
expect_raises(Enumerable::NotFoundError) { indexable.index!(-3) { false } }
end
end
describe "#find" do
it "finds the element matching the block" do
indexable = SafeIndexable.new(4)
indexable.find { |i| i > 2 }.should eq 3
end
it "finds the element matching the block after given offset" do
indexable = SafeIndexable.new(8)
indexable.find(5) { |i| i.even? }.should eq 6
end
it "does not find the element matching the block" do
indexable = SafeIndexable.new(4)
indexable.find { |i| i > 7 }.should be_nil
end
it "does not find the element matching the block, returns custom if_none value" do
indexable = SafeIndexable.new(4)
indexable.find(if_none: -1) { |i| i > 7 }.should eq -1
end
it "does not find the element matching the block after given offset, returns custom if_none value" do
indexable = SafeIndexable.new(5)
indexable.find(3, -3) { |i| i > 15 }.should eq -3
end
end
describe "#find!" do
it "finds the element matching the block" do
indexable = SafeIndexable.new(4)
indexable.find! { |i| i > 2 }.should eq 3
end
it "finds the element matching the block after given offset" do
indexable = SafeIndexable.new(8)
indexable.find!(5) { |i| i.even? }.should eq 6
end
it "does not find the element matching the block, raises not found" do
indexable = SafeIndexable.new(4)
expect_raises(Enumerable::NotFoundError) { indexable.find! { |i| i > 7 } }
end
end
describe "#rindex" do
it "does rindex with big negative offset" do
indexable = SafeIndexable.new(3)
indexable.rindex(0, -100).should be_nil
end
it "does rindex with big offset" do
indexable = SafeIndexable.new(3)
indexable.rindex(0, 100).should be_nil
end
it "offset type" do
indexable = SafeIndexable.new(3)
indexable.rindex(1, 2_i64).should eq 1
indexable.rindex(1, 2_i64).should be_a(Int64)
end
end
describe "#rindex!" do
it "does rindex with big negative offset" do
indexable = SafeIndexable.new(3)
expect_raises Enumerable::NotFoundError do
indexable.rindex!(0, -100)
end
end
it "does rindex with big offset" do
indexable = SafeIndexable.new(3)
expect_raises Enumerable::NotFoundError do
indexable.rindex!(0, 100)
end
end
it "offset type" do
indexable = SafeIndexable.new(3)
indexable.rindex!(1, 2_i64).should eq 1
indexable.rindex!(1, 2_i64).should be_a(Int64)
end
end
it "does each" do
indexable = SafeIndexable.new(3)
is = [] of Int32
indexable.each do |i|
is << i
end.should be_nil
is.should eq([0, 1, 2])
end
it "does each_index" do
indexable = SafeIndexable.new(3)
is = [] of Int32
indexable.each_index do |i|
is << i
end.should be_nil
is.should eq([0, 1, 2])
end
it "iterates through a subset of its elements (#3386)" do
indexable = SafeIndexable.new(5)
elems = [] of Int32
return_value = indexable.each(start: 2, count: 3) do |elem|
elems << elem
end
elems.should eq([2, 3, 4])
return_value.should eq(indexable)
end
it "iterates until its size (#3386)" do
indexable = SafeIndexable.new(5)
elems = [] of Int32
indexable.each(start: 3, count: 999) do |elem|
elems << elem
end
elems.should eq([3, 4])
end
it "iterates until its size, having mutated (#3386)" do
indexable = SafeIndexable.new(10)
elems = [] of Int32
indexable.each(start: 3, count: 999) do |elem|
# size is incremented 3 times
indexable.size += 1 if elem <= 5
elems << elem
end
# last was 9, but now is 12.
elems.should eq([3, 4, 5, 6, 7, 8, 9, 10, 11, 12])
end
it "iterates until its size, having mutated (#3386)" do
indexable = SafeIndexable.new(10)
elems = [] of Int32
indexable.each(start: 3, count: 5) do |elem|
indexable.size += 1
elems << elem
end
# last element iterated is still 7.
elems.should eq([3, 4, 5, 6, 7])
end
it "iterates within a range of indices (#3386)" do
indexable = SafeIndexable.new(5)
elems = [] of Int32
return_value = indexable.each(within: 2..3) do |elem|
elems << elem
end
elems.should eq([2, 3])
return_value.should eq(indexable)
end
it "iterates within a range of indices, no end" do
indexable = SafeIndexable.new(5)
elems = [] of Int32
return_value = indexable.each(within: 2..nil) do |elem|
elems << elem
end
elems.should eq([2, 3, 4])
return_value.should eq(indexable)
end
it "iterates within a range of indices, no beginning" do
indexable = SafeIndexable.new(5)
elems = [] of Int32
return_value = indexable.each(within: nil..2) do |elem|
elems << elem
end
elems.should eq([0, 1, 2])
return_value.should eq(indexable)
end
describe "#join" do
it "joins strings (empty case)" do
indexable = SafeStringIndexable.new(0)
indexable.join.should eq("")
indexable.join(", ").should eq("")
end
it "joins strings (non-empty case)" do
indexable = SafeStringIndexable.new(12)
indexable.join(", ").should eq("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11")
indexable.join(98).should eq("098198298398498598698798898998109811")
end
it "joins non-strings" do
indexable = SafeIndexable.new(12)
indexable.join(", ").should eq("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11")
indexable.join(98).should eq("098198298398498598698798898998109811")
end
it "joins when T has String" do
indexable = SafeMixedIndexable.new(12)
indexable.join(", ").should eq("0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11")
indexable.join(98).should eq("098198298398498598698798898998109811")
end
it "with IO" do
String.build do |io|
indexable = SafeStringIndexable.new(12)
indexable.join(io)
end.should eq "01234567891011"
end
end
describe "dig?" do
it "gets the value at given path given splat" do
indexable = SafeRecursiveIndexable.new(30)
indexable.dig?(20, 10, 4, 3).should eq(3)
end
it "returns nil if not found" do
indexable = SafeRecursiveIndexable.new(30)
indexable.dig?(2, 4).should be_nil
indexable.dig?(3, 7).should be_nil
end
end
describe "dig" do
it "gets the value at given path given splat" do
indexable = SafeRecursiveIndexable.new(30)
indexable.dig(20, 10, 4, 3).should eq(3)
end
it "raises IndexError if not found" do
indexable = SafeRecursiveIndexable.new(30)
expect_raises IndexError, %(Index out of bounds) do
indexable.dig(2, 4)
end
expect_raises IndexError, %(Indexable value not diggable for index: 3) do
indexable.dig(3, 7)
end
end
end
describe "fetch" do
it "fetches with default value" do
indexable = SafeIndexable.new(3)
a = indexable.to_a
indexable.fetch(2, 4).should eq(2)
indexable.fetch(3, 4).should eq(4)
a.should eq([0, 1, 2])
end
it "fetches with block" do
indexable = SafeIndexable.new(3)
a = indexable.to_a
indexable.fetch(2) { |k| k * 3 }.should eq(2)
indexable.fetch(3) { |k| k * 3 }.should eq(9)
a.should eq([0, 1, 2])
end
end
describe "#cartesian_product" do
it "does with 1 other Indexable" do
elems = SafeIndexable.new(2).cartesian_product(SafeIndexable.new(3))
elems.should eq([{0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 1}, {1, 2}])
elems = SafeIndexable.new(2).cartesian_product(SafeIndexable.new(0))
elems.should be_empty
elems = SafeIndexable.new(0).cartesian_product(SafeIndexable.new(3))
elems.should be_empty
end
it "does with >1 other Indexables" do
elems = SafeIndexable.new(2).cartesian_product(SafeStringIndexable.new(2), SafeIndexable.new(2))
elems.should eq([
{0, "0", 0}, {0, "0", 1}, {0, "1", 0}, {0, "1", 1},
{1, "0", 0}, {1, "0", 1}, {1, "1", 0}, {1, "1", 1},
])
end
end
describe ".cartesian_product" do
it "does with an Indexable of Indexables" do
elems = Indexable.cartesian_product(SafeNestedIndexable.new(2, 3))
elems.should eq([[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2], [2, 0], [2, 1], [2, 2]])
elems = Indexable.cartesian_product(SafeNestedIndexable.new(2, 0))
elems.should be_empty
elems = Indexable.cartesian_product(SafeNestedIndexable.new(0, 3))
elems.should eq([[] of Int32])
elems = Indexable.cartesian_product(SafeNestedIndexable.new(0, 0))
elems.should eq([[] of Int32])
end
it "does with a Tuple of Tuples with mixed types" do
elems = Indexable.cartesian_product({ {1, 'a'}, {"", 4}, {5, 6} })
elems.should be_a(Array(Array(Int32 | Char | String)))
elems.should eq([[1, "", 5], [1, "", 6], [1, 4, 5], [1, 4, 6], ['a', "", 5], ['a', "", 6], ['a', 4, 5], ['a', 4, 6]])
end
end
describe "#each_cartesian" do
it "does with 1 other Indexable, with block" do
r = [] of Int32 | String
indexable = SafeIndexable.new(3)
indexable.each_cartesian(SafeStringIndexable.new(2)) { |a, b| r << a; r << b }
r.should eq([0, "0", 0, "1", 1, "0", 1, "1", 2, "0", 2, "1"])
r = [] of Int32 | String
indexable = SafeIndexable.new(3)
indexable.each_cartesian(SafeStringIndexable.new(0)) { |a, b| r << a; r << b }
r.should be_empty
r = [] of Int32 | String
indexable = SafeIndexable.new(0)
indexable.each_cartesian(SafeStringIndexable.new(2)) { |a, b| r << a; r << b }
r.should be_empty
end
it "does with 1 other Indexable, without block" do
iter = SafeIndexable.new(3).each_cartesian(SafeStringIndexable.new(2))
iter.next.should eq({0, "0"})
iter.next.should eq({0, "1"})
iter.next.should eq({1, "0"})
iter.next.should eq({1, "1"})
iter.next.should eq({2, "0"})
iter.next.should eq({2, "1"})
iter.next.should be_a(Iterator::Stop)
end
it "does with 1 other Indexable, without block, combined with select" do
iter = SafeIndexable.new(3).each_cartesian(SafeStringIndexable.new(2))
iter = iter.select { |(x, y)| x > 0 }
iter.next.should eq({1, "0"})
iter.next.should eq({1, "1"})
iter.next.should eq({2, "0"})
iter.next.should eq({2, "1"})
iter.next.should be_a(Iterator::Stop)
end
it "does with >1 other Indexables, with block" do
r = [] of Int32 | String
i1 = SafeIndexable.new(2)
i2 = SafeIndexable.new(3)
i3 = SafeIndexable.new(4)
i1.each_cartesian(i2, i3) { |a, b, c| r << a + b + c }
r.should eq([0, 1, 2, 3, 1, 2, 3, 4, 2, 3, 4, 5, 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6])
end
it "does with >1 other Indexables, without block" do
i1 = SafeStringIndexable.new(2)
i2 = SafeStringIndexable.new(2)
i3 = SafeIndexable.new(2)
iter = i1.each_cartesian(i2, i3)
iter.next.should eq({"0", "0", 0})
iter.next.should eq({"0", "0", 1})
iter.next.should eq({"0", "1", 0})
iter.next.should eq({"0", "1", 1})
iter.next.should eq({"1", "0", 0})
iter.next.should eq({"1", "0", 1})
iter.next.should eq({"1", "1", 0})
iter.next.should eq({"1", "1", 1})
iter.next.should be_a(Iterator::Stop)
end
end
describe ".each_cartesian" do
it "does with an Indexable of Indexables, with block" do
r = [] of Int32
Indexable.each_cartesian(SafeNestedIndexable.new(3, 2)) { |v| r.concat(v) }
r.should eq([0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1])
r = [] of Int32
Indexable.each_cartesian(SafeNestedIndexable.new(3, 0)) { |v| r.concat(v) }
r.should be_empty
r = [] of Int32
Indexable.each_cartesian(SafeNestedIndexable.new(0, 2)) { |v| r.concat(v) }
r.should be_empty
r = [] of Int32
Indexable.each_cartesian(SafeNestedIndexable.new(0, 0)) { |v| r.concat(v) }
r.should be_empty
end
it "does with reuse = true, with block" do
r = [] of Int32
object_ids = Set(UInt64).new
indexables = SafeNestedIndexable.new(3, 2)
Indexable.each_cartesian(indexables, reuse: true) do |v|
object_ids << v.object_id
r.concat(v)
end
r.should eq([0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1])
object_ids.size.should eq(1)
end
it "does with reuse = array, with block" do
r = [] of Int32
buf = [] of Int32
indexables = SafeNestedIndexable.new(3, 2)
Indexable.each_cartesian(indexables, reuse: buf) do |v|
v.should be(buf)
r.concat(v)
end
r.should eq([0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1])
end
it "does with an Indexable of Indexables, without block" do
iter = Indexable.each_cartesian(SafeNestedIndexable.new(2, 3))
iter.next.should eq([0, 0])
iter.next.should eq([0, 1])
iter.next.should eq([0, 2])
iter.next.should eq([1, 0])
iter.next.should eq([1, 1])
iter.next.should eq([1, 2])
iter.next.should eq([2, 0])
iter.next.should eq([2, 1])
iter.next.should eq([2, 2])
iter.next.should be_a(Iterator::Stop)
iter = Indexable.each_cartesian(SafeNestedIndexable.new(0, 3))
iter.next.should eq([] of Int32)
iter.next.should be_a(Iterator::Stop)
end
it "does with an Indexable of Indexables, without block, combined with select" do
iter = Indexable.each_cartesian(SafeNestedIndexable.new(2, 3))
iter = iter.select { |(x, y)| x > 0 }
iter.next.should eq([1, 0])
iter.next.should eq([1, 1])
iter.next.should eq([1, 2])
iter.next.should eq([2, 0])
iter.next.should eq([2, 1])
iter.next.should eq([2, 2])
iter.next.should be_a(Iterator::Stop)
iter = Indexable.each_cartesian(SafeNestedIndexable.new(0, 3))
iter.next.should eq([] of Int32)
iter.next.should be_a(Iterator::Stop)
end
it "does with reuse = true, without block" do
iter = Indexable.each_cartesian(SafeNestedIndexable.new(2, 2), reuse: true)
buf = iter.next
buf.should eq([0, 0])
iter.next.should be(buf)
buf.should eq([0, 1])
iter.next.should be(buf)
buf.should eq([1, 0])
iter.next.should be(buf)
buf.should eq([1, 1])
iter.next.should be_a(Iterator::Stop)
end
it "does with reuse = array, without block" do
buf = [] of Int32
iter = Indexable.each_cartesian(SafeNestedIndexable.new(2, 2), reuse: buf)
iter.next.should be(buf)
buf.should eq([0, 0])
iter.next.should be(buf)
buf.should eq([0, 1])
iter.next.should be(buf)
buf.should eq([1, 0])
iter.next.should be(buf)
buf.should eq([1, 1])
end
end
describe "permutations" do
it { [1, 2, 2].permutations.should eq([[1, 2, 2], [1, 2, 2], [2, 1, 2], [2, 2, 1], [2, 1, 2], [2, 2, 1]]) }
it { SafeIndexable.new(3, 1).permutations.should eq([[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]) }
it { SafeIndexable.new(3, 1).permutations(1).should eq([[1], [2], [3]]) }
it { SafeIndexable.new(3, 1).permutations(2).should eq([[1, 2], [1, 3], [2, 1], [2, 3], [3, 1], [3, 2]]) }
it { SafeIndexable.new(3, 1).permutations(3).should eq([[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]) }
it { SafeIndexable.new(3, 1).permutations(0).should eq([[] of Int32]) }
it { SafeIndexable.new(3, 1).permutations(4).should eq([] of Array(Int32)) }
it { expect_raises(ArgumentError, "Size must be positive") { [1].permutations(-1) } }
it "accepts a block" do
sums = [] of Int32
SafeIndexable.new(3, 1).each_permutation(2) do |perm|
sums << perm.sum
end.should be_nil
sums.should eq([3, 4, 3, 5, 4, 5])
end
it "yielding dup of arrays" do
sums = [] of Int32
SafeIndexable.new(3, 1).each_permutation(3) do |perm|
perm.map! &.+(1)
sums << perm.sum
end.should be_nil
sums.should eq([9, 9, 9, 9, 9, 9])
end
it "yields with reuse = true" do
sums = [] of Int32
object_ids = Set(UInt64).new
SafeIndexable.new(3, 1).each_permutation(3, reuse: true) do |perm|
object_ids << perm.object_id
perm.map! &.+(1)
sums << perm.sum
end.should be_nil
sums.should eq([9, 9, 9, 9, 9, 9])
object_ids.size.should eq(1)
end
it { expect_raises(ArgumentError, "Size must be positive") { [1].each_permutation(-1) { } } }
it "returns iterator" do
a = SafeIndexable.new(3, 1)
perms = a.permutations
iter = a.each_permutation
perms.each do |perm|
iter.next.should eq(perm)
end
iter.next.should be_a(Iterator::Stop)
end
it "returns iterator with given size" do
a = SafeIndexable.new(3, 1)
perms = a.permutations(2)
iter = a.each_permutation(2)
perms.each do |perm|
iter.next.should eq(perm)
end
iter.next.should be_a(Iterator::Stop)
end
it "returns iterator with reuse = true" do
a = SafeIndexable.new(3, 1)
object_ids = Set(UInt64).new
perms = a.permutations
iter = a.each_permutation(reuse: true)
perms.each do |perm|
b = iter.next.as(Array)
object_ids << b.object_id
b.should eq(perm)
end
iter.next.should be_a(Iterator::Stop)
object_ids.size.should eq(1)
end
end
describe "combinations" do
it { [1, 2, 2].combinations.should eq([[1, 2, 2]]) }
it { SafeIndexable.new(3, 1).combinations.should eq([[1, 2, 3]]) }
it { SafeIndexable.new(3, 1).combinations(1).should eq([[1], [2], [3]]) }
it { SafeIndexable.new(3, 1).combinations(2).should eq([[1, 2], [1, 3], [2, 3]]) }
it { SafeIndexable.new(3, 1).combinations(3).should eq([[1, 2, 3]]) }
it { SafeIndexable.new(3, 1).combinations(0).should eq([[] of Int32]) }
it { SafeIndexable.new(3, 1).combinations(4).should eq([] of Array(Int32)) }
it { SafeIndexable.new(4, 1).combinations(3).should eq([[1, 2, 3], [1, 2, 4], [1, 3, 4], [2, 3, 4]]) }
it { SafeIndexable.new(4, 1).combinations(2).should eq([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]]) }
it { expect_raises(ArgumentError, "Size must be positive") { [1].combinations(-1) } }
it "accepts a block" do
sums = [] of Int32
SafeIndexable.new(3, 1).each_combination(2) do |comb|
sums << comb.sum
end.should be_nil
sums.should eq([3, 4, 5])
end
it "yielding dup of arrays" do
sums = [] of Int32
SafeIndexable.new(3, 1).each_combination(3) do |comb|
comb.map! &.+(1)
sums << comb.sum
end.should be_nil
sums.should eq([9])
end
it "does with reuse = true" do
sums = [] of Int32
object_ids = Set(UInt64).new
SafeIndexable.new(3, 1).each_combination(2, reuse: true) do |comb|
sums << comb.sum
object_ids << comb.object_id
end.should be_nil
sums.should eq([3, 4, 5])
object_ids.size.should eq(1)
end
it "does with reuse = array" do
sums = [] of Int32
reuse = [] of Int32
SafeIndexable.new(3, 1).each_combination(2, reuse: reuse) do |comb|
sums << comb.sum
comb.should be(reuse)
end.should be_nil
sums.should eq([3, 4, 5])
end
it { expect_raises(ArgumentError, "Size must be positive") { [1].each_combination(-1) { } } }
it "returns iterator" do
a = [1, 2, 3, 4]
combs = a.combinations(2)
iter = a.each_combination(2)
combs.each do |comb|
iter.next.should eq(comb)
end
iter.next.should be_a(Iterator::Stop)
end
it "returns iterator with reuse = true" do
a = [1, 2, 3, 4]
combs = a.combinations(2)
object_ids = Set(UInt64).new
iter = a.each_combination(2, reuse: true)
combs.each do |comb|
b = iter.next
object_ids << b.object_id
b.should eq(comb)
end
iter.next.should be_a(Iterator::Stop)
object_ids.size.should eq(1)
end
it "returns iterator with reuse = array" do
a = [1, 2, 3, 4]
reuse = [] of Int32
combs = a.combinations(2)
iter = a.each_combination(2, reuse: reuse)
combs.each do |comb|
b = iter.next
b.should be(reuse)
b.should eq(comb)
end
iter.next.should be_a(Iterator::Stop)
end
end
describe "repeated_combinations" do
it { [1, 2, 2].repeated_combinations.should eq([[1, 1, 1], [1, 1, 2], [1, 1, 2], [1, 2, 2], [1, 2, 2], [1, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2], [2, 2, 2]]) }
it { SafeIndexable.new(3, 1).repeated_combinations.should eq([[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]) }
it { SafeIndexable.new(3, 1).repeated_combinations(1).should eq([[1], [2], [3]]) }
it { SafeIndexable.new(3, 1).repeated_combinations(2).should eq([[1, 1], [1, 2], [1, 3], [2, 2], [2, 3], [3, 3]]) }
it { SafeIndexable.new(3, 1).repeated_combinations(3).should eq([[1, 1, 1], [1, 1, 2], [1, 1, 3], [1, 2, 2], [1, 2, 3], [1, 3, 3], [2, 2, 2], [2, 2, 3], [2, 3, 3], [3, 3, 3]]) }
it { SafeIndexable.new(3, 1).repeated_combinations(0).should eq([[] of Int32]) }
it { SafeIndexable.new(3, 1).repeated_combinations(4).should eq([[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 1, 3], [1, 1, 2, 2], [1, 1, 2, 3], [1, 1, 3, 3], [1, 2, 2, 2], [1, 2, 2, 3], [1, 2, 3, 3], [1, 3, 3, 3], [2, 2, 2, 2], [2, 2, 2, 3], [2, 2, 3, 3], [2, 3, 3, 3], [3, 3, 3, 3]]) }
it { expect_raises(ArgumentError, "Size must be positive") { [1].repeated_combinations(-1) } }
it "accepts a block" do
sums = [] of Int32
SafeIndexable.new(3, 1).each_repeated_combination(2) do |comb|
sums << comb.sum
end.should be_nil
sums.should eq([2, 3, 4, 4, 5, 6])
end
it "yielding dup of arrays" do
sums = [] of Int32
SafeIndexable.new(3, 1).each_repeated_combination(3) do |comb|
comb.map! &.+(1)
sums << comb.sum
end.should be_nil
sums.should eq([6, 7, 8, 8, 9, 10, 9, 10, 11, 12])
end
it { expect_raises(ArgumentError, "Size must be positive") { [1].each_repeated_combination(-1) { } } }
it "yields with reuse = true" do
sums = [] of Int32
object_ids = Set(UInt64).new
SafeIndexable.new(3, 1).each_repeated_combination(3, reuse: true) do |comb|
object_ids << comb.object_id
comb.map! &.+(1)
sums << comb.sum
end.should be_nil
sums.should eq([6, 7, 8, 8, 9, 10, 9, 10, 11, 12])
object_ids.size.should eq(1)
end
it "yields with reuse = array" do
sums = [] of Int32
reuse = [] of Int32
SafeIndexable.new(3, 1).each_repeated_combination(3, reuse: reuse) do |comb|
comb.should be(reuse)
comb.map! &.+(1)
sums << comb.sum
end.should be_nil
sums.should eq([6, 7, 8, 8, 9, 10, 9, 10, 11, 12])
end
it "returns iterator" do
a = [1, 2, 3, 4]
combs = a.repeated_combinations(2)
iter = a.each_repeated_combination(2)
combs.each do |comb|
iter.next.should eq(comb)
end
iter.next.should be_a(Iterator::Stop)
end
it "returns iterator with reuse = true" do
a = [1, 2, 3, 4]
object_ids = Set(UInt64).new
combs = a.repeated_combinations(2)
iter = a.each_repeated_combination(2, reuse: true)
combs.each do |comb|
b = iter.next
object_ids << b.object_id
b.should eq(comb)
end
iter.next.should be_a(Iterator::Stop)
object_ids.size.should eq(1)
end
it "returns iterator with reuse = array" do
a = [1, 2, 3, 4]
reuse = [] of Int32
combs = a.repeated_combinations(2)
iter = a.each_repeated_combination(2, reuse: reuse)
combs.each do |comb|
b = iter.next
b.should be(reuse)
b.should eq(comb)
end
iter.next.should be_a(Iterator::Stop)
end
describe "n > size (#14088)" do
it_iterates "#each_repeated_combination", [[1, 1, 1], [1, 1, 2], [1, 2, 2], [2, 2, 2]], SafeIndexable.new(2, 1).each_repeated_combination(3)
it_iterates "#each_repeated_combination", [[1, 1, 1, 1], [1, 1, 1, 2], [1, 1, 2, 2], [1, 2, 2, 2], [2, 2, 2, 2]], SafeIndexable.new(2, 1).each_repeated_combination(4)
end
end
describe "#to_a" do
it "without a block of an interface type" do
InterfaceIndexable.new.to_a.should eq [Three.new, Four.new]
end
end
end