From c5d1fd68f7c062a03f44e590699e5ff957fe06be Mon Sep 17 00:00:00 2001 From: knu Date: Tue, 3 Jun 2008 11:06:38 +0000 Subject: * enumerator.c (enumerator_init_copy): Take care of initialize_copy as well as initialize. * test/ruby/test_enumerator.rb: Pull in the test suite for enumerator from trunk. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8_7@16794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 8 ++++ enumerator.c | 7 ++- test/ruby/test_enumerator.rb | 105 +++++++++++++++++++++++++++++++++++++++++++ version.h | 2 +- 4 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 test/ruby/test_enumerator.rb diff --git a/ChangeLog b/ChangeLog index 16fb072edd..701c7ce250 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +Tue Jun 3 19:33:22 2008 Akinori MUSHA + + * enumerator.c (enumerator_init_copy): Take care of + initialize_copy as well as initialize. + + * test/ruby/test_enumerator.rb: Pull in the test suite for + enumerator from trunk. + Tue Jun 3 12:51:57 2008 Akinori MUSHA * enumerator.c (enumerator_allocate, enumerator_ptr): Properly diff --git a/enumerator.c b/enumerator.c index 8d5061d308..f95d931b62 100644 --- a/enumerator.c +++ b/enumerator.c @@ -293,7 +293,12 @@ enumerator_init_copy(obj, orig) struct enumerator *ptr0, *ptr1; ptr0 = enumerator_ptr(orig); - ptr1 = enumerator_ptr(obj); + + Data_Get_Struct(obj, struct enumerator, ptr1); + + if (!ptr1) { + rb_raise(rb_eArgError, "unallocated enumerator"); + } ptr1->obj = ptr0->obj; ptr1->meth = ptr0->meth; diff --git a/test/ruby/test_enumerator.rb b/test/ruby/test_enumerator.rb new file mode 100644 index 0000000000..2625791129 --- /dev/null +++ b/test/ruby/test_enumerator.rb @@ -0,0 +1,105 @@ +require 'test/unit' + +class TestEnumerator < Test::Unit::TestCase + def setup + @obj = Object.new + class << @obj + include Enumerable + def foo(*a) + a.each {|x| yield x } + end + end + end + + def enum_test obj + i = 0 + obj.map{|e| + e + }.sort + end + + def test_iterators + assert_equal [0, 1, 2], enum_test(3.times) + assert_equal ["x", "y", "z"], enum_test(["z", "y", "x"].each) + assert_equal [["x", 1], ["y", 2]], enum_test({"y"=>2, "x"=>1}) + end + + ## Enumerator as Iterator + + def test_next + e = 3.times + 3.times{|i| + assert_equal i, e.next + } + assert_raise(StopIteration){e.next} + end + + def test_loop + e = 3.times + i = 0 + loop{ + assert_equal(i, e.next) + i += 1 + } + end + + def test_nested_itaration + def (o = Object.new).each + yield :ok1 + yield [:ok2, :x].each.next + end + e = o.to_enum + assert_equal :ok1, e.next + assert_equal :ok2, e.next + assert_raise(StopIteration){e.next} + end + + + def test_initialize + assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).to_a) + assert_equal([1, 2, 3], Enumerable::Enumerator.new(@obj, :foo, 1, 2, 3).to_a) + assert_raise(ArgumentError) { Enumerable::Enumerator.new } + end + + def test_initialize_copy + assert_equal([1, 2, 3], @obj.to_enum(:foo, 1, 2, 3).dup.to_a) + e = @obj.to_enum(:foo, 1, 2, 3) + assert_nothing_raised { assert_equal(1, e.next) } + #assert_raise(TypeError) { e.dup } + end + + def test_gc + assert_nothing_raised do + 1.times do + foo = [1,2,3].to_enum + GC.start + end + GC.start + end + end + + def test_slice + assert_equal([[1,2,3],[4,5,6],[7,8,9],[10]], (1..10).each_slice(3).to_a) + end + + def test_cons + a = [[1,2,3], [2,3,4], [3,4,5], [4,5,6], [5,6,7], [6,7,8], [7,8,9], [8,9,10]] + assert_equal(a, (1..10).each_cons(3).to_a) + end + + def test_with_index + assert_equal([[1,0],[2,1],[3,2]], @obj.to_enum(:foo, 1, 2, 3).with_index.to_a) + end + + def test_next_rewind + e = @obj.to_enum(:foo, 1, 2, 3) + assert_equal(1, e.next) + assert_equal(2, e.next) + e.rewind + assert_equal(1, e.next) + assert_equal(2, e.next) + assert_equal(3, e.next) + assert_raise(StopIteration) { e.next } + end +end + diff --git a/version.h b/version.h index 9866b0878b..bf0c6ba1e5 100644 --- a/version.h +++ b/version.h @@ -2,7 +2,7 @@ #define RUBY_RELEASE_DATE "2008-06-03" #define RUBY_VERSION_CODE 187 #define RUBY_RELEASE_CODE 20080603 -#define RUBY_PATCHLEVEL 3 +#define RUBY_PATCHLEVEL 4 #define RUBY_VERSION_MAJOR 1 #define RUBY_VERSION_MINOR 8 -- cgit v1.2.3