1
0

Refactoring

This commit is contained in:
Serghei Cebotari 2023-12-07 21:44:13 +03:00
parent 923f2faa1f
commit 2f7e2da782
2 changed files with 19 additions and 52 deletions

View File

@ -1,11 +1,8 @@
namespace Codeforces.Test
{
public class IOTester
public class IOTester
{
private static readonly TextReader originalStdIn = Console.In;
private static StringReader? stdIn;
private static readonly TextWriter originalStdOut = Console.Out;
private static StringWriter? stdOut;
public static void Start()
@ -14,14 +11,6 @@ namespace Codeforces.Test
Console.SetOut(stdOut);
}
public static void End()
{
stdOut = null;
stdIn = null;
Console.SetOut(originalStdOut);
Console.SetIn(originalStdIn);
}
public static void SetInput(params string[] lines)
{
string input = string.Join(Environment.NewLine, lines);
@ -29,26 +18,12 @@ namespace Codeforces.Test
Console.SetIn(stdIn);
}
public static string GetOutput()
public static IEnumerable<string> GetOutputLines()
{
if (stdOut == null)
{
return string.Empty;
}
else
{
return stdOut.ToString();
}
}
public static string[] GetOutputLines()
{
return GetOutput().Split(Environment.NewLine).SkipLast(1).ToArray();
}
public static IList<string> GetOutputLinesAsList()
{
return new List<string>(GetOutputLines());
return stdOut?.ToString()
.Split(Environment.NewLine)
.SkipLast(1)
?? Enumerable.Empty<string>();
}
}
}

View File

@ -8,32 +8,28 @@ public class Tests
public void TestText()
{
IOTester.Start();
string[] input = new[]
{
"5",
string[] input =
[
"5",
"256 42",
"1000 1000",
"-1000 1000",
"-1000 1000",
"20 22"
};
];
IOTester.SetInput(input);
Program.Main();
string[] expectedOutput = new[]
{
"298",
string[] expected =
[
"298",
"2000",
"0",
"0",
"42"
};
string[] actualOutput = IOTester.GetOutputLines();
Assert.Equal(expectedOutput.Length, actualOutput.Length);
for (int i = 0; i < expectedOutput.Length; i++)
{
Assert.Equal(expectedOutput[i], actualOutput[i]);
}
];
string[] actual = IOTester.GetOutputLines().ToArray();
Assert.Equal(expected, actual);
}
[Theory]
@ -46,13 +42,9 @@ public class Tests
Program.Main();
string[] expectedOutput = File.ReadLines(output).ToArray();
string[] actualOutput = IOTester.GetOutputLines();
Assert.Equal(expectedOutput.Length, actualOutput.Length);
for (int i = 0; i < expectedOutput.Length; i++)
{
Assert.Equal(expectedOutput[i], actualOutput[i]);
}
var expectedOutput = File.ReadLines(output);
var actualOutput = IOTester.GetOutputLines();
Assert.Equal(expectedOutput, actualOutput);
}
}
public class FileNameGenerator : IEnumerable<object[]>