Advertisement
JeffGrigg

SquareWithDiagonalJUnitTests

Aug 20th, 2018
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.48 KB | None | 0 0
  1. // Implementation class:
  2. //  https://pastebin.com/sfBYAjN3
  3.  
  4. import org.junit.Test;
  5.  
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.PrintStream;
  8.  
  9. import static org.junit.Assert.*;
  10.  
  11. public class SquareWithDiagonalJUnitTests {
  12.  
  13.     @Test
  14.     public void simpleBoxWithCount() {
  15.         assertSquareWithDiagonalOutputEquals(new String[]{
  16.                 "00000",
  17.                 "01000",
  18.                 "00200",
  19.                 "00030",
  20.                 "00004",
  21.         }, () -> {
  22.             // Simplistic implementation hard-coded to produce the above:
  23.             SquareWithDiagonalImpl.main(null);
  24.         });
  25.     }
  26.  
  27.     @Test
  28.     public void box0() {
  29.         assertSquareWithDiagonalOutputEquals(new String[]{
  30.                 "0",
  31.         }, 0);
  32.     }
  33.  
  34.     @Test
  35.     public void box1() {
  36.         assertSquareWithDiagonalOutputEquals(new String[]{
  37.                 "00",
  38.                 "01",
  39.         }, 1);
  40.     }
  41.  
  42.     @Test
  43.     public void box2() {
  44.         assertSquareWithDiagonalOutputEquals(new String[]{
  45.                 "000",
  46.                 "010",
  47.                 "002",
  48.         }, 2);
  49.     }
  50.  
  51.     @Test
  52.     public void box3() {
  53.         assertSquareWithDiagonalOutputEquals(new String[]{
  54.                 "0000",
  55.                 "0100",
  56.                 "0020",
  57.                 "0003",
  58.         }, 3);
  59.     }
  60.  
  61.     @Test
  62.     public void box4() {
  63.         assertSquareWithDiagonalOutputEquals(new String[]{
  64.                 "00000",
  65.                 "01000",
  66.                 "00200",
  67.                 "00030",
  68.                 "00004",
  69.         }, 4);
  70.     }
  71.  
  72.     @Test
  73.     public void box5() {
  74.         assertSquareWithDiagonalOutputEquals(new String[]{
  75.                 "000000",
  76.                 "010000",
  77.                 "002000",
  78.                 "000300",
  79.                 "000040",
  80.                 "000005",
  81.         }, 5);
  82.     }
  83.  
  84.     @Test
  85.     public void box6() {
  86.         assertSquareWithDiagonalOutputEquals(new String[]{
  87.                 "0000000",
  88.                 "0100000",
  89.                 "0020000",
  90.                 "0003000",
  91.                 "0000400",
  92.                 "0000050",
  93.                 "0000006",
  94.         }, 6);
  95.     }
  96.  
  97.     @Test
  98.     public void box9() {
  99.         assertSquareWithDiagonalOutputEquals(new String[]{
  100.                 "0000000000",
  101.                 "0100000000",
  102.                 "0020000000",
  103.                 "0003000000",
  104.                 "0000400000",
  105.                 "0000050000",
  106.                 "0000006000",
  107.                 "0000000700",
  108.                 "0000000080",
  109.                 "0000000009",
  110.         }, 9);
  111.     }
  112.  
  113.     private static void assertSquareWithDiagonalOutputEquals(final String[] expectedOutputLines, final int size) {
  114.  
  115.         assertSquareWithDiagonalOutputEquals(expectedOutputLines, () -> {
  116.             SquareWithDiagonalImpl.simplisticImplementation_withSeparatePrintStatements(size);
  117.         });
  118.  
  119.         assertSquareWithDiagonalOutputEquals(expectedOutputLines, () -> {
  120.             SquareWithDiagonalImpl.simplisticImplementation_withTernaryOperator(size);
  121.         });
  122.  
  123.         assertSquareWithDiagonalOutputEquals(expectedOutputLines, () -> {
  124.             SquareWithDiagonalImpl.streamImplementation(size);
  125.         });
  126.     }
  127.  
  128.     private static void assertSquareWithDiagonalOutputEquals(final String[] expectedOutputLines, final Runnable callback) {
  129.         final PrintStream oldSystemOut = System.out;
  130.         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  131.         final PrintStream printStream = new PrintStream(byteArrayOutputStream);
  132.         System.setOut(printStream);
  133.         try {
  134.  
  135.             // Call code under test, which is expected to send output to "System.out"
  136.             callback.run();
  137.  
  138.         } finally {
  139.             System.setOut(oldSystemOut);
  140.             printStream.close();
  141.         }
  142.  
  143.         final String expectedOutput = buildExpectedOutputString(expectedOutputLines);
  144.         assertEquals(expectedOutput, byteArrayOutputStream.toString());
  145.     }
  146.  
  147.     private static String buildExpectedOutputString(String[] expectedOutputLines) {
  148.         final String NL = System.lineSeparator(); // Local system newline
  149.  
  150.         final StringBuilder expectedOutputStringBuilder = new StringBuilder();
  151.         for (String line : expectedOutputLines) {
  152.             expectedOutputStringBuilder.append(line);
  153.             expectedOutputStringBuilder.append(NL);
  154.         }
  155.         return expectedOutputStringBuilder.toString();
  156.     }
  157.  
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement