forked from exercism/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
scalaVersion := "2.12.1" | ||
|
||
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.1" % "test" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
object ArmstrongNumbers { | ||
def isArmstrongNumber(i: Int): Boolean = { | ||
val s = i.toString | ||
val power = s.length | ||
i == s.toStream.foldLeft(0.0)((acc, c) => acc + math.pow(c.asDigit, power)) | ||
} | ||
} |
Empty file.
44 changes: 44 additions & 0 deletions
44
exercises/armstrong-numbers/src/test/scala/ArmstrongNumbersTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import org.scalatest.{Matchers, FunSuite} | ||
|
||
/** @version 1.0.0 */ | ||
class ArmstrongNumbersTest extends FunSuite with Matchers { | ||
|
||
test("Single digit numbers are Armstrong numbers") { | ||
ArmstrongNumbers.isArmstrongNumber(5) should be (true) | ||
} | ||
|
||
test("There are no 2 digit Armstrong numbers") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(10) should be (false) | ||
} | ||
|
||
test("Three digit number that is an Armstrong number") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(153) should be (true) | ||
} | ||
|
||
test("Three digit number that is not an Armstrong number") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(100) should be (false) | ||
} | ||
|
||
test("Four digit number that is an Armstrong number") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(9474) should be (true) | ||
} | ||
|
||
test("Four digit number that is not an Armstrong number") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(9475) should be (false) | ||
} | ||
|
||
test("Seven digit number that is an Armstrong number") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(9926315) should be (true) | ||
} | ||
|
||
test("Seven digit number that is not an Armstrong number") { | ||
pending | ||
ArmstrongNumbers.isArmstrongNumber(9926314) should be (false) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
testgen/src/main/scala/ArmstrongNumbersTestGenerator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import java.io.File | ||
|
||
import testgen.TestSuiteBuilder._ | ||
import testgen._ | ||
|
||
object ArmstrongNumbersTestGenerator { | ||
def main(args: Array[String]): Unit = { | ||
val file = new File("src/main/resources/armstrong-numbers.json") | ||
|
||
def toString(expected: CanonicalDataParser.Expected): String = { | ||
expected match { | ||
case Right(b: Boolean) => b.toString | ||
case _ => throw new IllegalStateException() | ||
} | ||
} | ||
|
||
def fromLabeledTestFromInput(argNames: String*): ToTestCaseData = | ||
withLabeledTest { sut => | ||
labeledTest => | ||
val args = sutArgsFromInput(labeledTest.result, argNames: _*) | ||
val property = labeledTest.property | ||
val sutCall = | ||
s"""$sut.$property($args)""" | ||
val expected = toString(labeledTest.expected) | ||
TestCaseData(labeledTest.description, sutCall, expected) | ||
} | ||
|
||
val code = TestSuiteBuilder.build(file, fromLabeledTestFromInput("number")) | ||
println(s"-------------") | ||
println(code) | ||
println(s"-------------") | ||
} | ||
} |