Ruby on rails 如何检查测试用例是否通过或失败,并知道其索引值

Ruby on rails 如何检查测试用例是否通过或失败,并知道其索引值,ruby-on-rails,ruby,Ruby On Rails,Ruby,我需要检查测试用例的状态,比如它是通过还是失败,并知道它的索引值 我怎样才能做到这一点 我尝试了二进制搜索。代码如下: class Array def binary_search(val, low = 0, high = length - 1) return nil if high < low mid = (low + high) >> 1 case val <=> self[mid] when -1 binary_s

我需要检查测试用例的状态,比如它是通过还是失败,并知道它的索引值

我怎样才能做到这一点

我尝试了二进制搜索。代码如下:

class Array
  def binary_search(val, low = 0, high = length - 1)
    return nil if high < low
    mid = (low + high) >> 1
    case val <=> self[mid]
    when -1
      binary_search(val, low, mid - 1)
    when 1
      binary_search(val, mid + 1, high)
    else
      mid
    end
  end
end

ary = [0, 1, 4, 5, 6, 7, 8, 9, 12, 26, 45, 67, 78, 90]
[0, 42, 45, 24324, 99999].each do |val|
  i = ary.binary_search(val)
  if i
    puts "found #{val} at index #{i}: #{ary[i]}"
  else
    puts "#{val} not found in array"
  end
end
类数组
def二进制搜索(val,低=0,高=长度-1)
如果高<低,则返回零
中=(低+高)>>1
case val self[mid]
当-1
二进制搜索(val、low、mid-1)
当1
二进制搜索(val、mid+1、high)
其他的
中间
终止
终止
终止
ary=[0,1,4,5,6,7,8,9,12,26,45,67,78,90]
[0,42,45,2432499999]。每个do | val|
i=二进制搜索(val)
如果我
将“在索引#{i}:#{ary[i]}处找到#{val}”
其他的
将“#{val}放在数组中找不到”
终止
终止

但这是一个密钥是否被发现的问题。

你可能想考虑以下内容:

ary = [0, 1, 4, 5, 6, 7, 8, 9, 12, 26, 45, 67, 78, 90]
a = [0, 42, 45, 24324, 99999]

h = ary.each_with_index.to_h
  #=> {0=>0, 1=>1, 4=>2, 5=>3, 6=>4, 7=>5, 8=>6, 9=>7, 12=>8, 26=>9,
  #    45=>10, 67=>11, 78=>12, 90=>13}

a.each do |val|
   i = h[val]
   puts i ? "found #{val} at index #{i}: #{ary[i]}" : "#{val} not found in array"
end
found 0 at index 0: 0
42 not found in array
found 45 at index 10: 45
24324 not found in array
99999 not found in array

使用散列可以快速查找键,并在
h
中匹配
a
中的值。您的问题不清楚,测试用例和索引必须做什么。不同的术语我想检查测试用例是否通过了测试,如何才能做到这一点我不理解你的问题。您想知道如何使用(例如)测试代码吗?在任何情况下,你需要用编辑来阐明你的问题。OK我解释这个场景,例如在1到10个测试用例中,第一个测试用例已经通过,第十个已经失败了。我需要检查哪个测试用例已经失败了,让我们考虑它在第五失败了,然后不需要检查它下面,我需要检查它上面,,请帮助我解决此问题。我已尝试使用以下代码。现在,我想检查第50个测试用例是否失败,这意味着我需要检查上述测试用例,如何完成此操作test=Array.new(100)测试[1]=['pass']测试[50]='pass'测试[2]测试[4]测试[4]测试计数=测试。大小测试计数1=测试。大小/2.0测试[50]很抱歉,我仍然不明白你想做什么。您经常提到“测试用例”,但却没有解释它是什么。它是指数组中的单个元素
ary
还是
[0,42,45,2432499999]