Skip to content

Commit 381a967

Browse files
10110346srowen
authored andcommitted
[SPARK-25249][CORE][TEST] add a unit test for OpenHashMap
## What changes were proposed in this pull request? This PR adds a unit test for OpenHashMap , this can help developers to distinguish between the 0/0.0/0L and null ## How was this patch tested? Closes #22241 from 10110346/openhashmap. Authored-by: liuxian <[email protected]> Signed-off-by: Sean Owen <[email protected]>
1 parent 6193a20 commit 381a967

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

core/src/test/scala/org/apache/spark/util/collection/OpenHashMapSuite.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,50 @@ class OpenHashMapSuite extends SparkFunSuite with Matchers {
194194
val numInvalidValues = map.iterator.count(_._2 == 0)
195195
assertResult(0)(numInvalidValues)
196196
}
197+
198+
test("distinguish between the 0/0.0/0L and null") {
199+
val specializedMap1 = new OpenHashMap[String, Long]
200+
specializedMap1("a") = null.asInstanceOf[Long]
201+
specializedMap1("b") = 0L
202+
assert(specializedMap1.contains("a"))
203+
assert(!specializedMap1.contains("c"))
204+
// null.asInstance[Long] will return 0L
205+
assert(specializedMap1("a") === 0L)
206+
assert(specializedMap1("b") === 0L)
207+
// If the data type is in @specialized annotation, and
208+
// the `key` is not be contained, the `map(key)` will return 0
209+
assert(specializedMap1("c") === 0L)
210+
211+
val specializedMap2 = new OpenHashMap[String, Double]
212+
specializedMap2("a") = null.asInstanceOf[Double]
213+
specializedMap2("b") = 0.toDouble
214+
assert(specializedMap2.contains("a"))
215+
assert(!specializedMap2.contains("c"))
216+
// null.asInstance[Double] will return 0.0
217+
assert(specializedMap2("a") === 0.0)
218+
assert(specializedMap2("b") === 0.0)
219+
assert(specializedMap2("c") === 0.0)
220+
221+
val map1 = new OpenHashMap[String, Short]
222+
map1("a") = null.asInstanceOf[Short]
223+
map1("b") = 0.toShort
224+
assert(map1.contains("a"))
225+
assert(!map1.contains("c"))
226+
// null.asInstance[Short] will return 0
227+
assert(map1("a") === 0)
228+
assert(map1("b") === 0)
229+
// If the data type is not in @specialized annotation, and
230+
// the `key` is not be contained, the `map(key)` will return null
231+
assert(map1("c") === null)
232+
233+
val map2 = new OpenHashMap[String, Float]
234+
map2("a") = null.asInstanceOf[Float]
235+
map2("b") = 0.toFloat
236+
assert(map2.contains("a"))
237+
assert(!map2.contains("c"))
238+
// null.asInstance[Float] will return 0.0
239+
assert(map2("a") === 0.0)
240+
assert(map2("b") === 0.0)
241+
assert(map2("c") === null)
242+
}
197243
}

0 commit comments

Comments
 (0)