Skip to content

Instantly share code, notes, and snippets.

@davegurnell
Created March 22, 2021 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davegurnell/2c5ee6eb7a9739f5f7c4118d0a2ab9e7 to your computer and use it in GitHub Desktop.
Save davegurnell/2c5ee6eb7a9739f5f7c4118d0a2ab9e7 to your computer and use it in GitHub Desktop.
package basics
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpec
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
import org.scalacheck.Gen
import org.scalacheck.Arbitrary
class MathSpec extends AnyWordSpec with Matchers with ScalaCheckDrivenPropertyChecks {
def absolute(n: Int): Int =
if (n < 0) -n else n
"absolute" should {
"turn a negative into a positive" in {
val negatives = Gen.negNum[Int]
forAll(negatives) { (n: Int) =>
absolute(n) should be > 0
}
}
"leave a positive as-is" in {
val positives = Gen.negNum[Int]
forAll(positives) { (n: Int) =>
absolute(n) should be(n)
}
}
}
def isEven(num: Int): Boolean =
num % 2 == 0
"isEven" should {
"recognise an even number" in {
val numbers = Arbitrary.arbitrary[Int]
forAll(numbers) { (n: Int) =>
isEven(2 * n) should be(true)
}
}
"recognise an odd number" in {
val numbers = Arbitrary.arbitrary[Int]
forAll(numbers) { (n: Int) =>
isEven(2 * n - 1) should be(false)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment