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(); }
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(); }
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); }
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); }
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); }
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'"); } }
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); }
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"); }
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); }