Define IExecutionContext interface. Write some more tests
authorMichael Welch <michaelgwelch@gmail.com>
Sun, 26 Feb 2012 04:21:59 +0000 (22:21 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Thu, 1 Mar 2012 19:03:13 +0000 (11:03 -0800)
java/.classpath
java/src/welch/brainmess/ExecutionContext.java
java/src/welch/brainmess/IExecutionContext.java [new file with mode: 0644]
java/src/welch/brainmess/Instruction.java
java/test/welch/brainmess/InstructionTests.java [new file with mode: 0644]

index e72ef7c..74d5b5f 100644 (file)
@@ -3,6 +3,9 @@
        <classpathentry kind="src" path="src"/>
        <classpathentry kind="src" path="test"/>
        <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+       <classpathentry kind="lib" path="/Applications/eclipse/jmock-2.5.1/jmock-2.5.1.jar"/>
+       <classpathentry kind="lib" path="/Applications/eclipse/jmock-2.5.1/hamcrest-core-1.1.jar"/>
+       <classpathentry kind="lib" path="/Applications/eclipse/jmock-2.5.1/hamcrest-library-1.1.jar"/>
        <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
        <classpathentry kind="output" path="bin"/>
 </classpath>
index 5038416..02b39de 100644 (file)
@@ -14,7 +14,7 @@ import java.io.Reader;
  * @author Michael Welch
  *
  */
-public class ExecutionContext
+public class ExecutionContext implements IExecutionContext
 {
 
        private final Tape tape;
@@ -39,7 +39,7 @@ public class ExecutionContext
        public static void main(String[] args)
        {
                Program p = new Program(args[0]);
-               ExecutionContext context = new ExecutionContext(p, System.out, System.in);
+               IExecutionContext context = new ExecutionContext(p, System.out, System.in);
                
                while (!p.isEndOfProgram())
                {
@@ -48,28 +48,48 @@ public class ExecutionContext
        }
        
        
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#moveForward()
+        */
+       @Override
        public void moveForward()
        {
                tape.moveForward();
                
        }
 
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#moveBackward()
+        */
+       @Override
        public void moveBackward()
        {
                tape.moveBackward();
        }
 
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#increment()
+        */
+       @Override
        public void increment()
        {
                tape.increment();
        }
        
 
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#decrement()
+        */
+       @Override
        public void decrement()
        {
                tape.decrement();
        }
 
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#input()
+        */
+       @Override
        public void input()
        {
                int value;
@@ -83,6 +103,10 @@ public class ExecutionContext
                tape.setCurrent(value);
        }
 
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#ouput()
+        */
+       @Override
        public void ouput()
        {
                int value = tape.getCurrent();
@@ -90,12 +114,10 @@ public class ExecutionContext
                output.flush();
        }
 
-       /**
-        * Tests the current cell. If it is 0 this jumps the
-        * program forward to the matching ']' instruction.
-        * The next instruction to be fetched will then be the one
-        * immeidately following the ']' instruction.
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#testAndJumpForward()
         */
+       @Override
        public void testAndJumpForward()
        {
                if (tape.getCurrent() == 0)
@@ -104,12 +126,10 @@ public class ExecutionContext
                }
        }
 
-       /**
-        * Tests the current cell. If it is not 0 this jumps back
-        * to the matching '[' instruction. The next instruction
-        * to be fetched will then be the one immediately following the
-        * '['.
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#testAndJumpBackward()
         */
+       @Override
        public void testAndJumpBackward()
        {
                if (tape.getCurrent() != 0)
diff --git a/java/src/welch/brainmess/IExecutionContext.java b/java/src/welch/brainmess/IExecutionContext.java
new file mode 100644 (file)
index 0000000..06f5894
--- /dev/null
@@ -0,0 +1,33 @@
+package welch.brainmess;
+
+public interface IExecutionContext {
+
+       public abstract void moveForward();
+
+       public abstract void moveBackward();
+
+       public abstract void increment();
+
+       public abstract void decrement();
+
+       public abstract void input();
+
+       public abstract void ouput();
+
+       /**
+        * Tests the current cell. If it is 0 this jumps the
+        * program forward to the matching ']' instruction.
+        * The next instruction to be fetched will then be the one
+        * immeidately following the ']' instruction.
+        */
+       public abstract void testAndJumpForward();
+
+       /**
+        * Tests the current cell. If it is not 0 this jumps back
+        * to the matching '[' instruction. The next instruction
+        * to be fetched will then be the one immediately following the
+        * '['.
+        */
+       public abstract void testAndJumpBackward();
+
+}
\ No newline at end of file
index fcc88ee..beddfd3 100644 (file)
@@ -12,7 +12,7 @@ public enum Instruction
         */
        MoveForward
        {
-               public void execute(ExecutionContext context) { context.moveForward(); }
+               public void execute(IExecutionContext context) { context.moveForward(); }
        },
        
        /**
@@ -20,7 +20,7 @@ public enum Instruction
         */
        MoveBackward
        {
-               public void execute(ExecutionContext context) { context.moveBackward(); }
+               public void execute(IExecutionContext context) { context.moveBackward(); }
        },
        
        /**
@@ -28,7 +28,7 @@ public enum Instruction
         */
        Increment
        {
-               public void execute(ExecutionContext context) { context.increment(); }
+               public void execute(IExecutionContext context) { context.increment(); }
        },
        
        /**
@@ -36,7 +36,7 @@ public enum Instruction
         */
        Decrement
        {
-               public void execute(ExecutionContext context) { context.decrement(); }
+               public void execute(IExecutionContext context) { context.decrement(); }
        },
        
        /**
@@ -45,7 +45,7 @@ public enum Instruction
         */
        Input
        {
-               public void execute(ExecutionContext context) { context.input(); }
+               public void execute(IExecutionContext context) { context.input(); }
        },
        
        /**
@@ -54,7 +54,7 @@ public enum Instruction
         */
        Output
        {
-               public void execute(ExecutionContext context) { context.ouput(); }
+               public void execute(IExecutionContext context) { context.ouput(); }
        },
        
        /**
@@ -62,7 +62,7 @@ public enum Instruction
         */
        TestAndJumpForward
        {
-               public void execute(ExecutionContext context) { context.testAndJumpForward(); }
+               public void execute(IExecutionContext context) { context.testAndJumpForward(); }
        },
        
        /**
@@ -70,7 +70,7 @@ public enum Instruction
         */
        TestAndJumpBackward
        {
-               public void execute(ExecutionContext context) { context.testAndJumpBackward(); }
+               public void execute(IExecutionContext context) { context.testAndJumpBackward(); }
        },
        
        /**
@@ -78,7 +78,7 @@ public enum Instruction
         */
        NOP
        {
-               public void execute(ExecutionContext context) { }
+               public void execute(IExecutionContext context) { }
        };
        
        
@@ -86,7 +86,7 @@ public enum Instruction
         * Executes the current instruction.
         * @param context The execution environment in which to execute the instruction.
         */
-       public abstract void execute(ExecutionContext context);
+       public abstract void execute(IExecutionContext context);
        
        /**
         * Parses an Instruction from the specified character value.
diff --git a/java/test/welch/brainmess/InstructionTests.java b/java/test/welch/brainmess/InstructionTests.java
new file mode 100644 (file)
index 0000000..ac7be5a
--- /dev/null
@@ -0,0 +1,66 @@
+package welch.brainmess;
+
+
+import org.junit.Test;
+import org.jmock.*;
+
+public class InstructionTests {
+
+       @Test
+       public void moveForward_Execute() {
+
+               // Arrange
+               Mockery mock = new Mockery();
+               final IExecutionContext context = mock.mock(IExecutionContext.class);
+               
+               
+               mock.checking(new Expectations() {{
+                       oneOf(context).moveForward();
+               }});
+               
+               
+               // Act
+               Instruction.MoveForward.execute(context);
+               
+               // Assert
+               mock.assertIsSatisfied();
+       }
+
+
+       @Test
+       public void moveBackward_Execute() {
+
+               // Arrange
+               Mockery mock = new Mockery();
+               final IExecutionContext context = mock.mock(IExecutionContext.class);
+               
+               
+               mock.checking(new Expectations() {{
+                       oneOf(context).moveBackward();
+               }});
+               
+               
+               // Act
+               Instruction.MoveBackward.execute(context);
+               
+               // Assert
+               mock.assertIsSatisfied();
+       }
+       
+       @Test
+       public void increment_Execute() {
+               // Arrange
+               Mockery mock = new Mockery();
+               final IExecutionContext context = mock.mock(IExecutionContext.class);
+               
+               mock.checking(new Expectations() {{
+                       oneOf(context).testAndJumpForward();
+               }});
+               
+               // Act
+               Instruction.TestAndJumpForward.execute(context);
+               
+               // Assert
+               mock.assertIsSatisfied();
+       }
+}