Refactored more code and added unit tests for the built in functions

This commit is contained in:
Mike Cifelli 2017-01-04 13:57:16 -05:00
parent 9f80fc9abe
commit 3cb9d89616
4 changed files with 115 additions and 43 deletions

View File

@ -1,23 +1,20 @@
package function.builtin; package function.builtin;
import function.LispFunction; import function.*;
import sexpression.*; import sexpression.*;
/**
* <code>FUNCALL</code> represents the FUNCALL function in Lisp.
*/
public class FUNCALL extends LispFunction { public class FUNCALL extends LispFunction {
public SExpression call(Cons argList) { private ArgumentValidator argumentValidator;
// make sure we have received at least one argument
if (argList.nullp()) {
Cons originalSExpr = new Cons(new Symbol("FUNCALL"), argList);
throw new RuntimeException("too few arguments given to FUNCALL: " + originalSExpr); public FUNCALL() {
this.argumentValidator = new ArgumentValidator("FUNCALL");
this.argumentValidator.setMinimumNumberOfArguments(1);
} }
SExpression cdr = argList.getCdr(); public SExpression call(Cons argumentList) {
Cons applyArgs = new Cons(argList.getCar(), LIST.makeList(cdr)); argumentValidator.validate(argumentList);
Cons applyArgs = new Cons(argumentList.getCar(), LIST.makeList(argumentList.getCdr()));
return APPLY.apply(applyArgs); return APPLY.apply(applyArgs);
} }

View File

@ -1,49 +1,43 @@
package function.builtin; package function.builtin;
import function.LispFunction; import function.*;
import sexpression.*; import sexpression.*;
/**
* <code>GREATERP</code> represents the '&gt;' function in Lisp.
*/
public class GREATERP extends LispFunction { public class GREATERP extends LispFunction {
public SExpression call(Cons argList) { private ArgumentValidator argumentValidator;
// make sure we have received at least one argument
if (argList.nullp()) {
Cons originalSExpr = new Cons(new Symbol(">"), argList);
throw new RuntimeException("too few arguments given to >: " + originalSExpr); public GREATERP() {
this.argumentValidator = new ArgumentValidator("GREATERP");
this.argumentValidator.setMinimumNumberOfArguments(2);
this.argumentValidator.setEveryArgumentExpectedType(LispNumber.class);
} }
SExpression firstArg = argList.getCar(); public SExpression call(Cons argumentList) {
Cons argRest = (Cons) argList.getCdr(); argumentValidator.validate(argumentList);
// make sure that the first argument is a number return callTailRecursive(argumentList);
if (firstArg.numberp()) { }
LispNumber num1 = (LispNumber) firstArg;
if (argRest.nullp()) { private SExpression callTailRecursive(Cons argumentList) {
Cons remainingArguments = (Cons) argumentList.getCdr();
if (remainingArguments.nullp())
return Symbol.T; return Symbol.T;
}
SExpression secondArg = argRest.getCar(); SExpression firstArgument = argumentList.getCar();
SExpression secondArgument = remainingArguments.getCar();
// make sure that the second argument is a number as well LispNumber number1 = (LispNumber) firstArgument;
if (secondArg.numberp()) { LispNumber number2 = (LispNumber) secondArgument;
LispNumber num2 = (LispNumber) secondArg;
if (num1.getValue().compareTo(num2.getValue()) > 0) {
return call(argRest);
}
if (!isFirstGreater(number1, number2))
return Nil.getInstance(); return Nil.getInstance();
return callTailRecursive(remainingArguments);
} }
throw new RuntimeException(">: " + secondArg + " is not a number"); private boolean isFirstGreater(LispNumber number1, LispNumber number2) {
} return number1.getValue().compareTo(number2.getValue()) > 0;
throw new RuntimeException(">: " + firstArg + " is not a number");
} }
} }

View File

@ -0,0 +1,32 @@
package function.builtin;
import static testutil.TestUtilities.*;
import org.junit.Test;
import function.ArgumentValidator.TooFewArgumentsException;
public class FUNCALLTester {
@Test
public void testFuncallWithNumbers() {
String input = "(funcall '+ 1 2 3)";
assertSExpressionsMatch(evaluateString(input), parseString("6"));
}
@Test
public void testFuncallWithUserDefinedFunction() {
String defineUserFunction = "(defun x (n m) (+ n m))";
String input = "(funcall 'x 2 30)";
evaluateString(defineUserFunction);
assertSExpressionsMatch(evaluateString(input), parseString("32"));
}
@Test(expected = TooFewArgumentsException.class)
public void testFuncallWithTooFewArguments() {
evaluateString("(funcall)");
}
}

View File

@ -0,0 +1,49 @@
package function.builtin;
import static testutil.TestUtilities.*;
import org.junit.Test;
import function.ArgumentValidator.*;
public class GREATERPTester {
@Test
public void testGreaterpWithTwoNumbers_ReturnsNil() {
String input = "(> 1 2)";
assertSExpressionsMatch(evaluateString(input), parseString("nil"));
}
@Test
public void testGreaterpWithTwoNumbers_ReturnsT() {
String input = "(> 3 2)";
assertSExpressionsMatch(evaluateString(input), parseString("t"));
}
@Test
public void testGreaterpWithManyNumbers_ReturnsNil() {
String input = "(> 4 3 2 5 1)";
assertSExpressionsMatch(evaluateString(input), parseString("nil"));
}
@Test
public void testGreaterpWithManyNumbers_ReturnsT() {
String input = "(> 4 3 2 1 0)";
assertSExpressionsMatch(evaluateString(input), parseString("t"));
}
@Test(expected = BadArgumentTypeException.class)
public void testGreaterpWithNonNumbers() {
evaluateString("(> 'x 'x)");
}
@Test(expected = TooFewArgumentsException.class)
public void testGreaterpWithTooFewArguments() {
evaluateString("(> 1)");
}
}