Skip to content

Create SaddlePointsTestGenerator. Update SaddlePointsTest. Refs #370,… #471

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions exercises/saddle-points/example.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
case class Matrix(values: List[List[Int]]) {
lazy val saddlePoints: Set[(Int, Int)] = {
val rowMaxes = values.map(_.max).toVector
val columns = values.transpose
val colMins = columns.map(_.min).toVector
(for {
i <- 0 until values.length
j <- 0 until columns.length
if rowMaxes(i) == colMins(j)
} yield (i, j)).toSet
if (values.isEmpty || columns.isEmpty) {
Set()
} else {
val rowMaxes = values.map(_.max).toVector
val colMins = columns.map(_.min).toVector
(for {
i <- values.indices
j <- columns.indices
if rowMaxes(i) == colMins(j)
} yield (i, j)).toSet
}
}
}
35 changes: 21 additions & 14 deletions exercises/saddle-points/src/test/scala/SaddlePointsTest.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
import org.scalatest.{Matchers, FlatSpec}
import org.scalatest.{Matchers, FunSuite}

class SaddlePointsSpecs extends FlatSpec with Matchers {
it should "handle one saddle" in {
val points = Matrix(List(List(9, 8, 7), List(5, 3, 2), List(6, 6, 7))).saddlePoints
points should be (Set((1, 0)))
/** @version 1.0.0 */
class SaddlePointsTest extends FunSuite with Matchers {

test("Can identify single saddle point") {
Matrix(List(List(9, 8, 7), List(5, 3, 2), List(6, 6, 7))).saddlePoints should be(
Set((1, 0)))
}

test("Can identify that empty matrix has no saddle points") {
pending
Matrix(List(List())).saddlePoints should be(Set())
}

it should "handle multiple saddles" in {
test("Can identify lack of saddle points when there are none") {
pending
val points = Matrix(List(List(5, 3, 5, 4), List(6, 4, 7, 3), List(5, 1, 5, 3))).saddlePoints
points should be (Set((0, 0), (0, 2), (2, 0), (2, 2)))
Matrix(List(List(1, 2, 3), List(3, 1, 2), List(2, 3, 1))).saddlePoints should be(
Set())
}

it should "handle no saddles" in {
test("Can identify multiple saddle points") {
pending
val points = Matrix(List(List(2, 1), List(1, 2))).saddlePoints
points should be (Set())
Matrix(List(List(4, 5, 4), List(3, 5, 5), List(1, 5, 4))).saddlePoints should be(
Set((0, 1), (1, 1), (2, 1)))
}

it should "handle empty matrix" in {
test("Can identify saddle point in bottom right corner") {
pending
val points = Matrix(List()).saddlePoints
points should be (Set())
Matrix(List(List(8, 7, 9), List(6, 7, 6), List(3, 2, 5))).saddlePoints should be(
Set((2, 2)))
}
}
35 changes: 35 additions & 0 deletions testgen/src/main/scala/SaddlePointsTestGenerator.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.io.File

import testgen.TestSuiteBuilder._
import testgen._

object SaddlePointsTestGenerator {
def toString(expected: CanonicalDataParser.Expected): String = {
expected match {
case Right(xs: List[Map[String, Int]]) =>
val tuples = xs.map(m => s"(${m("row")}, ${m("column")})").mkString(", ")
s"""Set($tuples)"""
case _ => throw new IllegalArgumentException
}
}

def fromLabeledTest(argNames: String*): ToTestCaseData =
withLabeledTest { sut =>
labeledTest =>
val args = sutArgs(labeledTest.result, argNames: _*)
val property = labeledTest.property
val sutCall =
s"""Matrix($args).$property"""
val expected = toString(labeledTest.expected)
TestCaseData(labeledTest.description, sutCall, expected)
}

def main(args: Array[String]): Unit = {
val file = new File("src/main/resources/saddle-points.json")

val code = TestSuiteBuilder.build(file, fromLabeledTest("input"))
println(s"-------------")
println(code)
println(s"-------------")
}
}