|
require 'test/unit'
|
|
require "ostruct"
|
|
require 'benchmark'
|
|
|
|
module Enumerable
|
|
alias_method :_original_method_all?, :all?
|
|
def all?(&block)
|
|
return false if count == 0
|
|
|
|
if block_given?
|
|
_original_method_all?(&block)
|
|
else
|
|
_original_method_all?
|
|
end
|
|
end
|
|
end
|
|
|
|
class EnumerableTest < Test::Unit::TestCase
|
|
|
|
def test_all_method_without_block_for_empty_array
|
|
assert_equal false, [].all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_empty_array
|
|
assert_equal false, [].all?(&:undefined_mathod)
|
|
end
|
|
|
|
def test_all_method_without_block_for_filled_array
|
|
assert_equal true, [1].all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_filled_array
|
|
assert_equal false, [1].all?(&:nil?)
|
|
end
|
|
|
|
def test_all_method_for_filled_array_if_one_element_is_nill_without_block
|
|
assert_equal false, [1,2,nil,3].all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_filled_array_if_all_elements_is_nil
|
|
assert_equal true, [nil, nil, nil].all?(&:nil?)
|
|
end
|
|
|
|
def test_all_method_with_block_for_filled_array_if_all_elements_is_false
|
|
assert_equal true, [false, false, false].all?{ |e| e == false }
|
|
end
|
|
|
|
def test_all_method_without_block_for_empty_hash
|
|
assert_equal false, {}.all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_empty_hash
|
|
assert_equal false, {}.all?(&:undefined_mathod)
|
|
end
|
|
|
|
def test_all_method_with_block_for_filled_hash
|
|
assert_equal false, {first: 1, second: 2}.all?(&:nil?)
|
|
end
|
|
|
|
def test_all_method_with_block_for_filled_hash
|
|
assert_equal false, {first: 1, second: 2}.all?{ |e| e.last > 1}
|
|
end
|
|
|
|
def test_all_method_without_block_for_filled_hash
|
|
assert_equal true, {first: 1, second: 2}.all?
|
|
end
|
|
|
|
def test_all_method_without_block_for_correct_range
|
|
assert_equal true, (1..100).all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_correct_range
|
|
assert_equal true, (1..100).all?{ |e| e > 0 }
|
|
end
|
|
|
|
def test_all_method_without_block_for_incorrect_range
|
|
assert_equal false, (100..1).all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_incorrect_range
|
|
assert_equal false, (100..1).all?(&:nil?)
|
|
end
|
|
|
|
def test_all_method_with_block_for_still_incorrect_range
|
|
assert_equal false, (-2..-3).all?(&:undefined_mathod?)
|
|
end
|
|
|
|
def test_all_method_without_block_for_empty_struct_tms
|
|
assert_equal false, Struct::Tms.new.all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_empty_struct_tms
|
|
assert_equal false, Struct::Tms.new.all?(&:nil?)
|
|
end
|
|
|
|
def test_all_method_with_block_for_empty_struct_tms
|
|
assert_equal false, Struct::Tms.new(utime: 10).all?(&:nil?)
|
|
end
|
|
|
|
def test_all_method_without_block_for_dir
|
|
assert_equal true, Dir.new(Dir.pwd).all?
|
|
end
|
|
|
|
def test_all_method_with_block_for_dir
|
|
assert_equal false, Dir.new(Dir.pwd).all?(&:nil?)
|
|
end
|
|
end
|
|
|
|
n = 200000
|
|
|
|
Benchmark.bmbm do |x|
|
|
x.report("original -- all? with empty collections") { Array.new(n, []).each{|empty| empty._original_method_all?(&:bla) } }
|
|
x.report("modified -- all? with empty collections") { Array.new(n, []).each{|empty| empty.all?(&:bla) } }
|
|
|
|
x.report("original -- all? with full collections") { Array.new(n, [1,2,3]).each{|empty| empty._original_method_all?{|x| x > 0 } } }
|
|
x.report("modified -- all? with full collections") { Array.new(n, [1,2,3]).each{|empty| empty.all?{|x| x > 0 } } }
|
|
end
|