Advertisement
JeffGrigg

TwoLoopsTest

Apr 23rd, 2017
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.01 KB | None | 0 0
  1. import java.io.*;
  2. import junit.framework.TestCase;
  3.  
  4. public class TwoLoopsTest extends TestCase {
  5.  
  6.     public void testGaganAjayBansal() throws Exception {
  7.         final String systemOutput = captureSystemOut(() -> {
  8.             GaganAjayBansal.main(new String[0]);
  9.         });
  10.         assertEquals("Expected System output;", multipleLines(
  11.                 "1 ",
  12.                 "1 2 1 ",
  13.                 "1 2 3 2 1 ",
  14.                 "1 2 3 4 3 2 1 ",
  15.                 "1 2 3 4 5 4 3 2 1 ",
  16.                 "1 2 3 4 5 6 5 4 3 2 1 ",
  17.                 "1 2 3 4 5 6 7 6 5 4 3 2 1 "),
  18.                 systemOutput);
  19.     }
  20.  
  21.     public void testNiteshPatil() throws Exception {
  22.         final String systemOutput = captureSystemOut(() -> {
  23.             NiteshPatil.main(new String[0]);
  24.         });
  25.         assertEquals("Expected System output;", multipleLines(
  26.                 "1",
  27.                 "121",
  28.                 "12321",
  29.                 "1234321",
  30.                 "123454321",
  31.                 "12345654321",
  32.                 "1234567654321"),
  33.                 systemOutput);
  34.     }
  35.  
  36.     public void testJeffGriggOneLoop_Default_is7() throws Exception {
  37.         final String systemOutput = captureSystemOut(() -> {
  38.             JeffGriggOneLoop.main(new String[0]);
  39.         });
  40.         assertEquals("Expected System output;", multipleLines(
  41.                 "1 ",
  42.                 "1 2 1 ",
  43.                 "1 2 3 2 1 ",
  44.                 "1 2 3 4 3 2 1 ",
  45.                 "1 2 3 4 5 4 3 2 1 ",
  46.                 "1 2 3 4 5 6 5 4 3 2 1 ",
  47.                 "1 2 3 4 5 6 7 6 5 4 3 2 1 "),
  48.                 systemOutput);
  49.     }
  50.  
  51.     public void testJeffGriggOneLoop_printLinesWithValuesUpTo_3() throws Exception {
  52.         final String systemOutput = captureSystemOut(() -> {
  53.             JeffGriggOneLoop.printLinesWithValuesUpTo(3);
  54.         });
  55.         assertEquals("Expected System output;", multipleLines(
  56.                 "1 ",
  57.                 "1 2 1 ",
  58.                 "1 2 3 2 1 "),
  59.                 systemOutput);
  60.     }
  61.  
  62.     public void testJeffGriggOneLoop_printLinesWithValuesUpTo_2() throws Exception {
  63.         final String systemOutput = captureSystemOut(() -> {
  64.             JeffGriggOneLoop.printLinesWithValuesUpTo(2);
  65.         });
  66.         assertEquals("Expected System output;", multipleLines(
  67.                 "1 ",
  68.                 "1 2 1 "),
  69.                 systemOutput);
  70.     }
  71.  
  72.     public void testJeffGriggOneLoop_printLinesWithValuesUpTo_1() throws Exception {
  73.         final String systemOutput = captureSystemOut(() -> {
  74.             JeffGriggOneLoop.printLinesWithValuesUpTo(1);
  75.         });
  76.         assertEquals("Expected System output;", multipleLines(
  77.                 "1 "),
  78.                 systemOutput);
  79.     }
  80.  
  81.     public void testJeffGriggOneLoop_printLinesWithValuesUpTo_ZERO_NoOutput() throws Exception {
  82.         final String systemOutput = captureSystemOut(() -> {
  83.             JeffGriggOneLoop.printLinesWithValuesUpTo(0);
  84.         });
  85.         assertEquals("Expected System output;", "", systemOutput);
  86.     }
  87.  
  88.     private static String captureSystemOut(final Runnable runnable) throws IOException {
  89.         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  90.         try (final OutputStream os = outputStream) {
  91.             final PrintStream oldSystemOut = System.out;
  92.             try (final PrintStream outputPrintStream = new PrintStream(outputStream)) {
  93.                 System.setOut(outputPrintStream);
  94.                 try {
  95.                     runnable.run();
  96.                 } finally {
  97.                     System.setOut(oldSystemOut);
  98.                 }
  99.             }
  100.         }
  101.         return outputStream.toString();
  102.     }
  103.  
  104.     private String multipleLines(final String... lines) {
  105.         final String lineSeparator = System.getProperty("line.separator");
  106.         final StringBuilder result = new StringBuilder();
  107.         for (final String line : lines) {
  108.             result.append(line).append(lineSeparator);
  109.         }
  110.         return result.toString();
  111.     }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement