// original facebook post: // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/ // Implementation class: // https://pastebin.com/xjyyaBm0 // Related "Six Sided Star" classes: // Implementation: // https://pastebin.com/7g2keFVE // Tests: // https://pastebin.com/zmdNaJ7k import junit.framework.TestCase; import java.io.ByteArrayOutputStream; import java.io.PrintStream; public class IntersectingTrianglesTest extends TestCase { public void test0() { assertOutputEquals(new String[] { // No output at all. }, 0); } public void test1() { assertOutputEquals(new String[] { "*", }, 1); } public void test2() { assertOutputEquals(new String[] { " *", "* *", " *", }, 2); } public void test4() { assertOutputEquals(new String[] { " *", "* * * *", " * *", "* * * *", " *", }, 4); } public void test5() { assertOutputEquals(new String[] { " *", " * *", "* * * * *", " * *", "* * * * *", " * *", " *", }, 5); } public void test7() { assertOutputEquals(new String[] { " *", " * *", "* * * * * * *", " * * * *", " * *", " * * * *", "* * * * * * *", " * *", " *", }, 7); } public void test10() { assertOutputEquals(new String[] { " *", " * *", " * *", "* * * * * * * * * *", " * * * *", " * * * *", " * *", " * * * *", " * * * *", "* * * * * * * * * *", " * *", " * *", " *", }, 10); } public void test13() { assertOutputEquals(new String[] { " *", " * *", " * *", " * *", "* * * * * * * * * * * * *", " * * * *", " * * * *", " * * * *", " * *", " * * * *", " * * * *", " * * * *", "* * * * * * * * * * * * *", " * *", " * *", " * *", " *", }, 13); } public void test16() { assertOutputEquals(new String[] { " *", " * *", " * *", " * *", " * *", "* * * * * * * * * * * * * * * *", " * * * *", " * * * *", " * * * *", " * * * *", " * *", " * * * *", " * * * *", " * * * *", " * * * *", "* * * * * * * * * * * * * * * *", " * *", " * *", " * *", " * *", " *", }, 16); } public void testNeg1() { try { callImplementationBeingTested(-1); fail("Expected IllegalArgumentException."); } catch (final IllegalArgumentException ex) { assertEquals("Argument value for 'triangleLines' must not be negative.", ex.getMessage()); } } public void testNegativeMinValue() { try { callImplementationBeingTested(Integer.MIN_VALUE); fail("Expected IllegalArgumentException."); } catch (final IllegalArgumentException ex) { } } private static void assertOutputEquals(final String[] expectedOutputLines, final int triangleLines) { final PrintStream oldSystemOut = System.out; final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); final PrintStream printStream = new PrintStream(byteArrayOutputStream); System.setOut(printStream); try { callImplementationBeingTested(triangleLines); } finally { System.setOut(oldSystemOut); printStream.close(); } final String expectedOutput = buildExpectedOutputString(expectedOutputLines); assertEquals(expectedOutput, byteArrayOutputStream.toString()); } private static void callImplementationBeingTested(final int triangleLines) { // Implementation being tested: IntersectingTriangles.printIntersectingTriangles(triangleLines); } private static void callImplementationBeingTested(final int triangleLines, final int topLines) { // Implementation being tested: IntersectingTriangles.printIntersectingTriangles(triangleLines, topLines); } private static String buildExpectedOutputString(String[] expectedOutputLines) { final StringBuilder expectedOutputStringBuilder = new StringBuilder(); for (String line : expectedOutputLines) { expectedOutputStringBuilder.append(line); final String NL = System.lineSeparator(); // Local system newline expectedOutputStringBuilder.append(NL); } return expectedOutputStringBuilder.toString(); } }