Example #1
0
        public void execute_empty_instructions()
        {
            var computer = new MockComputerBuilder()
                           .WithDefaultRegisterValue(Computer.InstructionPointer, 0)
                           .Build();
            var initPointer = computer[Computer.InstructionPointer];

            computer.ExecuteAll();

            Assert.AreEqual(initPointer, computer[Computer.InstructionPointer]);
        }
Example #2
0
        public void execute_step_at_instruction_pointer_address()
        {
            var instruction  = new Mock <IInstruction>();
            var instructions = new [] { instruction.Object };
            var computer     = new MockComputerBuilder()
                               .WithDefaultRegisterValue(Computer.InstructionPointer, 0)
                               .WithInstructions(instructions)
                               .Build();

            computer.ExecuteStep();

            instruction.Verify(i => i.Execute(computer), Times.Once);
        }
Example #3
0
        public void move_by_jump_amount_when_integer()
        {
            var nonZeroNumber = Any.NonZeroNumber();
            var jumpDistance  = Any.JumpDistance();
            var computer      = new MockComputerBuilder()
                                .Build();
            var initialPosition = computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer];

            var instruction = new JumpInstruction(1, jumpDistance);

            instruction.Execute(computer);

            Assert.AreEqual(initialPosition + jumpDistance - 1, computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer]);
        }
Example #4
0
        public void execute_each_instruction_on_execute_all()
        {
            var instruction  = new Mock <IInstruction>();
            var instructions = new[] { instruction.Object };
            var computer     = new MockComputerBuilder()
                               .WithDefaultRegisterValue(Computer.InstructionPointer, 0)
                               .WithInstructions(instructions)
                               .Build();

            computer.ExecuteAll();

            instruction.Verify(i => i.Execute(computer), Times.Once);
            Assert.AreEqual(true, computer.Complete);
        }
Example #5
0
        public void advance_instruction_pointer_after_execute_step()
        {
            var instruction  = new Mock <IInstruction>();
            var instructions = new[] { instruction.Object };
            var computer     = new MockComputerBuilder()
                               .WithDefaultRegisterValue(Computer.InstructionPointer, 0)
                               .WithInstructions(instructions)
                               .Build();
            var currentInstructionPointer = computer[Computer.InstructionPointer];

            computer.ExecuteStep();

            Assert.AreEqual(currentInstructionPointer + 1, computer[Computer.InstructionPointer]);
        }
        public void increment_register()
        {
            var register             = Any.Register();
            var initialRegisterValue = Any.RegisterValue();

            var computer = new MockComputerBuilder()
                           .WithDefaultRegisterValue(register, initialRegisterValue)
                           .Build();
            var incrementInstruction = new IncrementInstruction(register);

            incrementInstruction.Execute(computer);

            Assert.AreEqual(initialRegisterValue + 1, computer[register]);
        }
Example #7
0
        public void stop_execution_when_all_executed()
        {
            var instruction  = new Mock <IInstruction>();
            var instructions = new[] { instruction.Object };
            var computer     = new MockComputerBuilder()
                               .WithDefaultRegisterValue(Computer.InstructionPointer, 0)
                               .WithInstructions(instructions)
                               .Build();

            computer.ExecuteAll();

            computer.ExecuteStep();

            instruction.Verify(i => i.Execute(computer), Times.Once);
        }
Example #8
0
        public void copy_value_to_register()
        {
            var register     = Any.Register();
            var initialValue = Any.RegisterValue();
            var newValue     = Any.RegisterValue();

            var computer = new MockComputerBuilder()
                           .WithDefaultRegisterValue(register, initialValue)
                           .Build();

            var copyInstruction = new CopyValueToRegisterInstruction(register, newValue);

            copyInstruction.Execute(computer);

            Assert.AreEqual(newValue, computer[register]);
        }
Example #9
0
        public void stay_for_zero_value_registry()
        {
            var register     = Any.Register();
            var jumpDistance = Any.JumpDistance();

            AdventOfCodeLibrary.Instructions.Computer computer = new MockComputerBuilder()
                                                                 .WithDefaultRegisterValue(register, 0)
                                                                 .Build();
            var initialPosition = computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer];

            var instruction = new JumpInstruction(register, jumpDistance);

            instruction.Execute(computer);

            Assert.AreEqual(initialPosition, computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer]);
        }
Example #10
0
        public void move_by_jump_amount_when_register_non_zero()
        {
            var register      = Any.Register();
            var registerValue = Any.RegisterValue() + 1;
            var jumpDistance  = Any.JumpDistance();

            AdventOfCodeLibrary.Instructions.Computer computer = new MockComputerBuilder()
                                                                 .WithDefaultRegisterValue(register, registerValue)
                                                                 .Build();
            var initialPosition = computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer];

            var instruction = new JumpInstruction(register, jumpDistance);

            instruction.Execute(computer);

            Assert.AreEqual(initialPosition + jumpDistance - 1, computer[AdventOfCodeLibrary.Instructions.Computer.InstructionPointer]);
        }
Example #11
0
        public void copy_register_to_register()
        {
            var registerSource      = Any.Register();
            var registerValueToCopy = Any.RegisterValue();
            var registerDestination = (char)(registerSource + 1);
            var initialValue        = registerValueToCopy + 1;

            var computer = new MockComputerBuilder()
                           .WithDefaultRegisterValue(registerSource, registerValueToCopy)
                           .WithDefaultRegisterValue(registerDestination, initialValue)
                           .Build();

            var copyInstruction = new CopyRegisterToRegisterInstruction(registerSource, registerDestination);

            copyInstruction.Execute(computer);

            Assert.AreEqual(registerValueToCopy, computer[registerDestination]);
        }
Example #12
0
        public void execute_all_with_jump()
        {
            var instructions = new IInstruction[6];

            instructions[0] = new CopyValueToRegisterInstruction('a', 41);
            instructions[1] = new IncrementInstruction('a');
            instructions[2] = new IncrementInstruction('a');
            instructions[3] = new DecrementInstruction('a');
            instructions[4] = new JumpInstruction('a', 2);
            instructions[5] = new DecrementInstruction('a');

            var computer = new MockComputerBuilder()
                           .WithInstructions(instructions)
                           .Build();

            computer.ExecuteAll();

            Assert.AreEqual(42, computer['a']);
        }