1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

test_optimization.rb: heredoc code style

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58395 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2017-04-18 13:02:56 +00:00
parent 37d2f9724c
commit 89b13fd364

View file

@ -4,7 +4,8 @@ require 'objspace'
class TestRubyOptimization < Test::Unit::TestCase class TestRubyOptimization < Test::Unit::TestCase
def assert_redefine_method(klass, method, code, msg = nil) def assert_redefine_method(klass, method, code, msg = nil)
assert_separately([], <<-"end;")# do assert_separately([], "#{<<-"begin;"}\n#{<<~"end;"}")
begin;
class #{klass} class #{klass}
undef #{method} undef #{method}
def #{method}(*args) def #{method}(*args)
@ -183,20 +184,22 @@ class TestRubyOptimization < Test::Unit::TestCase
def test_hash_aref_with def test_hash_aref_with
h = { "foo" => 1 } h = { "foo" => 1 }
assert_equal 1, h["foo"] assert_equal 1, h["foo"]
assert_redefine_method('Hash', '[]', <<-end) assert_redefine_method('Hash', '[]', "#{<<-"begin;"}\n#{<<~"end;"}")
begin;
h = { "foo" => 1 } h = { "foo" => 1 }
assert_equal "foo", h["foo"] assert_equal "foo", h["foo"]
end end;
end end
def test_hash_aset_with def test_hash_aset_with
h = {} h = {}
assert_equal 1, h["foo"] = 1 assert_equal 1, h["foo"] = 1
assert_redefine_method('Hash', '[]=', <<-end) assert_redefine_method('Hash', '[]=', "#{<<-"begin;"}\n#{<<~"end;"}")
begin;
h = {} h = {}
assert_equal 1, h["foo"] = 1, "assignment always returns value set" assert_equal 1, h["foo"] = 1, "assignment always returns value set"
assert_nil h["foo"] assert_nil h["foo"]
end end;
end end
class MyObj class MyObj
@ -235,7 +238,8 @@ class TestRubyOptimization < Test::Unit::TestCase
def test_tailcall def test_tailcall
bug4082 = '[ruby-core:33289]' bug4082 = '[ruby-core:33289]'
tailcall(<<-EOF) tailcall("#{<<-"begin;"}\n#{<<~"end;"}")
begin;
def fact_helper(n, res) def fact_helper(n, res)
if n == 1 if n == 1
res res
@ -246,14 +250,15 @@ class TestRubyOptimization < Test::Unit::TestCase
def fact(n) def fact(n)
fact_helper(n, 1) fact_helper(n, 1)
end end
EOF end;
assert_equal(9131, fact(3000).to_s.size, message(bug4082) {disasm(:fact_helper)}) assert_equal(9131, fact(3000).to_s.size, message(bug4082) {disasm(:fact_helper)})
end end
def test_tailcall_with_block def test_tailcall_with_block
bug6901 = '[ruby-dev:46065]' bug6901 = '[ruby-dev:46065]'
tailcall(<<-EOF) tailcall("#{<<-"begin;"}\n#{<<~"end;"}")
begin;
def identity(val) def identity(val)
val val
end end
@ -263,7 +268,7 @@ class TestRubyOptimization < Test::Unit::TestCase
identity(yield) identity(yield)
} }
end end
EOF end;
assert_equal(123, delay { 123 }.call, message(bug6901) {disasm(:delay)}) assert_equal(123, delay { 123 }.call, message(bug6901) {disasm(:delay)})
end end
@ -272,11 +277,12 @@ class TestRubyOptimization < Test::Unit::TestCase
end end
def test_tailcall_inhibited_by_block def test_tailcall_inhibited_by_block
tailcall(<<-EOF) tailcall("#{<<-"begin;"}\n#{<<~"end;"}")
begin;
def yield_result def yield_result
just_yield {:ok} just_yield {:ok}
end end
EOF end;
assert_equal(:ok, yield_result, message {disasm(:yield_result)}) assert_equal(:ok, yield_result, message {disasm(:yield_result)})
end end
@ -291,7 +297,8 @@ class TestRubyOptimization < Test::Unit::TestCase
def test_tailcall_inhibited_by_rescue def test_tailcall_inhibited_by_rescue
bug12082 = '[ruby-core:73871] [Bug #12082]' bug12082 = '[ruby-core:73871] [Bug #12082]'
tailcall(<<-'end;') tailcall("#{<<-"begin;"}\n#{<<~"end;"}")
begin;
def to_be_rescued def to_be_rescued
return do_raise return do_raise
1 + 2 1 + 2
@ -308,7 +315,8 @@ class TestRubyOptimization < Test::Unit::TestCase
def test_tailcall_symbol_block_arg def test_tailcall_symbol_block_arg
bug12565 = '[ruby-core:46065]' bug12565 = '[ruby-core:46065]'
tailcall(<<-EOF) tailcall("#{<<-"begin;"}\n#{<<~"end;"}")
begin;
def apply_one_and_two(&block) def apply_one_and_two(&block)
yield(1, 2) yield(1, 2)
end end
@ -316,28 +324,30 @@ class TestRubyOptimization < Test::Unit::TestCase
def add_one_and_two def add_one_and_two
apply_one_and_two(&:+) apply_one_and_two(&:+)
end end
EOF end;
assert_equal(3, add_one_and_two, assert_equal(3, add_one_and_two,
message(bug12565) {disasm(:add_one_and_two)}) message(bug12565) {disasm(:add_one_and_two)})
end end
def test_tailcall_interrupted_by_sigint def test_tailcall_interrupted_by_sigint
bug12576 = 'ruby-core:76327' bug12576 = 'ruby-core:76327'
script = <<EOS script = "#{<<-"begin;"}\n#{<<~'end;'}"
RubyVM::InstructionSequence.compile_option = { begin;
:tailcall_optimization => true, RubyVM::InstructionSequence.compile_option = {
:trace_instruction => false :tailcall_optimization => true,
} :trace_instruction => false
}
eval <<EOF eval "#{<<~"begin;"}\n#{<<~'end;1'}"
def foo begin;
foo def foo
end foo
puts("start") end
STDOUT.flush puts("start")
foo STDOUT.flush
EOF foo
EOS end;1
end;
status, _err = EnvUtil.invoke_ruby([], "", true, true, {}) { status, _err = EnvUtil.invoke_ruby([], "", true, true, {}) {
|in_p, out_p, err_p, pid| |in_p, out_p, err_p, pid|
in_p.write(script) in_p.write(script)
@ -365,7 +375,7 @@ EOS
def test_tailcall_condition_block def test_tailcall_condition_block
bug = '[ruby-core:78015] [Bug #12905]' bug = '[ruby-core:78015] [Bug #12905]'
src = "#{<<-"begin;"}\n#{<<-"end;"}" src = "#{<<-"begin;"}\n#{<<~"end;"}"
begin; begin;
def run(current, final) def run(current, final)
if current < final if current < final
@ -414,7 +424,8 @@ EOS
end end
def test_string_freeze_block def test_string_freeze_block
assert_separately([], <<-"end;")# do assert_separately([], "#{<<-"begin;"}\n#{<<~"end;"}")
begin;
class String class String
undef freeze undef freeze
def freeze def freeze
@ -427,7 +438,8 @@ EOS
end end
def test_opt_case_dispatch def test_opt_case_dispatch
code = <<-EOF code = "#{<<-"begin;"}\n#{<<~"end;"}"
begin;
case foo case foo
when "foo" then :foo when "foo" then :foo
when true then true when true then true
@ -440,7 +452,7 @@ EOS
else else
:nomatch :nomatch
end end
EOF end;
check = { check = {
'foo' => :foo, 'foo' => :foo,
true => true, true => true,
@ -459,7 +471,8 @@ EOS
assert_equal :nomatch, eval("foo = :blah\n#{code}") assert_equal :nomatch, eval("foo = :blah\n#{code}")
check.each do |foo, _| check.each do |foo, _|
klass = foo.class.to_s klass = foo.class.to_s
assert_separately([], <<-"end;") # do assert_separately([], "#{<<~"begin;"}\n#{<<~"end;"}")
begin;
class #{klass} class #{klass}
undef === undef ===
def ===(*args) def ===(*args)
@ -497,12 +510,13 @@ EOS
end end
def test_peephole_string_literal_range def test_peephole_string_literal_range
code = <<-EOF code = "#{<<~"begin;"}\n#{<<~"end;"}"
begin;
case ver case ver
when "2.0.0".."2.3.2" then :foo when "2.0.0".."2.3.2" then :foo
when "1.8.0"..."1.8.8" then :bar when "1.8.0"..."1.8.8" then :bar
end end
EOF end;
iseq = RubyVM::InstructionSequence.compile(code) iseq = RubyVM::InstructionSequence.compile(code)
insn = iseq.disasm insn = iseq.disasm
assert_match %r{putobject\s+#{Regexp.quote('"1.8.0"..."1.8.8"')}}, insn assert_match %r{putobject\s+#{Regexp.quote('"1.8.0"..."1.8.8"')}}, insn