-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from LibreCybernetics/improvements
Improvements
- Loading branch information
Showing
5 changed files
with
119 additions
and
49 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
69 changes: 69 additions & 0 deletions
69
scalatest/src/main/scala/dev/librecybernetics/data/BijectionScalatest.scala
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package dev.librecybernetics.data | ||
|
||
import scala.util.Try | ||
|
||
import org.scalacheck.Arbitrary | ||
import org.scalatest | ||
import org.scalatest.{Assertion, Succeeded} | ||
import org.scalatest.matchers.should.Matchers.* | ||
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks.* | ||
|
||
extension [A, B](mapBijection: MapBijection[A, B]) | ||
def checkForward(using Arbitrary[A]): Assertion = | ||
forAll[A, Assertion] { case a: A => | ||
whenever(mapBijection.isDefined(a)) { | ||
// Self Inverse | ||
mapBijection(a).flatMap(mapBijection.reverse(_)) should contain(a) | ||
} | ||
} | ||
|
||
def checkReverse(using Arbitrary[B]): Assertion = | ||
mapBijection.flip.checkForward | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.TryPartial")) | ||
def check(using Arbitrary[A], Arbitrary[B]): Assertion = | ||
(for | ||
Succeeded <- Try(checkForward) | ||
Succeeded <- Try(checkReverse) | ||
yield Succeeded).get | ||
end extension | ||
|
||
extension [A, B](pfnBijection: PFnBijection[A, B]) | ||
def checkForward(using Arbitrary[A]): Assertion = | ||
forAll[A, Assertion] { case a: A => | ||
whenever(pfnBijection.isDefined(a)) { | ||
// Self Inverse | ||
pfnBijection(a).flatMap(pfnBijection.reverse(_)) should contain(a) | ||
} | ||
} | ||
|
||
def checkReverse(using Arbitrary[B]): Assertion = | ||
pfnBijection.flip.checkForward | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.TryPartial")) | ||
def check(using Arbitrary[A], Arbitrary[B]): Assertion = | ||
(for | ||
Succeeded <- Try(checkForward) | ||
Succeeded <- Try(checkReverse) | ||
yield Succeeded).get | ||
end extension | ||
|
||
extension [A, B](fnBijection: FnBijection[A, B]) | ||
def checkForward(using Arbitrary[A]): Assertion = | ||
forAll[A, Assertion] { case a: A => | ||
whenever(fnBijection.isDefined(a)) { | ||
// Self Inverse | ||
fnBijection.reverse(fnBijection(a)) shouldBe a | ||
} | ||
} | ||
|
||
def checkReverse(using Arbitrary[B]): Assertion = | ||
fnBijection.flip.checkForward | ||
|
||
@SuppressWarnings(Array("org.wartremover.warts.TryPartial")) | ||
def check(using Arbitrary[A], Arbitrary[B]): Assertion = | ||
(for | ||
Succeeded <- Try(checkForward) | ||
Succeeded <- Try(checkReverse) | ||
yield Succeeded).get | ||
end extension |