From 783bee965f385428cf23211af3c13f97ececd9a1 Mon Sep 17 00:00:00 2001 From: Erik Schierboom Date: Tue, 13 Dec 2016 19:07:46 +0100 Subject: [PATCH] Add zeroth prime test case --- exercises/nth-prime/example.scala | 4 +++- .../nth-prime/src/test/scala/PrimeTest.scala | 17 +++++++++++------ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/exercises/nth-prime/example.scala b/exercises/nth-prime/example.scala index 53a367a9..7ab04c95 100644 --- a/exercises/nth-prime/example.scala +++ b/exercises/nth-prime/example.scala @@ -4,5 +4,7 @@ object Prime { private lazy val primes: Stream[BigInt] = Stream.cons(BigInt(2), primes.map(b => new BigInt(b.bigInteger.nextProbablePrime))) - def nth(n: Int): Int = primes.drop(n - 1).head.toInt + def nth(n: Int): Option[Int] = + if (n < 1) None + else Some(primes.drop(n - 1).head.toInt) } diff --git a/exercises/nth-prime/src/test/scala/PrimeTest.scala b/exercises/nth-prime/src/test/scala/PrimeTest.scala index c6d7a2f9..4f78a294 100644 --- a/exercises/nth-prime/src/test/scala/PrimeTest.scala +++ b/exercises/nth-prime/src/test/scala/PrimeTest.scala @@ -2,31 +2,36 @@ import org.scalatest.{Matchers, FlatSpec} class PrimeTest extends FlatSpec with Matchers { it should "calculate 1st prime" in { - Prime.nth(1) should be (2) + Prime.nth(1) should be (Some(2)) } it should "calculate 2nd prime" in { pending - Prime.nth(2) should be (3) + Prime.nth(2) should be (Some(3)) } it should "calculate 6th prime" in { pending - Prime.nth(6) should be (13) + Prime.nth(6) should be (Some(13)) } it should "calculate 1000th prime" in { pending - Prime.nth(1000) should be (7919) + Prime.nth(1000) should be (Some(7919)) } it should "calculate 10000th prime" in { pending - Prime.nth(10000) should be (104729) + Prime.nth(10000) should be (Some(104729)) } it should "calculate 10001th prime" in { pending - Prime.nth(10001) should be (104743) + Prime.nth(10001) should be (Some(104743)) + } + + it should "not find zeroth prime" in { + pending + Prime.nth(0) should be (None) } }