summaryrefslogtreecommitdiff
path: root/spec/ruby/core/array/rindex_spec.rb
blob: 13de88818c705a137684906da8af8c327e090954 (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
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require_relative '../enumerable/shared/enumeratorized'

# Modifying a collection while the contents are being iterated
# gives undefined behavior. See
# https://blade.ruby-lang.org/ruby-core/23633

describe "Array#rindex" do
  it "returns the first index backwards from the end where element == to object" do
    key = 3
    uno = mock('one')
    dos = mock('two')
    tres = mock('three')
    tres.should_receive(:==).any_number_of_times.and_return(false)
    dos.should_receive(:==).any_number_of_times.and_return(true)
    uno.should_not_receive(:==)
    ary = [uno, dos, tres]

    ary.rindex(key).should == 1
  end

  it "returns size-1 if last element == to object" do
    [2, 1, 3, 2, 5].rindex(5).should == 4
  end

  it "returns 0 if only first element == to object" do
    [2, 1, 3, 1, 5].rindex(2).should == 0
  end

  it "returns nil if no element == to object" do
    [1, 1, 3, 2, 1, 3].rindex(4).should == nil
  end

  it "returns correct index even after delete_at" do
    array = ["fish", "bird", "lion", "cat"]
    array.delete_at(0)
    array.rindex("lion").should == 1
  end

  it "properly handles empty recursive arrays" do
    empty = ArraySpecs.empty_recursive_array
    empty.rindex(empty).should == 0
    empty.rindex(1).should be_nil
  end

  it "properly handles recursive arrays" do
    array = ArraySpecs.recursive_array
    array.rindex(1).should == 0
    array.rindex(array).should == 7
  end

  it "accepts a block instead of an argument" do
    [4, 2, 1, 5, 1, 3].rindex { |x| x < 2 }.should == 4
  end

  it "ignores the block if there is an argument" do
    -> {
      [4, 2, 1, 5, 1, 3].rindex(5) { |x| x < 2 }.should == 3
    }.should complain(/given block not used/)
  end

  it "rechecks the array size during iteration" do
    ary = [4, 2, 1, 5, 1, 3]
    seen = []
    ary.rindex { |x| seen << x; ary.clear; false }

    seen.should == [3]
  end

  it "tolerates increasing an array size during iteration" do
    array = [:a, :b, :c]
    ScratchPad.record []
    i = 0

    array.rindex do |e|
      ScratchPad << e
      array.prepend i if i < 100
      i += 1
      false
    end

    ScratchPad.recorded.should == [:c, :a, 1]
  end

  describe "given no argument and no block" do
    it "produces an Enumerator" do
      enum = [4, 2, 1, 5, 1, 3].rindex
      enum.should be_an_instance_of(Enumerator)
      enum.each { |x| x < 2 }.should == 4
    end
  end

  it_behaves_like :enumeratorized_with_unknown_size, :bsearch, [1,2,3]
end