mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
refactoring a test code.
make a test more clear.
This commit is contained in:
parent
d247dedade
commit
6081ba4a87
1 changed files with 53 additions and 21 deletions
|
@ -417,32 +417,64 @@ assert_equal 'no _dump_data is defined for class Thread', %q{
|
||||||
}
|
}
|
||||||
|
|
||||||
# send sharable and unsharable objects
|
# send sharable and unsharable objects
|
||||||
assert_equal "[[[1, true], [:sym, true], [:xyzzy, true], [\"frozen\", true], " \
|
assert_equal "ok", %q{
|
||||||
"[(3/1), true], [(3+4i), true], [/regexp/, true], [C, true]], " \
|
echo_ractor = Ractor.new do
|
||||||
"[[\"mutable str\", false], [[:array], false], [{:hash=>true}, false]]]", %q{
|
loop do
|
||||||
r = Ractor.new do
|
v = Ractor.recv
|
||||||
while v = Ractor.recv
|
|
||||||
Ractor.yield v
|
Ractor.yield v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class C
|
class C; end
|
||||||
|
module M; end
|
||||||
|
|
||||||
|
shareable_objects = [
|
||||||
|
true,
|
||||||
|
false,
|
||||||
|
nil,
|
||||||
|
1,
|
||||||
|
1.1, # Float
|
||||||
|
1+2r, # Rational
|
||||||
|
3+4i, # Complex
|
||||||
|
2**128, # Bignum
|
||||||
|
:sym, # Symbol
|
||||||
|
'xyzzy'.to_sym, # dynamic symbol
|
||||||
|
'frozen'.freeze, # frozen String
|
||||||
|
/regexp/, # regexp literal
|
||||||
|
/reg{true}exp/.freeze, # frozen dregexp
|
||||||
|
[1, 2].freeze, # frozen Array which only refers to shareable
|
||||||
|
{a: 1}.freeze, # frozen Hash which only refers to shareable
|
||||||
|
[{a: 1}.freeze, 'str'.freeze].freeze, # nested frozen container
|
||||||
|
C, # class
|
||||||
|
M, # module
|
||||||
|
Ractor.current, # Ractor
|
||||||
|
]
|
||||||
|
|
||||||
|
unshareable_objects = [
|
||||||
|
'mutable str'.dup,
|
||||||
|
[:array],
|
||||||
|
{hash: true},
|
||||||
|
]
|
||||||
|
|
||||||
|
results = []
|
||||||
|
|
||||||
|
shareable_objects.map{|o|
|
||||||
|
echo_ractor << o
|
||||||
|
o2 = echo_ractor.take
|
||||||
|
results << "#{o} is copied" unless o.object_id == o2.object_id
|
||||||
|
}
|
||||||
|
|
||||||
|
unshareable_objects.map{|o|
|
||||||
|
echo_ractor << o
|
||||||
|
o2 = echo_ractor.take
|
||||||
|
results << "#{o.inspect} is not copied" if o.object_id == o2.object_id
|
||||||
|
}
|
||||||
|
|
||||||
|
if results.empty?
|
||||||
|
:ok
|
||||||
|
else
|
||||||
|
results.inspect
|
||||||
end
|
end
|
||||||
|
|
||||||
sharable_objects = [1, :sym, 'xyzzy'.to_sym, 'frozen'.freeze, 1+2r, 3+4i, /regexp/, C]
|
|
||||||
|
|
||||||
sr = sharable_objects.map{|o|
|
|
||||||
r << o
|
|
||||||
o2 = r.take
|
|
||||||
[o, o.object_id == o2.object_id]
|
|
||||||
}
|
|
||||||
|
|
||||||
ur = unsharable_objects = ['mutable str'.dup, [:array], {hash: true}].map{|o|
|
|
||||||
r << o
|
|
||||||
o2 = r.take
|
|
||||||
[o, o.object_id == o2.object_id]
|
|
||||||
}
|
|
||||||
[sr, ur].inspect
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# frozen Objects are shareable
|
# frozen Objects are shareable
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue