Skip to content

Commit 9674ae4

Browse files
committed
Create PalindromeProductsTestGenerator.scala. Update PalindromeProductsTest.scala. Refs #370, #331
1 parent d27cd1b commit 9674ae4

File tree

4 files changed

+112
-38
lines changed

4 files changed

+112
-38
lines changed

config.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,7 @@
746746
"unlocked_by": "sum-of-multiples",
747747
"difficulty": 6,
748748
"topics": [
749+
"Optional values",
749750
"Sets",
750751
"Strings",
751752
"Tuples",

exercises/palindrome-products/example.scala

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11

22
case class PalindromeProducts(minFactor: Int, maxFactor: Int) {
33

4-
lazy val (smallest: (Int, Set[(Int, Int)]),
5-
largest: (Int, Set[(Int, Int)])) = {
4+
lazy val (smallest: Option[(Int, Set[(Int, Int)])],
5+
largest: Option[(Int, Set[(Int, Int)])]) = {
66
val palindromes = for (a <- Range(minFactor, maxFactor + 1);
7-
b <- Range(a, maxFactor + 1) if isPalindrome(a * b)) yield (a * b, (a, b))
7+
b <- Range(a, maxFactor + 1) if isPalindrome(a * b)) yield (a * b, (a, b))
88
val mapped = palindromes.groupBy(_._1).map{case(k, v) => (k, v.map(_._2).toSet)}
9-
(mapped.minBy(_._1), mapped.maxBy(_._1))
9+
if (mapped.isEmpty)
10+
(None, None)
11+
else
12+
(Some(mapped.minBy(_._1)), Some(mapped.maxBy(_._1)))
1013
}
1114

1215
private def isPalindrome(i: Int) = i.toString == i.toString.reverse
Lines changed: 54 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,79 @@
1-
import org.scalatest.{Matchers, FlatSpec}
1+
import org.scalatest.{Matchers, FunSuite}
22

3-
class PalindromeProductsTest extends FlatSpec with Matchers {
3+
/** @version 1.0.0 */
4+
class PalindromeProductsTest extends FunSuite with Matchers {
45

5-
it should "find smallest palindrome from a single digit factors" in {
6-
val (value, factors) = PalindromeProducts(1, 9).smallest
7-
value should be (1)
8-
factors should be (Set((1, 1)))
6+
// PalindromeProducts largest call is expecting a return type of
7+
// Option[(Int, Set[(Int, Int)])] - None is expected for error cases.
8+
// Some is expected for valid cases. The first element of the tuple
9+
// is the largest palindrome product value. The second element of the
10+
// tuple is the list of factors.
11+
// PalindromeProducts smallest call is expecting a return type of
12+
// Option[(Int, Set[(Int, Int)])] - None is expected for error cases.
13+
// Some is expected for valid cases. The first element of the tuple
14+
// is the smallest palindrome product value. The second element of the
15+
// tuple is the list of factors.
16+
17+
test("finds the smallest palindrome from single digit factors") {
18+
PalindromeProducts(1, 9).smallest should be(Some((1, Set((1, 1)))))
19+
}
20+
21+
test("finds the largest palindrome from single digit factors") {
22+
pending
23+
PalindromeProducts(1, 9).largest should be(Some((9, Set((1, 9), (3, 3)))))
24+
}
25+
26+
test("find the smallest palindrome from double digit factors") {
27+
pending
28+
PalindromeProducts(10, 99).smallest should be(Some((121, Set((11, 11)))))
29+
}
30+
31+
test("find the largest palindrome from double digit factors") {
32+
pending
33+
PalindromeProducts(10, 99).largest should be(Some((9009, Set((91, 99)))))
34+
}
35+
36+
test("find smallest palindrome from triple digit factors") {
37+
pending
38+
PalindromeProducts(100, 999).smallest should be(
39+
Some((10201, Set((101, 101)))))
940
}
1041

11-
it should "find largest palindrome from a single digit factors" in {
42+
test("find the largest palindrome from triple digit factors") {
1243
pending
13-
val (value, factors) = PalindromeProducts(0, 9).largest
14-
value should be (9)
15-
factors should be (Set((1, 9), (3, 3)))
44+
PalindromeProducts(100, 999).largest should be(
45+
Some((906609, Set((913, 993)))))
1646
}
1747

18-
it should "find smallest palindrome from a double digit factors" in {
48+
test("find smallest palindrome from four digit factors") {
1949
pending
20-
val (value, factors) = PalindromeProducts(10, 99).smallest
21-
value should be (121)
22-
factors should be (Set((11, 11)))
50+
PalindromeProducts(1000, 9999).smallest should be(
51+
Some((1002001, Set((1001, 1001)))))
2352
}
2453

25-
it should "find largest palindrome from a double digit factors" in {
54+
test("find the largest palindrome from four digit factors") {
2655
pending
27-
val (value, factors) = PalindromeProducts(10, 99).largest
28-
value should be (9009)
29-
factors should be (Set((91, 99)))
56+
PalindromeProducts(1000, 9999).largest should be(
57+
Some((99000099, Set((9901, 9999)))))
3058
}
3159

32-
it should "find smallest palindrome from a triple digit factors" in {
60+
test("empty result for smallest if no palindrome in the range") {
3361
pending
34-
val (value, factors) = PalindromeProducts(100, 999).smallest
35-
value should be (10201)
36-
factors should be (Set((101, 101)))
62+
PalindromeProducts(1002, 1003).smallest should be(None)
3763
}
3864

39-
it should "find largest palindrome from a triple digit factors" in {
65+
test("empty result for largest if no palindrome in the range") {
4066
pending
41-
val (value, factors) = PalindromeProducts(100, 999).largest
42-
value should be (906609)
43-
factors should be (Set((913, 993)))
67+
PalindromeProducts(15, 15).largest should be(None)
4468
}
4569

46-
it should "find smallest palindrome from a four digit factors" in {
70+
test("error result for smallest if min is more than max") {
4771
pending
48-
val (value, factors) = PalindromeProducts(1000, 9999).smallest
49-
value should be (1002001)
50-
factors should be (Set((1001, 1001)))
72+
PalindromeProducts(10000, 1).smallest should be(None)
5173
}
5274

53-
it should "find largest palindrome from a four digit factors" in {
75+
test("error result for largest if min is more than max") {
5476
pending
55-
val (value, factors) = PalindromeProducts(1000, 9999).largest
56-
value should be (99000099)
57-
factors should be (Set((9901, 9999)))
77+
PalindromeProducts(2, 1).largest should be(None)
5878
}
5979
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.io.File
2+
3+
import testgen.TestSuiteBuilder.{toString, _}
4+
import testgen._
5+
6+
object PalindromeProductsTestGenerator {
7+
def main(args: Array[String]): Unit = {
8+
val file = new File("src/main/resources/palindrome-products.json")
9+
10+
def toStringFromMap(m: Map[String, Any]): String =
11+
s"Some((${m("value").toString}, " +
12+
s"Set(${m("factors").asInstanceOf[List[List[Int]]].map(xs => "(" + xs.map(_.toString).mkString(", ") + ")").mkString(", ")})))"
13+
14+
15+
def toString(expected: CanonicalDataParser.Expected): String = {
16+
expected match {
17+
case Left(_) => "None"
18+
case Right(m: Map[String, any]) => toStringFromMap(m)
19+
}
20+
}
21+
22+
def fromLabeledTest(argNames: String*): ToTestCaseData =
23+
withLabeledTest { sut =>
24+
labeledTest =>
25+
val args = sutArgs(labeledTest.result, argNames: _*)
26+
val property = labeledTest.property
27+
val sutCall = s"""$sut($args).$property"""
28+
val expected = toString(labeledTest.expected)
29+
TestCaseData(labeledTest.description, sutCall, expected)
30+
}
31+
32+
val code =
33+
TestSuiteBuilder.build(file, fromLabeledTest("input_min", "input_max"),
34+
Seq(),
35+
Seq("// PalindromeProducts largest call is expecting a return type of",
36+
"// Option[(Int, Set[(Int, Int)])] - None is expected for error cases.",
37+
"// Some is expected for valid cases. The first element of the tuple ",
38+
"// is the largest palindrome product value. The second element of the",
39+
"// tuple is the list of factors.",
40+
"// PalindromeProducts smallest call is expecting a return type of",
41+
"// Option[(Int, Set[(Int, Int)])] - None is expected for error cases.",
42+
"// Some is expected for valid cases. The first element of the tuple ",
43+
"// is the smallest palindrome product value. The second element of the",
44+
"// tuple is the list of factors."))
45+
46+
println(s"-------------")
47+
println(code)
48+
println(s"-------------")
49+
}
50+
}

0 commit comments

Comments
 (0)