Created
December 15, 2017 15:39
-
-
Save yasuabe/1feef267d4d1efefcffbbbda67205ff7 to your computer and use it in GitHub Desktop.
ch04 privacy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package fp_tdd | |
import cats.Apply | |
import cats.syntax.apply._ | |
import org.scalacheck.Prop.forAll | |
import org.scalacheck.{Gen, Properties} | |
object Chapter04 extends Properties("Ch04") { | |
// ======== TODO ======== | |
// $5 + 10 CHF = $10 if rate is 2:1 | |
// Money rounding? | |
// hashCode | |
// Equal null | |
// Equal object | |
// ======== DONE ======== | |
// 1. $5 * 2 = $10 | |
// 2. Dollar side effect | |
// 2. times -> `*` | |
// 3. Equality | |
// product code ------------------------------------------------------------------- | |
class Dollar(private val amount: Int) { | |
override def equals(a: Any): Boolean = | |
a.asInstanceOf[Dollar].amount == this.amount | |
def *(multiplier: Int): Dollar = Dollar(amount * multiplier) | |
} | |
object Dollar { def apply(amount: Int) = new Dollar(amount) } | |
// test code -------------------------------------------------------------------- | |
val intGen: Gen[Int] = Gen.chooseNum(-1000000, 1000000) | |
implicit val dollars: Gen[Dollar] = intGen.map(Dollar.apply) | |
def double[G[_]: Apply, A](ga: G[A]): G[(A, A)] = (ga, ga).mapN((_, _)) | |
def triple[G[_]: Apply, A](ga: G[A]): G[(A, A, A)] = (ga, ga, ga).mapN((_, _, _)) | |
implicit val genApply: Apply[Gen] = new Apply[Gen] { | |
override def ap[A, B](gf: Gen[A => B])(ga: Gen[A]) = gf flatMap ga.map | |
override def map[A, B](ga: Gen[A])(f: A => B) = ga map f | |
} | |
implicit val unique3: Gen[(Dollar, Dollar, Dollar)] = | |
triple(dollars).suchThat { case (a, b, c) => a != b && b != c && c != a } | |
// properties ------------------------------------------------------------------- | |
property("$0 * x = $0") = forAll { (x: Int) => | |
Dollar(0) * x == Dollar(0) | |
} | |
property("$1 * x = $x") = forAll { (x: Int) => | |
Dollar(1) * x == Dollar(x) | |
} | |
property("$a * b = $b * a") = forAll { (a: Int, b: Int) => | |
Dollar(a) * b == Dollar(b) * a | |
} | |
property("($a * b) * c = $a * (b * c)") = forAll { (a: Int, b: Int, c: Int) => | |
(Dollar(a) * b) * c == Dollar(a) * (b * c) | |
} | |
property("$a == $b <=> a == b") = forAll { (a: Int, b: Int) => | |
(Dollar(a) == Dollar(b)) == (a == b) | |
} | |
property("$a == $b <=> $a.equals($b)") = forAll(double(dollars)) { case (a, b) => | |
(a equals b) == (a == b) | |
} | |
property("equals is reflexive") = forAll(dollars) { (a) => | |
a equals a | |
} | |
property("equals is symmetric") = forAll(double(dollars)) { case (a, b) => | |
(a equals b) == (b equals a) | |
} | |
property("equals is transitive") = forAll( for { | |
oneOf3 <- unique3.map { case (a, b, c)=> Gen.oneOf(a, b, c) } | |
x <- oneOf3 | |
y <- oneOf3.suchThat(_ equals x) | |
z <- oneOf3.retryUntil(_ equals y) | |
} yield (x, z)) { case ((x, z)) => x equals z } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment