From c887fed151eac911f174373faa9f07c6d365a436 Mon Sep 17 00:00:00 2001
From: Piotr Szmielew
Date: Fri, 23 Dec 2016 22:30:14 +0100
Subject: [PATCH] Bugfix: #12970 (Equality of recursive sets fails)
This commit fixes problem with equality of recursive sets.
It's also change behaviour of subset and superset for recursive sets,
to be coheret with set theory approach (recursive set is both self
subset and superset).
Changes:
* lib/set.rb added rehashing inside set when added object is self
* test/test_set.rb added testcases for recursive sets equality,
subset and superset
---
lib/set.rb | 1 +
test/test_set.rb | 24 ++++++++++++++++++++++++
2 files changed, 25 insertions(+)
diff --git a/lib/set.rb b/lib/set.rb
index 43c388c..df5023f 100644
--- a/lib/set.rb
+++ b/lib/set.rb
@@ -329,6 +329,7 @@ def each(&block)
# add many elements at once.
def add(o)
@hash[o] = true
+ @hash.rehash if o == self
self
end
alias << add
diff --git a/test/test_set.rb b/test/test_set.rb
index 07c404a..b4b38ec 100644
--- a/test/test_set.rb
+++ b/test/test_set.rb
@@ -229,6 +229,14 @@ def test_superset?
}
end
+ def test_recursive_superset?
+ recursive_set_1 = Set.new([1, 2]).tap {|s| s << s}
+ recursive_set_2 = Set.new([1, 2]).tap {|s| s << s}
+ assert recursive_set_1.superset?(recursive_set_1)
+ assert recursive_set_2.superset?(recursive_set_1)
+ assert recursive_set_1.superset?(recursive_set_2)
+ end
+
def test_proper_superset?
set = Set[1,2,3]
@@ -287,6 +295,14 @@ def test_subset?
}
end
+ def test_recursive_subset?
+ recursive_set_1 = Set.new([1, 2]).tap {|s| s << s}
+ recursive_set_2 = Set.new([1, 2]).tap {|s| s << s}
+ assert recursive_set_1.subset?(recursive_set_1)
+ assert recursive_set_1.subset?(recursive_set_2)
+ assert recursive_set_2.subset?(recursive_set_1)
+ end
+
def test_proper_subset?
set = Set[1,2,3]
@@ -593,6 +609,14 @@ def test_eq
assert_not_equal(Set[Exception.new,nil], Set[Exception.new,Exception.new], "[ruby-dev:26127]")
end
+ def test_recursive_eq
+ recursive_set_1 = Set.new([1, 2]).tap {|s| s << s}
+ recursive_set_2 = Set.new([1, 2]).tap {|s| s << s}
+
+ assert_equal(recursive_set_1, recursive_set_1)
+ assert_equal(recursive_set_1, recursive_set_2)
+ end
+
def test_classify
set = Set.new(1..10)
ret = set.classify { |i| i % 3 }
--
2.9.0