test_for_enumerable_all.rb

Anton Shemerey, 07/21/2012 01:11 AM

Download (3.21 KB)

 
1
require 'test/unit'
2
require "ostruct"
3
require 'benchmark'
4

    
5
module Enumerable
6
  alias_method :_original_method_all?, :all?
7
  def all?(&block)
8
    return false if count == 0
9

    
10
    if block_given?
11
      _original_method_all?(&block)
12
    else
13
      _original_method_all?
14
    end
15
  end
16
end
17

    
18
class EnumerableTest < Test::Unit::TestCase
19

    
20
  def test_all_method_without_block_for_empty_array
21
    assert_equal false, [].all?
22
  end
23

    
24
  def test_all_method_with_block_for_empty_array
25
    assert_equal false, [].all?(&:undefined_mathod)
26
  end
27

    
28
  def test_all_method_without_block_for_filled_array
29
    assert_equal true, [1].all?
30
  end
31

    
32
  def test_all_method_with_block_for_filled_array
33
    assert_equal false, [1].all?(&:nil?)
34
  end
35

    
36
  def test_all_method_for_filled_array_if_one_element_is_nill_without_block
37
    assert_equal false, [1,2,nil,3].all?
38
  end
39

    
40
  def test_all_method_with_block_for_filled_array_if_all_elements_is_nil
41
    assert_equal true, [nil, nil, nil].all?(&:nil?)
42
  end
43

    
44
  def test_all_method_with_block_for_filled_array_if_all_elements_is_false
45
    assert_equal true, [false, false, false].all?{ |e| e == false }
46
  end
47

    
48
  def test_all_method_without_block_for_empty_hash
49
    assert_equal false, {}.all?
50
  end
51

    
52
  def test_all_method_with_block_for_empty_hash
53
    assert_equal false, {}.all?(&:undefined_mathod)
54
  end
55

    
56
  def test_all_method_with_block_for_filled_hash
57
    assert_equal false, {first: 1, second: 2}.all?(&:nil?)
58
  end
59

    
60
  def test_all_method_with_block_for_filled_hash
61
    assert_equal false, {first: 1, second: 2}.all?{ |e| e.last > 1}
62
  end
63

    
64
  def test_all_method_without_block_for_filled_hash
65
   assert_equal true, {first: 1, second: 2}.all?
66
  end
67

    
68
  def test_all_method_without_block_for_correct_range
69
    assert_equal true, (1..100).all?
70
  end
71

    
72
  def test_all_method_with_block_for_correct_range
73
    assert_equal true, (1..100).all?{ |e| e > 0 }
74
  end
75

    
76
  def test_all_method_without_block_for_incorrect_range
77
    assert_equal false, (100..1).all?
78
  end
79

    
80
  def test_all_method_with_block_for_incorrect_range
81
    assert_equal false, (100..1).all?(&:nil?)
82
  end
83

    
84
  def test_all_method_with_block_for_still_incorrect_range
85
    assert_equal false, (-2..-3).all?(&:undefined_mathod?)
86
  end
87

    
88
  def test_all_method_without_block_for_empty_struct_tms
89
    assert_equal false,  Struct::Tms.new.all?
90
  end
91

    
92
  def test_all_method_with_block_for_empty_struct_tms
93
    assert_equal false,  Struct::Tms.new.all?(&:nil?)
94
  end
95

    
96
  def test_all_method_with_block_for_empty_struct_tms
97
    assert_equal false,  Struct::Tms.new(utime: 10).all?(&:nil?)
98
  end
99

    
100
  def test_all_method_without_block_for_dir
101
    assert_equal true, Dir.new(Dir.pwd).all?
102
  end
103

    
104
  def test_all_method_with_block_for_dir
105
    assert_equal false, Dir.new(Dir.pwd).all?(&:nil?)
106
  end
107
end
108

    
109
n = 200000
110

    
111
Benchmark.bmbm do |x|
112
  x.report("original -- all? with empty collections") { Array.new(n, []).each{|empty| empty._original_method_all?(&:bla) } }
113
  x.report("modified -- all? with empty collections") { Array.new(n, []).each{|empty| empty.all?(&:bla) } }
114

    
115
  x.report("original -- all? with full  collections") { Array.new(n, [1,2,3]).each{|empty| empty._original_method_all?{|x| x > 0 } } }
116
  x.report("modified -- all? with full  collections") { Array.new(n, [1,2,3]).each{|empty| empty.all?{|x| x > 0 } } }
117
end