Skip to content

Commit 9956365

Browse files
committed
add ut for 0-based loadlibsvm exception
1 parent 5bd1f9a commit 9956365

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

mllib/src/test/scala/org/apache/spark/mllib/util/MLUtilsSuite.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import breeze.linalg.{squaredDistance => breezeSquaredDistance}
2727
import com.google.common.base.Charsets
2828
import com.google.common.io.Files
2929

30+
import org.apache.spark.SparkException
3031
import org.apache.spark.mllib.linalg.{DenseVector, SparseVector, Vectors}
3132
import org.apache.spark.mllib.regression.LabeledPoint
3233
import org.apache.spark.mllib.util.MLUtils._
@@ -109,6 +110,23 @@ class MLUtilsSuite extends FunSuite with MLlibTestSparkContext {
109110
Utils.deleteRecursively(tempDir)
110111
}
111112

113+
test("loadLibSVMFile throws SparkException when passing a zero-based vector") {
114+
val lines =
115+
"""
116+
|0
117+
|0 0:4.0 4:5.0 6:6.0
118+
""".stripMargin
119+
val tempDir = Utils.createTempDir()
120+
val file = new File(tempDir.getPath, "part-00000")
121+
Files.write(lines, file, Charsets.US_ASCII)
122+
val path = tempDir.toURI.toString
123+
124+
intercept[SparkException] {
125+
val pointsWithoutNumFeatures = loadLibSVMFile(sc, path).collect()
126+
}
127+
Utils.deleteRecursively(tempDir)
128+
}
129+
112130
test("saveAsLibSVMFile") {
113131
val examples = sc.parallelize(Seq(
114132
LabeledPoint(1.1, Vectors.sparse(3, Seq((0, 1.23), (2, 4.56)))),

0 commit comments

Comments
 (0)