PageRenderTime 80ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/cvml/Tests.cs

http://cvml.googlecode.com/
C# | 302 lines | 266 code | 30 blank | 6 comment | 22 complexity | 8b7e1effd65cdd68686ca1e4927cc555 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. namespace CVML
  7. {
  8. public class Tester
  9. {
  10. public void RunTests()
  11. {
  12. RunTest("one", 1);
  13. RunTest("two", 2);
  14. RunTest("zero", 0);
  15. RunTest("neg1", -1);
  16. RunTest("5", 5);
  17. RunTest("5 inc", 6);
  18. RunTest("5 dec", 4);
  19. RunTest("5 2 add", 7);
  20. RunTest("2 5 sub", 3);
  21. RunTest("5 2 sub", -3);
  22. RunTest("5 2 mul", 10);
  23. RunTest("2 5 div", 2);
  24. RunTest("5 2 div", 0);
  25. RunTest("5 2 mod", 2);
  26. RunTest("2 5 mod", 1);
  27. RunTest("2 5 shl", 20);
  28. RunTest("2 5 shr", 1);
  29. RunTest("(1 2 3) count", 3);
  30. RunTest("(1 2 3) 0 getat", 1);
  31. RunTest("(1 2 3) 1 getat", 2);
  32. RunTest("() count", 0);
  33. RunTest("() empty", 1);
  34. RunTest("(5) poplist", 5);
  35. RunTest("(5) count", 1);
  36. RunTest("(5) empty", 0);
  37. RunTest("(5) poplist pop count", 0);
  38. RunTest("() 5 pushlist count", 1);
  39. RunTest("() 5 pushlist 0 getat", 5);
  40. RunTest("() 5 pushlist dup 0 getat", 5);
  41. RunTest("() 5 pushlist dup 6 pushlist count", 2);
  42. RunTest("[1 2 add] call", 3);
  43. RunTest("[1 2] call add", 3);
  44. RunTest("1 3 5 if", 3);
  45. RunTest("2 3 5 if", 3);
  46. RunTest("0 3 5 if", 5);
  47. RunTest("5 0 [pop 3] ift", 5);
  48. RunTest("5 1 [pop 3] ift", 3);
  49. RunTest("0 [3] [5] ifte", 5);
  50. RunTest("1 [3] [5] ifte", 3);
  51. RunTest("3 5 0 [add] [mul] ifte", 15);
  52. RunTest("3 5 1 [add] [mul] ifte", 8);
  53. RunTest("42 10 [dec] while", 42);
  54. RunTest("0 10 [dec swap inc swap] while", 10);
  55. RunTest("{1:10 2:20 3:30} 1 tblfind", 10);
  56. RunTest("1 2 gt", 1);
  57. RunTest("2 1 gt", 0);
  58. RunTest("2 2 gt", 0);
  59. RunTest("2 2 eq", 1);
  60. RunTest("1 2 eq", 0);
  61. RunTest("2 2 neq", 0);
  62. RunTest("1 2 neq", 1);
  63. RunTest("2 2 gteq", 1);
  64. RunTest("2 2 lteq", 1);
  65. RunTest("3 2 lteq", 1);
  66. RunTest("3 2 lt", 1);
  67. RunTest("2 3 lt", 0);
  68. RunTest("two", 2);
  69. RunTest("f=[1] f call f call add", 2);
  70. RunTest("1 2 3 4 swapN%1", 3);
  71. RunTest("1 2 3 4 swapN%2", 2);
  72. RunTest("1 2 3 4 swapN%3", 1);
  73. RunTest("1 2 3 4 swapNM%1%2 pop", 2);
  74. RunTest("[1] [2] store", 1);
  75. RunTest("[1 swap restore] [2] store", 2);
  76. RunTest("() [pushlist] [] store count", 1);
  77. RunTest("() [pushlist store poplist restore] [] count", 0);
  78. RunTest("[1] [0] store 13 14 if", 13);
  79. RunTest("[1 swap restore] [0] store 13 14 if", 14);
  80. // Other incorrectly written tests.
  81. //RunTest("callcc=[store swap call] 13 [99 swap restore] callcc call", 13);
  82. //RunTest("13 store 99 swap restore", 13);
  83. // Incorrectly written tests, illustrate that it is stupidly hard to do something easy.
  84. //RunTest("gtz=[0 swap gt swap pop] 5 gtz call", 1);
  85. //RunTest("gtz=[0 swap gt swap pop] 5 gtz call pop", 5);
  86. }
  87. public void GetBinaryRep(StringBuilder sb,string title, byte[] block)
  88. {
  89. sb.AppendFormat("{0}, size = {1}", title, block.Length);
  90. sb.AppendLine();
  91. for (int i=0; i < block.Length; ++i)
  92. {
  93. var b = block[i];
  94. if (i > 0)
  95. {
  96. if (i % 8 == 0)
  97. sb.AppendLine();
  98. else
  99. sb.Append(' ');
  100. }
  101. sb.Append(b.ToString("x2"));
  102. }
  103. sb.AppendLine();
  104. }
  105. public string ObjToString(CVM.Object o)
  106. {
  107. if (o.Null)
  108. return "null";
  109. var sb = new StringBuilder();
  110. sb.Append(o.TypeString);
  111. sb.AppendFormat("${0}[{1}]", o.Id, o.Count);
  112. if (o.Deleted)
  113. sb.Append("!Deleted");
  114. sb.AppendFormat("#{0}", o.Refs);
  115. return sb.ToString();
  116. }
  117. public string TblToString(CVM.Table tbl)
  118. {
  119. StringBuilder sb = new StringBuilder();
  120. var kvs = tbl.KeyValues;
  121. sb.Append("{ ");
  122. foreach (var kv in kvs)
  123. {
  124. sb.Append(kv.Key.ToString());
  125. sb.Append(':');
  126. sb.Append(ValToString(kv.Value));
  127. sb.Append(' ');
  128. }
  129. sb.Append('}');
  130. return sb.ToString();
  131. }
  132. public string ValToString(CVM.Val val)
  133. {
  134. if (val.IsInt)
  135. return val.ToInt().ToString();
  136. else
  137. return ObjToString(val.ToObject());
  138. }
  139. StringBuilder BuildStackRep(List<CVM.Val> vals, StringBuilder sb)
  140. {
  141. for (int i=0; i < vals.Count; ++i)
  142. {
  143. if (i > 0) sb.Append(' ');
  144. string s = ValToString(vals[vals.Count - i - 1]);
  145. sb.Append(s);
  146. }
  147. return sb;
  148. }
  149. public void PrintVMCode(CVM.VM vm) {
  150. Console.WriteLine("VM loaded code");
  151. Console.WriteLine(vm.CodeString);
  152. }
  153. public void PrintVMConstants(CVM.VM vm)
  154. {
  155. var sb = new StringBuilder();
  156. foreach (var k in vm.Constants)
  157. sb.Append(ObjToString(k)).Append(' ');
  158. Console.WriteLine("Constants");
  159. Console.WriteLine(sb.ToString());
  160. }
  161. public void PrintVMMemory(CVM.VM vm)
  162. {
  163. var sb = new StringBuilder();
  164. var m = vm.GetHeapMemory();
  165. foreach (var o in m)
  166. sb.Append(ObjToString(o)).Append(' ');
  167. Console.WriteLine("Heap memory:");
  168. Console.WriteLine(" " + sb.ToString());
  169. }
  170. public void InspectStore(CVM.Val store)
  171. {
  172. var obj = store.ToObject();
  173. var bytes = obj.ArrayBytes;
  174. var dis = new ByteFileDisassembler(bytes);
  175. Console.WriteLine("code size: " + dis.code.Count);
  176. Console.WriteLine("ip: " + dis.ip);
  177. Console.WriteLine("constants size: " + dis.constants.Count);
  178. for (int i = 0; i < dis.constants.Count; ++i)
  179. {
  180. Console.Write(dis.constants[i]);
  181. Console.Write(' ');
  182. }
  183. Console.WriteLine();
  184. Console.WriteLine("stack size: " + dis.stack.Count);
  185. for (int i=0; i < dis.stack.Count; ++i) {
  186. Console.Write(dis.stack[i]);
  187. Console.Write(' ');
  188. }
  189. Console.WriteLine();
  190. Console.WriteLine("auxiliary stack size: " + dis.aux.Count);
  191. for (int i = 0; i < dis.aux.Count; ++i)
  192. {
  193. Console.Write(dis.aux[i]);
  194. Console.Write(' ');
  195. }
  196. Console.WriteLine();
  197. Console.WriteLine("array data size: " + dis.adata.Count);
  198. for (int i = 0; i < dis.adata.Count; ++i)
  199. {
  200. var a = dis.adata[i];
  201. Console.Write("array size: ");
  202. Console.Write(a.Length);
  203. Console.Write(' ');
  204. for (int j = 0; j < a.Length; ++j)
  205. {
  206. Console.Write(a[j]);
  207. Console.Write(' ');
  208. }
  209. Console.WriteLine();
  210. }
  211. Console.WriteLine();
  212. Console.WriteLine("object data size: " + dis.odata.Count);
  213. for (int i = 0; i < dis.odata.Count; ++i)
  214. {
  215. Console.Write(dis.odata[i].ToString());
  216. Console.Write(' ');
  217. }
  218. Console.WriteLine();
  219. }
  220. public void PrintVMState(CVM.VM vm)
  221. {
  222. Console.WriteLine("------------------------");
  223. PrintVMMemory(vm);
  224. StringBuilder sb = new StringBuilder();
  225. if (vm.CurOp.Code == CVM.OpCode.Op_restore)
  226. InspectStore(vm.At(0));
  227. sb.AppendFormat("ip = {0}", vm.IP).AppendLine();
  228. sb.AppendFormat("op = {0}", vm.CurOp.ToString()).AppendLine();
  229. sb.Append("stack: ");
  230. BuildStackRep(vm.Stack, sb);
  231. Console.WriteLine(sb.ToString());
  232. sb = new StringBuilder();
  233. sb.Append("aux: ");
  234. BuildStackRep(vm.AuxStack, sb);
  235. Console.WriteLine(sb.ToString());
  236. }
  237. public void RunTest(string input, int output)
  238. {
  239. List<Term> terms = Parser.ParseString(input);
  240. var ts = from t in terms select t.ToString();
  241. Console.WriteLine("Test Input (terms)");
  242. Console.WriteLine(string.Join(" ", ts.ToArray()));
  243. var bf = new ByteFile(terms);
  244. Console.WriteLine("Byte File representation");
  245. Console.WriteLine(bf.ToString());
  246. var vm = new CVM.VM();
  247. byte[] data = bf.GetBytes();
  248. vm.Load(data);
  249. PrintVMCode(vm);
  250. PrintVMConstants(vm);
  251. PrintVMState(vm);
  252. while (vm.Next())
  253. PrintVMState(vm);
  254. try
  255. {
  256. if (vm.Count < 1)
  257. throw new Exception("expected at least one item on the stack");
  258. CVM.Val v = vm.At(0);
  259. if (!v.IsInt)
  260. throw new Exception("expected an int");
  261. int n = v.ToInt();
  262. if (n != output)
  263. throw new Exception(String.Format("Expected {0} but got {1}", output, n));
  264. Console.WriteLine("Test passed");
  265. }
  266. catch (Exception e)
  267. {
  268. Console.WriteLine("Test failed " + e.Message);
  269. }
  270. }
  271. }
  272. }