Exemplo n.º 1
0
        public void ProcessorExecutePerformsNoOpOnHaltState()
        {
            ITape mockTape = Substitute.For<ITape>();
            mockTape.Read().Returns(' ');
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            Instruction testInstruction = new Instruction();
            testInstruction.State = "START";
            testInstruction.NextStates[' '] = "HALT";
            testInstruction.WriteSymbols[' '] = ' ';
            testInstruction.MoveDirections[' '] = MoveDirection.None;

            mockTable.GetInstruction(Arg.Any<String>()).Returns(testInstruction);

            processor.Execute();

            mockTape.ClearReceivedCalls();
            mockTable.ClearReceivedCalls();

            processor.Execute();

            mockTable.Received(0).GetInstruction(Arg.Any<String>());
            mockTape.Received(0).Read();
            mockTape.Received(0).Write(Arg.Any<char>());
            mockTape.Received(0).MoveLeft();
            mockTape.Received(0).MoveRight();
        }
Exemplo n.º 2
0
        public void ProcessorDoesNotMoveTapeOnNoMoveInstruction()
        {
            ITape mockTape = Substitute.For<ITape>();
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            char symbol = 'a';

            Instruction noMoveInstruction = new Instruction();
            noMoveInstruction.State = "START";
            noMoveInstruction.WriteSymbols[symbol] = symbol;
            noMoveInstruction.MoveDirections[symbol] = MoveDirection.None;
            noMoveInstruction.NextStates[symbol] = "HALT";

            mockTape.Read().Returns(symbol);
            mockTable.GetInstruction(Arg.Any<String>()).Returns(noMoveInstruction);

            processor.Execute();

            mockTable.Received(1).GetInstruction("START");
            mockTape.Received(1).Read();
            mockTape.Received(1).Write(symbol);
            mockTape.Received(0).MoveRight();
            mockTape.Received(0).MoveLeft();
        }
Exemplo n.º 3
0
        public void ProcessorExecutesStartStateFollowedByNextState()
        {
            ITape mockTape = Substitute.For<ITape>();
            mockTape.Read().Returns(' ');
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            Instruction testInstruction = new Instruction();
            testInstruction.State = "START";
            testInstruction.NextStates[' '] = "A";
            testInstruction.WriteSymbols[' '] = ' ';
            testInstruction.MoveDirections[' '] = MoveDirection.None;

            mockTable.GetInstruction(Arg.Any<String>()).Returns(testInstruction);

            processor.Execute();

            mockTable.Received(1).GetInstruction("START");
            mockTape.Received(1).Read();
            mockTape.Received(1).Write(' ');
            mockTape.Received(0).MoveRight();
            mockTape.Received(0).MoveLeft();

            processor.Execute();

            mockTable.Received(1).GetInstruction(testInstruction.NextStates[' ']);
        }
Exemplo n.º 4
0
        public void ProcessorExecuteReturnsFalseOnHaltState()
        {
            ITape mockTape = Substitute.For<ITape>();
            mockTape.Read().Returns(' ');
            IInstructionTable mockTable = Substitute.For<IInstructionTable>();
            Processor processor = new Processor(mockTape, mockTable);

            Instruction testInstruction = new Instruction();
            testInstruction.State = "START";
            testInstruction.NextStates[' '] = "HALT";
            testInstruction.WriteSymbols[' '] = ' ';
            testInstruction.MoveDirections[' '] = MoveDirection.None;

            mockTable.GetInstruction(Arg.Any<String>()).Returns(testInstruction);

            Assert.IsTrue(processor.Execute());
            Assert.IsFalse(processor.Execute());
        }
Exemplo n.º 5
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();
        }