summaryrefslogtreecommitdiff
path: root/spec/ruby/library/matrix/find_index_spec.rb
blob: 27672e526beeaf50ffd309c0b1645923dc06103b (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
require_relative '../../spec_helper'
require 'matrix'

describe "Matrix#find_index without any argument" do
  before :all do
    @m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
  end

  it "returns an Enumerator when called without a block" do
    enum = @m.find_index
    enum.should be_an_instance_of(Enumerator)
    enum.to_a.should == [1, 2, 3, 4, 5, 6, 7, 8]
  end

  it "returns nil if the block is always false" do
    @m.find_index{false}.should be_nil
  end

  it "returns the first index for which the block is true" do
    @m.find_index{|x| x >= 3}.should == [0, 2]
  end
end

describe "Matrix#find_index with a subselection argument" do
  before :all do
    @tests = [
    [  Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ], {
        diagonal: [1, 6]               ,
        off_diagonal: [2, 3, 4, 5, 7, 8],
        lower: [1, 5, 6]               ,
        strict_lower: [5]              ,
        strict_upper: [2, 3, 4, 7, 8]  ,
        upper: [1, 2, 3, 4, 6, 7, 8]   ,
      }
    ],
    [  Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ], {
        diagonal: [1, 4]               ,
        off_diagonal: [2, 3, 5, 6, 7, 8],
        lower: [1, 3, 4, 5, 6, 7, 8]   ,
        strict_lower: [3, 5, 6, 7, 8]  ,
        strict_upper: [2]              ,
        upper: [1, 2, 4]               ,
      }
    ]]
  end

  describe "and no generic argument" do
    it "returns an Enumerator when called without a block" do
      @tests.each do |matrix, h|
        h.each do |selector, result|
          matrix.find_index(selector).should be_an_instance_of(Enumerator)
        end
      end
    end

    it "yields the rights elements" do
      @tests.each do |matrix, h|
        h.each do |selector, result|
          matrix.find_index(selector).to_a.should == result
        end
      end
    end

    it "returns the first index for which the block returns true" do
      @tests.each do |matrix, h|
        h.each do |selector, result|
          cnt = result.size.div 2
          which = result[cnt]
          idx = matrix.find_index(selector){|x| cnt -= 1; x == which}
          matrix[*idx].should == which
          cnt.should == -1
        end
      end
    end

    it "returns nil if the block is always false" do
      @tests.each do |matrix, h|
        h.each do |selector, result|
          matrix.find_index(selector){ nil }.should == nil
        end
      end
    end

  end

  describe "and a generic argument" do
    it "ignores a block" do
      @m.find_index(42, :diagonal){raise "oups"}.should == nil
    end

    it "returns the index of the requested value" do
      @tests.each do |matrix, h|
        h.each do |selector, result|
          cnt = result.size / 2
          which = result[cnt]
          idx = matrix.find_index(which, selector)
          matrix[*idx].should == which
        end
      end
    end

    it "returns nil if the requested value is not found" do
      @tests.each do |matrix, h|
        h.each do |selector, result|
          matrix.find_index(42, selector).should == nil
        end
      end
    end
  end

end

describe "Matrix#find_index with only a generic argument" do
  before :all do
    @m = Matrix[ [1, 2, 3, 4], [1, 2, 3, 4] ]
  end

  it "returns nil if the value is not found" do
    @m.find_index(42).should be_nil
  end

  it "returns the first index for of the requested value" do
    @m.find_index(3).should == [0, 2]
  end

  it "ignores a block" do
    @m.find_index(4){raise "oups"}.should == [0, 3]
  end
end

describe "Matrix#find_index with two arguments" do
  it "raises an ArgumentError for an unrecognized last argument" do
    lambda {
      @m.find_index(1, "all"){}
    }.should raise_error(ArgumentError)
    lambda {
      @m.find_index(1, nil){}
    }.should raise_error(ArgumentError)
    lambda {
      @m.find_index(1, :left){}
    }.should raise_error(ArgumentError)
    lambda {
      @m.find_index(:diagonal, 1){}
    }.should raise_error(ArgumentError)
  end
end