Skip to content

Instantly share code, notes, and snippets.

View longmuir's full-sized avatar

Jamie Longmuir longmuir

  • Red Hat
View GitHub Profile
### Keybase proof
I hereby claim:
* I am longmuir on github.
* I am jamielongmuir (https://keybase.io/jamielongmuir) on keybase.
* I have a public key whose fingerprint is A874 2759 6319 3818 A5D7 86FF 0C6B 7F09 F1EF 9464
To claim this, I am signing this object:
@longmuir
longmuir / scalacheck-blog-8
Last active August 29, 2015 14:20
ScalaCheck - full example
import org.scalatest.WordSpec
import org.scalatest.prop.GeneratorDrivenPropertyChecks
import org.scalacheck.Gen
//Simplified case classes with companion objects and function definitions omitted for brevity
case class City(name: String)
case class Flight(number: Int, origin: City, dest: City, aircraft: Aircraft)
case class Aircraft(name: String, range: Int)
class FlightSpec extends WordSpec with GeneratorDrivenPropertyChecks {
@longmuir
longmuir / scalacheck-blog-7
Created May 8, 2015 18:37
ScalaCheck - filtered generator
val smallEvenInteger = Gen.choose(0,200) suchThat (_ % 2 == 0)
val propMakeList = forAll { n: Int =>
(n >= 0 && n < 10000) ==> (List.fill(n)("").length == n)
}
@longmuir
longmuir / scalacheck-blog-6
Created May 8, 2015 18:36
ScalaCheck Simple Generator Example
case class Customer(name: String, number: Int)
val customerGen:Gen[Customer] = for {
name <- Gen.oneOf("Bob", "Larry", "Ed", "Ted") //Could also be a custom name generator
number <- Gen.posNum[Int]
} yield Customer(name, number)
@longmuir
longmuir / scalacheck-blog-5
Created May 8, 2015 18:34
ScalaCheck Queue example
property("delete only element should yield empty") = forAll { a: A =>
val queue = insert(a, queue) //returns a new queue with ‘a’ added to ‘empty’
deleteMin(queue) == empty
}
@longmuir
longmuir / scalacheck-blog-4
Created May 8, 2015 18:32
ScalaCheck Example - SuperList
property("size of 2 super lists combined, should be equal to the sum of the size of the inputs") = {
forAll { (a: SuperList[A], b: SuperList[A]) =>
val expectedSum = a.size + b.size
(a ++ b).size == expectedSum
}
}
@longmuir
longmuir / scalacheck-blog-3
Created May 8, 2015 18:24
ScalaCheck Error Example
GeneratorDrivenPropertyCheckFailedException was thrown during property evaluation.
(DemoCheck.scala:40)
Falsified after 4 successful property evaluations.
Location: (DemoCheck.scala:40)
Occurred when passed generated values (
arg0 = -1 // 30 shrinks
)
@longmuir
longmuir / scalacheck-blog-2
Created May 8, 2015 18:21
Scala - Bad Property Example
property("Square root of integer should be less or equal to the input value") = {
forAll { (inputValue: Int) =>
Math.sqrt(inputValue) <= inputValue
}
}
@longmuir
longmuir / scalacheck-blog-1
Last active August 29, 2015 14:20
Traditional Unit Test Example
test("Square root of 16 is 4") {
assert(Math.sqrt(16) == 4)
}