1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
require 'test/unit'
require_relative 'envutil'
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
def test_include?
m = __callee__[/test_(.*)/, 1]
x = Object.new
k = "foo"
@wm[k] = x
assert_send([@wm, m, k])
assert_not_send([@wm, m, "FOO".downcase])
x = nil
GC.start
assert_not_send([@wm, m, k])
end
alias test_member? test_include?
alias test_key? test_include?
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
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
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
end
|