Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import junit.framework.TestCase;
- public class TwoLoopsTest extends TestCase {
- public void testGaganAjayBansal() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- GaganAjayBansal.main(new String[0]);
- });
- assertEquals("Expected System output;", multipleLines(
- "1 ",
- "1 2 1 ",
- "1 2 3 2 1 ",
- "1 2 3 4 3 2 1 ",
- "1 2 3 4 5 4 3 2 1 ",
- "1 2 3 4 5 6 5 4 3 2 1 ",
- "1 2 3 4 5 6 7 6 5 4 3 2 1 "),
- systemOutput);
- }
- public void testNiteshPatil() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- NiteshPatil.main(new String[0]);
- });
- assertEquals("Expected System output;", multipleLines(
- "1",
- "121",
- "12321",
- "1234321",
- "123454321",
- "12345654321",
- "1234567654321"),
- systemOutput);
- }
- public void testJeffGriggOneLoop_Default_is7() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- JeffGriggOneLoop.main(new String[0]);
- });
- assertEquals("Expected System output;", multipleLines(
- "1 ",
- "1 2 1 ",
- "1 2 3 2 1 ",
- "1 2 3 4 3 2 1 ",
- "1 2 3 4 5 4 3 2 1 ",
- "1 2 3 4 5 6 5 4 3 2 1 ",
- "1 2 3 4 5 6 7 6 5 4 3 2 1 "),
- systemOutput);
- }
- public void testJeffGriggOneLoop_printLinesWithValuesUpTo_3() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- JeffGriggOneLoop.printLinesWithValuesUpTo(3);
- });
- assertEquals("Expected System output;", multipleLines(
- "1 ",
- "1 2 1 ",
- "1 2 3 2 1 "),
- systemOutput);
- }
- public void testJeffGriggOneLoop_printLinesWithValuesUpTo_2() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- JeffGriggOneLoop.printLinesWithValuesUpTo(2);
- });
- assertEquals("Expected System output;", multipleLines(
- "1 ",
- "1 2 1 "),
- systemOutput);
- }
- public void testJeffGriggOneLoop_printLinesWithValuesUpTo_1() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- JeffGriggOneLoop.printLinesWithValuesUpTo(1);
- });
- assertEquals("Expected System output;", multipleLines(
- "1 "),
- systemOutput);
- }
- public void testJeffGriggOneLoop_printLinesWithValuesUpTo_ZERO_NoOutput() throws Exception {
- final String systemOutput = captureSystemOut(() -> {
- JeffGriggOneLoop.printLinesWithValuesUpTo(0);
- });
- assertEquals("Expected System output;", "", systemOutput);
- }
- private static String captureSystemOut(final Runnable runnable) throws IOException {
- final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
- try (final OutputStream os = outputStream) {
- final PrintStream oldSystemOut = System.out;
- try (final PrintStream outputPrintStream = new PrintStream(outputStream)) {
- System.setOut(outputPrintStream);
- try {
- runnable.run();
- } finally {
- System.setOut(oldSystemOut);
- }
- }
- }
- return outputStream.toString();
- }
- private String multipleLines(final String... lines) {
- final String lineSeparator = System.getProperty("line.separator");
- final StringBuilder result = new StringBuilder();
- for (final String line : lines) {
- result.append(line).append(lineSeparator);
- }
- return result.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement