Convert math tests to kotlin
This commit is contained in:
parent
cd158acdbf
commit
84f164397b
|
@ -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)
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -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("(-)");
|
||||
}
|
||||
}
|
|
@ -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("(-)")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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())
|
||||
}
|
||||
}
|
|
@ -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)");
|
||||
}
|
||||
}
|
|
@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)");
|
||||
}
|
||||
}
|
|
@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)");
|
||||
}
|
||||
}
|
|
@ -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)")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue