diff --git a/src/main/scala/scatan/model/map/UndirectedGraph.scala b/src/main/scala/scatan/model/map/UndirectedGraph.scala new file mode 100644 index 00000000..72edaced --- /dev/null +++ b/src/main/scala/scatan/model/map/UndirectedGraph.scala @@ -0,0 +1,28 @@ +package scatan.model.map + +/** A graph is a set of nodes and edges. + */ +trait UndirectedGraph[Node, Edge <: UnorderedPair[Node, Node]]: + + /** @return + * the set of nodes + */ + def nodes: Set[Node] + + /** @return + * the set of edges + */ + def edges: Set[Edge] + +/** An unordered pair of elements. + * @param a + * an element + * @param b + * another element + */ +case class UnorderedPair[A, B](a: A, b: B): + override def equals(that: Any): Boolean = that match + case UnorderedPair(a1, b1) => (a == a1 && b == b1) || (a == b1 && b == a1) + case _ => false + + override def hashCode: Int = a.hashCode + b.hashCode diff --git a/src/test/scala/scatan/model/map/UnorderedPairTest.scala b/src/test/scala/scatan/model/map/UnorderedPairTest.scala new file mode 100644 index 00000000..39ddad71 --- /dev/null +++ b/src/test/scala/scatan/model/map/UnorderedPairTest.scala @@ -0,0 +1,41 @@ +package scatan.model.map + +import cats.kernel.Monoid +import cats.syntax.semigroup.* +import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks +import scatan.BaseTest +import scatan.model.map.Hexagon.given +import scatan.model.map.HexTiledMap.* + +class UnorderedPairTest extends BaseTest with ScalaCheckPropertyChecks: + + "A UnorderedPair" should "have 2 elements" in { + assertCompiles("UnorderedPair(0, 0)") + assertDoesNotCompile("UnorderedPair()") + } + + it should "be equals based on its elements" in { + forAll { (a: Int, b: Int) => + val pair1 = UnorderedPair(a, b) + val pair2 = UnorderedPair(a, b) + pair1 shouldBe pair2 + } + } + + it should "be equals also in reverse order of its elements" in { + forAll { (a: Int, b: Int) => + val pair1 = UnorderedPair(a, b) + val pair2 = UnorderedPair(b, a) + pair1 shouldBe pair2 + } + } + + it should "be not equals if its elements are different" in { + forAll { (a1: Int, b1: Int, a2: Int, b2: Int) => + whenever(a1 != a2 || b1 != b2) { + val pair1 = UnorderedPair(a1, b1) + val pair2 = UnorderedPair(a2, b2) + pair1 should not be pair2 + } + } + }