Project

General

Profile

Bug #13648 ยป fix.patch

A test case and fix - akihikodaki (Akihiko Odaki), 06/09/2017 04:09 PM

View differences:

enumerator.c
1447 1447
#define LAZY_MEMO_BREAK_P(memo) ((memo)->memo_flags & LAZY_MEMO_BREAK)
1448 1448
#define LAZY_MEMO_PACKED_P(memo) ((memo)->memo_flags & LAZY_MEMO_PACKED)
1449 1449
#define LAZY_MEMO_SET_BREAK(memo) ((memo)->memo_flags |= LAZY_MEMO_BREAK)
1450
#define LAZY_MEMO_SET_VALUE(memo, value) MEMO_V2_SET(memo, value)
1450
#define LAZY_MEMO_SET_VALUE(memo, value, packed) \
1451
    ({ \
1452
	MEMO_V2_SET(memo, value); \
1453
	if (packed) (memo)->memo_flags |= LAZY_MEMO_PACKED; \
1454
	else (memo)->memo_flags &= ~LAZY_MEMO_PACKED; \
1455
    })
1451 1456

  
1452 1457
static VALUE
1453 1458
lazy_init_yielder(VALUE val, VALUE m, int argc, VALUE *argv)
......
1742 1747
lazy_map_proc(VALUE proc_entry, struct MEMO *result, VALUE memos, long memo_index)
1743 1748
{
1744 1749
    VALUE value = lazyenum_yield_values(proc_entry, result);
1745
    LAZY_MEMO_SET_VALUE(result, value);
1750
    LAZY_MEMO_SET_VALUE(result, value, FALSE);
1746 1751
    return result;
1747 1752
}
1748 1753

  
......
1912 1917

  
1913 1918
    if (!RTEST(chain)) return 0;
1914 1919
    value = rb_proc_call_with_block(entry->proc, 1, &(result->memo_value), Qnil);
1915
    LAZY_MEMO_SET_VALUE(result, value);
1920
    LAZY_MEMO_SET_VALUE(result, value, FALSE);
1916 1921

  
1917 1922
    return result;
1918 1923
}
test/ruby/test_lazy_enumerator.rb
14 14

  
15 15
    def each(*args)
16 16
      @args = args
17
      @enum.each {|i| @current = i; yield i}
17
      @enum.each do |v|
18
        @current = v
19
        if v.is_a? Enumerable
20
          yield *v
21
        else
22
          yield v
23
        end
24
      end
18 25
    end
19 26
  end
20 27

  
......
100 107
    assert_equal(1, a.current)
101 108
  end
102 109

  
110
  def test_map_packed_nested
111
    a = Step.new([[1, 2]])
112
    assert_equal([[[1, 2]]], a.lazy.map {|*args| args}.map {|*args| args}.to_a)
113
  end
114

  
103 115
  def test_flat_map
104 116
    a = Step.new(1..3)
105 117
    assert_equal(2, a.flat_map {|x| [x * 2]}.first)