2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2013-12-09 02:13:38 -05:00
|
|
|
require 'test/unit'
|
|
|
|
|
|
|
|
class TestWeakMap < Test::Unit::TestCase
|
|
|
|
def setup
|
|
|
|
@wm = ObjectSpace::WeakMap.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_map
|
|
|
|
x = Object.new
|
|
|
|
k = "foo"
|
|
|
|
@wm[k] = x
|
|
|
|
assert_same(x, @wm[k])
|
|
|
|
assert_not_same(x, @wm["FOO".downcase])
|
|
|
|
end
|
|
|
|
|
2013-12-10 00:17:19 -05:00
|
|
|
def test_aset_const
|
|
|
|
x = Object.new
|
2019-08-01 14:41:21 -04:00
|
|
|
@wm[true] = x
|
|
|
|
assert_same(x, @wm[true])
|
|
|
|
@wm[false] = x
|
|
|
|
assert_same(x, @wm[false])
|
|
|
|
@wm[nil] = x
|
|
|
|
assert_same(x, @wm[nil])
|
|
|
|
@wm[42] = x
|
|
|
|
assert_same(x, @wm[42])
|
|
|
|
@wm[:foo] = x
|
|
|
|
assert_same(x, @wm[:foo])
|
|
|
|
|
|
|
|
@wm[x] = true
|
|
|
|
assert_same(true, @wm[x])
|
|
|
|
@wm[x] = false
|
|
|
|
assert_same(false, @wm[x])
|
|
|
|
@wm[x] = nil
|
|
|
|
assert_same(nil, @wm[x])
|
|
|
|
@wm[x] = 42
|
|
|
|
assert_same(42, @wm[x])
|
|
|
|
@wm[x] = :foo
|
|
|
|
assert_same(:foo, @wm[x])
|
2013-12-10 00:17:19 -05:00
|
|
|
end
|
|
|
|
|
2018-03-14 07:55:07 -04:00
|
|
|
def assert_weak_include(m, k, n = 100)
|
|
|
|
if n > 0
|
|
|
|
return assert_weak_include(m, k, n-1)
|
|
|
|
end
|
2013-12-13 09:50:00 -05:00
|
|
|
1.times do
|
|
|
|
x = Object.new
|
2013-12-13 08:29:07 -05:00
|
|
|
@wm[k] = x
|
|
|
|
assert_send([@wm, m, k])
|
|
|
|
assert_not_send([@wm, m, "FOO".downcase])
|
2018-03-16 10:12:39 -04:00
|
|
|
x = Object.new
|
2013-12-13 08:29:07 -05:00
|
|
|
end
|
2018-03-14 07:55:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_include?
|
|
|
|
m = __callee__[/test_(.*)/, 1]
|
|
|
|
k = "foo"
|
|
|
|
1.times do
|
|
|
|
assert_weak_include(m, k)
|
|
|
|
end
|
2013-12-09 02:13:38 -05:00
|
|
|
GC.start
|
2022-01-16 08:43:04 -05:00
|
|
|
pend('TODO: failure introduced from 837fd5e494731d7d44786f29e7d6e8c27029806f')
|
2013-12-09 02:13:38 -05:00
|
|
|
assert_not_send([@wm, m, k])
|
|
|
|
end
|
|
|
|
alias test_member? test_include?
|
|
|
|
alias test_key? test_include?
|
|
|
|
|
2013-12-10 00:02:17 -05:00
|
|
|
def test_inspect
|
|
|
|
x = Object.new
|
|
|
|
k = BasicObject.new
|
|
|
|
@wm[k] = x
|
|
|
|
assert_match(/\A\#<#{@wm.class.name}:[^:]+:\s\#<BasicObject:[^:]*>\s=>\s\#<Object:[^:]*>>\z/,
|
|
|
|
@wm.inspect)
|
|
|
|
end
|
|
|
|
|
2021-12-07 05:33:03 -05:00
|
|
|
def test_inspect_garbage
|
|
|
|
1000.times do |i|
|
|
|
|
@wm[i] = Object.new
|
|
|
|
@wm.inspect
|
|
|
|
end
|
|
|
|
assert_match(/\A\#<#{@wm.class.name}:[^:]++:(?:\s\d+\s=>\s\#<(?:Object|collected):[^:<>]*+>(?:,|>\z))+/,
|
|
|
|
@wm.inspect)
|
|
|
|
end
|
|
|
|
|
2013-12-09 02:13:38 -05:00
|
|
|
def test_each
|
|
|
|
m = __callee__[/test_(.*)/, 1]
|
|
|
|
x1 = Object.new
|
|
|
|
k1 = "foo"
|
|
|
|
@wm[k1] = x1
|
|
|
|
x2 = Object.new
|
|
|
|
k2 = "bar"
|
|
|
|
@wm[k2] = x2
|
|
|
|
n = 0
|
|
|
|
@wm.__send__(m) do |k, v|
|
|
|
|
assert_match(/\A(?:foo|bar)\z/, k)
|
|
|
|
case k
|
|
|
|
when /foo/
|
|
|
|
assert_same(k1, k)
|
|
|
|
assert_same(x1, v)
|
|
|
|
when /bar/
|
|
|
|
assert_same(k2, k)
|
|
|
|
assert_same(x2, v)
|
|
|
|
end
|
|
|
|
n += 1
|
|
|
|
end
|
|
|
|
assert_equal(2, n)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_key
|
|
|
|
x1 = Object.new
|
|
|
|
k1 = "foo"
|
|
|
|
@wm[k1] = x1
|
|
|
|
x2 = Object.new
|
|
|
|
k2 = "bar"
|
|
|
|
@wm[k2] = x2
|
|
|
|
n = 0
|
|
|
|
@wm.each_key do |k|
|
|
|
|
assert_match(/\A(?:foo|bar)\z/, k)
|
|
|
|
case k
|
|
|
|
when /foo/
|
|
|
|
assert_same(k1, k)
|
|
|
|
when /bar/
|
|
|
|
assert_same(k2, k)
|
|
|
|
end
|
|
|
|
n += 1
|
|
|
|
end
|
|
|
|
assert_equal(2, n)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_each_value
|
|
|
|
x1 = "foo"
|
|
|
|
k1 = Object.new
|
|
|
|
@wm[k1] = x1
|
|
|
|
x2 = "bar"
|
|
|
|
k2 = Object.new
|
|
|
|
@wm[k2] = x2
|
|
|
|
n = 0
|
|
|
|
@wm.each_value do |v|
|
|
|
|
assert_match(/\A(?:foo|bar)\z/, v)
|
|
|
|
case v
|
|
|
|
when /foo/
|
|
|
|
assert_same(x1, v)
|
|
|
|
when /bar/
|
|
|
|
assert_same(x2, v)
|
|
|
|
end
|
|
|
|
n += 1
|
|
|
|
end
|
|
|
|
assert_equal(2, n)
|
|
|
|
end
|
2013-12-09 02:13:40 -05:00
|
|
|
|
|
|
|
def test_size
|
|
|
|
m = __callee__[/test_(.*)/, 1]
|
|
|
|
assert_equal(0, @wm.__send__(m))
|
|
|
|
x1 = "foo"
|
|
|
|
k1 = Object.new
|
|
|
|
@wm[k1] = x1
|
|
|
|
assert_equal(1, @wm.__send__(m))
|
|
|
|
x2 = "bar"
|
|
|
|
k2 = Object.new
|
|
|
|
@wm[k2] = x2
|
|
|
|
assert_equal(2, @wm.__send__(m))
|
|
|
|
end
|
|
|
|
alias test_length test_size
|
2019-06-22 11:31:16 -04:00
|
|
|
|
|
|
|
def test_frozen_object
|
|
|
|
o = Object.new.freeze
|
|
|
|
assert_nothing_raised(FrozenError) {@wm[o] = 'foo'}
|
|
|
|
assert_nothing_raised(FrozenError) {@wm['foo'] = o}
|
|
|
|
end
|
2013-12-09 02:13:38 -05:00
|
|
|
end
|