Пример #1
0
        public void ReadWriteMemory()
        {
            var vm = new My_VirtualMachine("wr", 10);

            vm.RegisterCommand('w', b => { b.Memory[3] = 42; });
            vm.RegisterCommand('r', b => { Assert.AreEqual(42, b.Memory[3]); });
            vm.Run();
        }
Пример #2
0
        public void ChangeMemoryPointer()
        {
            var vm = new My_VirtualMachine("xy", 10);

            vm.RegisterCommand('x', b => { b.MemoryPointer = b.MemoryPointer + 42; });
            vm.RegisterCommand('y', b => { Assert.AreEqual(42, b.MemoryPointer); });
            vm.Run();
        }
Пример #3
0
        public static void Run(string program, Func <int> read, Action <char> write, int memorySize = 30000)
        {
            var vm = new My_VirtualMachine(program, memorySize);

            My_BrainfuckBasicCommands.RegisterTo(vm, read, write);
            My_BrainfuckLoopCommands.RegisterTo(vm);
            vm.Run();
        }
Пример #4
0
        private IVirtualMachine Vm(string program, int memorySize = 10)
        {
            var vm = new My_VirtualMachine(program, memorySize);

            My_BrainfuckBasicCommands.RegisterTo(vm, () => - 1, c => {});
            My_BrainfuckLoopCommands.RegisterTo(vm);
            vm.Run();
            return(vm);
        }
Пример #5
0
        public void RunManyInstructions()
        {
            var vm    = new My_VirtualMachine(new string('a', 10000), 10);
            var count = 0;

            vm.RegisterCommand('a', b => { count++; });
            vm.Run();
            Assert.AreEqual(10000, count, "Number of executed instructions");
        }
Пример #6
0
        public void IncrementInstructionPointer()
        {
            var res = "";
            var vm  = new My_VirtualMachine("xy", 10);

            vm.RegisterCommand('x', b => { res += "x->" + b.InstructionPointer + ", "; });
            vm.RegisterCommand('y', b => { res += "y->" + b.InstructionPointer; });
            vm.Run();
            Assert.AreEqual("x->0, y->1", res);
        }
Пример #7
0
        public void Initialize()
        {
            var vm = new My_VirtualMachine("xxx", 12345);

            vm.RegisterCommand('x', b => { });
            Assert.AreEqual(12345, vm.Memory.Length);
            Assert.AreEqual(0, vm.MemoryPointer);
            Assert.AreEqual("xxx", vm.Instructions);
            Assert.AreEqual(0, vm.InstructionPointer);
        }
Пример #8
0
        public void MoveInstructionPointerForward()
        {
            var res = "";
            var vm  = new My_VirtualMachine("xyz", 10);

            vm.RegisterCommand('x', b => { b.InstructionPointer++; });
            vm.RegisterCommand('y', b => { Assert.Fail(); });
            vm.RegisterCommand('z', b => { res += "z"; });
            vm.Run();
            Assert.AreEqual("z", res);
        }
Пример #9
0
        public void InstructionPointerCanMovedOutside()
        {
            var res = "";
            var vm  = new My_VirtualMachine("yxz", 10);

            vm.RegisterCommand('x', b => { res += "x"; });
            vm.RegisterCommand('y', b => { Assert.Fail(); });
            vm.RegisterCommand('z', b => { res += "z"; });
            vm.InstructionPointer++;
            vm.Run();
            Assert.AreEqual("xz", res);
        }
Пример #10
0
        public void RunInstructionsInRightOrder()
        {
            var vm  = new My_VirtualMachine("abbaaa", 10);
            var res = "";

            vm.RegisterCommand('a', b => { res += "a"; });
            vm.RegisterCommand('b', b => { res += "b"; });
            if (res != "")
            {
                Assert.Fail("Instructions should not be executed before Run() call");
            }
            vm.Run();
            if (res != "")
            {
                Assert.AreEqual("abbaaa", res, "Execution order of program 'abbaaa'");
            }
        }
Пример #11
0
        public void MoveInstructionPointerBackward()
        {
            var res = "";
            var vm  = new My_VirtualMachine(">><", 10);

            vm.RegisterCommand('>', b =>
            {
                b.InstructionPointer++;
                res += ">";
            });
            vm.RegisterCommand('<', b =>
            {
                b.InstructionPointer -= 2;
                res += "<";
            });
            vm.Run();
            Assert.AreEqual("><>", res);
        }
Пример #12
0
        public void SetMemorySize()
        {
            var vm = new My_VirtualMachine("", 42);

            Assert.AreEqual(42, vm.Memory.Length);
        }