aboutsummaryrefslogtreecommitdiffstats
path: root/spec/ruby/core/enumerable/max_spec.rb
blob: e283a5d0e22e2a4c50f9b1f6d59a2e512d3a4ef6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
require File.expand_path('../../../spec_helper', __FILE__)
require File.expand_path('../fixtures/classes', __FILE__)

describe "Enumerable#max" do
  before :each do
    @e_strs = EnumerableSpecs::EachDefiner.new("333", "22", "666666", "1", "55555", "1010101010")
    @e_ints = EnumerableSpecs::EachDefiner.new( 333,   22,   666666,   55555, 1010101010)
  end

  it "returns the maximum element" do
    EnumerableSpecs::Numerous.new.max.should == 6
  end

  it "returns the maximum element (basics cases)" do
    EnumerableSpecs::EachDefiner.new(55).max.should == 55

    EnumerableSpecs::EachDefiner.new(11,99).max.should == 99
    EnumerableSpecs::EachDefiner.new(99,11).max.should == 99
    EnumerableSpecs::EachDefiner.new(2, 33, 4, 11).max.should == 33

    EnumerableSpecs::EachDefiner.new(1,2,3,4,5).max.should == 5
    EnumerableSpecs::EachDefiner.new(5,4,3,2,1).max.should == 5
    EnumerableSpecs::EachDefiner.new(1,4,3,5,2).max.should == 5
    EnumerableSpecs::EachDefiner.new(5,5,5,5,5).max.should == 5

    EnumerableSpecs::EachDefiner.new("aa","tt").max.should == "tt"
    EnumerableSpecs::EachDefiner.new("tt","aa").max.should == "tt"
    EnumerableSpecs::EachDefiner.new("2","33","4","11").max.should == "4"

    @e_strs.max.should == "666666"
    @e_ints.max.should == 1010101010
  end

  it "returns nil for an empty Enumerable" do
    EnumerableSpecs::EachDefiner.new.max.should == nil
  end

  it "raises a NoMethodError for elements without #<=>" do
    lambda do
      EnumerableSpecs::EachDefiner.new(BasicObject.new, BasicObject.new).max
    end.should raise_error(NoMethodError)
  end

  it "raises an ArgumentError for incomparable elements" do
    lambda do
      EnumerableSpecs::EachDefiner.new(11,"22").max
    end.should raise_error(ArgumentError)
    lambda do
      EnumerableSpecs::EachDefiner.new(11,12,22,33).max{|a, b| nil}
    end.should raise_error(ArgumentError)
  end

  context "when passed a block" do
    it "returns the maximum element" do
      EnumerableSpecs::EachDefiner.new("2","33","4","11").max {|a,b| a <=> b }.should == "4"
      EnumerableSpecs::EachDefiner.new( 2 , 33 , 4 , 11 ).max {|a,b| a <=> b }.should == 33

      EnumerableSpecs::EachDefiner.new("2","33","4","11").max {|a,b| b <=> a }.should == "11"
      EnumerableSpecs::EachDefiner.new( 2 , 33 , 4 , 11 ).max {|a,b| b <=> a }.should == 2

      @e_strs.max {|a,b| a.length <=> b.length }.should == "1010101010"

      @e_strs.max {|a,b| a <=> b }.should == "666666"
      @e_strs.max {|a,b| a.to_i <=> b.to_i }.should == "1010101010"

      @e_ints.max {|a,b| a <=> b }.should == 1010101010
      @e_ints.max {|a,b| a.to_s <=> b.to_s }.should == 666666
    end
  end

  it "returns the maximum for enumerables that contain nils" do
    arr = EnumerableSpecs::Numerous.new(nil, nil, true)
    arr.max { |a, b|
      x = a.nil? ? 1 : a ? 0 : -1
      y = b.nil? ? 1 : b ? 0 : -1
      x <=> y
    }.should == nil
  end

  it "gathers whole arrays as elements when each yields multiple" do
    multi = EnumerableSpecs::YieldsMulti.new
    multi.max.should == [6, 7, 8, 9]
  end

  context "when called with an argument n" do
    context "without a block" do
      it "returns an array containing the maximum n elements" do
        result = @e_ints.max(2)
        result.should == [1010101010, 666666]
      end
    end

    context "with a block" do
      it "returns an array containing the maximum n elements" do
        result = @e_ints.max(2) { |a, b| a * 2 <=> b * 2 }
        result.should == [1010101010, 666666]
      end
    end

    context "on a enumerable of length x where x < n" do
      it "returns an array containing the maximum n elements of length x" do
        result = @e_ints.max(500)
        result.length.should == 5
      end
    end

    context "that is negative" do
      it "raises an ArgumentError" do
        lambda { @e_ints.max(-1) }.should raise_error(ArgumentError)
      end
    end
  end

  context "that is nil" do
    it "returns the maximum element" do
      @e_ints.max(nil).should == 1010101010
    end
  end
end