summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorMarc-Andre Lafortune <github@marc-andre.ca>2021-03-15 23:51:13 -0400
committerMarc-Andre Lafortune <github@marc-andre.ca>2021-03-18 07:34:40 -0400
commitd094c3ef046aba0bb99fd08bcbc72ff87216e736 (patch)
tree08986627bc91061cf33259de1d8059553f289d3c /test
parent85f99f4b715a5954124d5014002c16652995b128 (diff)
Avoid rehashing in Hash#select/reject [Bug #16996]
Diffstat (limited to 'test')
-rw-r--r--test/ruby/test_hash.rb25
1 files changed, 25 insertions, 0 deletions
diff --git a/test/ruby/test_hash.rb b/test/ruby/test_hash.rb
index 62d8b3f836..25dfaf4f64 100644
--- a/test/ruby/test_hash.rb
+++ b/test/ruby/test_hash.rb
@@ -122,6 +122,31 @@ class TestHash < Test::Unit::TestCase
assert_equal set2, set2.dup
end
+ def assert_hash_does_not_rehash
+ obj = Object.new
+ class << obj
+ attr_accessor :hash_calls
+ def hash
+ @hash_calls += 1
+ super
+ end
+ end
+ obj.hash_calls = 0
+ hash = {obj => 42}
+ assert_equal(1, obj.hash_calls)
+ yield hash
+ assert_equal(1, obj.hash_calls)
+ end
+
+ def test_select_reject_will_not_rehash
+ assert_hash_does_not_rehash do |hash|
+ hash.select { true }
+ end
+ assert_hash_does_not_rehash do |hash|
+ hash.reject { false }
+ end
+ end
+
def test_s_AREF
h = @cls["a" => 100, "b" => 200]
assert_equal(100, h['a'])