ruby-changes:48184
From: knu <ko1@a...>
Date: Sun, 22 Oct 2017 00:38:44 +0900 (JST)
Subject: [ruby-changes:48184] knu:r60298 (trunk): Allow a SortedSet to be frozen and still functional [Bug #12091]
knu 2017-10-22 00:38:38 +0900 (Sun, 22 Oct 2017) New Revision: 60298 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60298 Log: Allow a SortedSet to be frozen and still functional [Bug #12091] Modified files: trunk/lib/set.rb trunk/test/test_set.rb Index: test/test_set.rb =================================================================== --- test/test_set.rb (revision 60297) +++ test/test_set.rb (revision 60298) @@ -829,6 +829,45 @@ class TC_SortedSet < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/test_set.rb#L829 set << 42 assert_equal(7, e.size) end + + def test_freeze + orig = set = SortedSet[3,2,1] + assert_equal false, set.frozen? + set << 4 + assert_same orig, set.freeze + assert_equal true, set.frozen? + assert_raise(RuntimeError) { + set << 5 + } + assert_equal 4, set.size + + # https://bugs.ruby-lang.org/issues/12091 + assert_nothing_raised { + assert_equal [1,2,3,4], set.to_a + } + end + + def test_freeze_dup + set1 = SortedSet[1,2,3] + set1.freeze + set2 = set1.dup + + assert_not_predicate set2, :frozen? + assert_nothing_raised { + set2.add 4 + } + end + + def test_freeze_clone + set1 = SortedSet[1,2,3] + set1.freeze + set2 = set1.clone + + assert_predicate set2, :frozen? + assert_raise(RuntimeError) { + set2.add 5 + } + end end class TC_Enumerable < Test::Unit::TestCase Index: lib/set.rb =================================================================== --- lib/set.rb (revision 60297) +++ lib/set.rb (revision 60298) @@ -723,6 +723,11 @@ class SortedSet < Set https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L723 (@keys = @hash.keys).sort! unless @keys @keys end + + def freeze + to_a + super + end END end module_eval { -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/