Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.14 KB | None | 0 0
  1. import fi.helsinki.cs.tmc.edutestutils.MockStdio;
  2. import fi.helsinki.cs.tmc.edutestutils.Points;
  3. import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils;
  4. import java.lang.reflect.Method;
  5. import java.util.NoSuchElementException;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. import org.junit.*;
  9. import static org.junit.Assert.*;
  10.  
  11. @Points("36.2")
  12. public class Part2Test {
  13.     @Rule
  14.     public MockStdio io = new MockStdio();
  15.  
  16.     @Test
  17.     public void test() {
  18.         int[][] syotteet = {
  19.             {1, -1, 1},
  20.             {2, 5, -1, 7},
  21.             {6, 1, 4, 7, 4, 9, -1, 31}
  22.         };
  23.  
  24.         for (int i = 0; i < syotteet.length; i++) {
  25.             tarkasta(syotteet[i], "the sum is");
  26.         }
  27.     }
  28.  
  29.     private void tarkasta(int[] syotteet, String mj) {
  30.         int oldOut = io.getSysOut().length();
  31.         io.setSysIn(stringiksi(syotteet));
  32.         callMain(LoopsEndingRemembering.class);
  33.         String out = io.getSysOut().substring(oldOut);
  34.         int odotettu = tulos(syotteet);
  35.  
  36.         String virheIlm = "With input "+stringiksiValilla(syotteet)+
  37.                 " you should print \""+mj+" "+odotettu+"\"";
  38.         assertTrue("You should print something", out.length() > 0);
  39.         assertEquals(virheIlm, odotettu, otaLukuLopusta(rivi(out, mj)));
  40.     }
  41.  
  42.     private void callMain(Class kl) {
  43.         try {
  44.             kl = ReflectionUtils.newInstanceOfClass(kl);
  45.             String[] t = null;
  46.             String x[] = new String[0];
  47.             Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass());
  48.             ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x);
  49.         } catch (NoSuchElementException e) {
  50.             fail("remember to exit the loop when user enters -1");
  51.         } catch (Throwable e) {
  52.             fail("Something unexpected happened, more info: +e");
  53.         }
  54.     }
  55.  
  56.     private static int otaLukuLopusta(String inputStr) {
  57.         String patternStr = "(?s).*?(\\d+)\\s*$";
  58.  
  59.         Matcher matcher = Pattern.compile(patternStr).matcher(inputStr);
  60.  
  61.         assertTrue("Output should be of the form \"The sum is 3\"", matcher.find());
  62.  
  63.         int luku = Integer.parseInt(matcher.group(1));
  64.         return luku;
  65.     }
  66.  
  67.     private String stringiksi(int[] taulukko) {
  68.         String tuloste = "";
  69.         for (int i = 0; i < taulukko.length-1; i++) {
  70.             tuloste += taulukko[i] + "\n";
  71.            
  72.         }        
  73.  
  74.         return tuloste;
  75.     }
  76.    
  77.     private String stringiksiValilla(int[] taulukko) {
  78.         String tuloste = "";
  79.         for (int i = 0; i < taulukko.length-1; i++) {
  80.             tuloste += taulukko[i] + " ";            
  81.         }  
  82.  
  83.         return tuloste;
  84.     }
  85.  
  86.     private int tulos(int[] syotteet) {
  87.         return syotteet[syotteet.length-1];
  88.     }
  89.  
  90.     private String rivi(String out, String mj) {
  91.         for (String rivi : out.split("\n")) {
  92.             if (rivi.toLowerCase().contains(mj.toLowerCase())) {
  93.                 return rivi;
  94.             }
  95.         }
  96.        
  97.         fail("You should output the sum of inputs in the following style \""+mj+" 10\"");
  98.         return "";    
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement