Skip to content

Commit bcc2b6e

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

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

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

+100
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,104 @@ 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+
ourChanges -> theirChanges
115+
116+
ourChanges.mergeByKey(theirChanges)
117+
118+
Assert.assertEquals("Expect all the keys to appear in an outer join.", ourChanges.mergeByKey(theirChanges).keys, allKeys)
119+
120+
theirChanges.mergeByKey(ourChanges)
121+
122+
Assert.assertEquals("Expect all the keys to appear in an outer join.", theirChanges.mergeByKey(ourChanges).keys, allKeys)
123+
124+
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.",
125+
ourChanges
126+
.mergeByKey(theirChanges)
127+
.values
128+
.map(_.swap)
129+
.toList
130+
.sorted
131+
.sameElements(theirChanges.mergeByKey(ourChanges).values.toList.sorted))
132+
133+
Assert.assertEquals(ourChanges.mergeByKey(theirChanges).keySet, theirChanges
134+
.mergeByKey(ourChanges)
135+
.keys)
136+
}
137+
138+
{
139+
// In this test case, associated values collide across keys...
140+
141+
val theirChangesRedux = Map(
142+
(
143+
arthur,
144+
sharedValue
145+
),
146+
(
147+
sandra,
148+
sharedValue
149+
)
150+
)
151+
152+
ourChanges -> theirChangesRedux
153+
154+
ourChanges.mergeByKey(theirChangesRedux)
155+
156+
Assert.assertEquals("Expect all the keys to appear in an outer join, but they don't.", ourChanges.mergeByKey(theirChangesRedux).keys, allKeys)
157+
158+
theirChangesRedux.mergeByKey(ourChanges)
159+
160+
Assert.assertEquals("Expect all the keys to appear in an outer join, and they do, good.", theirChangesRedux.mergeByKey(ourChanges).keys, allKeys)
161+
162+
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.",
163+
ourChanges
164+
.mergeByKey(theirChangesRedux)
165+
.values
166+
.map(_.swap)
167+
.toList
168+
.sorted
169+
.sameElements(theirChangesRedux.mergeByKey(ourChanges).values.toList.sorted))
170+
171+
Assert.assertEquals("Expect these to be equal, but they aren't.", ourChanges.mergeByKey(theirChangesRedux).keySet, theirChangesRedux
172+
.mergeByKey(ourChanges)
173+
.keys)
174+
}
175+
}
176+
77177
}

0 commit comments

Comments
 (0)