public void AssertDoesNothingIfAssertSucceeds() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.ASSERT); bytecode.AddRange(BitConverter.GetBytes(1)); var interpreter = new Interpreter( bytecode.ToArray(), new List<string> { "hello", "world" }, new List<string> { "line 1", "line 2" }, 0); interpreter.Stack.Push(1); var output = new List<string>(); interpreter.SetPrinter((string str) => output.Add(str)); interpreter.Run(); Assert.AreEqual(0, interpreter.Stack.Count); Assert.AreEqual(5, interpreter.PC); Assert.AreEqual(0, output.Count); }
public void PrintIntWorks() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.PUSH_INT); bytecode.AddRange(BitConverter.GetBytes(23L)); bytecode.Add(Bytecode.PRINT_INT); var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "" }, null, 0); var output = new List<string>(); interpreter.SetPrinter((string str) => output.Add(str)); interpreter.Run(); Assert.AreEqual(0, interpreter.Stack.Count); Assert.AreEqual(10, interpreter.PC); Assert.AreEqual(1, output.Count); Assert.AreEqual("23", output[0]); }
public void PrintStringWorks() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.PUSH_STRING); bytecode.AddRange(BitConverter.GetBytes(1)); bytecode.Add(Bytecode.PRINT_STRING); var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "hello", "world" }, null, 0); var output = new List<string>(); interpreter.SetPrinter((string str) => output.Add(str)); interpreter.Run(); Assert.AreEqual(0, interpreter.Stack.Count); Assert.AreEqual(6, interpreter.PC); Assert.AreEqual(1, output.Count); Assert.AreEqual("world", output[0]); }
public void AssertPrintsDiagnosticMessageIfAssertFails() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.ASSERT); bytecode.AddRange(BitConverter.GetBytes(1)); var interpreter = new Interpreter( bytecode.ToArray(), new List<string> { "hello", "world" }, new List<string> { "line 1", "line 2" }, 0); interpreter.Stack.Push(0); var output = new List<string>(); interpreter.SetPrinter((string str) => output.Add(str)); interpreter.Run(); Assert.AreEqual(0, interpreter.Stack.Count); Assert.AreEqual(5, interpreter.PC); Assert.AreEqual(2, output.Count); Assert.AreEqual("Assert failed at line 2", output[0]); Assert.AreEqual("\n\tline 2", output[1]); }