summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-11-27 02:54:29 +0000
committeryugui <yugui@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-11-27 02:54:29 +0000
commit6e6417c8d38afced7eb7c10781f20f7b19ebbc89 (patch)
treea0e48c71d1584691383297d9c7f46cdc300ad8a3
parent9abf98bbe47b65dbc058b8ab752aa81ba8c7754b (diff)
merges r25010 from trunk into ruby_1_9_1 and adds tests for it.
-- * struct.c (rb_struct_equal, rb_struct_eql): Handle comparison of recursive structures [ruby-core:24759] * range.c (range_eq, range_eql): ditto for ranges -- test for r25010 * test/ruby/test_struct.rb (TestStruct#test_comparison_when_recursive): new test. * test/ruby/test_range.rb (TestRange#test_comparison_when_recursive): new test. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_9_1@25943 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r--ChangeLog7
-rw-r--r--range.c49
-rw-r--r--struct.c38
-rw-r--r--test/ruby/test_range.rb34
-rw-r--r--test/ruby/test_struct.rb30
-rw-r--r--version.h2
6 files changed, 128 insertions, 32 deletions
diff --git a/ChangeLog b/ChangeLog
index edbf2e1f12..b15c38d79d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+Sun Sep 20 11:11:34 2009 Marc-Andre Lafortune <ruby-core@marc-andre.ca>
+
+ * struct.c (rb_struct_equal, rb_struct_eql): Handle comparison of
+ recursive structures [ruby-core:24759]
+
+ * range.c (range_eq, range_eql): ditto for ranges
+
Fri Sep 18 23:51:17 2009 Nobuyoshi Nakada <nobu@ruby-lang.org>
* marshal.c (r_object0): entry regexp object before its encoding
diff --git a/range.c b/range.c
index 1e66bba856..1e68dbaff9 100644
--- a/range.c
+++ b/range.c
@@ -105,6 +105,20 @@ range_exclude_end_p(VALUE range)
return EXCL(range) ? Qtrue : Qfalse;
}
+static VALUE
+recursive_equal(VALUE range, VALUE obj, int recur)
+{
+ if (recur) return Qtrue; /* Subtle! */
+ if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
+ return Qfalse;
+ if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
+ return Qfalse;
+
+ if (EXCL(range) != EXCL(obj))
+ return Qfalse;
+ return Qtrue;
+}
+
/*
* call-seq:
@@ -128,15 +142,7 @@ range_eq(VALUE range, VALUE obj)
if (!rb_obj_is_kind_of(obj, rb_cRange))
return Qfalse;
- if (!rb_equal(RANGE_BEG(range), RANGE_BEG(obj)))
- return Qfalse;
- if (!rb_equal(RANGE_END(range), RANGE_END(obj)))
- return Qfalse;
-
- if (EXCL(range) != EXCL(obj))
- return Qfalse;
-
- return Qtrue;
+ return rb_exec_recursive_paired(recursive_equal, range, obj, obj);
}
static int
@@ -168,6 +174,20 @@ r_le(VALUE a, VALUE b)
}
+static VALUE
+recursive_eql(VALUE range, VALUE obj, int recur)
+{
+ if (recur) return Qtrue; /* Subtle! */
+ if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
+ return Qfalse;
+ if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
+ return Qfalse;
+
+ if (EXCL(range) != EXCL(obj))
+ return Qfalse;
+ return Qtrue;
+}
+
/*
* call-seq:
* rng.eql?(obj) => true or false
@@ -189,16 +209,7 @@ range_eql(VALUE range, VALUE obj)
return Qtrue;
if (!rb_obj_is_kind_of(obj, rb_cRange))
return Qfalse;
-
- if (!rb_eql(RANGE_BEG(range), RANGE_BEG(obj)))
- return Qfalse;
- if (!rb_eql(RANGE_END(range), RANGE_END(obj)))
- return Qfalse;
-
- if (EXCL(range) != EXCL(obj))
- return Qfalse;
-
- return Qtrue;
+ return rb_exec_recursive_paired(recursive_eql, range, obj, obj);
}
/*
diff --git a/struct.c b/struct.c
index 3d876bb216..79403a5fb6 100644
--- a/struct.c
+++ b/struct.c
@@ -760,6 +760,18 @@ rb_struct_select(int argc, VALUE *argv, VALUE s)
return result;
}
+static VALUE
+recursive_equal(VALUE s, VALUE s2, int recur)
+{
+ long i;
+
+ if (recur) return Qtrue; /* Subtle! */
+ for (i=0; i<RSTRUCT_LEN(s); i++) {
+ if (!rb_equal(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) return Qfalse;
+ }
+ return Qtrue;
+}
+
/*
* call-seq:
* struct == other_struct => true or false
@@ -780,8 +792,6 @@ rb_struct_select(int argc, VALUE *argv, VALUE s)
static VALUE
rb_struct_equal(VALUE s, VALUE s2)
{
- long i;
-
if (s == s2) return Qtrue;
if (TYPE(s2) != T_STRUCT) return Qfalse;
if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
@@ -789,10 +799,7 @@ rb_struct_equal(VALUE s, VALUE s2)
rb_bug("inconsistent struct"); /* should never happen */
}
- for (i=0; i<RSTRUCT_LEN(s); i++) {
- if (!rb_equal(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) return Qfalse;
- }
- return Qtrue;
+ return rb_exec_recursive_paired(recursive_equal, s, s2, s2);
}
/*
@@ -817,6 +824,18 @@ rb_struct_hash(VALUE s)
return LONG2FIX(h);
}
+static VALUE
+recursive_eql(VALUE s, VALUE s2, int recur)
+{
+ long i;
+
+ if (recur) return Qtrue; /* Subtle! */
+ for (i=0; i<RSTRUCT_LEN(s); i++) {
+ if (!rb_eql(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) return Qfalse;
+ }
+ return Qtrue;
+}
+
/*
* code-seq:
* struct.eql?(other) => true or false
@@ -828,8 +847,6 @@ rb_struct_hash(VALUE s)
static VALUE
rb_struct_eql(VALUE s, VALUE s2)
{
- long i;
-
if (s == s2) return Qtrue;
if (TYPE(s2) != T_STRUCT) return Qfalse;
if (rb_obj_class(s) != rb_obj_class(s2)) return Qfalse;
@@ -837,10 +854,7 @@ rb_struct_eql(VALUE s, VALUE s2)
rb_bug("inconsistent struct"); /* should never happen */
}
- for (i=0; i<RSTRUCT_LEN(s); i++) {
- if (!rb_eql(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) return Qfalse;
- }
- return Qtrue;
+ return rb_exec_recursive_paired(recursive_eql, s, s2, s2);
}
/*
diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb
index 9dfd29012a..dabe7fab87 100644
--- a/test/ruby/test_range.rb
+++ b/test/ruby/test_range.rb
@@ -1,4 +1,5 @@
require 'test/unit'
+require 'timeout'
class TestRange < Test::Unit::TestCase
def test_range_string
@@ -280,4 +281,37 @@ class TestRange < Test::Unit::TestCase
o.instance_eval { initialize(o, 1) }
assert_equal("(... .. ...)..1", o.inspect)
end
+
+ def test_comparison_when_recursive
+ x = CyclicRange.allocate; x.send(:initialize, x, 1)
+ y = CyclicRange.allocate; y.send(:initialize, y, 1)
+ Timeout.timeout(1) {
+ assert x == y
+ assert x.eql? y
+ }
+
+ z = CyclicRange.allocate; z.send(:initialize, z, :another)
+ Timeout.timeout(1) {
+ assert x != z
+ assert !x.eql?(z)
+ }
+
+ x = CyclicRange.allocate
+ y = CyclicRange.allocate
+ x.send(:initialize, y, 1)
+ y.send(:initialize, x, 1)
+ Timeout.timeout(1) {
+ assert x == y
+ assert x.eql?(y)
+ }
+
+ x = CyclicRange.allocate
+ z = CyclicRange.allocate
+ x.send(:initialize, z, 1)
+ z.send(:initialize, x, :other)
+ Timeout.timeout(1) {
+ assert x != z
+ assert !x.eql?(z)
+ }
+ end
end
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb
index 1b68429d0c..688012a1fd 100644
--- a/test/ruby/test_struct.rb
+++ b/test/ruby/test_struct.rb
@@ -1,4 +1,5 @@
require 'test/unit'
+require 'timeout'
class TestStruct < Test::Unit::TestCase
def test_struct
@@ -219,4 +220,33 @@ class TestStruct < Test::Unit::TestCase
a = struct_test.new(42)
assert_equal("#<struct Struct::R\u{e9}sum\u{e9} r\u{e9}sum\u{e9}=42>", a.inspect, '[ruby-core:24849]')
end
+
+ def test_comparison_when_recursive
+ klass1 = Struct.new(:a, :b, :c)
+
+ x = klass1.new(1, 2, nil); x.c = x
+ y = klass1.new(1, 2, nil); y.c = y
+ Timeout.timeout(1) {
+ assert x == y
+ assert x.eql? y
+ }
+
+ z = klass1.new(:something, :other, nil); z.c = z
+ Timeout.timeout(1) {
+ assert x != z
+ assert !x.eql?(z)
+ }
+
+ x.c = y; y.c = x
+ Timeout.timeout(1) {
+ assert x == y
+ assert x.eql?(y)
+ }
+
+ x.c = z; z.c = x
+ Timeout.timeout(1) {
+ assert x != z
+ assert !x.eql?(z)
+ }
+ end
end
diff --git a/version.h b/version.h
index bb6fca4c4e..f67ea54e42 100644
--- a/version.h
+++ b/version.h
@@ -1,5 +1,5 @@
#define RUBY_VERSION "1.9.1"
-#define RUBY_PATCHLEVEL 344
+#define RUBY_PATCHLEVEL 345
#define RUBY_VERSION_MAJOR 1
#define RUBY_VERSION_MINOR 9
#define RUBY_VERSION_TEENY 1