Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package week2;
  2.  
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5.  
  6. import java.io.*;
  7.  
  8.  
  9. /**
  10.  * Created by Benedikt on 27.10.2016.
  11.  */
  12. public class ATest {
  13.     private static final InputStream sIn = System.in;
  14.     public static final PrintStream sOut = System.out;
  15.  
  16.     static String inputOutput(String input){
  17.         try {
  18.         InputStream inputStream = new ByteArrayInputStream(input.getBytes("UTF-8"));
  19.         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  20.         System.setOut(new PrintStream(outputStream));
  21.         System.setIn(inputStream);
  22.  
  23.  
  24.             A.main(new String[1]);
  25.  
  26.  
  27.         System.out.flush();
  28.         System.setIn(sIn);
  29.         System.setOut(sOut);
  30.         return  outputStream.toString("UTF-8").trim();
  31.  
  32.         } catch (Exception e) {
  33.             e.printStackTrace();
  34.         } return "";
  35.     }
  36.  
  37.  
  38.     @Test
  39.     public void noCandidates() throws IOException {
  40.         String input = "1\n" +
  41.                 "1 0 0\n\n";
  42.         String shouldOutput = "Case #1: impossible\n";
  43.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  44.     }
  45.  
  46.     @Test
  47.     public void alreadyMarried() throws IOException {
  48.         String input = "1\n" +
  49.                 "2 0 1\n" +
  50.                 "1\n" +
  51.                 "1 2\n\n";
  52.         String shouldOutput = "Case #1: impossible\n";
  53.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  54.     }
  55.  
  56.     @Test
  57.     public void onlyRelated() throws IOException {
  58.         String input = "1\n" +
  59.                 "2 1 0\n" +
  60.                 "1\n" +
  61.                 "1 2\n\n";
  62.         String shouldOutput = "Case #1: impossible\n";
  63.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  64.     }
  65.  
  66.     @Test
  67.     public void transitivelyRelated() throws IOException {
  68.         String input = "1\n" +
  69.                 "3 2 0\n" +
  70.                 "1 2\n" +
  71.                 "2 3\n" +
  72.                 "1 2\n\n";
  73.         String shouldOutput = "Case #1: impossible\n";
  74.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  75.     }
  76.  
  77.     @Test
  78.     public void transitivelyRelatedThroughMarriage() throws IOException {
  79.         String input = "1\n" +
  80.                 "3 1 1\n" +
  81.                 "1 2\n" +
  82.                 "2 3\n" +
  83.                 "1 2\n\n";
  84.         String shouldOutput = "Case #1: impossible\n";
  85.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  86.     }
  87.  
  88.     @Test
  89.     public void oneCandidate() throws IOException {
  90.         String input = "1\n" +
  91.                 "2 0 0\n" +
  92.                 "1\n";
  93.         String shouldOutput = "Case #1: 1\n";
  94.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  95.     }
  96.  
  97.     @Test
  98.     public void twoImpossibles() throws IOException {
  99.         String input = "2\n" +
  100.                 "2 0 1\n" +
  101.                 "1\n" +
  102.                 "1 2\n\n" +
  103.                 "2 0 1\n" +
  104.                 "1\n" +
  105.                 "1 2\n\n";
  106.         String shouldOutput = "Case #1: impossible\nCase #2: impossible\n";
  107.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  108.     }
  109.  
  110.     @Test
  111.     public void fourImpossibles() throws IOException {
  112.         String input = "4\n" +
  113.                 "2 0 1\n" +
  114.                 "1\n" +
  115.                 "1 2\n\n" +
  116.                 "1 0 0\n\n\n" +
  117.                 "2 0 1\n" +
  118.                 "1\n" +
  119.                 "1 2\n\n" +
  120.                 "1 0 0\n\n\n";
  121.         String shouldOutput = "Case #1: impossible\nCase #2: impossible\nCase #3: impossible\nCase #4: impossible\n";
  122.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  123.     }
  124.  
  125.     @Test
  126.     public void mixPossibleImpossible() throws IOException {
  127.         String input = "4\n" +
  128.                 "2 0 1\n" +
  129.                 "1\n" +
  130.                 "1 2\n\n" +
  131.                 "5 4 0\n" +
  132.                 "619991 362808 274506 846462\n" +
  133.                 "3 2\n" +
  134.                 "3 2\n" +
  135.                 "4 1\n" +
  136.                 "2 5\n\n" +
  137.                 "1 0 0\n\n\n" +
  138.                 "7 2 1\n" +
  139.                 "1 2 3 6 4 5\n" +
  140.                 "6 7\n" +
  141.                 "5 4\n" +
  142.                 "4 6\n\n";
  143.         String shouldOutput = "Case #1: impossible\nCase #2: 846462\nCase #3: impossible\nCase #4: 3\n";
  144.         Assert.assertEquals(shouldOutput.trim(), inputOutput(input));
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement