Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用不同的名称访问scalacheck生成的元组元素_Scala_Scalatest_Scalacheck - Fatal编程技术网

使用不同的名称访问scalacheck生成的元组元素

使用不同的名称访问scalacheck生成的元组元素,scala,scalatest,scalacheck,Scala,Scalatest,Scalacheck,我同时使用ScalaTest和Scalacheck。这是我的惰性对(它不起作用,因为这里我们得到的是Gen[(Int,Int)],而不是Tuples2,所以我不能使用模式匹配): 我可以这样创建一个Gen[(Int,Int)]: forAll (endInterval, startInterval) { (start: Int, end: Int) => assert(sumOfCubes(start, end) === 0) } private lazy v

我同时使用ScalaTest和Scalacheck。这是我的惰性对(它不起作用,因为这里我们得到的是Gen[(Int,Int)],而不是Tuples2,所以我不能使用模式匹配):

我可以这样创建一个Gen[(Int,Int)]:

forAll (endInterval, startInterval) { (start: Int, end: Int) =>
        assert(sumOfCubes(start, end) === 0)
      }
private lazy val genPairs =
  for {
    start <- Gen.choose(-10000, 10000)
    end <- Gen.choose(-10000, 10000) if end > start
  } yield (start, end)
以及:


但是,当所有生成的开始和结束不满足条件时,测试可能会失败。因此,这并不是最好的解决方案(或者至少是一个好的解决方案)。

您可以使用scalatest的所有功能访问它们,如下所示:

  import org.scalatest.prop.GeneratorDrivenPropertyChecks._

  forAll (genPairs) { 
    case (start, end) => doSomething(start, end)
  }
private lazy val startInterval = Gen.choose(-10000, 10000)
private lazy val endInterval = Gen.choose(-10000, 10000)
forAll (startInterval, endInterval) { (start: Int, end: Int) =>
        whenever(start > end) {
          assert(sumOfCubes(start, end) === 0)
        }
}
  import org.scalatest.prop.GeneratorDrivenPropertyChecks._

  forAll (genPairs) { 
    case (start, end) => doSomething(start, end)
  }