Skip to content

Commit

Permalink
move unit test to a separate suite
Browse files Browse the repository at this point in the history
  • Loading branch information
mengxr committed Mar 31, 2015
1 parent 85dd559 commit ae7ac3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ case class CreateArray(children: Seq[Expression]) extends Expression {

/**
* Returns a Row containing the evaluation of all children expressions.
* TODO: [[CreateStruct]] does not support codegen.
*/
case class CreateStruct(children: Seq[NamedExpression]) extends Expression {
override type EvaluatedType = Row
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,34 @@ import org.apache.spark.sql.catalyst.analysis.UnresolvedGetField
import org.apache.spark.sql.types._


class ExpressionEvaluationSuite extends FunSuite {
class ExpressionEvaluationBaseSuite extends FunSuite {

def evaluate(expression: Expression, inputRow: Row = EmptyRow): Any = {
expression.eval(inputRow)
}

def checkEvaluation(expression: Expression, expected: Any, inputRow: Row = EmptyRow): Unit = {
val actual = try evaluate(expression, inputRow) catch {
case e: Exception => fail(s"Exception evaluating $expression", e)
}
if(actual != expected) {
val input = if(inputRow == EmptyRow) "" else s", input: $inputRow"
fail(s"Incorrect Evaluation: $expression, actual: $actual, expected: $expected$input")
}
}

def checkDoubleEvaluation(
expression: Expression,
expected: Spread[Double],
inputRow: Row = EmptyRow): Unit = {
val actual = try evaluate(expression, inputRow) catch {
case e: Exception => fail(s"Exception evaluating $expression", e)
}
actual.asInstanceOf[Double] shouldBe expected
}
}

class ExpressionEvaluationSuite extends ExpressionEvaluationBaseSuite {

test("literals") {
checkEvaluation(Literal(1), 1)
Expand Down Expand Up @@ -134,27 +161,6 @@ class ExpressionEvaluationSuite extends FunSuite {
}
}

def evaluate(expression: Expression, inputRow: Row = EmptyRow): Any = {
expression.eval(inputRow)
}

def checkEvaluation(expression: Expression, expected: Any, inputRow: Row = EmptyRow): Unit = {
val actual = try evaluate(expression, inputRow) catch {
case e: Exception => fail(s"Exception evaluating $expression", e)
}
if(actual != expected) {
val input = if(inputRow == EmptyRow) "" else s", input: $inputRow"
fail(s"Incorrect Evaluation: $expression, actual: $actual, expected: $expected$input")
}
}

def checkDoubleEvaluation(expression: Expression, expected: Spread[Double], inputRow: Row = EmptyRow): Unit = {
val actual = try evaluate(expression, inputRow) catch {
case e: Exception => fail(s"Exception evaluating $expression", e)
}
actual.asInstanceOf[Double] shouldBe expected
}

test("IN") {
checkEvaluation(In(Literal(1), Seq(Literal(1), Literal(2))), true)
checkEvaluation(In(Literal(2), Seq(Literal(1), Literal(2))), true)
Expand Down Expand Up @@ -1080,6 +1086,10 @@ class ExpressionEvaluationSuite extends FunSuite {
checkEvaluation(c1 ^ c2, 3, row)
checkEvaluation(~c1, -2, row)
}
}

// TODO: Make the tests work with codegen.
class ExpressionEvaluationWithoutCodeGenSuite extends ExpressionEvaluationBaseSuite {

test("CreateStruct") {
val row = Row(1, 2, 3)
Expand Down

0 comments on commit ae7ac3e

Please sign in to comment.