From 39b33658e3764ee621a9c2c9cc003e21da4e327e Mon Sep 17 00:00:00 2001 From: naruse Date: Wed, 18 Jul 2012 03:56:58 +0000 Subject: * lib/benchmark.rb: Fix Benchmark.benchmark output with an empty caption. patched by Benoit Daloze. [ruby-core:45719] [Bug #6610] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36427 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- test/benchmark/test_benchmark.rb | 102 +++++++++++++++++++++------------------ 1 file changed, 54 insertions(+), 48 deletions(-) (limited to 'test') diff --git a/test/benchmark/test_benchmark.rb b/test/benchmark/test_benchmark.rb index 801da47dae..7477fa8e05 100644 --- a/test/benchmark/test_benchmark.rb +++ b/test/benchmark/test_benchmark.rb @@ -4,7 +4,7 @@ require 'benchmark' MiniTest::Unit.autorun describe Benchmark do - BENCH_FOR_TIMES_UPTO = lambda do |x| + bench_for_times_upto = lambda do |x| n = 1000 tf = x.report("for:") { for _ in 1..n; '1'; end } tt = x.report("times:") { n.times do ; '1'; end } @@ -12,21 +12,23 @@ describe Benchmark do [tf+tt+tu, (tf+tt+tu)/3] end - BENCH_FOR_TIMES_UPTO_NO_LABEL = lambda do |x| + bench_for_times_upto_no_label = lambda do |x| n = 1000 x.report { for _ in 1..n; '1'; end } x.report { n.times do ; '1'; end } x.report { 1.upto(n) do ; '1'; end } end - LABELS = %w[first second third] + def labels + %w[first second third] + end def bench(type = :bm, *args, &block) if block Benchmark.send(type, *args, &block) else Benchmark.send(type, *args) do |x| - LABELS.each { |label| + labels.each { |label| x.report(label) {} } end @@ -59,12 +61,29 @@ describe Benchmark do end end + benchmark_output_with_total_avg = <total: --time-- --time-- --time-- ( --time--) +>avg: --time-- --time-- --time-- ( --time--) +BENCH + describe 'benchmark' do + it 'does not print any space if the given caption is empty' do + capture_bench_output(:benchmark).must_equal <<-BENCH +first --time-- --time-- --time-- ( --time--) +second --time-- --time-- --time-- ( --time--) +third --time-- --time-- --time-- ( --time--) +BENCH + end + it 'makes extra calcultations with an Array at the end of the benchmark and show the result' do capture_bench_output(:benchmark, Benchmark::CAPTION, 7, Benchmark::FORMAT, ">total:", ">avg:", - &BENCH_FOR_TIMES_UPTO).must_equal BENCHMARK_OUTPUT_WITH_TOTAL_AVG + &bench_for_times_upto).must_equal benchmark_output_with_total_avg end end @@ -74,8 +93,8 @@ describe Benchmark do capture_io do results = bench(meth) results.must_be_instance_of Array - results.size.must_equal LABELS.size - results.zip(LABELS).each { |tms, label| + results.size.must_equal labels.size + results.zip(labels).each { |tms, label| tms.must_be_instance_of Benchmark::Tms tms.label.must_equal label } @@ -84,26 +103,49 @@ describe Benchmark do end it 'correctly output when the label width is given' do - capture_bench_output(:bm, 6).must_equal BM_OUTPUT + capture_bench_output(:bm, 6).must_equal <<-BENCH + user system total real +first --time-- --time-- --time-- ( --time--) +second --time-- --time-- --time-- ( --time--) +third --time-- --time-- --time-- ( --time--) +BENCH end it 'correctly output when no label is given' do - capture_bench_output(:bm, &BENCH_FOR_TIMES_UPTO_NO_LABEL).must_equal BM_OUTPUT_NO_LABEL + capture_bench_output(:bm, &bench_for_times_upto_no_label).must_equal <<-BENCH + user system total real + --time-- --time-- --time-- ( --time--) + --time-- --time-- --time-- ( --time--) + --time-- --time-- --time-- ( --time--) +BENCH end it 'can make extra calcultations with an array at the end of the benchmark' do capture_bench_output(:bm, 7, ">total:", ">avg:", - &BENCH_FOR_TIMES_UPTO).must_equal BENCHMARK_OUTPUT_WITH_TOTAL_AVG + &bench_for_times_upto).must_equal benchmark_output_with_total_avg end end describe 'bmbm' do + bmbm_output = <total: --time-- --time-- --time-- ( --time--) ->avg: --time-- --time-- --time-- ( --time--) -BENCH -- cgit v1.2.3