Added unit tests for the FunctionTable and ExecutionContext
This commit is contained in:
		
							parent
							
								
									c2a373dc85
								
							
						
					
					
						commit
						4719e14d7f
					
				@ -25,15 +25,19 @@ public class FunctionTable {
 | 
				
			|||||||
        uniqueInstance.functionTable.put(functionName, function);
 | 
					        uniqueInstance.functionTable.put(functionName, function);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
 | 
					    public static void reset() {
 | 
				
			||||||
 | 
					        uniqueInstance.initializeFunctionTable();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
    private HashMap<String, LispFunction> functionTable;
 | 
					    private HashMap<String, LispFunction> functionTable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private FunctionTable() {
 | 
					    private FunctionTable() {
 | 
				
			||||||
        this.functionTable = new HashMap<>();
 | 
					 | 
				
			||||||
        
 | 
					 | 
				
			||||||
        initializeFunctionTable();
 | 
					        initializeFunctionTable();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private void initializeFunctionTable() {
 | 
					    private void initializeFunctionTable() {
 | 
				
			||||||
 | 
					        functionTable = new HashMap<>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        functionTable.put("*", new MULTIPLY());
 | 
					        functionTable.put("*", new MULTIPLY());
 | 
				
			||||||
        functionTable.put("+", new PLUS());
 | 
					        functionTable.put("+", new PLUS());
 | 
				
			||||||
        functionTable.put("-", new MINUS());
 | 
					        functionTable.put("-", new MINUS());
 | 
				
			||||||
 | 
				
			|||||||
@ -2,12 +2,23 @@ package function.builtin;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import static testutil.TestUtilities.*;
 | 
					import static testutil.TestUtilities.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.junit.Test;
 | 
					import org.junit.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import function.ArgumentValidator.TooFewArgumentsException;
 | 
					import function.ArgumentValidator.TooFewArgumentsException;
 | 
				
			||||||
 | 
					import table.FunctionTable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class FUNCALLTester {
 | 
					public class FUNCALLTester {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Before
 | 
				
			||||||
 | 
					    public void setUp() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @After
 | 
				
			||||||
 | 
					    public void tearDown() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    public void testFuncallWithNumbers() {
 | 
					    public void testFuncallWithNumbers() {
 | 
				
			||||||
        String input = "(funcall '+ 1 2 3)";
 | 
					        String input = "(funcall '+ 1 2 3)";
 | 
				
			||||||
 | 
				
			|||||||
@ -3,14 +3,25 @@ package function.builtin;
 | 
				
			|||||||
import static org.junit.Assert.*;
 | 
					import static org.junit.Assert.*;
 | 
				
			||||||
import static testutil.TestUtilities.evaluateString;
 | 
					import static testutil.TestUtilities.evaluateString;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.junit.Test;
 | 
					import org.junit.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import function.ArgumentValidator.*;
 | 
					import function.ArgumentValidator.*;
 | 
				
			||||||
import function.builtin.SYMBOL_FUNCTION.UndefinedSymbolFunctionException;
 | 
					import function.builtin.SYMBOL_FUNCTION.UndefinedSymbolFunctionException;
 | 
				
			||||||
import sexpression.Nil;
 | 
					import sexpression.Nil;
 | 
				
			||||||
 | 
					import table.FunctionTable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class SYMBOL_FUNCTIONTester {
 | 
					public class SYMBOL_FUNCTIONTester {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Before
 | 
				
			||||||
 | 
					    public void setUp() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @After
 | 
				
			||||||
 | 
					    public void tearDown() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    public void testSymbolFunction_BuiltinFunction() {
 | 
					    public void testSymbolFunction_BuiltinFunction() {
 | 
				
			||||||
        String input = "(symbol-function '+)";
 | 
					        String input = "(symbol-function '+)";
 | 
				
			||||||
 | 
				
			|||||||
@ -9,6 +9,7 @@ import org.junit.*;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import environment.Environment;
 | 
					import environment.Environment;
 | 
				
			||||||
import function.ArgumentValidator.*;
 | 
					import function.ArgumentValidator.*;
 | 
				
			||||||
 | 
					import table.FunctionTable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class DEFUNTester {
 | 
					public class DEFUNTester {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -22,6 +23,12 @@ public class DEFUNTester {
 | 
				
			|||||||
    public void setUp() {
 | 
					    public void setUp() {
 | 
				
			||||||
        this.outputStream = new ByteArrayOutputStream();
 | 
					        this.outputStream = new ByteArrayOutputStream();
 | 
				
			||||||
        Environment.getInstance().setOutput(new PrintStream(outputStream));
 | 
					        Environment.getInstance().setOutput(new PrintStream(outputStream));
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @After
 | 
				
			||||||
 | 
					    public void tearDown() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										84
									
								
								test/table/ExecutionContextTester.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										84
									
								
								test/table/ExecutionContextTester.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,84 @@
 | 
				
			|||||||
 | 
					package table;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import sexpression.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class ExecutionContextTester {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private ExecutionContext executionContext;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    public ExecutionContextTester() {
 | 
				
			||||||
 | 
					        this.executionContext = ExecutionContext.getInstance();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Before
 | 
				
			||||||
 | 
					    public void setUp() {
 | 
				
			||||||
 | 
					        executionContext.clearContext();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @After
 | 
				
			||||||
 | 
					    public void tearDown() {
 | 
				
			||||||
 | 
					        executionContext.clearContext();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void assignANewScope() {
 | 
				
			||||||
 | 
					        SymbolTable scope = new SymbolTable();
 | 
				
			||||||
 | 
					        executionContext.setScope(scope);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertEquals(scope, executionContext.getScope());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void clearContext() {
 | 
				
			||||||
 | 
					        SymbolTable scope = new SymbolTable();
 | 
				
			||||||
 | 
					        executionContext.setScope(scope);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertEquals(scope, executionContext.getScope());
 | 
				
			||||||
 | 
					        executionContext.clearContext();
 | 
				
			||||||
 | 
					        assertNotEquals(scope, executionContext.getScope());
 | 
				
			||||||
 | 
					        assertNull(executionContext.getScope().getParent());
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void lookupVariable() {
 | 
				
			||||||
 | 
					        executionContext.getScope().put("test", Symbol.T);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertEquals(Symbol.T, executionContext.lookupSymbolValue("test"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void lookupLocalVariable() {
 | 
				
			||||||
 | 
					        SymbolTable scope = new SymbolTable(executionContext.getScope());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        scope.put("local", Symbol.T);
 | 
				
			||||||
 | 
					        executionContext.setScope(scope);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertEquals(Symbol.T, executionContext.lookupSymbolValue("local"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void lookupGlobalVariable() {
 | 
				
			||||||
 | 
					        SymbolTable scope = new SymbolTable(executionContext.getScope());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        executionContext.getScope().put("global", Symbol.T);
 | 
				
			||||||
 | 
					        executionContext.setScope(scope);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertEquals(Symbol.T, executionContext.lookupSymbolValue("global"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void lookupShadowedVariable() {
 | 
				
			||||||
 | 
					        SymbolTable scope = new SymbolTable(executionContext.getScope());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        scope.put("shadowed", Nil.getInstance());
 | 
				
			||||||
 | 
					        executionContext.getScope().put("shadowed", Symbol.T);
 | 
				
			||||||
 | 
					        executionContext.setScope(scope);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertEquals(Nil.getInstance(), executionContext.lookupSymbolValue("shadowed"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										77
									
								
								test/table/FunctionTableTester.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								test/table/FunctionTableTester.java
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,77 @@
 | 
				
			|||||||
 | 
					package table;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import static org.junit.Assert.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import org.junit.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import function.LispFunction;
 | 
				
			||||||
 | 
					import sexpression.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					public class FunctionTableTester {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private LispFunction createLispFunction() {
 | 
				
			||||||
 | 
					        return new LispFunction() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            @Override
 | 
				
			||||||
 | 
					            public SExpression call(Cons argList) {
 | 
				
			||||||
 | 
					                return Symbol.T;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Before
 | 
				
			||||||
 | 
					    public void setUp() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @After
 | 
				
			||||||
 | 
					    public void tearDown() {
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void builtinFunctionIsDefined() {
 | 
				
			||||||
 | 
					        assertTrue(FunctionTable.isAlreadyDefined("CONS"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void undefinedFunctionIsNotDefined() {
 | 
				
			||||||
 | 
					        assertFalse(FunctionTable.isAlreadyDefined("undefined"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void lookupBuiltinFunction_ReturnsFunction() {
 | 
				
			||||||
 | 
					        assertNotNull(FunctionTable.lookupFunction("CONS"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void lookupUndefinedFunction_ReturnsNull() {
 | 
				
			||||||
 | 
					        assertNull(FunctionTable.lookupFunction("undefined"));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void defineFunction() {
 | 
				
			||||||
 | 
					        String functionName = "testFunction";
 | 
				
			||||||
 | 
					        LispFunction testFunction = createLispFunction();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertNull(FunctionTable.lookupFunction(functionName));
 | 
				
			||||||
 | 
					        assertFalse(FunctionTable.isAlreadyDefined(functionName));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        FunctionTable.defineFunction(functionName, testFunction);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertTrue(FunctionTable.isAlreadyDefined(functionName));
 | 
				
			||||||
 | 
					        assertEquals(testFunction, FunctionTable.lookupFunction(functionName));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    @Test
 | 
				
			||||||
 | 
					    public void resetFunctionTable() {
 | 
				
			||||||
 | 
					        String functionName = "testFunction";
 | 
				
			||||||
 | 
					        LispFunction testFunction = createLispFunction();
 | 
				
			||||||
 | 
					        FunctionTable.defineFunction(functionName, testFunction);
 | 
				
			||||||
 | 
					        FunctionTable.reset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        assertFalse(FunctionTable.isAlreadyDefined(functionName));
 | 
				
			||||||
 | 
					        assertNull(FunctionTable.lookupFunction(functionName));
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -11,7 +11,7 @@ public class SymbolTableTester {
 | 
				
			|||||||
    private SymbolTable symbolTable;
 | 
					    private SymbolTable symbolTable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Before
 | 
					    @Before
 | 
				
			||||||
    public void setUp() throws Exception {
 | 
					    public void setUp() {
 | 
				
			||||||
        symbolTable = new SymbolTable();
 | 
					        symbolTable = new SymbolTable();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -23,12 +23,14 @@ public class SymbolTableTester {
 | 
				
			|||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    public void lookupSymbolInTable() {
 | 
					    public void lookupSymbolInTable() {
 | 
				
			||||||
        symbolTable.put("symbol", Symbol.T);
 | 
					        symbolTable.put("symbol", Symbol.T);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assertTrue(symbolTable.contains("symbol"));
 | 
					        assertTrue(symbolTable.contains("symbol"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Test
 | 
					    @Test
 | 
				
			||||||
    public void retrieveSymbolValue() {
 | 
					    public void retrieveSymbolValue() {
 | 
				
			||||||
        symbolTable.put("symbol", Symbol.T);
 | 
					        symbolTable.put("symbol", Symbol.T);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assertEquals(Symbol.T, symbolTable.get("symbol"));
 | 
					        assertEquals(Symbol.T, symbolTable.get("symbol"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -36,6 +38,7 @@ public class SymbolTableTester {
 | 
				
			|||||||
    public void redefineSymbolValue() {
 | 
					    public void redefineSymbolValue() {
 | 
				
			||||||
        symbolTable.put("symbol", Symbol.T);
 | 
					        symbolTable.put("symbol", Symbol.T);
 | 
				
			||||||
        symbolTable.put("symbol", Nil.getInstance());
 | 
					        symbolTable.put("symbol", Nil.getInstance());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        assertEquals(Nil.getInstance(), symbolTable.get("symbol"));
 | 
					        assertEquals(Nil.getInstance(), symbolTable.get("symbol"));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user