Example #1
0
        public void GetSymbolReturnsBlankCharForUninitialisedTapeEntries()
        {
            Tape tape = new Tape();

            Assert.AreEqual(' ', tape.GetSymbol(-1));
            Assert.AreEqual(' ', tape.GetSymbol(1));
        }
Example #2
0
        public void GetSymbolReturnsCharStoredInTapeElements()
        {
            Tape tape = new Tape();

            tape.Write('a');
            Assert.AreEqual('a', tape.GetSymbol(0));
        }
Example #3
0
        public void MovingTapeRightBringsBlankCellIntoView()
        {
            Tape tape = new Tape();

            tape.Write('a');
            tape.MoveRight();

            Assert.AreEqual(' ', tape.Read());
        }
Example #4
0
        public void GetIndexReturnsCurrentTapeIndex()
        {
            Tape tape = new Tape();

            tape.MoveRight();
            Assert.AreEqual(1, tape.GetIndex());
            tape.MoveLeft();
            Assert.AreEqual(0, tape.GetIndex());
        }
Example #5
0
        public void MovingTapeLeftThenRightReturnsTapeToOriginalPosition()
        {
            Tape tape = new Tape();

            char symbol = 'a';

            tape.Write(symbol);
            tape.MoveLeft();
            tape.MoveRight();

            Assert.AreEqual(symbol, tape.Read());
        }
Example #6
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Default;

            ITape tape = null;

            try
            {
                tape = new Tape(File.ReadAllText("./init.tap"));
            }
            catch(Exception)
            {
                tape = new Tape();
            }

            IInstructionTable instructionTable = null;

            try
            {
                instructionTable = ParseInstructions(args);
            }
            catch(Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Failed to load instruction table. " + ex.Message);
                Console.ReadLine();
                return;
            }

            Processor processor = new Processor(tape, instructionTable);

            Console.CursorVisible = false;

            int clockDelay = 500;

            try
            {
                clockDelay = Int32.Parse(ConfigurationManager.AppSettings.Get("ClockDelay"));
            }
            catch(Exception)
            {

            }

            while(true)
            {
                Console.SetCursorPosition(0, 0);
                DrawTape(tape);
                DrawHead(processor);
                Console.WriteLine();
                Console.WriteLine("Tick: " + processor.Tick);

                if(!processor.Execute())
                {
                    break;
                }
                Thread.Sleep(clockDelay);
            }

            Console.WriteLine("Execution completed!");
            Console.ReadLine();
        }
Example #7
0
        public void GetSymbolReturnsBlankCharForNullElements()
        {
            Tape tape = new Tape();

            Assert.AreEqual(' ', tape.GetSymbol(0));
        }
Example #8
0
        public void TapeStoresAllWrittenSymbolsAsItMovesRight()
        {
            Tape tape = new Tape();

            List<char> symbols = new List<char>() { 'a', 'b', 'c', 'd' };

            for(int symbolIndex = 0; symbolIndex < symbols.Count; ++symbolIndex)
            {
                tape.Write(symbols[symbolIndex]);
                tape.MoveRight();
            }

            for (int symbolIndex = symbols.Count - 1; symbolIndex >= 0; --symbolIndex)
            {
                tape.MoveLeft();
                Assert.AreEqual(symbols[symbolIndex], tape.Read());
            }
        }
Example #9
0
        public void TapeCanBeInitialisedWithHeadBeforeInitialisedCellsAndWrittenTo()
        {
            Tape tape = new Tape("a,b\n-1");

            tape.Write('c');
            Assert.AreEqual('c', tape.Read());
        }
Example #10
0
        public void TapeCanBeInitialisedWithHeadAfterInitialisedCellsAndRead()
        {
            Tape tape = new Tape("a,b\n2");

            Assert.AreEqual(' ', tape.Read());
            tape.Write('c');
            Assert.AreEqual('c', tape.Read());
        }
Example #11
0
        public void TapeCanBeInitialisedWithDifferentValues()
        {
            Tape tape = new Tape("a,,b,,c");

            Assert.AreEqual('a', tape.Read());
            tape.MoveRight();
            Assert.AreEqual(' ', tape.Read());
            tape.MoveRight();
            Assert.AreEqual('b', tape.Read());
            tape.MoveRight();
            Assert.AreEqual(' ', tape.Read());
            tape.MoveRight();
            Assert.AreEqual('c', tape.Read());
            tape.MoveRight();
        }
Example #12
0
        public void TapeCanBeInitialisedToDifferentStartPosition()
        {
            Tape tape = new Tape("a,,b,,c\n2");

            Assert.AreEqual('b', tape.Read());
            tape.MoveRight();
            Assert.AreEqual(' ', tape.Read());
            tape.MoveRight();
            Assert.AreEqual('c', tape.Read());
            tape.MoveRight();
        }
Example #13
0
        public void ReadReturnsLastWrittenCharacter()
        {
            Tape tape = new Tape();

            char symbol = 'a';
            tape.Write(symbol);

            Assert.AreEqual(symbol, tape.Read());
        }
Example #14
0
        public void ReadingNewTapeReturnsNull()
        {
            Tape tape = new Tape();

            Assert.AreEqual(' ', tape.Read());
        }