Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import junit.framework.TestCase;
- import java.io.ByteArrayOutputStream;
- import java.io.PrintStream;
- public class CrossingStarsTest 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 test6() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * * *",
- " * * * *",
- " * * * * *",
- " * * * * * *",
- "***************************",
- " * * * * * *",
- " * * * * *",
- " * * * *",
- " * * *",
- " * *",
- " *",
- }, 6);
- }
- public void test7() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * * *",
- " * * * *",
- " * * * * *",
- " * * * * * *",
- " * * * * * * *",
- "***********************************",
- " * * * * * * *",
- " * * * * * *",
- " * * * * *",
- " * * * *",
- " * * *",
- " * *",
- " *",
- }, 7);
- }
- public void test8() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * * *",
- " * * * *",
- " * * * * *",
- " * * * * * *",
- " * * * * * * *",
- " * * * * * * * *",
- "********************************************",
- " * * * * * * * *",
- " * * * * * * *",
- " * * * * * *",
- " * * * * *",
- " * * * *",
- " * * *",
- " * *",
- " *",
- }, 8);
- }
- public void test9() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * * *",
- " * * * *",
- " * * * * *",
- " * * * * * *",
- " * * * * * * *",
- " * * * * * * * *",
- " * * * * * * * * *",
- "******************************************************",
- " * * * * * * * * *",
- " * * * * * * * *",
- " * * * * * * *",
- " * * * * * *",
- " * * * * *",
- " * * * *",
- " * * *",
- " * *",
- " *",
- }, 9);
- }
- public void test10() {
- assertOutputEquals(new String[] {
- " *",
- " * *",
- " * * *",
- " * * * *",
- " * * * * *",
- " * * * * * *",
- " * * * * * * *",
- " * * * * * * * *",
- " * * * * * * * * *",
- " * * * * * * * * * *",
- "*****************************************************************",
- " * * * * * * * * * *",
- " * * * * * * * * *",
- " * * * * * * * *",
- " * * * * * * *",
- " * * * * * *",
- " * * * * *",
- " * * * *",
- " * * *",
- " * *",
- " *",
- }, 10);
- }
- 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 {
- Ideone.printStars(n);
- } finally {
- System.setOut(oldSystemOut);
- printStream.close();
- }
- final String expectedOutput = buildExpectedOutputString(expectedOutputLines);
- assertEquals(expectedOutput, byteArrayOutputStream.toString());
- }
- private static String buildExpectedOutputString(String[] expectedOutputLines) {
- final StringBuilder expectedOutputStringBuilder = new StringBuilder();
- for (String line : expectedOutputLines) {
- line = line.replaceAll("(.)", " $1"); // Add spaces
- expectedOutputStringBuilder.append(line);
- // final String NL = System.lineSeparator(); // Local system newline
- final String NL = "\n"; // Unix standard newline
- expectedOutputStringBuilder.append(NL);
- }
- return expectedOutputStringBuilder.toString();
- }
- }
Add Comment
Please, Sign In to add comment