1 # frozen_string_literal: false
4 class TestWeakMap < Test::Unit::TestCase
6 @wm = ObjectSpace::WeakMap.new
13 assert_same(x, @wm[k])
14 assert_not_same(x, @wm["FOO".downcase])
20 assert_same(x, @wm[true])
22 assert_same(x, @wm[false])
24 assert_same(x, @wm[nil])
26 assert_same(x, @wm[42])
28 assert_same(x, @wm[:foo])
31 assert_same(true, @wm[x])
33 assert_same(false, @wm[x])
35 assert_same(nil, @wm[x])
37 assert_same(42, @wm[x])
39 assert_same(:foo, @wm[x])
42 def assert_weak_include(m, k, n = 100)
44 return assert_weak_include(m, k, n-1)
49 assert_send([@wm, m, k])
50 assert_not_send([@wm, m, "FOO".downcase])
56 m = __callee__[/test_(.*)/, 1]
59 assert_weak_include(m, k)
62 pend('TODO: failure introduced from 837fd5e494731d7d44786f29e7d6e8c27029806f')
63 assert_not_send([@wm, m, k])
65 alias test_member? test_include?
66 alias test_key? test_include?
72 assert_match(/\A\#<#{@wm.class.name}:[^:]+:\s\#<BasicObject:[^:]*>\s=>\s\#<Object:[^:]*>>\z/,
76 def test_inspect_garbage
81 assert_match(/\A\#<#{@wm.class.name}:[^:]++:(?:\s\d+\s=>\s\#<(?:Object|collected):[^:<>]*+>(?:,|>\z))+/,
86 m = __callee__[/test_(.*)/, 1]
94 @wm.__send__(m) do |k, v|
95 assert_match(/\A(?:foo|bar)\z/, k)
118 assert_match(/\A(?:foo|bar)\z/, k)
138 @wm.each_value do |v|
139 assert_match(/\A(?:foo|bar)\z/, v)
152 m = __callee__[/test_(.*)/, 1]
153 assert_equal(0, @wm.__send__(m))
157 assert_equal(1, @wm.__send__(m))
161 assert_equal(2, @wm.__send__(m))
163 alias test_length test_size
165 def test_frozen_object
166 o = Object.new.freeze
167 assert_nothing_raised(FrozenError) {@wm[o] = 'foo'}
168 assert_nothing_raised(FrozenError) {@wm['foo'] = o}
171 def test_no_memory_leak
172 assert_no_memory_leak([], '', "#{<<~"begin;"}\n#{<<~'end;'}", "[Bug #19398]", rss: true, limit: 1.5, timeout: 60)
175 ObjectSpace::WeakMap.new
180 def test_compaction_bug_19529
187 assert_separately(%w(--disable-gems), <<-'end;')
188 wm = ObjectSpace::WeakMap.new