From d29143b99ea0fb2ea4821a804d489447cf92ed68 Mon Sep 17 00:00:00 2001 From: knu Date: Sat, 7 Sep 2002 10:48:14 +0000 Subject: [PATCH] * lib/set.rb: Disallow Set.new(false). Add even more tests. [Submitted by: "Christoph" ] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@2814 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 5 +++++ lib/set.rb | 37 +++++++++++++++++++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index e41de262d5..195d1cc7d8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sat Sep 7 19:46:57 2002 Akinori MUSHA + + * lib/set.rb: Disallow Set.new(false). Add even more tests. + [Submitted by: "Christoph" ] + Sat Sep 7 19:23:56 2002 Akinori MUSHA * lib/set.rb: Fix a bug in flatten()'s recursive set detection. diff --git a/lib/set.rb b/lib/set.rb index 195d6a6938..374b43da3f 100644 --- a/lib/set.rb +++ b/lib/set.rb @@ -188,10 +188,10 @@ class Set def initialize(enum = nil) @hash = {} - if enum - enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable" - enum.each { |o| @hash[o] = true } - end + enum.nil? and return + + enum.is_a?(Enumerable) or raise ArgumentError, "value must be enumerable" + enum.each { |o| @hash[o] = true } end def dup @@ -464,6 +464,9 @@ class TC_Set < Test::Unit::TestCase Set.new('a'..'c') Set.new('XYZ') } + assert_raises(ArgumentError) { + Set.new(false) + } assert_raises(ArgumentError) { Set.new(1) } @@ -577,6 +580,23 @@ class TC_Set < Test::Unit::TestCase assert_raises(ArgumentError) { set1.flatten! } + + # test5; miscellaneus + empty = Set[] + set = Set[Set[empty, "a"],Set[empty, "b"]] + + assert_nothing_raised { + set.flatten + } + + set1 = empty.merge(Set["no_more", set]) + + assert_nil(Set.new(0..31).flatten!) + + x = Set[Set[],Set[1,2]].flatten! + y = Set[1,2] + + assert_equal(x, y) end def test_include? @@ -726,6 +746,15 @@ class TC_Set < Test::Unit::TestCase assert_equal(set1, set1) assert_equal(set1, set2) assert_not_equal(Set[1], [1]) + + set1 = Class.new(Set)["a", "b"] + set2 = Set["a", "b", set1] + set1 = set1.add(set1.clone) + + assert_equal(set1, set2) + assert_equal(set2, set1) + assert_equal(set2, set2.clone) + assert_equal(set1.clone, set1) end # def test_hash