streams lab − equality of sets − Finding Neighbors
Hello,
I am facing an issue about Scala's set equality on the Finding Neighbors step. As said in this note from #79 (closed) (or from its definition), Sets are unordered. Hence, the equality between two sets would be defined with their contents.
However, I faced a quite strange issue when implementing the test and I can not find by my own why it does not work, despite testing.
My test simply consisted of copy-pasting the given example.
test("my own test") {
new Level1:
assert( neighborsWithHistory(Block(Pos(1,1),Pos(1,1)), List(Move.Left,Move.Up)).toSet == Set(
(Block(Pos(1,2),Pos(1,3)), List(Right,Left,Move.Up)),
(Block(Pos(2,1),Pos(3,1)), List(Move.Down,Left,Move.Up))
))
}
But this test is marked as failed… :(
==> X streams.BloxorzSuite.my own test 0.071s munit.FailException: C:\…\cs210-streams\src\test\scala\streams\BloxorzSuite.scala:48 assertion failed
47: (Block(Pos(2,1),Pos(3,1)), List(Move.Down,Left,Move.Up))
48: ))
49: }
at munit.FunSuite.assert(FunSuite.scala:11)
at streams.BloxorzSuite$$anon$1.<init>(BloxorzSuite.scala:15)
at streams.BloxorzSuite.$init$$$anonfun$1(BloxorzSuite.scala:48)
Printing the two sets followed by the equality check gives me this (in the test itself which is not a pretty good practice):
streams.BloxorzSuite:
Set((Block(Pos(2,1),Pos(3,1)),List(Down, Left, Up)), (Block(Pos(1,2),Pos(1,3)),List(Right, Left, Up)))
Set((Block(Pos(1,2),Pos(1,3)),List(Right, Left, Up)), (Block(Pos(2,1),Pos(3,1)),List(Down, Left, Up)))
false
==> X streams.BloxorzSuite.my own test
And testing on a worksheet gives true ??
…
class Test extends SolutionChecker :
/* copy of level1 */
val level =
"""ooo-------
|oSoooo----
|ooooooooo-
|-ooooooooo
|-----ooToo
|------ooo-""".stripMargin
import Move.*
def lel = neighborsWithHistory(Block(Pos(1,1),Pos(1,1)), List(Move.Left,Move.Up)).toSet == Set(
(Block(Pos(1,2),Pos(1,3)), List(Right,Left,Move.Up)),
(Block(Pos(2,1),Pos(3,1)), List(Move.Down,Left,Move.Up))
)
val scala = new Test // : Test = repl.MdocSession$App$Test@7d3016c1
scala.lel // : Boolean = true
Using import Move._
inside my test makes everything working back again. So, I ask myself this question: why? I would assume that Scala considers Move.Right
and Right
to be equal, no? It is the same object.