Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // original facebook post:
- // https://www.facebook.com/groups/javaharikrishna/permalink/1596271237165097/
- // Implementation Class:
- // https://pastebin.com/7g2keFVE
- // Related "Intersecting Triangles" classes:
- // Implementation:
- // https://pastebin.com/xjyyaBm0
- // JUnit test classes:
- // https://pastebin.com/iiUDpLZG
- // https://pastebin.com/9CCBV1C9
- import junit.framework.TestCase;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- public class SixSidedStarTest extends TestCase {
- public void test0() {
- assertOutputEquals(new String[] {
- }, 0);
- }
- public void test1() {
- assertOutputEquals(new String[] {
- " *",
- "* * * *",
- " * *",
- "* * * *",
- " *",
- }, 1);
- }
- public void test2() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- "* * * * * * *",
- " * * * *",
- " * *",
- " * * * *",
- "* * * * * * *",
- " * *",
- " *",
- }, 2);
- }
- public void test3() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * *",
- "* * * * * * * * * *",
- " * * * *",
- " * * * *",
- " * *",
- " * * * *",
- " * * * *",
- "* * * * * * * * * *",
- " * *",
- " * *",
- " *",
- }, 3);
- }
- public void test4() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * *",
- " * *",
- "* * * * * * * * * * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * *",
- " * * * *",
- " * * * *",
- " * * * *",
- "* * * * * * * * * * * * *",
- " * *",
- " * *",
- " * *",
- " *",
- }, 4);
- }
- public void test5() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * *",
- " * *",
- " * *",
- "* * * * * * * * * * * * * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- "* * * * * * * * * * * * * * * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " *",
- }, 5);
- }
- public void test10() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- " * * * *",
- "* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " * *",
- " *",
- }, 10);
- }
- public void testNeg1() {
- try {
- callImplementationBeingTested(-1);
- fail("Expected IllegalArgumentException.");
- } catch (final IllegalArgumentException ex) {
- assertEquals("Argument value for 'n' 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 n) {
- final PrintStream oldSystemOut = System.out;
- final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
- final PrintStream printStream = new PrintStream(byteArrayOutputStream);
- System.setOut(printStream);
- try {
- callImplementationBeingTested(n);
- } finally {
- System.setOut(oldSystemOut);
- printStream.close();
- }
- final String expectedOutput = buildExpectedOutputString(expectedOutputLines);
- assertEquals(expectedOutput, byteArrayOutputStream.toString());
- }
- private static void callImplementationBeingTested(int n) {
- // Implementation being tested:
- SixSidedStar.printSixSidedStar(n);
- }
- 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();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement