Scala / Intellij:如果输入语句为false,则阻塞

时间:2019-01-12 09:37:17

标签: scala intellij-idea sbt

这是我在sbt项目上工作时与IntelliJ一起看到的最奇怪的事情-如有必要,我可以分享有关用例的更多详细信息。

我有一个if语句,在调试中,我观察到该语句被评估为false时进入了它后面的块。

我尝试运行sbt compilesbt reload。我还使缓存无效并重新启动了IntelliJ。

这是代码:

class MedianOf3Partitioning[T: ClassTag : Ordering] extends QuickSort[T] with MedianOfPartitioning[T] {

  protected val MEDIAN_OF = 3

  override protected def sort(a: Array[T], lo: Int, hi: Int): Array[T] = {
    if (hi > lo || (hi - lo) > MEDIAN_OF) {
      val sampleArray = getSample(a.slice(lo, hi), MEDIAN_OF)
      patchArray(a, lo, hi, sampleArray)
      val j = partition(a, lo, hi)
      sort(a, lo, j - 1)
      sort(a, j + 1, hi)
    } else return super.sort(a, lo, hi)

  }
}

这是一个调试会话的屏幕截图,演示了正在发生的事情:

How is this even possible?

所以这两个条件都为假,但是输入了该语句后花括号中的代码。

我的IntelliJ版本是2018.3

更新

我已经通过运行以下测试类来执行代码:

class MedianOf3PartitioningSpec extends BaseSpec {

  private val testCase: Array[Char] = "EASYQUICKSORTQUESTION" toCharArray

  behavior of "MedianOf3PartitioningSpec"

  it should "sort" in {
    val medianOf3Partitioning = new MedianOf3Partitioning[Char]
    val maybeSortedArray = medianOf3Partitioning sort(testCase)
    maybeSortedArray shouldBe sorted
  }

}

我正在使用IntelliJ运行它(就像我通常那样)。我只是尝试了简单的命令行testOnly *MedianOf3PartitioningSpec,但由于相同的原因而失败。

如果我让代码运行(忽略我上面共享的这个奇怪的观察),则测试将失败:

[info] - should sort *** FAILED ***
[info]   java.lang.IllegalArgumentException: bound must be positive
[info]   at java.util.Random.nextInt(Random.java:388)
[info]   at scala.util.Random.nextInt(Random.scala:66)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOfPartitioning.getSample(MedianOfPartitioning.scala:14)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOfPartitioning.getSample$(MedianOfPartitioning.scala:13)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOf3Partitioning.getSample(MedianOf3Partitioning.scala:16)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOf3Partitioning.sort(MedianOf3Partitioning.scala:22)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOf3Partitioning.sort(MedianOf3Partitioning.scala:25)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOf3Partitioning.sort(MedianOf3Partitioning.scala:26)
[info]   at ca.vgorcinschi.algorithms2_3.QuickSort.sort(QuickSort.scala:13)
[info]   at ca.vgorcinschi.algorithms2_3.MedianOf3PartitioningSpec.$anonfun$new$1(MedianOf3PartitioningSpec.scala:13)

... getSample正在这样做:

trait MedianOfPartitioning [T] {

  this: BaseSort[T] =>

  protected val MEDIAN_OF: Int

  protected def getSample(a: Array[T], medianOf: Int): Array[T] = {
    val sampleArrayOffset = Random.nextInt(a.length - medianOf)
    a.slice(sampleArrayOffset, sampleArrayOffset + medianOf)
  }

  /**
    * Mutate the main array to have at its head the smallest value from sample
    * array and at its last index the biggest value from sample array
    * @param a - target array
    * @param sampleArray - size should be equal to {@link MEDIAN_OF}
    */
  def patchArray(a: Array[T], lo: Int, hi: Int, sampleArray: Array[T]): Unit = {
    val (minVal, maxVal) = ((sampleArray.head, sampleArray.head) /: sampleArray) { case ((tempMin, tempMax), current) =>
      (min(tempMin, current), max(tempMax, current))
    }
    exch(a, a.indexOf(minVal), lo)
    exch(a, a.indexOf(maxVal), hi)
  }
}

但是,如果if语句评估正确,则不应首先输入getSample(这是故意的)。可能房间里有一头大象没看见,但如果有人能指着我,我将不胜感激:-)

0 个答案:

没有答案