Skip to content

Commit

Permalink
Added test to reproduce bug described in scala#239. Test fails, as ex…
Browse files Browse the repository at this point in the history
…pected...
  • Loading branch information
sageserpent-open committed Apr 26, 2024
1 parent fc72c06 commit bcc2b6e
Showing 1 changed file with 100 additions and 0 deletions.
100 changes: 100 additions & 0 deletions src/test/scala/scala/collection/decorators/MapDecoratorTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,104 @@ class MapDecoratorTest {
// Assert.assertEquals(expected, zipped2)
}

@Test
def mergingByKeyPerformsFullOuterJoin(): Unit = {
val arthur = "arthur.txt"

val tyson = "tyson.txt"

val sandra = "sandra.txt"

val allKeys = Set(arthur, tyson, sandra)

val sharedValue = 1

val ourChanges = Map(
(
arthur,
sharedValue
),
(
tyson,
2
)
)

{
// In this test case, none of the associated values collide across keys...

val theirChanges = Map(
(
arthur,
sharedValue
),
(
sandra,
3
)
)

ourChanges -> theirChanges

ourChanges.mergeByKey(theirChanges)

Assert.assertEquals("Expect all the keys to appear in an outer join.", ourChanges.mergeByKey(theirChanges).keys, allKeys)

theirChanges.mergeByKey(ourChanges)

Assert.assertEquals("Expect all the keys to appear in an outer join.", theirChanges.mergeByKey(ourChanges).keys, allKeys)

Assert.assertTrue("Expect the same associated values to appear in the join taken either way around, albeit swapped around and not necessarily in the same key order.",
ourChanges
.mergeByKey(theirChanges)
.values
.map(_.swap)
.toList
.sorted
.sameElements(theirChanges.mergeByKey(ourChanges).values.toList.sorted))

Assert.assertEquals(ourChanges.mergeByKey(theirChanges).keySet, theirChanges
.mergeByKey(ourChanges)
.keys)
}

{
// In this test case, associated values collide across keys...

val theirChangesRedux = Map(
(
arthur,
sharedValue
),
(
sandra,
sharedValue
)
)

ourChanges -> theirChangesRedux

ourChanges.mergeByKey(theirChangesRedux)

Assert.assertEquals("Expect all the keys to appear in an outer join, but they don't.", ourChanges.mergeByKey(theirChangesRedux).keys, allKeys)

theirChangesRedux.mergeByKey(ourChanges)

Assert.assertEquals("Expect all the keys to appear in an outer join, and they do, good.", theirChangesRedux.mergeByKey(ourChanges).keys, allKeys)

Assert.assertTrue("Expect the same associated values to appear in the join taken either way around, albeit swapped around and not necessarily in the same key order.",
ourChanges
.mergeByKey(theirChangesRedux)
.values
.map(_.swap)
.toList
.sorted
.sameElements(theirChangesRedux.mergeByKey(ourChanges).values.toList.sorted))

Assert.assertEquals("Expect these to be equal, but they aren't.", ourChanges.mergeByKey(theirChangesRedux).keySet, theirChangesRedux
.mergeByKey(ourChanges)
.keys)
}
}

}

0 comments on commit bcc2b6e

Please sign in to comment.