Advertisement
JeffGrigg

RecursionTest

Jan 2nd, 2019
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 4.77 KB | None | 0 0
  1. // Question posted at
  2. //  https://www.facebook.com/groups/Javagroup123/permalink/10156805252566664/
  3.  
  4. import junit.framework.TestCase;
  5.  
  6. import java.io.ByteArrayOutputStream;
  7. import java.io.PrintStream;
  8.  
  9. public class RecursionTest extends TestCase {
  10.  
  11.     public void test_0_and_0() {
  12.         assertOutputEquals(new String[]{
  13.                 // No output at all.
  14.         }, 0, 0);
  15.     }
  16.  
  17.     public void test_0_and_10() {
  18.         assertOutputEquals(new String[]{
  19.                 // No output at all.
  20.         }, 0, 10);
  21.     }
  22.  
  23.     public void test_1_of_1_only() {
  24.         assertOutputEquals(new String[]{
  25.                 "1",
  26.         }, 1, 1);
  27.     }
  28.  
  29.     public void test_1_of_1_to_2() {
  30.         assertOutputEquals(new String[]{
  31.                 "1",
  32.                 "2",
  33.         }, 1, 2);
  34.     }
  35.  
  36.     public void test_1_of_1_to_3() {
  37.         assertOutputEquals(new String[]{
  38.                 "1",
  39.                 "2",
  40.                 "3",
  41.         }, 1, 3);
  42.     }
  43.  
  44.     public void test_1_of_1_to_5() {
  45.         assertOutputEquals(new String[]{
  46.                 "1",
  47.                 "2",
  48.                 "3",
  49.                 "4",
  50.                 "5",
  51.         }, 1, 5);
  52.     }
  53.  
  54.     public void test_2_of_1_only() {
  55.         assertOutputEquals(new String[]{
  56.                 "1 1",
  57.         }, 2, 1);
  58.     }
  59.  
  60.     public void test_2_of_1_to_2() {
  61.         assertOutputEquals(new String[]{
  62.                 "1 1",
  63.                 "1 2",
  64.                 "2 2",
  65.         }, 2, 2);
  66.     }
  67.  
  68.     public void test_2_of_1_to_3() {
  69.         assertOutputEquals(new String[]{
  70.                 "1 1",
  71.                 "1 2",
  72.                 "1 3",
  73.                 "2 2",
  74.                 "2 3",
  75.                 "3 3",
  76.         }, 2, 3);
  77.     }
  78.  
  79.     public void test_3_of_1_only() {
  80.         assertOutputEquals(new String[]{
  81.                 "1 1 1",
  82.         }, 3, 1);
  83.     }
  84.  
  85.     public void test_5_of_1_only() {
  86.         assertOutputEquals(new String[]{
  87.                 "1 1 1 1 1",
  88.         }, 5, 1);
  89.     }
  90.  
  91.     public void test_15_of_1_only() {
  92.         assertOutputEquals(new String[]{
  93.                 "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
  94.         }, 15, 1);
  95.     }
  96.  
  97.     public void test_3_of_1_to_2() {
  98.         assertOutputEquals(new String[]{
  99.                 "1 1 1",
  100.                 "1 1 2",
  101.                 "1 2 2",
  102.                 "2 2 2",
  103.         }, 3, 2);
  104.     }
  105.  
  106.     public void test_3_of_1_to_3() {
  107.         assertOutputEquals(new String[]{
  108.                 "1 1 1",
  109.                 "1 1 2",
  110.                 "1 1 3",
  111.                 "1 2 2",
  112.                 "1 2 3",
  113.                 "1 3 3",
  114.                 "2 2 2",
  115.                 "2 2 3",
  116.                 "2 3 3",
  117.                 "3 3 3",
  118.         }, 3, 3);
  119.     }
  120.  
  121.     public void test_10_and_0() {
  122.         assertOutputEquals(new String[]{
  123.                 // No output at all.
  124.         }, 10, 0);
  125.     }
  126.  
  127.     private static final String NL = System.lineSeparator();
  128.  
  129.     private static void assertOutputEquals(final String[] expectedOutputLines, final int n, final int max) {
  130.         final String actualOutput = callFunctionAndReturnStandardOutput(n, max);
  131.         final String trimmedActualOutput = trimTrailingSpacesFromLines(actualOutput);
  132.  
  133.         final String expectedOutput = buildExpectedOutputString(expectedOutputLines);
  134.         assertEquals(expectedOutput, trimmedActualOutput);
  135.     }
  136.  
  137.     private static String callFunctionAndReturnStandardOutput(final int n, final int max) {
  138.         final PrintStream oldSystemOut = System.out;
  139.         final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  140.         final PrintStream printStream = new PrintStream(byteArrayOutputStream);
  141.         System.setOut(printStream);
  142.         try {
  143.  
  144.             // Function call being tested:
  145.             Recursion.generateAndPrintValuesWithMax(n, max);
  146.  
  147.         } finally {
  148.             System.setOut(oldSystemOut);
  149.             printStream.close();
  150.         }
  151.         return byteArrayOutputStream.toString();
  152.     }
  153.  
  154.     private static String trimTrailingSpacesFromLines(final String actualOutput) {
  155.         final StringBuilder result = new StringBuilder();
  156.         if (!"".equals(actualOutput)) {
  157.             for (final String line : actualOutput.split(NL)) {
  158.                 result.append(line.replaceFirst("  *$", "")).append(NL);
  159.             }
  160.         }
  161.         return result.toString();
  162.     }
  163.  
  164.     private static String buildExpectedOutputString(String[] expectedOutputLines) {
  165.         final StringBuilder expectedOutputStringBuilder = new StringBuilder();
  166.         for (String line : expectedOutputLines) {
  167.             expectedOutputStringBuilder.append(line);
  168.             expectedOutputStringBuilder.append(NL);
  169.         }
  170.         return expectedOutputStringBuilder.toString();
  171.     }
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement