Advertisement
JeffGrigg

SixSidedStarTest

Jul 14th, 2018
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 8.16 KB | None | 0 0
  1. // original facebook post:
  2. // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/
  3.  
  4. // Implementation Class:
  5. // https://pastebin.com/7g2keFVE
  6.  
  7. // Related "Intersecting Triangles" classes:
  8. // Implementation:
  9. // https://pastebin.com/xjyyaBm0
  10. // JUnit test classes:
  11. // https://pastebin.com/iiUDpLZG
  12. // https://pastebin.com/9CCBV1C9
  13.  
  14. import junit.framework.TestCase;
  15.  
  16. import java.io.ByteArrayOutputStream;
  17. import java.io.PrintStream;
  18.  
  19. public class SixSidedStarTest extends TestCase {
  20.  
  21.     public void test0() {
  22.         assertOutputEquals(new String[] {
  23.         }, 0);
  24.     }
  25.  
  26.     public void test1() {
  27.         assertOutputEquals(new String[] {
  28.                 "   *",
  29.                 "* * * *",
  30.                 " *   *",
  31.                 "* * * *",
  32.                 "   *",
  33.         }, 1);
  34.     }
  35.  
  36.     public void test2() {
  37.         assertOutputEquals(new String[] {
  38.                 "      *",
  39.                 "     * *",
  40.                 "* * * * * * *",
  41.                 " * *     * *",
  42.                 "  *       *",
  43.                 " * *     * *",
  44.                 "* * * * * * *",
  45.                 "     * *",
  46.                 "      *",
  47.         }, 2);
  48.     }
  49.  
  50.     public void test3() {
  51.         assertOutputEquals(new String[] {
  52.                 "         *",
  53.                 "        * *",
  54.                 "       *   *",
  55.                 "* * * * * * * * * *",
  56.                 " *   *       *   *",
  57.                 "  * *         * *",
  58.                 "   *           *",
  59.                 "  * *         * *",
  60.                 " *   *       *   *",
  61.                 "* * * * * * * * * *",
  62.                 "       *   *",
  63.                 "        * *",
  64.                 "         *",
  65.         }, 3);
  66.     }
  67.  
  68.     public void test4() {
  69.         assertOutputEquals(new String[] {
  70.                 "            *",
  71.                 "           * *",
  72.                 "          *   *",
  73.                 "         *     *",
  74.                 "* * * * * * * * * * * * *",
  75.                 " *     *         *     *",
  76.                 "  *   *           *   *",
  77.                 "   * *             * *",
  78.                 "    *               *",
  79.                 "   * *             * *",
  80.                 "  *   *           *   *",
  81.                 " *     *         *     *",
  82.                 "* * * * * * * * * * * * *",
  83.                 "         *     *",
  84.                 "          *   *",
  85.                 "           * *",
  86.                 "            *",
  87.         }, 4);
  88.     }
  89.  
  90.     public void test5() {
  91.         assertOutputEquals(new String[] {
  92.                 "               *",
  93.                 "              * *",
  94.                 "             *   *",
  95.                 "            *     *",
  96.                 "           *       *",
  97.                 "* * * * * * * * * * * * * * * *",
  98.                 " *       *           *       *",
  99.                 "  *     *             *     *",
  100.                 "   *   *               *   *",
  101.                 "    * *                 * *",
  102.                 "     *                   *",
  103.                 "    * *                 * *",
  104.                 "   *   *               *   *",
  105.                 "  *     *             *     *",
  106.                 " *       *           *       *",
  107.                 "* * * * * * * * * * * * * * * *",
  108.                 "           *       *",
  109.                 "            *     *",
  110.                 "             *   *",
  111.                 "              * *",
  112.                 "               *",
  113.         }, 5);
  114.     }
  115.  
  116.     public void test10() {
  117.         assertOutputEquals(new String[] {
  118.                 "                              *",
  119.                 "                             * *",
  120.                 "                            *   *",
  121.                 "                           *     *",
  122.                 "                          *       *",
  123.                 "                         *         *",
  124.                 "                        *           *",
  125.                 "                       *             *",
  126.                 "                      *               *",
  127.                 "                     *                 *",
  128.                 "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *",
  129.                 " *                 *                     *                 *",
  130.                 "  *               *                       *               *",
  131.                 "   *             *                         *             *",
  132.                 "    *           *                           *           *",
  133.                 "     *         *                             *         *",
  134.                 "      *       *                               *       *",
  135.                 "       *     *                                 *     *",
  136.                 "        *   *                                   *   *",
  137.                 "         * *                                     * *",
  138.                 "          *                                       *",
  139.                 "         * *                                     * *",
  140.                 "        *   *                                   *   *",
  141.                 "       *     *                                 *     *",
  142.                 "      *       *                               *       *",
  143.                 "     *         *                             *         *",
  144.                 "    *           *                           *           *",
  145.                 "   *             *                         *             *",
  146.                 "  *               *                       *               *",
  147.                 " *                 *                     *                 *",
  148.                 "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *",
  149.                 "                     *                 *",
  150.                 "                      *               *",
  151.                 "                       *             *",
  152.                 "                        *           *",
  153.                 "                         *         *",
  154.                 "                          *       *",
  155.                 "                           *     *",
  156.                 "                            *   *",
  157.                 "                             * *",
  158.                 "                              *",
  159.         }, 10);
  160.     }
  161.  
  162.     public void testNeg1() {
  163.         try {
  164.             callImplementationBeingTested(-1);
  165.             fail("Expected IllegalArgumentException.");
  166.         } catch (final IllegalArgumentException ex) {
  167.             assertEquals("Argument value for 'n' must not be negative.", ex.getMessage());
  168.         }
  169.     }
  170.  
  171.     public void testNegativeMinValue() {
  172.         try {
  173.             callImplementationBeingTested(Integer.MIN_VALUE);
  174.             fail("Expected IllegalArgumentException.");
  175.         } catch (final IllegalArgumentException ex) {
  176.         }
  177.     }
  178.  
  179.  
  180.     private static void assertOutputEquals(final String[] expectedOutputLines, final int n) {
  181.         final PrintStream oldSystemOut = System.out;
  182.         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  183.         final PrintStream printStream = new PrintStream(byteArrayOutputStream);
  184.         System.setOut(printStream);
  185.         try {
  186.             callImplementationBeingTested(n);
  187.         } finally {
  188.             System.setOut(oldSystemOut);
  189.             printStream.close();
  190.         }
  191.  
  192.         final String expectedOutput = buildExpectedOutputString(expectedOutputLines);
  193.         assertEquals(expectedOutput, byteArrayOutputStream.toString());
  194.     }
  195.  
  196.     private static void callImplementationBeingTested(int n) {
  197.         // Implementation being tested:
  198.         SixSidedStar.printSixSidedStar(n);
  199.     }
  200.  
  201.     private static String buildExpectedOutputString(String[] expectedOutputLines) {
  202.         final StringBuilder expectedOutputStringBuilder = new StringBuilder();
  203.         for (String line : expectedOutputLines) {
  204.  
  205.             expectedOutputStringBuilder.append(line);
  206.  
  207.             final String NL = System.lineSeparator(); // Local system newline
  208.  
  209.             expectedOutputStringBuilder.append(NL);
  210.         }
  211.         return expectedOutputStringBuilder.toString();
  212.     }
  213.  
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement