From 84f164397b2b2b3ace76d7e147687594a84a3cb2 Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Sun, 18 Nov 2018 07:56:12 -0500 Subject: [PATCH] Convert math tests to kotlin --- .../builtin/math/{Module.kt => Modulo.kt} | 2 +- .../function/builtin/math/DivideTest.java | 77 ----------- .../function/builtin/math/DivideTest.kt | 85 ++++++++++++ .../function/builtin/math/MinusTest.java | 51 ------- .../kotlin/function/builtin/math/MinusTest.kt | 57 ++++++++ .../function/builtin/math/ModuleTest.java | 116 ---------------- .../function/builtin/math/ModuloTest.kt | 128 ++++++++++++++++++ .../function/builtin/math/MultiplyTest.java | 52 ------- .../function/builtin/math/MultiplyTest.kt | 56 ++++++++ .../function/builtin/math/PlusTest.java | 52 ------- .../kotlin/function/builtin/math/PlusTest.kt | 56 ++++++++ .../function/builtin/math/RemainderTest.java | 104 -------------- .../function/builtin/math/RemainderTest.kt | 115 ++++++++++++++++ 13 files changed, 498 insertions(+), 453 deletions(-) rename src/main/kotlin/function/builtin/math/{Module.kt => Modulo.kt} (95%) delete mode 100644 src/test/kotlin/function/builtin/math/DivideTest.java create mode 100644 src/test/kotlin/function/builtin/math/DivideTest.kt delete mode 100644 src/test/kotlin/function/builtin/math/MinusTest.java create mode 100644 src/test/kotlin/function/builtin/math/MinusTest.kt delete mode 100644 src/test/kotlin/function/builtin/math/ModuleTest.java create mode 100644 src/test/kotlin/function/builtin/math/ModuloTest.kt delete mode 100644 src/test/kotlin/function/builtin/math/MultiplyTest.java create mode 100644 src/test/kotlin/function/builtin/math/MultiplyTest.kt delete mode 100644 src/test/kotlin/function/builtin/math/PlusTest.java create mode 100644 src/test/kotlin/function/builtin/math/PlusTest.kt delete mode 100644 src/test/kotlin/function/builtin/math/RemainderTest.java create mode 100644 src/test/kotlin/function/builtin/math/RemainderTest.kt diff --git a/src/main/kotlin/function/builtin/math/Module.kt b/src/main/kotlin/function/builtin/math/Modulo.kt similarity index 95% rename from src/main/kotlin/function/builtin/math/Module.kt rename to src/main/kotlin/function/builtin/math/Modulo.kt index 518376a..6eb2032 100644 --- a/src/main/kotlin/function/builtin/math/Module.kt +++ b/src/main/kotlin/function/builtin/math/Modulo.kt @@ -9,7 +9,7 @@ import sexpression.LispNumber import sexpression.SExpression @FunctionNames("MOD", "MODULO", "%") -class Module(name: String) : LispFunction() { +class Modulo(name: String) : LispFunction() { private val argumentValidator = ArgumentValidator(name).apply { setExactNumberOfArguments(2) diff --git a/src/test/kotlin/function/builtin/math/DivideTest.java b/src/test/kotlin/function/builtin/math/DivideTest.java deleted file mode 100644 index 258099d..0000000 --- a/src/test/kotlin/function/builtin/math/DivideTest.java +++ /dev/null @@ -1,77 +0,0 @@ -package function.builtin.math; - -import function.ArgumentValidator.BadArgumentTypeException; -import function.ArgumentValidator.TooFewArgumentsException; -import function.builtin.math.Divide.DivideByZeroException; -import org.junit.Test; -import testutil.SymbolAndFunctionCleaner; - -import static testutil.TestUtilities.assertIsErrorWithMessage; -import static testutil.TestUtilities.assertSExpressionsMatch; -import static testutil.TestUtilities.evaluateString; -import static testutil.TestUtilities.parseString; - -public class DivideTest extends SymbolAndFunctionCleaner { - - @Test - public void divideWithOne() { - String input = "(/ 1)"; - - assertSExpressionsMatch(parseString("1"), evaluateString(input)); - } - - @Test - public void divideWithTwo() { - String input = "(/ 2)"; - - assertSExpressionsMatch(parseString("0"), evaluateString(input)); - } - - @Test - public void divideTwoNumbers() { - String input = "(/ 24 3)"; - - assertSExpressionsMatch(parseString("8"), evaluateString(input)); - } - - @Test - public void divideSeveralNumbers() { - String input = "(/ 256 2 2 8)"; - - assertSExpressionsMatch(parseString("8"), evaluateString(input)); - } - - @Test - public void divideTwoNumbersWithRemainder() { - String input = "(/ 9 2)"; - - assertSExpressionsMatch(parseString("4"), evaluateString(input)); - } - - @Test - public void divideSeveralNumbersWithRemainder() { - String input = "(/ 19 2 5)"; - - assertSExpressionsMatch(parseString("1"), evaluateString(input)); - } - - @Test(expected = BadArgumentTypeException.class) - public void divideWithNonNumber() { - evaluateString("(/ 'x)"); - } - - @Test(expected = TooFewArgumentsException.class) - public void divideWithTooFewArguments() { - evaluateString("(/)"); - } - - @Test(expected = DivideByZeroException.class) - public void divideByZero() { - evaluateString("(/ 2 0)"); - } - - @Test - public void divideByZeroException_HasCorrectAttributes() { - assertIsErrorWithMessage(new DivideByZeroException()); - } -} diff --git a/src/test/kotlin/function/builtin/math/DivideTest.kt b/src/test/kotlin/function/builtin/math/DivideTest.kt new file mode 100644 index 0000000..5cc5745 --- /dev/null +++ b/src/test/kotlin/function/builtin/math/DivideTest.kt @@ -0,0 +1,85 @@ +package function.builtin.math + +import function.ArgumentValidator.BadArgumentTypeException +import function.ArgumentValidator.TooFewArgumentsException +import function.builtin.math.Divide.DivideByZeroException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import testutil.LispTestInstance +import testutil.SymbolAndFunctionCleaner +import testutil.TestUtilities.assertIsErrorWithMessage +import testutil.TestUtilities.assertSExpressionsMatch +import testutil.TestUtilities.evaluateString +import testutil.TestUtilities.parseString + +@LispTestInstance +class DivideTest : SymbolAndFunctionCleaner() { + + @Test + fun divideWithOne() { + val input = "(/ 1)" + + assertSExpressionsMatch(parseString("1"), evaluateString(input)) + } + + @Test + fun divideWithTwo() { + val input = "(/ 2)" + + assertSExpressionsMatch(parseString("0"), evaluateString(input)) + } + + @Test + fun divideTwoNumbers() { + val input = "(/ 24 3)" + + assertSExpressionsMatch(parseString("8"), evaluateString(input)) + } + + @Test + fun divideSeveralNumbers() { + val input = "(/ 256 2 2 8)" + + assertSExpressionsMatch(parseString("8"), evaluateString(input)) + } + + @Test + fun divideTwoNumbersWithRemainder() { + val input = "(/ 9 2)" + + assertSExpressionsMatch(parseString("4"), evaluateString(input)) + } + + @Test + fun divideSeveralNumbersWithRemainder() { + val input = "(/ 19 2 5)" + + assertSExpressionsMatch(parseString("1"), evaluateString(input)) + } + + @Test + fun divideWithNonNumber() { + assertThrows(BadArgumentTypeException::class.java) { + evaluateString("(/ 'x)") + } + } + + @Test + fun divideWithTooFewArguments() { + assertThrows(TooFewArgumentsException::class.java) { + evaluateString("(/)") + } + } + + @Test + fun divideByZero() { + assertThrows(DivideByZeroException::class.java) { + evaluateString("(/ 2 0)") + } + } + + @Test + fun divideByZeroException_HasCorrectAttributes() { + assertIsErrorWithMessage(DivideByZeroException()) + } +} diff --git a/src/test/kotlin/function/builtin/math/MinusTest.java b/src/test/kotlin/function/builtin/math/MinusTest.java deleted file mode 100644 index 63352d1..0000000 --- a/src/test/kotlin/function/builtin/math/MinusTest.java +++ /dev/null @@ -1,51 +0,0 @@ -package function.builtin.math; - -import function.ArgumentValidator.BadArgumentTypeException; -import function.ArgumentValidator.TooFewArgumentsException; -import org.junit.Test; -import sexpression.LispNumber; -import testutil.SymbolAndFunctionCleaner; - -import static testutil.TestUtilities.assertSExpressionsMatch; -import static testutil.TestUtilities.evaluateString; - -public class MinusTest extends SymbolAndFunctionCleaner { - - @Test - public void minusWithOneNumber() { - String input = "(- 27)"; - - assertSExpressionsMatch(new LispNumber("-27"), evaluateString(input)); - } - - @Test - public void minusWithTwoNumbers() { - String input = "(- 5 3)"; - - assertSExpressionsMatch(new LispNumber("2"), evaluateString(input)); - } - - @Test - public void minusWithManyNumbers_PositiveResult() { - String input = "(- 200 100 10 5)"; - - assertSExpressionsMatch(new LispNumber("85"), evaluateString(input)); - } - - @Test - public void minusWithManyNumbers_NegativeResult() { - String input = "(- 100 200 20 5)"; - - assertSExpressionsMatch(new LispNumber("-125"), evaluateString(input)); - } - - @Test(expected = BadArgumentTypeException.class) - public void minusWithNonNumber() { - evaluateString("(- 'a 'b)"); - } - - @Test(expected = TooFewArgumentsException.class) - public void minusWithTooFewArguments() { - evaluateString("(-)"); - } -} diff --git a/src/test/kotlin/function/builtin/math/MinusTest.kt b/src/test/kotlin/function/builtin/math/MinusTest.kt new file mode 100644 index 0000000..68f7a86 --- /dev/null +++ b/src/test/kotlin/function/builtin/math/MinusTest.kt @@ -0,0 +1,57 @@ +package function.builtin.math + +import function.ArgumentValidator.BadArgumentTypeException +import function.ArgumentValidator.TooFewArgumentsException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import sexpression.LispNumber +import testutil.LispTestInstance +import testutil.SymbolAndFunctionCleaner +import testutil.TestUtilities.assertSExpressionsMatch +import testutil.TestUtilities.evaluateString + +@LispTestInstance +class MinusTest : SymbolAndFunctionCleaner() { + + @Test + fun minusWithOneNumber() { + val input = "(- 27)" + + assertSExpressionsMatch(LispNumber("-27"), evaluateString(input)) + } + + @Test + fun minusWithTwoNumbers() { + val input = "(- 5 3)" + + assertSExpressionsMatch(LispNumber("2"), evaluateString(input)) + } + + @Test + fun minusWithManyNumbers_PositiveResult() { + val input = "(- 200 100 10 5)" + + assertSExpressionsMatch(LispNumber("85"), evaluateString(input)) + } + + @Test + fun minusWithManyNumbers_NegativeResult() { + val input = "(- 100 200 20 5)" + + assertSExpressionsMatch(LispNumber("-125"), evaluateString(input)) + } + + @Test + fun minusWithNonNumber() { + assertThrows(BadArgumentTypeException::class.java) { + evaluateString("(- 'a 'b)") + } + } + + @Test + fun minusWithTooFewArguments() { + assertThrows(TooFewArgumentsException::class.java) { + evaluateString("(-)") + } + } +} diff --git a/src/test/kotlin/function/builtin/math/ModuleTest.java b/src/test/kotlin/function/builtin/math/ModuleTest.java deleted file mode 100644 index 1b67c77..0000000 --- a/src/test/kotlin/function/builtin/math/ModuleTest.java +++ /dev/null @@ -1,116 +0,0 @@ -package function.builtin.math; - -import function.ArgumentValidator.BadArgumentTypeException; -import function.ArgumentValidator.TooFewArgumentsException; -import function.ArgumentValidator.TooManyArgumentsException; -import function.builtin.math.Module.ModulusNotPositiveException; -import org.junit.Test; -import sexpression.LispNumber; -import testutil.SymbolAndFunctionCleaner; - -import static testutil.TestUtilities.assertIsErrorWithMessage; -import static testutil.TestUtilities.assertSExpressionsMatch; -import static testutil.TestUtilities.evaluateString; - -public class ModuleTest extends SymbolAndFunctionCleaner { - - @Test - public void mod() { - String input = "(mod 5 3)"; - - assertSExpressionsMatch(new LispNumber("2"), evaluateString(input)); - } - - @Test - public void modulo() { - String input = "(modulo 11 7)"; - - assertSExpressionsMatch(new LispNumber("4"), evaluateString(input)); - } - - @Test - public void moduloSymbol() { - String input = "(% 8 5)"; - - assertSExpressionsMatch(new LispNumber("3"), evaluateString(input)); - } - - @Test - public void dividendGreaterThanDivisor() { - String input = "(mod 21 19)"; - - assertSExpressionsMatch(new LispNumber("2"), evaluateString(input)); - } - - @Test - public void dividendLessThanDivisor() { - String input = "(mod 5 239)"; - - assertSExpressionsMatch(new LispNumber("5"), evaluateString(input)); - } - - @Test - public void dividendEqualToDivisor() { - String input = "(mod 5 5)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void dividendMultipleOfDivisor() { - String input = "(mod 20 5)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void divisorOfOne() { - String input = "(mod 5 1)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void dividendOfZero() { - String input = "(mod 0 2309)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void negativeDividend() { - String input = "(mod -23 25)"; - - assertSExpressionsMatch(new LispNumber("2"), evaluateString(input)); - } - - @Test(expected = ModulusNotPositiveException.class) - public void negativeDivisor() { - evaluateString("(mod 5 -10)"); - } - - @Test(expected = ModulusNotPositiveException.class) - public void divisorOfZero() { - evaluateString("(mod 5 0)"); - } - - @Test(expected = BadArgumentTypeException.class) - public void modWithNonNumber() { - evaluateString("(mod 'a 'b)"); - } - - @Test(expected = TooFewArgumentsException.class) - public void modWithTooFewArguments() { - evaluateString("(mod 1)"); - } - - @Test(expected = TooManyArgumentsException.class) - public void modWithTooManyArguments() { - evaluateString("(mod 1 2 3)"); - } - - @Test - public void moduloNotPositiveException_HasCorrectAttributes() { - assertIsErrorWithMessage(new ModulusNotPositiveException()); - } -} diff --git a/src/test/kotlin/function/builtin/math/ModuloTest.kt b/src/test/kotlin/function/builtin/math/ModuloTest.kt new file mode 100644 index 0000000..12e0b4c --- /dev/null +++ b/src/test/kotlin/function/builtin/math/ModuloTest.kt @@ -0,0 +1,128 @@ +package function.builtin.math + +import function.ArgumentValidator.BadArgumentTypeException +import function.ArgumentValidator.TooFewArgumentsException +import function.ArgumentValidator.TooManyArgumentsException +import function.builtin.math.Modulo.ModulusNotPositiveException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import sexpression.LispNumber +import testutil.LispTestInstance +import testutil.SymbolAndFunctionCleaner +import testutil.TestUtilities.assertIsErrorWithMessage +import testutil.TestUtilities.assertSExpressionsMatch +import testutil.TestUtilities.evaluateString + +@LispTestInstance +class ModuloTest : SymbolAndFunctionCleaner() { + + @Test + fun mod() { + val input = "(mod 5 3)" + + assertSExpressionsMatch(LispNumber("2"), evaluateString(input)) + } + + @Test + fun modulo() { + val input = "(modulo 11 7)" + + assertSExpressionsMatch(LispNumber("4"), evaluateString(input)) + } + + @Test + fun moduloSymbol() { + val input = "(% 8 5)" + + assertSExpressionsMatch(LispNumber("3"), evaluateString(input)) + } + + @Test + fun dividendGreaterThanDivisor() { + val input = "(mod 21 19)" + + assertSExpressionsMatch(LispNumber("2"), evaluateString(input)) + } + + @Test + fun dividendLessThanDivisor() { + val input = "(mod 5 239)" + + assertSExpressionsMatch(LispNumber("5"), evaluateString(input)) + } + + @Test + fun dividendEqualToDivisor() { + val input = "(mod 5 5)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun dividendMultipleOfDivisor() { + val input = "(mod 20 5)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun divisorOfOne() { + val input = "(mod 5 1)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun dividendOfZero() { + val input = "(mod 0 2309)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun negativeDividend() { + val input = "(mod -23 25)" + + assertSExpressionsMatch(LispNumber("2"), evaluateString(input)) + } + + @Test + fun negativeDivisor() { + assertThrows(ModulusNotPositiveException::class.java) { + evaluateString("(mod 5 -10)") + } + } + + @Test + fun divisorOfZero() { + assertThrows(ModulusNotPositiveException::class.java) { + evaluateString("(mod 5 0)") + } + } + + @Test + fun modWithNonNumber() { + assertThrows(BadArgumentTypeException::class.java) { + evaluateString("(mod 'a 'b)") + } + } + + @Test + fun modWithTooFewArguments() { + assertThrows(TooFewArgumentsException::class.java) { + evaluateString("(mod 1)") + } + } + + @Test + fun modWithTooManyArguments() { + assertThrows(TooManyArgumentsException::class.java) { + evaluateString("(mod 1 2 3)") + } + } + + @Test + fun moduloNotPositiveException_HasCorrectAttributes() { + assertIsErrorWithMessage(ModulusNotPositiveException()) + } +} diff --git a/src/test/kotlin/function/builtin/math/MultiplyTest.java b/src/test/kotlin/function/builtin/math/MultiplyTest.java deleted file mode 100644 index 01375fe..0000000 --- a/src/test/kotlin/function/builtin/math/MultiplyTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package function.builtin.math; - -import function.ArgumentValidator.BadArgumentTypeException; -import org.junit.Test; -import sexpression.LispNumber; -import testutil.SymbolAndFunctionCleaner; - -import static testutil.TestUtilities.assertSExpressionsMatch; -import static testutil.TestUtilities.evaluateString; - -public class MultiplyTest extends SymbolAndFunctionCleaner { - - @Test - public void multiplyWithNoArguments() { - String input = "(*)"; - - assertSExpressionsMatch(new LispNumber("1"), evaluateString(input)); - } - - @Test - public void multiplyWithOneNumber() { - String input = "(* 8)"; - - assertSExpressionsMatch(new LispNumber("8"), evaluateString(input)); - } - - @Test - public void multiplyWithTwoNumbers() { - String input = "(* 5 3)"; - - assertSExpressionsMatch(new LispNumber("15"), evaluateString(input)); - } - - @Test - public void multiplyWithManyNumbers_PositiveResult() { - String input = "(* 2 3 5 1)"; - - assertSExpressionsMatch(new LispNumber("30"), evaluateString(input)); - } - - @Test - public void multiplyWithManyNumbers_NegativeResult() { - String input = "(* 3 (- 2) 10 2)"; - - assertSExpressionsMatch(new LispNumber("-120"), evaluateString(input)); - } - - @Test(expected = BadArgumentTypeException.class) - public void multiplyWithNonNumber() { - evaluateString("(* 'a 'b)"); - } -} diff --git a/src/test/kotlin/function/builtin/math/MultiplyTest.kt b/src/test/kotlin/function/builtin/math/MultiplyTest.kt new file mode 100644 index 0000000..a7e1499 --- /dev/null +++ b/src/test/kotlin/function/builtin/math/MultiplyTest.kt @@ -0,0 +1,56 @@ +package function.builtin.math + +import function.ArgumentValidator.BadArgumentTypeException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import sexpression.LispNumber +import testutil.LispTestInstance +import testutil.SymbolAndFunctionCleaner +import testutil.TestUtilities.assertSExpressionsMatch +import testutil.TestUtilities.evaluateString + +@LispTestInstance +class MultiplyTest : SymbolAndFunctionCleaner() { + + @Test + fun multiplyWithNoArguments() { + val input = "(*)" + + assertSExpressionsMatch(LispNumber("1"), evaluateString(input)) + } + + @Test + fun multiplyWithOneNumber() { + val input = "(* 8)" + + assertSExpressionsMatch(LispNumber("8"), evaluateString(input)) + } + + @Test + fun multiplyWithTwoNumbers() { + val input = "(* 5 3)" + + assertSExpressionsMatch(LispNumber("15"), evaluateString(input)) + } + + @Test + fun multiplyWithManyNumbers_PositiveResult() { + val input = "(* 2 3 5 1)" + + assertSExpressionsMatch(LispNumber("30"), evaluateString(input)) + } + + @Test + fun multiplyWithManyNumbers_NegativeResult() { + val input = "(* 3 (- 2) 10 2)" + + assertSExpressionsMatch(LispNumber("-120"), evaluateString(input)) + } + + @Test + fun multiplyWithNonNumber() { + assertThrows(BadArgumentTypeException::class.java) { + evaluateString("(* 'a 'b)") + } + } +} diff --git a/src/test/kotlin/function/builtin/math/PlusTest.java b/src/test/kotlin/function/builtin/math/PlusTest.java deleted file mode 100644 index f2d5193..0000000 --- a/src/test/kotlin/function/builtin/math/PlusTest.java +++ /dev/null @@ -1,52 +0,0 @@ -package function.builtin.math; - -import function.ArgumentValidator.BadArgumentTypeException; -import org.junit.Test; -import sexpression.LispNumber; -import testutil.SymbolAndFunctionCleaner; - -import static testutil.TestUtilities.assertSExpressionsMatch; -import static testutil.TestUtilities.evaluateString; - -public class PlusTest extends SymbolAndFunctionCleaner { - - @Test - public void plusWithNoArguments() { - String input = "(+)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void plusWithOneNumber() { - String input = "(+ 27)"; - - assertSExpressionsMatch(new LispNumber("27"), evaluateString(input)); - } - - @Test - public void plusWithTwoNumbers() { - String input = "(+ 5 3)"; - - assertSExpressionsMatch(new LispNumber("8"), evaluateString(input)); - } - - @Test - public void plusWithManyNumbers_PositiveResult() { - String input = "(+ 200 100 10 5)"; - - assertSExpressionsMatch(new LispNumber("315"), evaluateString(input)); - } - - @Test - public void plusWithManyNumbers_NegativeResult() { - String input = "(+ 100 (- 200) 20 5)"; - - assertSExpressionsMatch(new LispNumber("-75"), evaluateString(input)); - } - - @Test(expected = BadArgumentTypeException.class) - public void plusWithNonNumber() { - evaluateString("(+ 'a 'b)"); - } -} diff --git a/src/test/kotlin/function/builtin/math/PlusTest.kt b/src/test/kotlin/function/builtin/math/PlusTest.kt new file mode 100644 index 0000000..016b4fd --- /dev/null +++ b/src/test/kotlin/function/builtin/math/PlusTest.kt @@ -0,0 +1,56 @@ +package function.builtin.math + +import function.ArgumentValidator.BadArgumentTypeException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import sexpression.LispNumber +import testutil.LispTestInstance +import testutil.SymbolAndFunctionCleaner +import testutil.TestUtilities.assertSExpressionsMatch +import testutil.TestUtilities.evaluateString + +@LispTestInstance +class PlusTest : SymbolAndFunctionCleaner() { + + @Test + fun plusWithNoArguments() { + val input = "(+)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun plusWithOneNumber() { + val input = "(+ 27)" + + assertSExpressionsMatch(LispNumber("27"), evaluateString(input)) + } + + @Test + fun plusWithTwoNumbers() { + val input = "(+ 5 3)" + + assertSExpressionsMatch(LispNumber("8"), evaluateString(input)) + } + + @Test + fun plusWithManyNumbers_PositiveResult() { + val input = "(+ 200 100 10 5)" + + assertSExpressionsMatch(LispNumber("315"), evaluateString(input)) + } + + @Test + fun plusWithManyNumbers_NegativeResult() { + val input = "(+ 100 (- 200) 20 5)" + + assertSExpressionsMatch(LispNumber("-75"), evaluateString(input)) + } + + @Test + fun plusWithNonNumber() { + assertThrows(BadArgumentTypeException::class.java) { + evaluateString("(+ 'a 'b)") + } + } +} diff --git a/src/test/kotlin/function/builtin/math/RemainderTest.java b/src/test/kotlin/function/builtin/math/RemainderTest.java deleted file mode 100644 index effb586..0000000 --- a/src/test/kotlin/function/builtin/math/RemainderTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package function.builtin.math; - -import function.ArgumentValidator.BadArgumentTypeException; -import function.ArgumentValidator.TooFewArgumentsException; -import function.ArgumentValidator.TooManyArgumentsException; -import function.builtin.math.Divide.DivideByZeroException; -import org.junit.Test; -import sexpression.LispNumber; -import testutil.SymbolAndFunctionCleaner; - -import static testutil.TestUtilities.assertSExpressionsMatch; -import static testutil.TestUtilities.evaluateString; - -public class RemainderTest extends SymbolAndFunctionCleaner { - - @Test - public void rem() { - String input = "(rem 5 3)"; - - assertSExpressionsMatch(new LispNumber("2"), evaluateString(input)); - } - - @Test - public void remainder() { - String input = "(remainder 11 7)"; - - assertSExpressionsMatch(new LispNumber("4"), evaluateString(input)); - } - - @Test - public void dividendGreaterThanDivisor() { - String input = "(rem 21 19)"; - - assertSExpressionsMatch(new LispNumber("2"), evaluateString(input)); - } - - @Test - public void dividendLessThanDivisor() { - String input = "(rem 5 239)"; - - assertSExpressionsMatch(new LispNumber("5"), evaluateString(input)); - } - - @Test - public void dividendEqualToDivisor() { - String input = "(rem 5 5)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void dividendMultipleOfDivisor() { - String input = "(rem 20 5)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void divisorOfOne() { - String input = "(rem 5 1)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void dividendOfZero() { - String input = "(rem 0 2309)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test - public void negativeDividend() { - String input = "(rem -23 25)"; - - assertSExpressionsMatch(new LispNumber("-23"), evaluateString(input)); - } - - public void negativeDivisor() { - String input = "(rem 5 -11)"; - - assertSExpressionsMatch(new LispNumber("0"), evaluateString(input)); - } - - @Test(expected = DivideByZeroException.class) - public void divisorOfZero() { - evaluateString("(rem 5 0)"); - } - - @Test(expected = BadArgumentTypeException.class) - public void remWithNonNumber() { - evaluateString("(rem 'a 'b)"); - } - - @Test(expected = TooFewArgumentsException.class) - public void remWithTooFewArguments() { - evaluateString("(rem 1)"); - } - - @Test(expected = TooManyArgumentsException.class) - public void remWithTooManyArguments() { - evaluateString("(rem 1 2 3)"); - } -} diff --git a/src/test/kotlin/function/builtin/math/RemainderTest.kt b/src/test/kotlin/function/builtin/math/RemainderTest.kt new file mode 100644 index 0000000..369313d --- /dev/null +++ b/src/test/kotlin/function/builtin/math/RemainderTest.kt @@ -0,0 +1,115 @@ +package function.builtin.math + +import function.ArgumentValidator.BadArgumentTypeException +import function.ArgumentValidator.TooFewArgumentsException +import function.ArgumentValidator.TooManyArgumentsException +import function.builtin.math.Divide.DivideByZeroException +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import sexpression.LispNumber +import testutil.LispTestInstance +import testutil.SymbolAndFunctionCleaner +import testutil.TestUtilities.assertSExpressionsMatch +import testutil.TestUtilities.evaluateString + +@LispTestInstance +class RemainderTest : SymbolAndFunctionCleaner() { + + @Test + fun rem() { + val input = "(rem 5 3)" + + assertSExpressionsMatch(LispNumber("2"), evaluateString(input)) + } + + @Test + fun remainder() { + val input = "(remainder 11 7)" + + assertSExpressionsMatch(LispNumber("4"), evaluateString(input)) + } + + @Test + fun dividendGreaterThanDivisor() { + val input = "(rem 21 19)" + + assertSExpressionsMatch(LispNumber("2"), evaluateString(input)) + } + + @Test + fun dividendLessThanDivisor() { + val input = "(rem 5 239)" + + assertSExpressionsMatch(LispNumber("5"), evaluateString(input)) + } + + @Test + fun dividendEqualToDivisor() { + val input = "(rem 5 5)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun dividendMultipleOfDivisor() { + val input = "(rem 20 5)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun divisorOfOne() { + val input = "(rem 5 1)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun dividendOfZero() { + val input = "(rem 0 2309)" + + assertSExpressionsMatch(LispNumber("0"), evaluateString(input)) + } + + @Test + fun negativeDividend() { + val input = "(rem -23 25)" + + assertSExpressionsMatch(LispNumber("-23"), evaluateString(input)) + } + + @Test + fun negativeDivisor() { + val input = "(rem 5 -11)" + + assertSExpressionsMatch(LispNumber("5"), evaluateString(input)) + } + + @Test + fun divisorOfZero() { + assertThrows(DivideByZeroException::class.java) { + evaluateString("(rem 5 0)") + } + } + + @Test + fun remWithNonNumber() { + assertThrows(BadArgumentTypeException::class.java) { + evaluateString("(rem 'a 'b)") + } + } + + @Test + fun remWithTooFewArguments() { + assertThrows(TooFewArgumentsException::class.java) { + evaluateString("(rem 1)") + } + } + + @Test + fun remWithTooManyArguments() { + assertThrows(TooManyArgumentsException::class.java) { + evaluateString("(rem 1 2 3)") + } + } +}