123 lines
3.7 KiB
Java
123 lines
3.7 KiB
Java
package function.builtin.special;
|
|
|
|
import static org.junit.Assert.*;
|
|
import static sexpression.LispNumber.ONE;
|
|
import static sexpression.Nil.NIL;
|
|
import static sexpression.Symbol.T;
|
|
import static testutil.TestUtilities.*;
|
|
|
|
import org.junit.Test;
|
|
|
|
import function.ArgumentValidator.*;
|
|
import sexpression.*;
|
|
|
|
public class LAMBDATester {
|
|
|
|
@Test
|
|
public void lambda() {
|
|
String input = "(lambda (x) x)";
|
|
|
|
assertSExpressionsMatch(parseString("(LAMBDA (X) X)"), evaluateString(input));
|
|
}
|
|
|
|
@Test
|
|
public void lambdaWithNoBody() {
|
|
String input = "(lambda ())";
|
|
|
|
assertSExpressionsMatch(parseString("(LAMBDA ())"), evaluateString(input));
|
|
}
|
|
|
|
@Test
|
|
public void lambdaExpressionIsLambdaExpression() {
|
|
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL)));
|
|
|
|
assertTrue(LAMBDA.isLambdaExpression(lambdaExpression));
|
|
}
|
|
|
|
@Test
|
|
public void somethingElseIsNotLambdaExpression() {
|
|
assertFalse(LAMBDA.isLambdaExpression(T));
|
|
}
|
|
|
|
@Test
|
|
public void createLambdaExpression() {
|
|
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, new Cons(NIL, NIL)));
|
|
|
|
assertSExpressionsMatch(parseString("(:LAMBDA () ())"),
|
|
LAMBDA.createFunction(lambdaExpression).getLambdaExpression());
|
|
}
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
public void lambdaWithDottedArgumentList() {
|
|
String input = "(apply 'lambda (cons '(x) 1))";
|
|
|
|
evaluateString(input);
|
|
}
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
public void lambdaWithDottedLambdaList() {
|
|
String input = "(funcall 'lambda (cons 'a 'b) ())";
|
|
|
|
evaluateString(input);
|
|
}
|
|
|
|
@Test(expected = DottedArgumentListException.class)
|
|
public void createFunctionWithDottedArgumentList() {
|
|
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), new Cons(NIL, ONE));
|
|
|
|
LAMBDA.createFunction(lambdaExpression);
|
|
}
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
public void createFunctionWithNonList() {
|
|
Cons lambdaExpression = new Cons(new Symbol("LAMBDA"), ONE);
|
|
|
|
LAMBDA.createFunction(lambdaExpression);
|
|
}
|
|
|
|
@Test(expected = BadArgumentTypeException.class)
|
|
public void lambdaWithNonSymbolParameter() {
|
|
evaluateString("(lambda (1) ())");
|
|
}
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
public void lambdaWithTooFewArguments() {
|
|
evaluateString("(lambda)");
|
|
}
|
|
|
|
@Test
|
|
public void anonymousLambdaCall() {
|
|
String input = "((lambda (x) x) 203)";
|
|
|
|
assertSExpressionsMatch(new LispNumber("203"), evaluateString(input));
|
|
}
|
|
|
|
@Test
|
|
public void anonymousLambdaCallWithMultipleArguments() {
|
|
String input = "((lambda (x y) (+ x y)) 203 2)";
|
|
|
|
assertSExpressionsMatch(new LispNumber("205"), evaluateString(input));
|
|
}
|
|
|
|
@Test(expected = TooFewArgumentsException.class)
|
|
public void anonymousLambdaCallWithTooFewArguments() {
|
|
evaluateString("((lambda (x) x))");
|
|
}
|
|
|
|
@Test(expected = TooManyArgumentsException.class)
|
|
public void anonymousLambdaCallWithTooManyArguments() {
|
|
evaluateString("((lambda (x y) x) 1 2 3)");
|
|
}
|
|
|
|
@Test
|
|
public void lexicalClosure() {
|
|
evaluateString("(setq increment-count (let ((counter 0)) (lambda () (setq counter (+ 1 counter)))))");
|
|
|
|
assertSExpressionsMatch(parseString("1"), evaluateString("(funcall increment-count)"));
|
|
assertSExpressionsMatch(parseString("2"), evaluateString("(funcall increment-count)"));
|
|
assertSExpressionsMatch(parseString("3"), evaluateString("(funcall increment-count)"));
|
|
assertSExpressionsMatch(parseString("4"), evaluateString("(funcall increment-count)"));
|
|
}
|
|
|
|
}
|