Update to more closely follow Java naming standards.
authorMichael Welch <michaelgwelch@gmail.com>
Sun, 26 Feb 2012 15:54:24 +0000 (09:54 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Thu, 1 Mar 2012 19:03:13 +0000 (11:03 -0800)
Renamed IExecutionContext to ExecutionContext.
Use all caps for Instruction members.
Addeded some more comments.
Started on a parse method for Tape.

java/src/welch/brainmess/DefaultExecutionContext.java [new file with mode: 0644]
java/src/welch/brainmess/ExecutionContext.java
java/src/welch/brainmess/IExecutionContext.java [deleted file]
java/src/welch/brainmess/Instruction.java
java/src/welch/brainmess/Tape.java
java/test/welch/brainmess/InstructionTests.java
java/test/welch/brainmess/TestTape.java

diff --git a/java/src/welch/brainmess/DefaultExecutionContext.java b/java/src/welch/brainmess/DefaultExecutionContext.java
new file mode 100644 (file)
index 0000000..a3ad5fc
--- /dev/null
@@ -0,0 +1,143 @@
+package welch.brainmess;
+
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.io.PrintWriter;
+import java.io.Reader;
+
+/**
+ * Represents the execution environment for executing a brain mess program.
+ * It also has a main method that is the entry point for executing a program.
+ * @author Michael Welch
+ *
+ */
+public class DefaultExecutionContext implements ExecutionContext
+{
+
+       private final Tape tape;
+       private final Program program;
+       private final PrintWriter output;
+       private final Reader input;
+       
+       
+       public DefaultExecutionContext(Program program, PrintStream output, InputStream input)
+       {
+               this.program = program;
+               this.output = new PrintWriter(output);
+               this.input = new InputStreamReader(input);
+               tape = new Tape();
+       }
+       
+       /**
+        * Executes a brain mess program.
+        * @param args The first value in args is expected to be the name of a file
+        * that contains the brain mess program to execute.
+        */
+       public static void main(String[] args)
+       {
+               Program p = new Program(args[0]);
+               ExecutionContext context = new DefaultExecutionContext(p, System.out, System.in);
+               
+               while (!p.isEndOfProgram())
+               {
+                       p.fetch().execute(context);
+               }
+       }
+       
+       
+       /* (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;
+               try
+               {
+                       value = input.read();
+               } catch (IOException e)
+               {
+                       throw new RuntimeException("An input exception was encountered. Program must terminate", e);
+               }
+               tape.setCurrent(value);
+       }
+
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#ouput()
+        */
+       @Override
+       public void ouput()
+       {
+               int value = tape.getCurrent();
+               output.write((char)value);
+               output.flush();
+       }
+
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#testAndJumpForward()
+        */
+       @Override
+       public void testAndJumpForward()
+       {
+               if (tape.getCurrent() == 0)
+               {
+                       program.jumpForward();
+               }
+       }
+
+       /* (non-Javadoc)
+        * @see welch.brainmess.IExecutionContext#testAndJumpBackward()
+        */
+       @Override
+       public void testAndJumpBackward()
+       {
+               if (tape.getCurrent() != 0)
+               {
+                       program.jumpBackward();
+               }
+       }
+
+}
+
+       
\ No newline at end of file
index 02b39de..4824894 100644 (file)
 package welch.brainmess;
 
-
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.PrintStream;
-import java.io.PrintWriter;
-import java.io.Reader;
-
 /**
- * Represents the execution environment for executing a brain mess program.
- * It also has a main method that is the entry point for executing a program.
- * @author Michael Welch
+ * Represents and encapsulates the entire environment that a brainmess program runs within.
+ * This exposes all of the methods that an instruction needs access to execute itself.
+ * @author mgwelch
  *
  */
-public class ExecutionContext implements IExecutionContext
-{
+public interface ExecutionContext {
 
-       private final Tape tape;
-       private final Program program;
-       private final PrintWriter output;
-       private final Reader input;
-       
-       
-       public ExecutionContext(Program program, PrintStream output, InputStream input)
-       {
-               this.program = program;
-               this.output = new PrintWriter(output);
-               this.input = new InputStreamReader(input);
-               tape = new Tape();
-       }
-       
        /**
-        * Executes a brain mess program.
-        * @param args The first value in args is expected to be the name of a file
-        * that contains the brain mess program to execute.
-        */
-       public static void main(String[] args)
-       {
-               Program p = new Program(args[0]);
-               IExecutionContext context = new ExecutionContext(p, System.out, System.in);
-               
-               while (!p.isEndOfProgram())
-               {
-                       p.fetch().execute(context);
-               }
-       }
-       
-       
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#moveForward()
+        * Tells the tape to move forward
         */
-       @Override
-       public void moveForward()
-       {
-               tape.moveForward();
-               
-       }
+       public abstract void moveForward();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#moveBackward()
+       /**
+        * Tells the tape to move backward.
         */
-       @Override
-       public void moveBackward()
-       {
-               tape.moveBackward();
-       }
+       public abstract void moveBackward();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#increment()
+       /**
+        * Increments the current cell of the tape by one.
         */
-       @Override
-       public void increment()
-       {
-               tape.increment();
-       }
-       
+       public abstract void increment();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#decrement()
+       /**
+        * Decrements the current cell of the tape by one.
         */
-       @Override
-       public void decrement()
-       {
-               tape.decrement();
-       }
+       public abstract void decrement();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#input()
+       /**
+        * Reads a character from the input and writes its
+        * integer value to the current cell of the tape.
         */
-       @Override
-       public void input()
-       {
-               int value;
-               try
-               {
-                       value = input.read();
-               } catch (IOException e)
-               {
-                       throw new RuntimeException("An input exception was encountered. Program must terminate", e);
-               }
-               tape.setCurrent(value);
-       }
+       public abstract void input();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#ouput()
+       /**
+        * Reads the value from the current cell of the tape
+        * and writes it as a character to the output.
         */
-       @Override
-       public void ouput()
-       {
-               int value = tape.getCurrent();
-               output.write((char)value);
-               output.flush();
-       }
+       public abstract void ouput();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#testAndJumpForward()
+       /**
+        * Tests the current cell of the tape. If it is 0 this jumps the
+        * program forward to the the matching ']' instruction.
+        * The next instruction to be fetched will then be the one
+        * immediately following the ']' instruction.
         */
-       @Override
-       public void testAndJumpForward()
-       {
-               if (tape.getCurrent() == 0)
-               {
-                       program.jumpForward();
-               }
-       }
+       public abstract void testAndJumpForward();
 
-       /* (non-Javadoc)
-        * @see welch.brainmess.IExecutionContext#testAndJumpBackward()
+       /**
+        * 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 '[' instruction.
         */
-       @Override
-       public void testAndJumpBackward()
-       {
-               if (tape.getCurrent() != 0)
-               {
-                       program.jumpBackward();
-               }
-       }
-
-}
+       public abstract void testAndJumpBackward();
 
-       
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/java/src/welch/brainmess/IExecutionContext.java b/java/src/welch/brainmess/IExecutionContext.java
deleted file mode 100644 (file)
index 06f5894..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-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 092d6b4..8846c8c 100644 (file)
@@ -16,75 +16,75 @@ public enum Instruction
        /**
         * Represents the instruction that moves the tape forward one cell.
         */
-       MoveForward
+       MOVE_FORWARD
        {
-               public void execute(IExecutionContext context) { context.moveForward(); }
+               public void execute(ExecutionContext context) { context.moveForward(); }
        },
        
        /**
         * Represents the instruction that moves the tape backward one cell.
         */
-       MoveBackward
+       MOVE_BACKWARD
        {
-               public void execute(IExecutionContext context) { context.moveBackward(); }
+               public void execute(ExecutionContext context) { context.moveBackward(); }
        },
        
        /**
         * Represents the instruction that increments the value of the current cell.
         */
-       Increment
+       INCREMENT
        {
-               public void execute(IExecutionContext context) { context.increment(); }
+               public void execute(ExecutionContext context) { context.increment(); }
        },
        
        /**
         * Represents the instruction that decrements the value of the current cell.
         */
-       Decrement
+       DECREMENT
        {
-               public void execute(IExecutionContext context) { context.decrement(); }
+               public void execute(ExecutionContext context) { context.decrement(); }
        },
        
        /**
         * Represents the instruction that inputs a character and stores its integer
         * value on the tape at the current location.
         */
-       Input
+       INPUT
        {
-               public void execute(IExecutionContext context) { context.input(); }
+               public void execute(ExecutionContext context) { context.input(); }
        },
        
        /**
         * Represents the instruction that reads the value of the current cell and outputs
         * the character it represents.
         */
-       Output
+       OUTPUT
        {
-               public void execute(IExecutionContext context) { context.ouput(); }
+               public void execute(ExecutionContext context) { context.ouput(); }
        },
        
        /**
         * Represents the test and jump forward instruction.
         */
-       TestAndJumpForward
+       TEST_AND_JUMP_FORWARD
        {
-               public void execute(IExecutionContext context) { context.testAndJumpForward(); }
+               public void execute(ExecutionContext context) { context.testAndJumpForward(); }
        },
        
        /**
         * Represents the test and jump backward instruction.
         */
-       TestAndJumpBackward
+       TEST_AND_JUMP_BACKWARD
        {
-               public void execute(IExecutionContext context) { context.testAndJumpBackward(); }
+               public void execute(ExecutionContext context) { context.testAndJumpBackward(); }
        },
        
        /**
         * A no operation instruction. Used for all non-instruction characters in the program.
         */
-       NOP
+       NO_OPERATION
        {
-               public void execute(IExecutionContext context) { }
+               public void execute(ExecutionContext context) { }
        };
        
        
@@ -92,7 +92,7 @@ public enum Instruction
         * Executes the current instruction.
         * @param context The execution environment in which to execute the instruction.
         */
-       public abstract void execute(IExecutionContext context);
+       public abstract void execute(ExecutionContext context);
        
        /**
         * Parses an Instruction from the specified character value.
@@ -104,31 +104,31 @@ public enum Instruction
                switch(value)
                {
                case '>':
-                       return Instruction.MoveForward;
+                       return Instruction.MOVE_FORWARD;
                        
                case '<':
-                       return Instruction.MoveBackward;
+                       return Instruction.MOVE_BACKWARD;
                        
                case '+':
-                       return Instruction.Increment;
+                       return Instruction.INCREMENT;
                        
                case '-':
-                       return Instruction.Decrement;
+                       return Instruction.DECREMENT;
                        
                case '.':
-                       return Instruction.Output;
+                       return Instruction.OUTPUT;
                        
                case ',':
-                       return Instruction.Input;
+                       return Instruction.INPUT;
                        
                case '[':
-                       return Instruction.TestAndJumpForward;
+                       return Instruction.TEST_AND_JUMP_FORWARD;
                        
                case ']':
-                       return Instruction.TestAndJumpBackward;
+                       return Instruction.TEST_AND_JUMP_BACKWARD;
                        
                default:
-                       return Instruction.NOP;
+                       return Instruction.NO_OPERATION;
                }
        }
        
index 41d4327..295ceb4 100644 (file)
@@ -85,7 +85,7 @@ public class Tape
         * Returns the index value of the current cell.
         * @return
         */
-       public int currentIndex()
+       private int currentIndex()
        {
                return traveler.currentIndex();
        }
index ad99c9b..0e72844 100644 (file)
@@ -11,7 +11,7 @@ public class InstructionTests {
 
                // Arrange
                Mockery mock = new Mockery();
-               final IExecutionContext context = mock.mock(IExecutionContext.class);
+               final ExecutionContext context = mock.mock(ExecutionContext.class);
                
                
                mock.checking(new Expectations() {{
@@ -20,7 +20,7 @@ public class InstructionTests {
                
                
                // Act
-               Instruction.MoveForward.execute(context);
+               Instruction.MOVE_FORWARD.execute(context);
                
                // Assert
                mock.assertIsSatisfied();
@@ -32,7 +32,7 @@ public class InstructionTests {
 
                // Arrange
                Mockery mock = new Mockery();
-               final IExecutionContext context = mock.mock(IExecutionContext.class);
+               final ExecutionContext context = mock.mock(ExecutionContext.class);
                
                
                mock.checking(new Expectations() {{
@@ -41,7 +41,7 @@ public class InstructionTests {
                
                
                // Act
-               Instruction.MoveBackward.execute(context);
+               Instruction.MOVE_BACKWARD.execute(context);
                
                // Assert
                mock.assertIsSatisfied();
@@ -51,14 +51,14 @@ public class InstructionTests {
        public void testAndJumpForward_Execute() {
                // Arrange
                Mockery mock = new Mockery();
-               final IExecutionContext context = mock.mock(IExecutionContext.class);
+               final ExecutionContext context = mock.mock(ExecutionContext.class);
                
                mock.checking(new Expectations() {{
                        oneOf(context).testAndJumpForward();
                }});
                
                // Act
-               Instruction.TestAndJumpForward.execute(context);
+               Instruction.TEST_AND_JUMP_FORWARD.execute(context);
                
                // Assert
                mock.assertIsSatisfied();
index 44a537d..1603786 100644 (file)
@@ -3,6 +3,15 @@ package welch.brainmess;
 import org.junit.Test;
 import static org.junit.Assert.*;
 
+// In these tests I take a different approach than normal. I compare
+// expected string representations of tapes with the actual strings 
+// returned by toString of the tape under test.
+
+// I normally wouldn't do this. However, I see Fowler and lots
+// of java test examples that do this. Seems very fragile. What if you
+// want to change your string representation? Every test breaks.
+// However, it is very easy to understand the test.
+
 public class TestTape
 {
        @Test
@@ -52,7 +61,7 @@ public class TestTape
        public void testCurrentIndexOnNewTape()
        {
                Tape tape = new Tape();
-               assertEquals(0, tape.currentIndex());
+               assertEquals("[*0*]", tape.toString());
        }
        
        @Test
@@ -62,7 +71,7 @@ public class TestTape
                Tape tape = new Tape();
                tape.moveForward();
                tape.moveBackward();
-               assertEquals(0, tape.currentIndex());
+               assertEquals("[*0*, 0]", tape.toString());
        }
        
        @Test
@@ -72,42 +81,8 @@ public class TestTape
                Tape tape = new Tape();
                tape.moveBackward();
                tape.moveBackward();
-               assertEquals(0, tape.currentIndex());
+               assertEquals("[*0*, 0, 0]", tape.toString());
        }
        
-       @Test
-       public void testForwardSeries()
-       {
-               Tape tape = new Tape();
-               for (int i = 0; i < 10; i++)
-               {
-                       assertEquals(i, tape.currentIndex());
-                       tape.moveForward();
-               }
-               
-               for (int i = 10; i >= 0; i--)
-               {
-                       assertEquals(i, tape.currentIndex());
-                       tape.moveBackward();
-               }
-               
-       }
-       
-       @Test
-       public void testBackwardSeries()
-       {
-               Tape tape = new Tape();
-               for (int i = 0; i < 10; i++)
-               {
-                       assertEquals(0, tape.currentIndex());
-                       tape.moveBackward();
-               }
-               
-               for (int i = 0; i < 10; i++)
-               {
-                       assertEquals(i, tape.currentIndex());
-                       tape.moveForward();
-               }
-       }
 
 }