From 1c8a68ae210b43bc3c2a09850192307e07ec22cc Mon Sep 17 00:00:00 2001 From: Quinton Miller Date: Fri, 15 Dec 2023 22:41:02 +0800 Subject: [PATCH] Fix `Indexable#each_repeated_combination(n)` when `n > size` (#14092) --- spec/std/indexable_spec.cr | 6 ++++++ src/indexable.cr | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/std/indexable_spec.cr b/spec/std/indexable_spec.cr index f7c39f7ba485..75a20f14e521 100644 --- a/spec/std/indexable_spec.cr +++ b/spec/std/indexable_spec.cr @@ -1,4 +1,5 @@ require "spec" +require "spec/helpers/iterate" private class SafeIndexable include Indexable(Int32) @@ -811,5 +812,10 @@ describe Indexable do 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 end diff --git a/src/indexable.cr b/src/indexable.cr index 4c69ee3015f4..3725c1552e38 100644 --- a/src/indexable.cr +++ b/src/indexable.cr @@ -1483,7 +1483,7 @@ module Indexable(T) @copy = array.dup @indices = Array.new(@size, 0) @pool = @indices.map { |i| @copy[i] } - @stop = @size > @n + @stop = false @i = @size - 1 @first = true end