Created
October 14, 2018 17:37
-
-
Save wolfendale/f470a6b4d2421536bbb2d83d0a89d370 to your computer and use it in GitHub Desktop.
Group
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
package wolfendale.scalacheck.regexp.models | |
sealed trait Group[A] { | |
def intersect(other: Group[A]): Group[A] | |
def ++(other: Group[A]): Group[A] | |
def --(other: Group[A]): Group[A] | |
} | |
object Group { | |
final case class Inclusion[A](values: Set[A]) extends Group[A] { | |
override def intersect(other: Group[A]): Group[A] = | |
other match { | |
case Inclusion(others) => | |
Inclusion(values intersect others) | |
case Exclusion(others) => | |
Inclusion(values -- others) | |
} | |
override def ++(other: Group[A]): Group[A] = | |
other match { | |
case Inclusion(others) => | |
Inclusion(values ++ others) | |
case Exclusion(others) => | |
Exclusion(others -- values) | |
} | |
override def --(other: Group[A]): Group[A] = | |
other match { | |
case Inclusion(others) => | |
Inclusion(values -- others) | |
case Exclusion(others) => | |
Inclusion(values intersect others) | |
} | |
} | |
final case class Exclusion[A](values: Set[A]) extends Group[A] { | |
override def intersect(other: Group[A]): Group[A] = | |
other match { | |
case Inclusion(others) => | |
Inclusion(others -- values) | |
case Exclusion(others) => | |
Exclusion(values ++ others) | |
} | |
override def ++(other: Group[A]): Group[A] = | |
other match { | |
case Inclusion(others) => | |
Exclusion(values -- others) | |
case Exclusion(others) => | |
Exclusion(values intersect others) | |
} | |
override def --(other: Group[A]): Group[A] = | |
other match { | |
case Inclusion(others) => | |
Exclusion(values ++ others) | |
case Exclusion(others) => | |
Inclusion(others -- values) | |
} | |
} | |
def apply[A](values: Set[A]): Group[A] = | |
Inclusion(values) | |
} |
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
package wolfendale.scalacheck.regexp.models | |
import org.scalatest.{FreeSpec, MustMatchers} | |
class GroupSpec extends FreeSpec with MustMatchers { | |
"a group" - { | |
"must return the intersection of" - { | |
"an inclusion and an inclusion" in { | |
val a = Group.Inclusion(Set(1, 2, 3)) | |
val b = Group.Inclusion(Set(2, 3, 4)) | |
(a intersect b) mustEqual Group.Inclusion(Set(2, 3)) | |
} | |
"an inclusion and an exclusion" in { | |
val a = Group.Inclusion(Set(1, 2, 3)) | |
val b = Group.Exclusion(Set(2, 3, 4)) | |
(a intersect b) mustEqual Group.Inclusion(Set(1)) | |
} | |
"an exclusion and an inclusion" in { | |
val a = Group.Exclusion(Set(2, 3, 4)) | |
val b = Group.Inclusion(Set(1, 2, 3)) | |
(a intersect b) mustEqual Group.Inclusion(Set(1)) | |
} | |
"an exclusion and an exclusion" in { | |
val a = Group.Exclusion(Set(1, 2, 3)) | |
val b = Group.Exclusion(Set(2, 3, 4)) | |
(a intersect b) mustEqual Group.Exclusion(Set(1, 2, 3, 4)) | |
} | |
} | |
"must return the addition of" - { | |
"an inclusion and an inclusion" in { | |
val a = Group.Inclusion(Set(1, 2, 3)) | |
val b = Group.Inclusion(Set(2, 3, 4)) | |
(a ++ b) mustEqual Group.Inclusion(Set(1, 2, 3, 4)) | |
} | |
"an inclusion and an exclusion" in { | |
val a = Group.Inclusion(Set(1, 2, 3)) | |
val b = Group.Exclusion(Set(2, 3, 4)) | |
(a ++ b) mustEqual Group.Exclusion(Set(4)) | |
} | |
"an exclusion and an inclusion" in { | |
val a = Group.Exclusion(Set(1, 2, 3)) | |
val b = Group.Inclusion(Set(2, 3, 4)) | |
(a ++ b) mustEqual Group.Exclusion(Set(1)) | |
} | |
"an exclusion and an exclusion" in { | |
val a = Group.Exclusion(Set(1, 2, 3)) | |
val b = Group.Exclusion(Set(2, 3, 4)) | |
(a ++ b) mustEqual Group.Exclusion(Set(2, 3)) | |
} | |
} | |
"must return the subtraction of" - { | |
"an inclusion and an inclusion" in { | |
val a = Group.Inclusion(Set(1, 2, 3)) | |
val b = Group.Inclusion(Set(2, 3, 4)) | |
(a -- b) mustEqual Group.Inclusion(Set(1)) | |
} | |
"an inclusion and an exclusion" in { | |
val a = Group.Inclusion(Set(1, 2, 3)) | |
val b = Group.Exclusion(Set(2, 3, 4)) | |
(a -- b) mustEqual Group.Inclusion(Set(2, 3)) | |
} | |
"an exclusion and an inclusion" in { | |
val a = Group.Exclusion(Set(1, 2, 3)) | |
val b = Group.Inclusion(Set(2, 3, 4)) | |
(a -- b) mustEqual Group.Exclusion(Set(1, 2, 3, 4)) | |
} | |
"an exclusion and an exclusion" in { | |
val a = Group.Exclusion(Set(1, 2, 3)) | |
val b = Group.Exclusion(Set(2, 3, 4)) | |
(a -- b) mustEqual Group.Inclusion(Set(4)) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment