From f925993093d90788136a4d2a170fa8bd730e36da Mon Sep 17 00:00:00 2001 From: Mike Cifelli Date: Mon, 19 Dec 2016 13:29:31 -0500 Subject: [PATCH] Added unit tests --- test/sexpression/SExpressionTester.java | 31 ++++++++++++- test/table/SymbolTableTester.java | 58 +++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 test/table/SymbolTableTester.java diff --git a/test/sexpression/SExpressionTester.java b/test/sexpression/SExpressionTester.java index 7f9cd9b..c3535b4 100644 --- a/test/sexpression/SExpressionTester.java +++ b/test/sexpression/SExpressionTester.java @@ -2,10 +2,10 @@ package sexpression; import static org.junit.Assert.*; -import org.junit.Before; -import org.junit.Test; +import org.junit.*; import error.ErrorManager; +import function.UserDefinedFunction; import sexpression.LispNumber.InvalidNumberException; public class SExpressionTester { @@ -78,6 +78,33 @@ public class SExpressionTester { assertSExpressionMatchesString(expected, list); } + @Test + public void testLambdaExpressionToString() { + String expected = "(LAMBDA)"; + LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getUniqueInstance()), null); + + assertSExpressionMatchesString(expected, lambda); + } + + @Test + public void testLambdaExpressionGetLambdaExpression() { + String expected = "(LAMBDA)"; + LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getUniqueInstance()), null); + + assertSExpressionMatchesString(expected, lambda.getLambdaExpression()); + } + + @Test + public void testLambdaExpressionGetFunction() { + String expected = "(LAMBDA)"; + UserDefinedFunction function = new UserDefinedFunction(expected, Nil.getUniqueInstance(), + Nil.getUniqueInstance()); + LambdaExpression lambda = new LambdaExpression(new Cons(new Symbol("lambda"), Nil.getUniqueInstance()), + function); + + assertEquals(function, lambda.getFunction()); + } + @Test public void testCarOfNilIsNil() { assertEquals(Nil.getUniqueInstance().getCar(), Nil.getUniqueInstance()); diff --git a/test/table/SymbolTableTester.java b/test/table/SymbolTableTester.java new file mode 100644 index 0000000..6791364 --- /dev/null +++ b/test/table/SymbolTableTester.java @@ -0,0 +1,58 @@ +package table; + +import static org.junit.Assert.*; + +import org.junit.*; + +import sexpression.*; + +public class SymbolTableTester { + + private SymbolTable symbolTable; + + @Before + public void setUp() throws Exception { + symbolTable = new SymbolTable(); + } + + @Test + public void lookupSymbolNotInTable() { + assertFalse(symbolTable.contains("symbol")); + } + + @Test + public void lookupSymbolInTable() { + symbolTable.put("symbol", Symbol.T); + assertTrue(symbolTable.contains("symbol")); + } + + @Test + public void retrieveSymbolValue() { + symbolTable.put("symbol", Symbol.T); + assertEquals(symbolTable.get("symbol"), Symbol.T); + } + + @Test + public void redefineSymbolValue() { + symbolTable.put("symbol", Symbol.T); + symbolTable.put("symbol", Nil.getUniqueInstance()); + assertEquals(symbolTable.get("symbol"), Nil.getUniqueInstance()); + } + + @Test + public void checkParentTableIsCorrect() { + SymbolTable childTable = new SymbolTable(symbolTable); + + assertEquals(childTable.getParent(), symbolTable); + } + + @Test + public void lookupSymbolInParentTable() { + symbolTable.put("symbol", Symbol.T); + SymbolTable childTable = new SymbolTable(symbolTable); + SymbolTable parentTable = childTable.getParent(); + + assertEquals(parentTable.get("symbol"), Symbol.T); + } + +}