Search code examples
scalascalacheck

Using scalacheck to see if list is was changed correctly?


How could I use scalacheck to see if each item in a list has been incremented correctly. The function added just adds 5 to it's parameter.Where I labeled problem area isn't working, how could I make it better to check each element of one list against anothe?

      val added = forAll { (a:Array[Int]) => {
         val l=added(a)
         all(
         "size" |: a.size==l.size,
         "sort/elements" |: for(i<-0 to a.size) a(i)+5==l(i) //problem area
            )
           }
          }

Solution

  • As far as can see you get an exception when the array is empty. One way to solve that is to write the for loop differently:

    (for(i<-1 until a.size) a(i - 1)+5==l(i - 1))
    

    You can also zip the 2 sequences together and check the equality of elements in pairs

    a.zip(l).forall { case (i, j) => i + 5 == j }