Advertisement
JeffGrigg

CrossingStarsTest

Mar 22nd, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.12 KB | None | 0 0
  1. import junit.framework.TestCase;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.PrintStream;
  5.  
  6. public class CrossingStarsTest extends TestCase {
  7.  
  8.     private static void printCrossingStars(final int n) {
  9.         // Your implementation here.
  10.     }
  11.  
  12.     public void test0() {
  13.         assertOutputEquals(new String[] {
  14.                 }, 0);
  15.     }
  16.  
  17.     public void test1() {
  18.         assertOutputEquals(new String[] {
  19.                         " *",
  20.                         "**",
  21.                         " *",
  22.                 }, 1);
  23.     }
  24.  
  25.     public void test2() {
  26.         assertOutputEquals(new String[] {
  27.                         "    *",
  28.                         " *  *",
  29.                         "*****",
  30.                         " *  *",
  31.                         "    *",
  32.                 }, 2);
  33.     }
  34.  
  35.     public void test3() {
  36.         assertOutputEquals(new String[] {
  37.                         "        *",
  38.                         "    *   *",
  39.                         " *  *   *",
  40.                         "*********",
  41.                         " *  *   *",
  42.                         "    *   *",
  43.                         "        *",
  44.                 }, 3);
  45.     }
  46.  
  47.     public void test4() {
  48.         assertOutputEquals(new String[] {
  49.                         "             *",
  50.                         "        *    *",
  51.                         "    *   *    *",
  52.                         " *  *   *    *",
  53.                         "**************",
  54.                         " *  *   *    *",
  55.                         "    *   *    *",
  56.                         "        *    *",
  57.                         "             *",
  58.                 }, 4);
  59.     }
  60.  
  61.     public void test5() {
  62.         assertOutputEquals(new String[] {
  63.                         "                   *",
  64.                         "             *     *",
  65.                         "        *    *     *",
  66.                         "    *   *    *     *",
  67.                         " *  *   *    *     *",
  68.                         "********************",
  69.                         " *  *   *    *     *",
  70.                         "    *   *    *     *",
  71.                         "        *    *     *",
  72.                         "             *     *",
  73.                         "                   *",
  74.                 }, 5);
  75.     }
  76.  
  77.     private static void assertOutputEquals(final String[] expectedOutputLines, final int n) {
  78.         final PrintStream oldSystemOut = System.out;
  79.         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  80.         final PrintStream printStream = new PrintStream(byteArrayOutputStream);
  81.         System.setOut(printStream);
  82.         try {
  83.             printCrossingStars(n);
  84.         } finally {
  85.             System.setOut(oldSystemOut);
  86.             printStream.close();
  87.         }
  88.  
  89.         final StringBuilder expectedOutputStringBuilder = new StringBuilder();
  90.         for (final String line : expectedOutputLines) {
  91.             expectedOutputStringBuilder.append(line).append(System.lineSeparator());
  92.         }
  93.         assertEquals(expectedOutputStringBuilder.toString(), byteArrayOutputStream.toString());
  94.     }
  95.  
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement