Created
December 21, 2018 13:46
-
-
Save yasuabe/4694c49d86e2274ffdf78fff57a8810a to your computer and use it in GitHub Desktop.
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 adcal_1222 | |
import adcal_1222.Amida._ | |
import cats.instances.AllInstances | |
import cats.kernel.laws.discipline.GroupTests | |
import cats.{Eq, Group} | |
import org.scalacheck.{Arbitrary, Gen} | |
import org.specs2.Specification | |
import org.typelevel.discipline.specs2.Discipline | |
case class Amida(legs: List[Int]) { | |
def combine(another: Amida): Amida = Amida(legs ++ another.legs) | |
def inverse: Amida = Amida(legs.reverse) | |
def permutation: Vector[Int] = legs.foldLeft((0 until Lines).toVector) { (acc, l) => | |
acc.updated(l, acc(l + 1)).updated(l + 1, acc(l)) | |
} | |
} | |
object Amida { | |
val Lines = 4 | |
val Empty = Amida(List.empty[Int]) | |
implicit val amidaGroup: Group[Amida] = new Group[Amida] { | |
def inverse(a: Amida): Amida = a.inverse | |
def empty: Amida = Amida.Empty | |
def combine(x: Amida, y: Amida): Amida = x combine y | |
} | |
} | |
class GroupSpec extends Specification with Discipline with AllInstances { | |
import Gen.{chooseNum, listOfN} | |
implicit val eqAmida: Eq[Amida] = _.permutation == _.permutation | |
implicit val arbAmida: Arbitrary[Amida] = Arbitrary { | |
for { | |
len <- chooseNum(0, 5) | |
legs <- listOfN(len, chooseNum(0, Lines - 2)) | |
} yield Amida(legs) | |
} | |
private val check = checkAll("group of Amida", GroupTests[Amida].group) | |
def is = s2"Amida satisfies group laws $check" | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment