summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-27 13:47:40 +0000
committerusa <usa@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2018-08-27 13:47:40 +0000
commit7d9572819bfd3932f206549576fbf931551947a6 (patch)
tree20963c93fc1f2089fc73ab6919b2f4f4b80fc276
parent8b310ae75dc6b189e25909c6bf96a6f575382088 (diff)
merge revision(s) 62731,62735: [Backport #14495]
Bug Fix Enumerator::Lazy#uniq state for multiple call * enumerator.c (lazy_uniq_i): create new hash for each calls. [Fix GH-1820] Currently 2.5.0-preview1 :001 > arr = (0..100).lazy.uniq{|i| i % 10} => #<Enumerator::Lazy: #<Enumerator::Lazy: 0..100>:uniq> 2.5.0-preview1 :002 > arr.to_a => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 2.5.0-preview1 :003 > arr.to_a => [] Expected arr.to_a to always return same output From: Anmol Chopra <anmolchopra@rocketbox.in> test_enumerator.rb: duplicate assertions * test/ruby/test_enumerator.rb (test_uniq): remove assertions which ared duplicate of lazy enumerator tests in test_lazy_enumerator.rb. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_2_4@64558 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--enumerator.c21
-rw-r--r--test/ruby/test_enumerator.rb5
-rw-r--r--test/ruby/test_lazy_enumerator.rb9
-rw-r--r--version.h8
4 files changed, 30 insertions, 13 deletions
diff --git a/enumerator.c b/enumerator.c
index a996566b7b..c73d612abe 100644
--- a/enumerator.c
+++ b/enumerator.c
@@ -2233,27 +2233,35 @@ lazy_drop_while(VALUE obj)
}
static VALUE
-lazy_uniq_i(VALUE i, VALUE hash, int argc, const VALUE *argv, VALUE yielder)
+lazy_uniq_i(VALUE i, int argc, const VALUE *argv, VALUE yielder)
{
+ VALUE hash;
+
+ hash = rb_attr_get(yielder, id_memo);
+ if (NIL_P(hash)) {
+ hash = rb_obj_hide(rb_hash_new());
+ rb_ivar_set(yielder, id_memo, hash);
+ }
+
if (rb_hash_add_new_element(hash, i, Qfalse))
return Qnil;
return rb_funcallv(yielder, id_yield, argc, argv);
}
static VALUE
-lazy_uniq_func(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
+lazy_uniq_func(RB_BLOCK_CALL_FUNC_ARGLIST(i, m))
{
VALUE yielder = (--argc, *argv++);
i = rb_enum_values_pack(argc, argv);
- return lazy_uniq_i(i, hash, argc, argv, yielder);
+ return lazy_uniq_i(i, argc, argv, yielder);
}
static VALUE
-lazy_uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, hash))
+lazy_uniq_iter(RB_BLOCK_CALL_FUNC_ARGLIST(i, m))
{
VALUE yielder = (--argc, *argv++);
i = rb_yield_values2(argc, argv);
- return lazy_uniq_i(i, hash, argc, argv, yielder);
+ return lazy_uniq_i(i, argc, argv, yielder);
}
static VALUE
@@ -2261,9 +2269,8 @@ lazy_uniq(VALUE obj)
{
rb_block_call_func *const func =
rb_block_given_p() ? lazy_uniq_iter : lazy_uniq_func;
- VALUE hash = rb_obj_hide(rb_hash_new());
return lazy_set_method(rb_block_call(rb_cLazy, id_new, 1, &obj,
- func, hash),
+ func, 0),
0, 0);
}
diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb
index 51f69be37d..76a2b8ab18 100644
--- a/test/ruby/test_enumerator.rb
+++ b/test/ruby/test_enumerator.rb
@@ -657,8 +657,9 @@ class TestEnumerator < Test::Unit::TestCase
end
def test_uniq
- assert_equal([1, 2, 3, 4, 5, 10],
- (1..Float::INFINITY).lazy.uniq{|x| (x**2) % 10 }.first(6))
+ u = [0, 1, 0, 1].to_enum.lazy.uniq
+ assert_equal([0, 1], u.force)
+ assert_equal([0, 1], u.force)
end
end
diff --git a/test/ruby/test_lazy_enumerator.rb b/test/ruby/test_lazy_enumerator.rb
index 56890f4b6e..e98d2f48bd 100644
--- a/test/ruby/test_lazy_enumerator.rb
+++ b/test/ruby/test_lazy_enumerator.rb
@@ -569,4 +569,13 @@ EOS
[1, 2, 3].lazy.map(&:undefined).map(&:to_s).force
end
end
+
+ def test_uniq
+ u = (1..Float::INFINITY).lazy.uniq do |x|
+ raise "too big" if x > 10000
+ (x**2) % 10
+ end
+ assert_equal([1, 2, 3, 4, 5, 10], u.first(6))
+ assert_equal([1, 2, 3, 4, 5, 10], u.first(6))
+ end
end
diff --git a/version.h b/version.h
index 45b5e25a7d..59f7433613 100644
--- a/version.h
+++ b/version.h
@@ -1,10 +1,10 @@
#define RUBY_VERSION "2.4.5"
-#define RUBY_RELEASE_DATE "2018-07-31"
-#define RUBY_PATCHLEVEL 315
+#define RUBY_RELEASE_DATE "2018-08-27"
+#define RUBY_PATCHLEVEL 316
#define RUBY_RELEASE_YEAR 2018
-#define RUBY_RELEASE_MONTH 7
-#define RUBY_RELEASE_DAY 31
+#define RUBY_RELEASE_MONTH 8
+#define RUBY_RELEASE_DAY 27
#include "ruby/version.h"