【问题标题】:How to check whether test case has passed or failed and to know its index value如何检查测试用例是通过还是失败并知道它的索引值
【发布时间】:2017-10-06 05:22:12
【问题描述】:

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

我怎样才能做到这一点?

我尝试了二进制搜索。这是代码:

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

但那是关于是否找到密钥。

【问题讨论】:

  • 你的问题不清楚,测试用例和索引是做什么的。不同的术语

标签: ruby-on-rails ruby


【解决方案1】:

您不妨考虑以下事项。

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

【讨论】:

  • 我想检查测试用例是通过还是失败了如何实现这个
  • 我不明白你的问题。您想知道如何使用例如rspec 来测试您的代码吗?无论如何,您需要通过编辑来澄清您的问题。
  • 好的,我会解释一下场景,例如,在 1 到 10 个测试用例之间,第一个测试用例已通过,第 10 个测试用例已失败,我需要检查哪个测试用例失败了让我们认为它在第 5 次失败了,那么它不需要检查它下面,我需要检查它上面,,请帮我解决这个问题
  • 我已经尝试使用下面的代码现在我想检查第 50 个测试用例是否失败意味着我需要检查上面的测试用例,如何做到这一点 test= Array.new(100) test [1]=['pass'] test [50] ='pass' puts test [2] puts test[4] puts count= test.size puts count1=test.size/2.0 puts test[50]
  • 很抱歉,但我仍然不明白您要做什么。您经常提到“测试用例”,但无处解释那是什么。它是指数组中的单个元素ary 还是[0, 42, 45, 24324, 99999]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
  • 2019-05-08
  • 2015-05-30
  • 2013-08-30
  • 1970-01-01
  • 2019-05-29
  • 1970-01-01
相关资源
最近更新 更多