Skip to content

Commit f3a4329

Browse files
committed
round out tests
1 parent ac067e7 commit f3a4329

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

mllib-local/src/test/scala/org/apache/spark/ml/linalg/VectorsSuite.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,18 @@ class VectorsSuite extends SparkMLFunSuite {
366366
assert(v.slice(Array(2, 0)) === new SparseVector(2, Array(0), Array(2.2)))
367367
assert(v.slice(Array(2, 0, 3, 4)) === new SparseVector(4, Array(0, 3), Array(2.2, 4.4)))
368368
}
369+
370+
test("sparse vector only support non-negative length") {
371+
val v1 = Vectors.sparse(0, Array.emptyIntArray, Array.emptyDoubleArray)
372+
val v2 = Vectors.sparse(0, Array.empty[(Int, Double)])
373+
assert(v1.size === 0)
374+
assert(v2.size === 0)
375+
376+
intercept[IllegalArgumentException] {
377+
Vectors.sparse(-1, Array(1), Array(2.0))
378+
}
379+
intercept[IllegalArgumentException] {
380+
Vectors.sparse(-1, Array((1, 2.0)))
381+
}
382+
}
369383
}

mllib/src/test/scala/org/apache/spark/mllib/linalg/VectorsSuite.scala

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,6 @@ class VectorsSuite extends SparkFunSuite with Logging {
113113
assert(vec.toArray === arr)
114114
}
115115

116-
test("zero-length sparse vector") {
117-
val v1 = Vectors.sparse(0, Array.emptyIntArray, Array.emptyDoubleArray)
118-
val v2 = Vectors.sparse(0, Array.empty[(Int, Double)])
119-
assert(v1.size === 0)
120-
assert(v2.size === 0)
121-
}
122-
123116
test("sparse argmax") {
124117
val vec = Vectors.sparse(0, Array.empty[Int], Array.empty[Double]).asInstanceOf[SparseVector]
125118
assert(vec.argmax === -1)
@@ -502,4 +495,18 @@ class VectorsSuite extends SparkFunSuite with Logging {
502495
assert(mlDenseVectorToArray(dv) === mlDenseVectorToArray(newDV))
503496
assert(mlSparseVectorToArray(sv) === mlSparseVectorToArray(newSV))
504497
}
498+
499+
test("sparse vector only support non-negative length") {
500+
val v1 = Vectors.sparse(0, Array.emptyIntArray, Array.emptyDoubleArray)
501+
val v2 = Vectors.sparse(0, Array.empty[(Int, Double)])
502+
assert(v1.size === 0)
503+
assert(v2.size === 0)
504+
505+
intercept[IllegalArgumentException] {
506+
Vectors.sparse(-1, Array(1), Array(2.0))
507+
}
508+
intercept[IllegalArgumentException] {
509+
Vectors.sparse(-1, Array((1, 2.0)))
510+
}
511+
}
505512
}

0 commit comments

Comments
 (0)