Advertisement
Guest User

Tester for FibHeap

a guest
May 23rd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.17 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. using System.Diagnostics;
  5. using System.Collections;
  6. using System.Collections.Specialized;
  7.  
  8. namespace ProcessUtilities
  9. {
  10.     /// <summary>
  11.     /// Encapsulates an executable program.
  12.     /// This class makes it easy to run a console app and have that app's output appear
  13.     /// in the parent console's window, and to redirect input and output from/to files.
  14.     /// </summary>
  15.     /// <remarks>
  16.     /// To use this class:
  17.     /// (1) Create an instance.
  18.     /// (2) Set the ProgramFileName property if a filename wasn't specified in the constructor.
  19.     /// (3) Set other properties if required.
  20.     /// (4) Call Run().
  21.     /// </remarks>
  22.  
  23.     public class Executable
  24.     {
  25.         #region Constructor
  26.  
  27.         /// <summary>Runs the specified program file name.</summary>
  28.         /// <param name="programFileName">Name of the program file to run.</param>
  29.  
  30.         public Executable(string programFileName)
  31.         {
  32.             ProgramFileName = programFileName;
  33.  
  34.             _processStartInfo.ErrorDialog = false;
  35.             _processStartInfo.CreateNoWindow = false;
  36.             _processStartInfo.UseShellExecute = false;
  37.             _processStartInfo.RedirectStandardOutput = false;
  38.             _processStartInfo.RedirectStandardError = false;
  39.             _processStartInfo.RedirectStandardInput = false;
  40.             _processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  41.             _processStartInfo.Arguments = "";
  42.         }
  43.  
  44.         /// <summary>Constructor.</summary>
  45.  
  46.         public Executable() : this(string.Empty)
  47.         {
  48.         }
  49.  
  50.         #endregion  // Constructor
  51.  
  52.         #region Public Properties
  53.  
  54.         /// <summary>The filename (full pathname) of the executable.</summary>
  55.  
  56.         public string ProgramFileName
  57.         {
  58.             get
  59.             {
  60.                 return _processStartInfo.FileName;
  61.             }
  62.  
  63.             set
  64.             {
  65.                 _processStartInfo.FileName = value;
  66.             }
  67.         }
  68.  
  69.         /// <summary>The command-line arguments passed to the executable when run. </summary>
  70.  
  71.         public string Arguments
  72.         {
  73.             get
  74.             {
  75.                 return _processStartInfo.Arguments;
  76.             }
  77.  
  78.             set
  79.             {
  80.                 _processStartInfo.Arguments = value;
  81.             }
  82.         }
  83.  
  84.         /// <summary>The working directory set for the executable when run.</summary>
  85.  
  86.         public string WorkingDirectory
  87.         {
  88.             get
  89.             {
  90.                 return _processStartInfo.WorkingDirectory;
  91.             }
  92.  
  93.             set
  94.             {
  95.                 _processStartInfo.WorkingDirectory = value;
  96.             }
  97.         }
  98.  
  99.         /// <summary>
  100.         /// The file to be used if standard input is redirected,
  101.         /// or null or string.Empty to not redirect standard input.
  102.         /// </summary>
  103.  
  104.         public string StandardInputFileName
  105.         {
  106.             set
  107.             {
  108.                 _standardInputFileName = value;
  109.                 _processStartInfo.RedirectStandardInput = !string.IsNullOrEmpty(value);
  110.             }
  111.  
  112.             get
  113.             {
  114.                 return _standardInputFileName;
  115.             }
  116.         }
  117.  
  118.         /// <summary>
  119.         /// The file to be used if standard output is redirected,
  120.         /// or null or string.Empty to not redirect standard output.
  121.         /// </summary>
  122.  
  123.         public string StandardOutputFileName
  124.         {
  125.             set
  126.             {
  127.                 _standardOutputFileName = value;
  128.                 _processStartInfo.RedirectStandardOutput = !string.IsNullOrEmpty(value);
  129.             }
  130.  
  131.             get
  132.             {
  133.                 return _standardOutputFileName;
  134.             }
  135.         }
  136.  
  137.         /// <summary>
  138.         /// The file to be used if standard error is redirected,
  139.         /// or null or string.Empty to not redirect standard error.
  140.         /// </summary>
  141.  
  142.         public string StandardErrorFileName
  143.         {
  144.             set
  145.             {
  146.                 _standardErrorFileName = value;
  147.                 _processStartInfo.RedirectStandardError = !string.IsNullOrEmpty(value);
  148.             }
  149.  
  150.             get
  151.             {
  152.                 return _standardErrorFileName;
  153.             }
  154.         }
  155.  
  156.         #endregion  // Public Properties
  157.  
  158.         #region Public Methods
  159.  
  160.         /// <summary>Add a set of name-value pairs into the set of environment variables available to the executable.</summary>
  161.         /// <param name="variables">The name-value pairs to add.</param>
  162.  
  163.         public void AddEnvironmentVariables(StringDictionary variables)
  164.         {
  165.             if (variables == null)
  166.                 throw new ArgumentNullException("variables");
  167.  
  168.             StringDictionary environmentVariables = _processStartInfo.EnvironmentVariables;
  169.  
  170.             foreach (DictionaryEntry e in variables)
  171.                 environmentVariables[(string)e.Key] = (string)e.Value;
  172.         }
  173.  
  174.         /// <summary>Run the executable and wait until the it has terminated.</summary>
  175.         /// <returns>The exit code returned from the executable.</returns>
  176.  
  177.         public int Run()
  178.         {
  179.             Thread standardInputThread = null;
  180.             Thread standardOutputThread = null;
  181.             Thread standardErrorThread = null;
  182.  
  183.             _standardInput = null;
  184.             _standardError = null;
  185.             _standardOutput = null;
  186.  
  187.             int exitCode = -1;
  188.  
  189.             try
  190.             {
  191.                 using (Process process = new Process())
  192.                 {
  193.                     process.StartInfo = _processStartInfo;
  194.                     process.Start();
  195.  
  196.                     if (process.StartInfo.RedirectStandardInput)
  197.                     {
  198.                         _standardInput = process.StandardInput;
  199.                         standardInputThread = startThread(new ThreadStart(supplyStandardInput), "StandardInput");
  200.                     }
  201.  
  202.                     if (process.StartInfo.RedirectStandardError)
  203.                     {
  204.                         _standardError = process.StandardError;
  205.                         standardErrorThread = startThread(new ThreadStart(writeStandardError), "StandardError");
  206.                     }
  207.  
  208.                     if (process.StartInfo.RedirectStandardOutput)
  209.                     {
  210.                         _standardOutput = process.StandardOutput;
  211.                         standardOutputThread = startThread(new ThreadStart(writeStandardOutput), "StandardOutput");
  212.                     }
  213.  
  214.                     process.WaitForExit(400);
  215.                     if(!process.HasExited)
  216.                     {
  217.                         Console.ForegroundColor = ConsoleColor.Red;
  218.                         Console.WriteLine("TLE");
  219.                         process.Kill();
  220.                         Environment.Exit(-1);
  221.                     }
  222.                     exitCode = process.ExitCode;
  223.                 }
  224.             }
  225.  
  226.             finally  // Ensure that the threads do not persist beyond the process being run
  227.             {
  228.                 if (standardInputThread != null)
  229.                     standardInputThread.Join();
  230.  
  231.                 if (standardOutputThread != null)
  232.                     standardOutputThread.Join();
  233.  
  234.                 if (standardErrorThread != null)
  235.                     standardErrorThread.Join();
  236.             }
  237.  
  238.             return exitCode;
  239.         }
  240.  
  241.         #endregion  // Public Methods
  242.  
  243.         #region Private Methods
  244.  
  245.         /// <summary>Start a thread.</summary>
  246.         /// <param name="startInfo">start information for this thread</param>
  247.         /// <param name="name">name of the thread</param>
  248.         /// <returns>thread object</returns>
  249.  
  250.         private static Thread startThread(ThreadStart startInfo, string name)
  251.         {
  252.             Thread t = new Thread(startInfo);
  253.             t.IsBackground = true;
  254.             t.Name = name;
  255.             t.Start();
  256.             return t;
  257.         }
  258.  
  259.         /// <summary>Thread which supplies standard input from the appropriate file to the running executable.</summary>
  260.  
  261.         private void supplyStandardInput()
  262.         {
  263.             // feed text from the file a line at a time into the standard input stream
  264.  
  265.             using (StreamReader reader = File.OpenText(_standardInputFileName))
  266.             using (StreamWriter writer = _standardInput)
  267.             {
  268.                 writer.AutoFlush = true;
  269.  
  270.                 for (; ; )
  271.                 {
  272.                     string textLine = reader.ReadLine();
  273.  
  274.                     if (textLine == null)
  275.                         break;
  276.  
  277.                     writer.WriteLine(textLine);
  278.                 }
  279.             }
  280.         }
  281.  
  282.         /// <summary>Thread which outputs standard output from the running executable to the appropriate file.</summary>
  283.  
  284.         private void writeStandardOutput()
  285.         {
  286.             using (StreamWriter writer = File.CreateText(_standardOutputFileName))
  287.             using (StreamReader reader = _standardOutput)
  288.             {
  289.                 writer.AutoFlush = true;
  290.  
  291.                 for (; ; )
  292.                 {
  293.                     string textLine = reader.ReadLine();
  294.  
  295.                     if (textLine == null)
  296.                         break;
  297.  
  298.                     writer.WriteLine(textLine);
  299.                 }
  300.             }
  301.  
  302.             if (File.Exists(_standardOutputFileName))
  303.             {
  304.                 FileInfo info = new FileInfo(_standardOutputFileName);
  305.  
  306.                 // if the error info is empty or just contains eof etc.
  307.  
  308.                 if (info.Length < 4)
  309.                     info.Delete();
  310.             }
  311.         }
  312.  
  313.         /// <summary>Thread which outputs standard error output from the running executable to the appropriate file.</summary>
  314.  
  315.         private void writeStandardError()
  316.         {
  317.             using (StreamWriter writer = File.CreateText(_standardErrorFileName))
  318.             using (StreamReader reader = _standardError)
  319.             {
  320.                 writer.AutoFlush = true;
  321.  
  322.                 for (; ; )
  323.                 {
  324.                     string textLine = reader.ReadLine();
  325.  
  326.                     if (textLine == null)
  327.                         break;
  328.  
  329.                     writer.WriteLine(textLine);
  330.                 }
  331.             }
  332.  
  333.             if (File.Exists(_standardErrorFileName))
  334.             {
  335.                 FileInfo info = new FileInfo(_standardErrorFileName);
  336.  
  337.                 // if the error info is empty or just contains eof etc.
  338.  
  339.                 if (info.Length < 4)
  340.                     info.Delete();
  341.             }
  342.         }
  343.  
  344.         #endregion  // Private Methods
  345.  
  346.         #region Private Fields
  347.  
  348.         private StreamReader _standardError;
  349.         private StreamReader _standardOutput;
  350.         private StreamWriter _standardInput;
  351.  
  352.         private string _standardInputFileName;
  353.         private string _standardOutputFileName;
  354.         private string _standardErrorFileName;
  355.  
  356.         ProcessStartInfo _processStartInfo = new ProcessStartInfo();
  357.  
  358.  
  359.         #endregion  // Private Fields
  360.     }
  361. }
  362.  
  363. namespace Tester
  364. {
  365.     class Program
  366.     {  
  367.         static string GenerateTest(int n, int m, int w)
  368.         {
  369.             Random rnd = new Random();
  370.             int vertex_count = n;
  371.             int edges_count = m;
  372.             string test = "1\n" + Convert.ToString(vertex_count) + " " + Convert.ToString(edges_count);
  373.             for (int i = 0; i < edges_count; ++i)
  374.             {
  375.                 test += "\n" + Convert.ToString(rnd.Next(0, vertex_count - 1)) + " " + Convert.ToString(rnd.Next(0, vertex_count - 1)) + " " + Convert.ToString(rnd.Next(0, w));
  376.                 Console.WriteLine(i);
  377.             }
  378.             test += "\n0";
  379.             return test;
  380.         }
  381.         static void Main(string[] args)
  382.         {
  383.             int n, m, w;
  384.             n = Convert.ToInt32(Console.ReadLine());
  385.             m = Convert.ToInt32(Console.ReadLine());
  386.             w = Convert.ToInt32(Console.ReadLine());
  387.             ProcessUtilities.Executable program = new ProcessUtilities.Executable("C:\\Users\\Юрий\\Desktop\\program.exe");
  388.             ProcessUtilities.Executable checker = new ProcessUtilities.Executable("C:\\Users\\Юрий\\Desktop\\checker.exe");
  389.             while(true)
  390.             {
  391.                 string test = GenerateTest(n, m, w);
  392.                 Console.WriteLine("Test generated\n");
  393.                 //Console.WriteLine(test);
  394.                 File.Delete("test.txt");
  395.                 File.Delete("tested.txt");
  396.                 File.Delete("correct.txt");
  397.                 File.Create("test.txt");
  398.                 File.Create("tested.txt");
  399.                 File.Create("correct.txt")
  400.                 File.WriteAllText("test.txt", test);
  401.                 program.StandardInputFileName = "test.txt";
  402.                 checker.StandardInputFileName = "test.txt";
  403.                 program.StandardOutputFileName = "tested.txt";
  404.                 checker.StandardOutputFileName = "correct.txt";
  405.                 program.Run();
  406.                 checker.Run();
  407.                 string correct_output = File.ReadAllText("correct.txt");
  408.                 string my_output = File.ReadAllText("tested.txt");
  409.                 if(my_output != correct_output)
  410.                 {
  411.                     Console.ForegroundColor = ConsoleColor.Red;
  412.                     Console.WriteLine("WA\n");
  413.                     Environment.Exit(-1);
  414.                 }
  415.                 Console.ForegroundColor = ConsoleColor.Green;
  416.                 Console.WriteLine("OK\n");
  417.             }
  418.         }
  419.     }
  420. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement