@@ -1484,6 +1484,18 @@ describe "Array" do
1484
1484
sums.should eq([9 , 9 , 9 , 9 , 9 , 9 ])
1485
1485
end
1486
1486
1487
+ it " yields with reuse = true" do
1488
+ sums = [] of Int32
1489
+ object_ids = Set (UInt64 ).new
1490
+ [1 , 2 , 3 ].each_permutation(3 , reuse: true ) do |perm |
1491
+ object_ids << perm.object_id
1492
+ perm.map! & .+ (1 )
1493
+ sums << perm.sum
1494
+ end .should eq([1 , 2 , 3 ])
1495
+ sums.should eq([9 , 9 , 9 , 9 , 9 , 9 ])
1496
+ object_ids.size.should eq(1 )
1497
+ end
1498
+
1487
1499
assert { expect_raises(ArgumentError , " size must be positive" ) { [1 ].each_permutation(-1 ) { } } }
1488
1500
1489
1501
it " returns iterator" do
@@ -1511,6 +1523,20 @@ describe "Array" do
1511
1523
iter.rewind
1512
1524
iter.next.should eq(perms[0 ])
1513
1525
end
1526
+
1527
+ it " returns iterator with reuse = true" do
1528
+ a = [1 , 2 , 3 ]
1529
+ object_ids = Set (UInt64 ).new
1530
+ perms = a.permutations
1531
+ iter = a.each_permutation(reuse: true )
1532
+ perms.each do |perm |
1533
+ b = iter.next.as(Array )
1534
+ object_ids << b.object_id
1535
+ b.should eq(perm)
1536
+ end
1537
+ iter.next.should be_a(Iterator ::Stop )
1538
+ object_ids.size.should eq(1 )
1539
+ end
1514
1540
end
1515
1541
1516
1542
describe " combinations" do
@@ -1542,6 +1568,27 @@ describe "Array" do
1542
1568
sums.should eq([9 ])
1543
1569
end
1544
1570
1571
+ it " does with reuse = true" do
1572
+ sums = [] of Int32
1573
+ object_ids = Set (UInt64 ).new
1574
+ [1 , 2 , 3 ].each_combination(2 , reuse: true ) do |comb |
1575
+ sums << comb.sum
1576
+ object_ids << comb.object_id
1577
+ end
1578
+ sums.should eq([3 , 4 , 5 ])
1579
+ object_ids.size.should eq(1 )
1580
+ end
1581
+
1582
+ it " does with reuse = array" do
1583
+ sums = [] of Int32
1584
+ reuse = [] of Int32
1585
+ [1 , 2 , 3 ].each_combination(2 , reuse: reuse) do |comb |
1586
+ sums << comb.sum
1587
+ comb.should be(reuse)
1588
+ end
1589
+ sums.should eq([3 , 4 , 5 ])
1590
+ end
1591
+
1545
1592
assert { expect_raises(ArgumentError , " size must be positive" ) { [1 ].each_combination(-1 ) { } } }
1546
1593
1547
1594
it " returns iterator" do
@@ -1556,6 +1603,33 @@ describe "Array" do
1556
1603
iter.rewind
1557
1604
iter.next.should eq(combs[0 ])
1558
1605
end
1606
+
1607
+ it " returns iterator with reuse = true" do
1608
+ a = [1 , 2 , 3 , 4 ]
1609
+ combs = a.combinations(2 )
1610
+ object_ids = Set (UInt64 ).new
1611
+ iter = a.each_combination(2 , reuse: true )
1612
+ combs.each do |comb |
1613
+ b = iter.next
1614
+ object_ids << b.object_id
1615
+ b.should eq(comb)
1616
+ end
1617
+ iter.next.should be_a(Iterator ::Stop )
1618
+ object_ids.size.should eq(1 )
1619
+ end
1620
+
1621
+ it " returns iterator with reuse = array" do
1622
+ a = [1 , 2 , 3 , 4 ]
1623
+ reuse = [] of Int32
1624
+ combs = a.combinations(2 )
1625
+ iter = a.each_combination(2 , reuse: reuse)
1626
+ combs.each do |comb |
1627
+ b = iter.next
1628
+ b.should be(reuse)
1629
+ b.should eq(comb)
1630
+ end
1631
+ iter.next.should be_a(Iterator ::Stop )
1632
+ end
1559
1633
end
1560
1634
1561
1635
describe " repeated_combinations" do
@@ -1587,6 +1661,29 @@ describe "Array" do
1587
1661
1588
1662
assert { expect_raises(ArgumentError , " size must be positive" ) { [1 ].each_repeated_combination(-1 ) { } } }
1589
1663
1664
+ it " yields with reuse = true" do
1665
+ sums = [] of Int32
1666
+ object_ids = Set (UInt64 ).new
1667
+ [1 , 2 , 3 ].each_repeated_combination(3 , reuse: true ) do |comb |
1668
+ object_ids << comb.object_id
1669
+ comb.map! & .+ (1 )
1670
+ sums << comb.sum
1671
+ end .should eq([1 , 2 , 3 ])
1672
+ sums.should eq([6 , 7 , 8 , 8 , 9 , 10 , 9 , 10 , 11 , 12 ])
1673
+ object_ids.size.should eq(1 )
1674
+ end
1675
+
1676
+ it " yields with reuse = array" do
1677
+ sums = [] of Int32
1678
+ reuse = [] of Int32
1679
+ [1 , 2 , 3 ].each_repeated_combination(3 , reuse: reuse) do |comb |
1680
+ comb.should be(reuse)
1681
+ comb.map! & .+ (1 )
1682
+ sums << comb.sum
1683
+ end .should eq([1 , 2 , 3 ])
1684
+ sums.should eq([6 , 7 , 8 , 8 , 9 , 10 , 9 , 10 , 11 , 12 ])
1685
+ end
1686
+
1590
1687
it " returns iterator" do
1591
1688
a = [1 , 2 , 3 , 4 ]
1592
1689
combs = a.repeated_combinations(2 )
@@ -1599,6 +1696,33 @@ describe "Array" do
1599
1696
iter.rewind
1600
1697
iter.next.should eq(combs[0 ])
1601
1698
end
1699
+
1700
+ it " returns iterator with reuse = true" do
1701
+ a = [1 , 2 , 3 , 4 ]
1702
+ object_ids = Set (UInt64 ).new
1703
+ combs = a.repeated_combinations(2 )
1704
+ iter = a.each_repeated_combination(2 , reuse: true )
1705
+ combs.each do |comb |
1706
+ b = iter.next
1707
+ object_ids << b.object_id
1708
+ b.should eq(comb)
1709
+ end
1710
+ iter.next.should be_a(Iterator ::Stop )
1711
+ object_ids.size.should eq(1 )
1712
+ end
1713
+
1714
+ it " returns iterator with reuse = array" do
1715
+ a = [1 , 2 , 3 , 4 ]
1716
+ reuse = [] of Int32
1717
+ combs = a.repeated_combinations(2 )
1718
+ iter = a.each_repeated_combination(2 , reuse: reuse)
1719
+ combs.each do |comb |
1720
+ b = iter.next
1721
+ b.should be(reuse)
1722
+ b.should eq(comb)
1723
+ end
1724
+ iter.next.should be_a(Iterator ::Stop )
1725
+ end
1602
1726
end
1603
1727
1604
1728
describe " repeated_permutations" do
@@ -1628,6 +1752,29 @@ describe "Array" do
1628
1752
sums.should eq([6 , 7 , 8 , 7 , 8 , 9 , 8 , 9 , 10 , 7 , 8 , 9 , 8 , 9 , 10 , 9 , 10 , 11 , 8 , 9 , 10 , 9 , 10 , 11 , 10 , 11 , 12 ])
1629
1753
end
1630
1754
1755
+ it " yields with reuse = true" do
1756
+ sums = [] of Int32
1757
+ object_ids = Set (UInt64 ).new
1758
+ [1 , 2 , 3 ].each_repeated_permutation(3 , reuse: true ) do |a |
1759
+ object_ids << a.object_id
1760
+ a.map! & .+ (1 )
1761
+ sums << a.sum
1762
+ end .should eq([1 , 2 , 3 ])
1763
+ sums.should eq([6 , 7 , 8 , 7 , 8 , 9 , 8 , 9 , 10 , 7 , 8 , 9 , 8 , 9 , 10 , 9 , 10 , 11 , 8 , 9 , 10 , 9 , 10 , 11 , 10 , 11 , 12 ])
1764
+ object_ids.size.should eq(1 )
1765
+ end
1766
+
1767
+ it " yields with reuse = array" do
1768
+ sums = [] of Int32
1769
+ reuse = [] of Int32
1770
+ [1 , 2 , 3 ].each_repeated_permutation(3 , reuse: reuse) do |a |
1771
+ a.should be(reuse)
1772
+ a.map! & .+ (1 )
1773
+ sums << a.sum
1774
+ end .should eq([1 , 2 , 3 ])
1775
+ sums.should eq([6 , 7 , 8 , 7 , 8 , 9 , 8 , 9 , 10 , 7 , 8 , 9 , 8 , 9 , 10 , 9 , 10 , 11 , 8 , 9 , 10 , 9 , 10 , 11 , 10 , 11 , 12 ])
1776
+ end
1777
+
1631
1778
assert { expect_raises(ArgumentError , " size must be positive" ) { [1 ].each_repeated_permutation(-1 ) { } } }
1632
1779
end
1633
1780
@@ -1656,6 +1803,17 @@ describe "Array" do
1656
1803
res.should eq([[1 , 3 , 5 ], [1 , 3 , 6 ], [2 , 3 , 5 ], [2 , 3 , 6 ]])
1657
1804
end
1658
1805
1806
+ it " more arrays, reuse = true" do
1807
+ res = [] of Array (Int32 )
1808
+ object_ids = Set (UInt64 ).new
1809
+ Array .each_product([[1 , 2 ], [3 ], [5 , 6 ]], reuse: true ) do |r |
1810
+ object_ids << r.object_id
1811
+ res << r.dup
1812
+ end
1813
+ res.should eq([[1 , 3 , 5 ], [1 , 3 , 6 ], [2 , 3 , 5 ], [2 , 3 , 6 ]])
1814
+ object_ids.size.should eq(1 )
1815
+ end
1816
+
1659
1817
it " with splat" do
1660
1818
res = [] of Array (Int32 | Char )
1661
1819
Array .each_product([1 , 2 ], ['a' , 'b' ]) { |r | res << r }
0 commit comments