Skip to content

Commit 4fe84ca

Browse files
Added test to reproduce bug described in #239. Test fails, as expected...
1 parent fc72c06 commit 4fe84ca

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

src/test/scala/scala/collection/decorators/MapDecoratorTest.scala

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,92 @@ class MapDecoratorTest {
7474
// Assert.assertEquals(expected, zipped2)
7575
}
7676

77+
@Test
78+
def mergingByKeyPerformsFullOuterJoin(): Unit = {
79+
val arthur = "arthur.txt"
80+
81+
val tyson = "tyson.txt"
82+
83+
val sandra = "sandra.txt"
84+
85+
val allKeys = Set(arthur, tyson, sandra)
86+
87+
val sharedValue = 1
88+
89+
val ourChanges = Map(
90+
(
91+
arthur,
92+
sharedValue
93+
),
94+
(
95+
tyson,
96+
2
97+
)
98+
)
99+
100+
{
101+
// In this test case, none of the associated values collide across keys...
102+
103+
val theirChanges = Map(
104+
(
105+
arthur,
106+
sharedValue
107+
),
108+
(
109+
sandra,
110+
3
111+
)
112+
)
113+
114+
Assert.assertEquals("Expect the same keys to appear in the join taken either way around.", ourChanges.mergeByKey(theirChanges).keySet, theirChanges
115+
.mergeByKey(ourChanges)
116+
.keys)
117+
118+
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.",
119+
ourChanges
120+
.mergeByKey(theirChanges)
121+
.values
122+
.map(_.swap)
123+
.toList
124+
.sorted
125+
.sameElements(theirChanges.mergeByKey(ourChanges).values.toList.sorted))
126+
127+
Assert.assertEquals("Expect all the keys to appear in an outer join.", ourChanges.mergeByKey(theirChanges).keys, allKeys)
128+
129+
Assert.assertEquals("Expect all the keys to appear in an outer join.", theirChanges.mergeByKey(ourChanges).keys, allKeys)
130+
}
131+
132+
{
133+
// In this test case, associated values collide across keys...
134+
135+
val theirChangesRedux = Map(
136+
(
137+
arthur,
138+
sharedValue
139+
),
140+
(
141+
sandra,
142+
sharedValue
143+
)
144+
)
145+
146+
Assert.assertEquals("Expect the same keys to appear in the join taken either way around.", ourChanges.mergeByKey(theirChangesRedux).keySet, theirChangesRedux
147+
.mergeByKey(ourChanges)
148+
.keys)
149+
150+
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.",
151+
ourChanges
152+
.mergeByKey(theirChangesRedux)
153+
.values
154+
.map(_.swap)
155+
.toList
156+
.sorted
157+
.sameElements(theirChangesRedux.mergeByKey(ourChanges).values.toList.sorted))
158+
159+
Assert.assertEquals("Expect all the keys to appear in an outer join.", ourChanges.mergeByKey(theirChangesRedux).keys, allKeys)
160+
161+
Assert.assertEquals("Expect all the keys to appear in an outer join.", theirChangesRedux.mergeByKey(ourChanges).keys, allKeys)
162+
}
163+
}
164+
77165
}

0 commit comments

Comments
 (0)