[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 <liu.xian3@zte.com.cn>
Signed-off-by: Sean Owen <sean.owen@databricks.com>
This commit is contained in:
liuxian 2018-08-27 12:05:33 -05:00 committed by Sean Owen
parent 6193a202aa
commit 381a967a76

View file

@ -194,4 +194,50 @@ class OpenHashMapSuite extends SparkFunSuite with Matchers {
val numInvalidValues = map.iterator.count(_._2 == 0) val numInvalidValues = map.iterator.count(_._2 == 0)
assertResult(0)(numInvalidValues) assertResult(0)(numInvalidValues)
} }
test("distinguish between the 0/0.0/0L and null") {
val specializedMap1 = new OpenHashMap[String, Long]
specializedMap1("a") = null.asInstanceOf[Long]
specializedMap1("b") = 0L
assert(specializedMap1.contains("a"))
assert(!specializedMap1.contains("c"))
// null.asInstance[Long] will return 0L
assert(specializedMap1("a") === 0L)
assert(specializedMap1("b") === 0L)
// If the data type is in @specialized annotation, and
// the `key` is not be contained, the `map(key)` will return 0
assert(specializedMap1("c") === 0L)
val specializedMap2 = new OpenHashMap[String, Double]
specializedMap2("a") = null.asInstanceOf[Double]
specializedMap2("b") = 0.toDouble
assert(specializedMap2.contains("a"))
assert(!specializedMap2.contains("c"))
// null.asInstance[Double] will return 0.0
assert(specializedMap2("a") === 0.0)
assert(specializedMap2("b") === 0.0)
assert(specializedMap2("c") === 0.0)
val map1 = new OpenHashMap[String, Short]
map1("a") = null.asInstanceOf[Short]
map1("b") = 0.toShort
assert(map1.contains("a"))
assert(!map1.contains("c"))
// null.asInstance[Short] will return 0
assert(map1("a") === 0)
assert(map1("b") === 0)
// If the data type is not in @specialized annotation, and
// the `key` is not be contained, the `map(key)` will return null
assert(map1("c") === null)
val map2 = new OpenHashMap[String, Float]
map2("a") = null.asInstanceOf[Float]
map2("b") = 0.toFloat
assert(map2.contains("a"))
assert(!map2.contains("c"))
// null.asInstance[Float] will return 0.0
assert(map2("a") === 0.0)
assert(map2("b") === 0.0)
assert(map2("c") === null)
}
} }