public void ReadIntWorks() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.READ_INT); bytecode.AddRange(BitConverter.GetBytes(4)); bytecode.Add(Bytecode.READ_INT); bytecode.AddRange(BitConverter.GetBytes(1)); bytecode.Add(Bytecode.READ_INT); bytecode.AddRange(BitConverter.GetBytes(2)); var input = new List<string> { "4", "9", "12345" }; int pos = 0; var interpreter = new Interpreter(bytecode.ToArray(), new List<string> {}, null, 5); interpreter.SetReader(() => input[pos++]); interpreter.Run(); Assert.AreEqual(0, interpreter.Stack.Count); Assert.AreEqual(15, interpreter.PC); Assert.AreEqual(4, interpreter.Variables[4]); Assert.AreEqual(9, interpreter.Variables[1]); Assert.AreEqual(12345, interpreter.Variables[2]); }
public void ReadStringWorks() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.READ_STRING); bytecode.AddRange(BitConverter.GetBytes(4)); bytecode.Add(Bytecode.READ_STRING); bytecode.AddRange(BitConverter.GetBytes(1)); bytecode.Add(Bytecode.READ_STRING); bytecode.AddRange(BitConverter.GetBytes(2)); var input = new List<string> { "abc112", "hello", "abababababa" }; int pos = 0; var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { "hello", "world" }, null, 5); interpreter.SetReader(() => input[pos++]); interpreter.Run(); Assert.AreEqual(0, interpreter.Stack.Count); Assert.AreEqual(15, interpreter.PC); Assert.AreEqual(2, interpreter.Variables[4]); Assert.AreEqual(0, interpreter.Variables[1]); Assert.AreEqual(3, interpreter.Variables[2]); Assert.AreEqual("abc112", interpreter.Strings[2]); Assert.AreEqual("abababababa", interpreter.Strings[3]); }
public void ReadIntThrowsIfInputIsNotNumeric() { var bytecode = new List<byte>(); bytecode.Add(Bytecode.READ_INT); bytecode.AddRange(BitConverter.GetBytes(4)); bytecode.Add(Bytecode.READ_INT); bytecode.AddRange(BitConverter.GetBytes(1)); bytecode.Add(Bytecode.READ_INT); bytecode.AddRange(BitConverter.GetBytes(2)); var input = new List<string> { "abcdef", }; int pos = 0; var interpreter = new Interpreter(bytecode.ToArray(), new List<string> { }, null, 5); interpreter.SetReader(() => input[pos++]); interpreter.Stack.Push(4); interpreter.Stack.Push(1); interpreter.Stack.Push(2); interpreter.Run(); }