JeffGrigg icon

SquareWithDiagonalJUnitTests

JeffGrigg | PRO | 08/20/18 07:52:51 PM UTC | 0 ⭐ | 937 👁️ | Never ⏰ | []
Java 5 |

0 B

|

None

|

0 👍

/

0 👎

// Implementation class:
//  https://pastebin.com/sfBYAjN3
 
import org.junit.Test;
 
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
 
import static org.junit.Assert.*;
 
public class SquareWithDiagonalJUnitTests {
 
    @Test
    public void simpleBoxWithCount() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "00000",
                "01000",
                "00200",
                "00030",
                "00004",
        }, () -> {
            // Simplistic implementation hard-coded to produce the above:
            SquareWithDiagonalImpl.main(null);
        });
    }
 
    @Test
    public void box0() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "0",
        }, 0);
    }
 
    @Test
    public void box1() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "00",
                "01",
        }, 1);
    }
 
    @Test
    public void box2() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "000",
                "010",
                "002",
        }, 2);
    }
 
    @Test
    public void box3() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "0000",
                "0100",
                "0020",
                "0003",
        }, 3);
    }
 
    @Test
    public void box4() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "00000",
                "01000",
                "00200",
                "00030",
                "00004",
        }, 4);
    }
 
    @Test
    public void box5() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "000000",
                "010000",
                "002000",
                "000300",
                "000040",
                "000005",
        }, 5);
    }
 
    @Test
    public void box6() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "0000000",
                "0100000",
                "0020000",
                "0003000",
                "0000400",
                "0000050",
                "0000006",
        }, 6);
    }
 
    @Test
    public void box9() {
        assertSquareWithDiagonalOutputEquals(new String[]{
                "0000000000",
                "0100000000",
                "0020000000",
                "0003000000",
                "0000400000",
                "0000050000",
                "0000006000",
                "0000000700",
                "0000000080",
                "0000000009",
        }, 9);
    }
 
    private static void assertSquareWithDiagonalOutputEquals(final String[] expectedOutputLines, final int size) {
 
        assertSquareWithDiagonalOutputEquals(expectedOutputLines, () -> {
            SquareWithDiagonalImpl.simplisticImplementation_withSeparatePrintStatements(size);
        });
 
        assertSquareWithDiagonalOutputEquals(expectedOutputLines, () -> {
            SquareWithDiagonalImpl.simplisticImplementation_withTernaryOperator(size);
        });
 
        assertSquareWithDiagonalOutputEquals(expectedOutputLines, () -> {
            SquareWithDiagonalImpl.streamImplementation(size);
        });
    }
 
    private static void assertSquareWithDiagonalOutputEquals(final String[] expectedOutputLines, final Runnable callback) {
        final PrintStream oldSystemOut = System.out;
        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final PrintStream printStream = new PrintStream(byteArrayOutputStream);
        System.setOut(printStream);
        try {
 
            // Call code under test, which is expected to send output to "System.out"
            callback.run();
 
        } finally {
            System.setOut(oldSystemOut);
            printStream.close();
        }
 
        final String expectedOutput = buildExpectedOutputString(expectedOutputLines);
        assertEquals(expectedOutput, byteArrayOutputStream.toString());
    }
 
    private static String buildExpectedOutputString(String[] expectedOutputLines) {
        final String NL = System.lineSeparator(); // Local system newline
 
        final StringBuilder expectedOutputStringBuilder = new StringBuilder();
        for (String line : expectedOutputLines) {
            expectedOutputStringBuilder.append(line);
            expectedOutputStringBuilder.append(NL);
        }
        return expectedOutputStringBuilder.toString();
    }
 
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎