Skip to content

Commit

Permalink
Update all-your-base to match latest canonical json.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricemery committed Feb 8, 2018
1 parent b0ab07b commit 70cc35a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
9 changes: 5 additions & 4 deletions exercises/all-your-base/example.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ object AllYourBase {
if (inputBase < 2 || outputBase < 2)
None
else if (inputDigits.isEmpty)
Some(List())
Some(List(0))
else {
fromDigits(0, inputBase, inputDigits) match {
case None => None
case x if x.sum == 0 => Some(List(0))
case Some(x) => Some(toDigits(outputBase, x, List()))
}
}
Expand All @@ -18,9 +19,9 @@ object AllYourBase {
private def fromDigits(acc: Int, base: Int, digits: List[Int]): Option[Int] = {
digits match {
case x::xs => if (x >= 0 && x < base)
fromDigits(acc * base + x, base, xs)
else
None
fromDigits(acc * base + x, base, xs)
else
None
case Nil => Option(acc)
}
}
Expand Down
53 changes: 26 additions & 27 deletions exercises/all-your-base/src/test/scala/AllYourBaseTest.scala
Original file line number Diff line number Diff line change
@@ -1,109 +1,108 @@

import org.scalatest.{Matchers, FunSuite}

/** @version 1.1.0 */
/** @version 2.2.0 */
class AllYourBaseTest extends FunSuite with Matchers {

test("single bit one to decimal") {
test("single bit one to decimal") {
AllYourBase.rebase(2, List(1), 10) should be (Some(List(1)))
}

test("binary to single decimal") {
test("binary to single decimal") {
pending
AllYourBase.rebase(2, List(1, 0, 1), 10) should be (Some(List(5)))
}

test("single decimal to binary") {
test("single decimal to binary") {
pending
AllYourBase.rebase(10, List(5), 2) should be (Some(List(1, 0, 1)))
}

test("binary to multiple decimal") {
test("binary to multiple decimal") {
pending
AllYourBase.rebase(2, List(1, 0, 1, 0, 1, 0), 10) should be (Some(List(4, 2)))
}

test("decimal to binary") {
test("decimal to binary") {
pending
AllYourBase.rebase(10, List(4, 2), 2) should be (Some(List(1, 0, 1, 0, 1, 0)))
}

test("trinary to hexadecimal") {
test("trinary to hexadecimal") {
pending
AllYourBase.rebase(3, List(1, 1, 2, 0), 16) should be (Some(List(2, 10)))
}

test("hexadecimal to trinary") {
test("hexadecimal to trinary") {
pending
AllYourBase.rebase(16, List(2, 10), 3) should be (Some(List(1, 1, 2, 0)))
}

test("15-bit integer") {
test("15-bit integer") {
pending
AllYourBase.rebase(97, List(3, 46, 60), 73) should be (Some(List(6, 10, 45)))
}

test("empty list") {
test("empty list") {
pending
AllYourBase.rebase(2, List(), 10) should be (None)
AllYourBase.rebase(2, List(), 10) should be (Some(List(0)))
}

test("single zero") {
test("single zero") {
pending
AllYourBase.rebase(10, List(0), 2) should be (None)
AllYourBase.rebase(10, List(0), 2) should be (Some(List(0)))
}

test("multiple zeros") {
test("multiple zeros") {
pending
AllYourBase.rebase(10, List(0, 0, 0), 2) should be (None)
AllYourBase.rebase(10, List(0, 0, 0), 2) should be (Some(List(0)))
}

test("leading zeros") {
test("leading zeros") {
pending
AllYourBase.rebase(7, List(0, 6, 0), 10) should be (None)
AllYourBase.rebase(7, List(0, 6, 0), 10) should be (Some(List(4, 2)))
}

test("first base is one") {
test("input base is one") {
pending
AllYourBase.rebase(1, List(), 10) should be (None)
}

test("first base is zero") {
test("input base is zero") {
pending
AllYourBase.rebase(0, List(), 10) should be (None)
}

test("first base is negative") {
test("input base is negative") {
pending
AllYourBase.rebase(-2, List(1), 10) should be (None)
}

test("negative digit") {
test("negative digit") {
pending
AllYourBase.rebase(2, List(1, -1, 1, 0, 1, 0), 10) should be (None)
}

test("invalid positive digit") {
test("invalid positive digit") {
pending
AllYourBase.rebase(2, List(1, 2, 1, 0, 1, 0), 10) should be (None)
}

test("second base is one") {
test("output base is one") {
pending
AllYourBase.rebase(2, List(1, 0, 1, 0, 1, 0), 1) should be (None)
}

test("second base is zero") {
test("output base is zero") {
pending
AllYourBase.rebase(10, List(7), 0) should be (None)
}

test("second base is negative") {
test("output base is negative") {
pending
AllYourBase.rebase(2, List(1), -7) should be (None)
}

test("both bases are negative") {
test("both bases are negative") {
pending
AllYourBase.rebase(-2, List(1), -7) should be (None)
}
Expand Down
8 changes: 3 additions & 5 deletions testgen/src/main/scala/AllYourBaseTestGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ object AllYourBaseTestGenerator {
}
}

def fromLabeledTest(argNames: String*): ToTestCaseData =
def fromLabeledTestFromInput(argNames: String*): ToTestCaseData =
withLabeledTest { sut =>
labeledTest =>
val args = sutArgs(labeledTest.result, argNames: _*)
val args = sutArgsFromInput(labeledTest.result, argNames: _*)
val property = labeledTest.property
val sutCall =
s"""AllYourBase.$property($args)"""
Expand All @@ -28,11 +28,9 @@ object AllYourBaseTestGenerator {

val code =
TestSuiteBuilder.build(file,
fromLabeledTest("input_base", "input_digits", "output_base"))
fromLabeledTestFromInput("inputBase", "digits", "outputBase"))
println(s"-------------")
println(code)
println(s"-------------")

TestSuiteBuilder.writeToFile(code, new File("AllYourBaseTest.scala"))
}
}

0 comments on commit 70cc35a

Please sign in to comment.