Advertisement
Guest User

VM Interpreter Test Code

a guest
Nov 23rd, 2014
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.90 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4.  
  5. namespace CodeGolf
  6. {
  7.     class Program
  8.     {
  9.         static byte[] program = { 0x00,0x00,0x00,0x00,0x00,0x00,
  10.             0x00,0x01,0x00,0x00,0x00,0x00,
  11.             0x00,0x02,0x00,0x00,0x00,0x00,
  12.             0x02,0x01,
  13.             0x06,0x01,0x02,
  14.             0x02,0x02,
  15.             0x00,0x02,0x00,0x00,0x00,0x03,
  16.             0x07,0x01,0x02,
  17.             0x03,0x02,
  18.             0x04,0x00,0x01,
  19.             0x03,0x01,
  20.             0x02,0x02,
  21.             0x00,0x02,0x00,0x00,0x00,0x01,
  22.             0x04,0x01,0x02,
  23.             0x03,0x02,
  24.             0x02,0x02,
  25.             0x00,0x02,0x00,0x00,0x27,0x10,
  26.             0x09,0x01,0x02,
  27.             0x03,0x02,
  28.             0x0a,0x00,0x00,0x00,0x05,
  29.             0x00,0x01,0x00,0x00,0x00,0x01,
  30.             0x04,0x02,0x01,
  31.             0x00,0x01,0x00,0x00,0x27,0x10,
  32.             0x09,0x02,0x01,
  33.             0x00,0x01,0x00,0x00,0x00,0x00,
  34.             0x0a,0x00,0x00,0x00,0x03};
  35.         static void Main(string[] args)
  36.         {
  37.             if (args.Length < 2) {
  38.                 Console.WriteLine("Arguments invalid:\nArgument 1 is the filename of the program or interpreter.\n Argument 2 is the collection of arguments to be passed to the program or interpreter.");
  39.             }
  40.  
  41.             long[] times = new long[15];
  42.             for (int i = 0; i < times.Length; i++)
  43.             {
  44.                 System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
  45.  
  46.  
  47.                 string processOutput = "";
  48.                 var prc = new Process();
  49.                 prc.StartInfo.FileName = args[0];
  50.                 prc.StartInfo.Arguments = args[1];
  51.                 prc.StartInfo.RedirectStandardOutput = true;
  52.                 prc.StartInfo.RedirectStandardError = true;
  53.                 prc.EnableRaisingEvents = true;
  54.                 prc.StartInfo.CreateNoWindow = true;
  55.                 prc.StartInfo.UseShellExecute = false;
  56.                
  57.                 prc.OutputDataReceived += (sender, eventArgs) =>
  58.                 {
  59.                     processOutput += eventArgs.Data + '\n';
  60.                 };
  61.  
  62.                 prc.Start();
  63.  
  64.                 prc.StandardInput.Write(program); //Pass the info over standardinput
  65.  
  66.                 prc.WaitForExit();
  67.  
  68.                 if (processOutput != "R0 1168066930\nR1 0\nR2 10000")
  69.                 {
  70.                     Console.WriteLine("Invalid output");
  71.                     Console.Read();
  72.                     return;
  73.                 }
  74.                 else
  75.                 {
  76.                     sw.Stop();
  77.                     times[i] = sw.ElapsedMilliseconds;
  78.  
  79.                     Console.WriteLine("Test " + i + ": " + times[i].ToString("N0") + " ms");
  80.                 }
  81.             }
  82.  
  83.             Console.WriteLine("Average time: " + times.Average().ToString("N2") + " ms");
  84.             Console.Read();
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement