Skip to content

Commit

Permalink
[SPARK-25249][CORE][TEST] add a unit test for OpenHashMap
Browse files Browse the repository at this point in the history
## 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>
  • Loading branch information
10110346 authored and srowen committed Aug 27, 2018
1 parent 6193a20 commit 381a967
Showing 1 changed file with 46 additions and 0 deletions.
Expand Up @@ -194,4 +194,50 @@ class OpenHashMapSuite extends SparkFunSuite with Matchers {
val numInvalidValues = map.iterator.count(_._2 == 0)
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)
}
}

0 comments on commit 381a967

Please sign in to comment.