1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
package org.apache.spark.util.collection
import scala.collection.mutable.HashSet
import org.scalatest.FunSuite
class OpenHashMapSuite extends FunSuite {
test("initialization") {
val goodMap1 = new OpenHashMap[String, Int](1)
assert(goodMap1.size === 0)
val goodMap2 = new OpenHashMap[String, Int](255)
assert(goodMap2.size === 0)
val goodMap3 = new OpenHashMap[String, String](256)
assert(goodMap3.size === 0)
intercept[IllegalArgumentException] {
new OpenHashMap[String, Int](1 << 30) // Invalid map size: bigger than 2^29
}
intercept[IllegalArgumentException] {
new OpenHashMap[String, Int](-1)
}
intercept[IllegalArgumentException] {
new OpenHashMap[String, String](0)
}
}
test("primitive value") {
val map = new OpenHashMap[String, Int]
for (i <- 1 to 1000) {
map(i.toString) = i
assert(map(i.toString) === i)
}
assert(map.size === 1000)
assert(map(null) === 0)
map(null) = -1
assert(map.size === 1001)
assert(map(null) === -1)
for (i <- 1 to 1000) {
assert(map(i.toString) === i)
}
// Test iterator
val set = new HashSet[(String, Int)]
for ((k, v) <- map) {
set.add((k, v))
}
val expected = (1 to 1000).map(x => (x.toString, x)) :+ (null.asInstanceOf[String], -1)
assert(set === expected.toSet)
}
test("non-primitive value") {
val map = new OpenHashMap[String, String]
for (i <- 1 to 1000) {
map(i.toString) = i.toString
assert(map(i.toString) === i.toString)
}
assert(map.size === 1000)
assert(map(null) === null)
map(null) = "-1"
assert(map.size === 1001)
assert(map(null) === "-1")
for (i <- 1 to 1000) {
assert(map(i.toString) === i.toString)
}
// Test iterator
val set = new HashSet[(String, String)]
for ((k, v) <- map) {
set.add((k, v))
}
val expected = (1 to 1000).map(_.toString).map(x => (x, x)) :+ (null.asInstanceOf[String], "-1")
assert(set === expected.toSet)
}
test("null keys") {
val map = new OpenHashMap[String, String]()
for (i <- 1 to 100) {
map(i.toString) = i.toString
}
assert(map.size === 100)
assert(map(null) === null)
map(null) = "hello"
assert(map.size === 101)
assert(map(null) === "hello")
}
test("null values") {
val map = new OpenHashMap[String, String]()
for (i <- 1 to 100) {
map(i.toString) = null
}
assert(map.size === 100)
assert(map("1") === null)
assert(map(null) === null)
assert(map.size === 100)
map(null) = null
assert(map.size === 101)
assert(map(null) === null)
}
test("changeValue") {
val map = new OpenHashMap[String, String]()
for (i <- 1 to 100) {
map(i.toString) = i.toString
}
assert(map.size === 100)
for (i <- 1 to 100) {
val res = map.changeValue(i.toString, { assert(false); "" }, v => {
assert(v === i.toString)
v + "!"
})
assert(res === i + "!")
}
// Iterate from 101 to 400 to make sure the map grows a couple of times, because we had a
// bug where changeValue would return the wrong result when the map grew on that insert
for (i <- 101 to 400) {
val res = map.changeValue(i.toString, { i + "!" }, v => { assert(false); v })
assert(res === i + "!")
}
assert(map.size === 400)
assert(map(null) === null)
map.changeValue(null, { "null!" }, v => { assert(false); v })
assert(map.size === 401)
map.changeValue(null, { assert(false); "" }, v => {
assert(v === "null!")
"null!!"
})
assert(map.size === 401)
}
test("inserting in capacity-1 map") {
val map = new OpenHashMap[String, String](1)
for (i <- 1 to 100) {
map(i.toString) = i.toString
}
assert(map.size === 100)
for (i <- 1 to 100) {
assert(map(i.toString) === i.toString)
}
}
}
|