Guest User

Untitled

a guest
Jun 19th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Diagnostics;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ConsoleApplication19
  9. {
  10. class Program
  11. {
  12. public static Random ran = new Random();
  13. private static readonly char[] alphaNum = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM".ToCharArray();
  14. private const int MAX_LENGTH = 50;
  15. private const int SPACE_CHANCE = 6;
  16. private const string JAVA_EXE = "c:\\Program Files\\Java\\jre6\\bin\\java.exe";
  17. private const string WORK_DIR = "D:\\src\\csc349\\lab5\\";
  18.  
  19. static void Main(string[] args)
  20. {
  21. while (true)
  22. {
  23. string source = getRandomWord();
  24. string target = getRandomWord();
  25.  
  26. var myStart = new ProcessStartInfo(JAVA_EXE, string.Format("-cp out\\production\\csc349lab5 -ea EditDistance"));
  27. var goodStart = new ProcessStartInfo(JAVA_EXE, string.Format("-cp GivenEditDistance.jar -ea EditDistance"));
  28.  
  29. var outTask = getOutput(myStart, source, target);
  30. var goodTask = getOutput(goodStart, source, target);
  31.  
  32. var myOut = outTask.Result;
  33. var goodOut = goodTask.Result;
  34.  
  35. if (!linewiseEqual(myOut, goodOut))
  36. {
  37. Console.WriteLine();
  38. Console.WriteLine(source);
  39. Console.WriteLine(target);
  40. }
  41. else
  42. {
  43. Console.Write(".");
  44. }
  45. }
  46. }
  47.  
  48. readonly static char[] newlineChars = new[] { '\n', '\r' };
  49. private static bool linewiseEqual(string a, string b)
  50. {
  51. string[] aes = a.Split(newlineChars, StringSplitOptions.RemoveEmptyEntries);
  52. string[] bes = b.Split(newlineChars, StringSplitOptions.RemoveEmptyEntries);
  53.  
  54. if (aes.Length != bes.Length)
  55. return false;
  56.  
  57. for (int i = 0; i < aes.Length; i++)
  58. {
  59. if (!string.Equals(aes[i], bes[i]))
  60. return false;
  61. }
  62.  
  63. return true;
  64. }
  65.  
  66. private static Task<string> getOutput(ProcessStartInfo start, string source, string target)
  67. {
  68. return Task.Factory.StartNew<string>(() =>
  69. {
  70. start.WorkingDirectory = WORK_DIR;
  71. start.RedirectStandardInput = true;
  72. start.RedirectStandardOutput = true;
  73. start.UseShellExecute = false;
  74.  
  75. var p = Process.Start(start);
  76. p.StandardInput.WriteLine(source);
  77. p.StandardInput.WriteLine(target);
  78.  
  79. p.WaitForExit();
  80.  
  81. return p.StandardOutput.ReadToEnd();
  82. });
  83. }
  84.  
  85. private static string getRandomWord()
  86. {
  87. int length = ran.Next(MAX_LENGTH);
  88. char[] chars = new char[length];
  89.  
  90. for (int i = 0; i < length; i++)
  91. {
  92. bool isSpace = ran.Next(SPACE_CHANCE) == 0;
  93. if (isSpace)
  94. chars[i] = ' ';
  95. else
  96. chars[i] = alphaNum[ran.Next(alphaNum.Length)];
  97. }
  98.  
  99. return new string(chars);
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment