[前][次][番号順一覧][スレッド一覧]

ruby-changes:51613

From: nagachika <ko1@a...>
Date: Mon, 2 Jul 2018 17:30:42 +0900 (JST)
Subject: [ruby-changes:51613] nagachika:r63824 (ruby_2_5): merge revision(s) 62731, 62735: [Backport #14495]

nagachika	2018-07-02 17:30:36 +0900 (Mon, 02 Jul 2018)

  New Revision: 63824

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63824

  Log:
    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@r...>
    
    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.

  Modified directories:
    branches/ruby_2_5/
  Modified files:
    branches/ruby_2_5/enumerator.c
    branches/ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb
    branches/ruby_2_5/test/ruby/test_enumerator.rb
    branches/ruby_2_5/test/ruby/test_lazy_enumerator.rb
    branches/ruby_2_5/version.h
Index: ruby_2_5/test/ruby/test_enumerator.rb
===================================================================
--- ruby_2_5/test/ruby/test_enumerator.rb	(revision 63823)
+++ ruby_2_5/test/ruby/test_enumerator.rb	(revision 63824)
@@ -657,8 +657,9 @@ class TestEnumerator < Test::Unit::TestC https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_enumerator.rb#L657
   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
 
Index: ruby_2_5/test/ruby/test_lazy_enumerator.rb
===================================================================
--- ruby_2_5/test/ruby/test_lazy_enumerator.rb	(revision 63823)
+++ ruby_2_5/test/ruby/test_lazy_enumerator.rb	(revision 63824)
@@ -569,4 +569,13 @@ EOS https://github.com/ruby/ruby/blob/trunk/ruby_2_5/test/ruby/test_lazy_enumerator.rb#L569
       [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
Index: ruby_2_5/enumerator.c
===================================================================
--- ruby_2_5/enumerator.c	(revision 63823)
+++ ruby_2_5/enumerator.c	(revision 63824)
@@ -2233,27 +2233,35 @@ lazy_drop_while(VALUE obj) https://github.com/ruby/ruby/blob/trunk/ruby_2_5/enumerator.c#L2233
 }
 
 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) https://github.com/ruby/ruby/blob/trunk/ruby_2_5/enumerator.c#L2269
 {
     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);
 }
 
Index: ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb
===================================================================
--- ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb	(revision 63823)
+++ ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb	(revision 63824)
@@ -3,6 +3,26 @@ require File.expand_path('../fixtures/cl https://github.com/ruby/ruby/blob/trunk/ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb#L3
 
 ruby_version_is '2.4' do
   describe 'Enumerator::Lazy#uniq' do
+    context 'without block' do
+      before :each do
+        @lazy = [0, 1, 0, 1].to_enum.lazy.uniq
+      end
+
+      it 'returns a lazy enumerator' do
+        @lazy.should be_an_instance_of(Enumerator::Lazy)
+        @lazy.force.should == [0, 1]
+      end
+
+      it 'return same value after rewind' do
+        @lazy.force.should == [0, 1]
+        @lazy.force.should == [0, 1]
+      end
+
+      it 'sets the size to nil' do
+        @lazy.size.should == nil
+      end
+    end
+
     context 'when yielded with an argument' do
       before :each do
         @lazy = [0, 1, 2, 3].to_enum.lazy.uniq(&:even?)
@@ -13,6 +33,11 @@ ruby_version_is '2.4' do https://github.com/ruby/ruby/blob/trunk/ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb#L33
         @lazy.force.should == [0, 1]
       end
 
+      it 'return same value after rewind' do
+        @lazy.force.should == [0, 1]
+        @lazy.force.should == [0, 1]
+      end
+
       it 'sets the size to nil' do
         @lazy.size.should == nil
       end
@@ -31,6 +56,12 @@ ruby_version_is '2.4' do https://github.com/ruby/ruby/blob/trunk/ruby_2_5/spec/ruby/core/enumerator/lazy/uniq_spec.rb#L56
         @lazy = enum.lazy
       end
 
+      it 'return same value after rewind' do
+        enum = @lazy.uniq { |_, label| label.downcase }
+        enum.force.should == [[0, 'foo'], [2, 'bar']]
+        enum.force.should == [[0, 'foo'], [2, 'bar']]
+      end
+
       it 'returns all yield arguments as an array' do
         @lazy.uniq { |_, label| label.downcase }.force.should == [[0, 'foo'], [2, 'bar']]
       end
Index: ruby_2_5/version.h
===================================================================
--- ruby_2_5/version.h	(revision 63823)
+++ ruby_2_5/version.h	(revision 63824)
@@ -1,6 +1,6 @@ https://github.com/ruby/ruby/blob/trunk/ruby_2_5/version.h#L1
 #define RUBY_VERSION "2.5.2"
 #define RUBY_RELEASE_DATE "2018-07-02"
-#define RUBY_PATCHLEVEL 62
+#define RUBY_PATCHLEVEL 63
 
 #define RUBY_RELEASE_YEAR 2018
 #define RUBY_RELEASE_MONTH 7
Index: ruby_2_5
===================================================================
--- ruby_2_5	(revision 63823)
+++ ruby_2_5	(revision 63824)

Property changes on: ruby_2_5
___________________________________________________________________
Modified: svn:mergeinfo
## -0,0 +0,1 ##
   Merged /trunk:r62731,62735

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]