Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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)
}
}