Skip to content

Commit

Permalink
add tests for LocalIndexEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Dec 30, 2014
1 parent 80b8e61 commit d0f99d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Expand Up @@ -789,7 +789,7 @@ private object ALS extends Logging {
*
* @param numBlocks number of blocks
*/
private class LocalIndexEncoder(numBlocks: Int) extends Serializable {
private[recommendation] class LocalIndexEncoder(numBlocks: Int) extends Serializable {

require(numBlocks > 0, s"numBlocks must be positive but found $numBlocks.")

Expand Down
@@ -0,0 +1,44 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.spark.ml.recommendation

import java.util.Random

import org.scalatest.FunSuite

import org.apache.spark.ml.recommendation.ALS.LocalIndexEncoder
import org.apache.spark.mllib.util.MLlibTestSparkContext

class ALSSuite extends FunSuite with MLlibTestSparkContext {

test("local index encoder") {
val random = new Random
for (numBlocks <- Seq(1, 2, 5, 10, 20, 50, 100)) {
val encoder = new LocalIndexEncoder(numBlocks)
val maxLocalIndex = Int.MaxValue / numBlocks
val tests = Seq.fill(5)((random.nextInt(numBlocks), random.nextInt(maxLocalIndex))) ++
Seq((0, 0), (numBlocks - 1, maxLocalIndex))
tests.foreach { case (blockId, localIndex) =>
val err = s"Failed with numBlocks=$numBlocks, blockId=$blockId, and localIndex=$localIndex."
val encoded = encoder.encode(blockId, localIndex)
assert(encoder.blockId(encoded) === blockId, err)
assert(encoder.localIndex(encoded) === localIndex, err)
}
}
}
}

0 comments on commit d0f99d3

Please sign in to comment.