From ff53f828d439442f5586cb8ec0723029a00e19da Mon Sep 17 00:00:00 2001 From: Ric Emery Date: Mon, 2 Oct 2017 14:53:36 -0700 Subject: [PATCH] Create SaddlePointsTestGenerator. Update SaddlePointsTest. Refs #370, #331 --- exercises/saddle-points/example.scala | 18 ++++++---- .../src/test/scala/SaddlePointsTest.scala | 35 +++++++++++-------- .../scala/SaddlePointsTestGenerator.scala | 35 +++++++++++++++++++ 3 files changed, 67 insertions(+), 21 deletions(-) create mode 100644 testgen/src/main/scala/SaddlePointsTestGenerator.scala diff --git a/exercises/saddle-points/example.scala b/exercises/saddle-points/example.scala index 3e90acbd..cf9bbdef 100644 --- a/exercises/saddle-points/example.scala +++ b/exercises/saddle-points/example.scala @@ -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 + } } } diff --git a/exercises/saddle-points/src/test/scala/SaddlePointsTest.scala b/exercises/saddle-points/src/test/scala/SaddlePointsTest.scala index 48b07e60..237e7ec3 100644 --- a/exercises/saddle-points/src/test/scala/SaddlePointsTest.scala +++ b/exercises/saddle-points/src/test/scala/SaddlePointsTest.scala @@ -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))) } } diff --git a/testgen/src/main/scala/SaddlePointsTestGenerator.scala b/testgen/src/main/scala/SaddlePointsTestGenerator.scala new file mode 100644 index 00000000..c236c858 --- /dev/null +++ b/testgen/src/main/scala/SaddlePointsTestGenerator.scala @@ -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"-------------") + } +}