Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.98 KB | None | 0 0
  1. package org.hyperskill.hstest.search_engine;
  2.  
  3. import org.hyperskill.hstest.stage.MainMethodTest;
  4. import org.hyperskill.hstest.testcase.CheckResult;
  5. import org.hyperskill.hstest.testcase.TestCase;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.Arrays;
  9. import java.util.LinkedList;
  10. import java.util.List;
  11.  
  12. class TestClue {
  13.     int referencesCount, requestsCount;
  14.     String input;
  15.  
  16.     TestClue(int referencesCount, int requestsCount, String input) {
  17.         this.referencesCount = referencesCount;
  18.         this.requestsCount = requestsCount;
  19.         this.input = input;
  20.     }
  21. }
  22.  
  23. public class SearchEngineStage2Test extends MainMethodTest<TestClue> {
  24.  
  25.     private static String testCaseInput1 = "1\n" +
  26.             "John\n" +
  27.             "1\n" +
  28.             "this text will definitely be no match";
  29.     private static String testCaseInput2 = "3\n"
  30.             + "john smith smith@gmail.com\n"
  31.             + "John lock john_lock@somecompany.com\n"
  32.             + "help me iamaprogrammer@gmail.com\n"
  33.             + "4\n"
  34.             + "john\n"
  35.             + "Somecompany  \n"
  36.             + "@\n"
  37.             + "this text will definitely be no match";
  38.     private static String testCaseInput3 = "3\n"
  39.             + "john smith smith@gmail.com\n"
  40.             + "John lock john_lock@somecompany.com\n"
  41.             + "help me iamaprogrammer@gmail.com\n"
  42.             + "4\n"
  43.             + "john\n"
  44.             + "Somecompany  \n"
  45.             + "@\n"
  46.             + "this text will definitely be no match";
  47.  
  48.     public SearchEngineStage2Test() throws Exception {
  49.         super(SearchEngineStage2.class);
  50.     }
  51.  
  52.     @Override
  53.     public List<TestCase<TestClue>> generateTestCases() {
  54.         return Arrays.asList(
  55.                 new TestCase<>(new TestClue(1, 1,
  56.                         testCaseInput1), testCaseInput1),
  57.                 new TestCase<>(new TestClue(3, 4,
  58.                         testCaseInput2), testCaseInput2),
  59.                 new TestCase<>(new TestClue(3, 4,
  60.                         testCaseInput3), testCaseInput3)
  61.         );
  62.     }
  63.  
  64.     @Override
  65.     public CheckResult check(String reply, TestClue clue) {
  66.         String cR = "\n";
  67.         List<String> outputLines = new LinkedList<String>(Arrays.asList(reply.split(cR)));
  68.         String[] inputLines = clue.input.split(cR);
  69.         String[] reference;
  70.         String[] searchResult;
  71.  
  72.         int referenceCount, requestsCount;
  73.  
  74.         //check count of iteration to fill search reference
  75.         try {
  76.             referenceCount = Integer.parseInt(inputLines[0]);
  77.         } catch (NumberFormatException e) {
  78.             return new CheckResult(false, "The number of lines to search must be a number!");
  79.         }
  80.  
  81.         reference = new String[referenceCount];
  82.  
  83.         for (int i = 0; i < referenceCount; i++) {
  84.             reference[i] = inputLines[i + 1];
  85.         }
  86.  
  87.         //check count of iteration to search some string;
  88.         try {
  89.             requestsCount = Integer.parseInt(inputLines[referenceCount + 1]);
  90.         } catch (NumberFormatException e) {
  91.             return new CheckResult(false, "The number of requests to search must be a number or " +
  92.                     "count of reference lines doesn't match input data!");
  93.         }
  94.  
  95.         //clear the list of unnecessary lines, if any
  96.         List<String> cleanedOutput = new ArrayList<String>();
  97.         for (int i = 0; i < outputLines.size(); i++) {
  98.             if (ContainsItemFromList(outputLines.get(i), reference)) {
  99.                 cleanedOutput.add(outputLines.get(i));
  100.             }
  101.         }
  102.  
  103.         //check found matches accuracy
  104.         int actuallTotalMatches = cleanedOutput.size();
  105.         int requiredTotalMatches = 0;
  106.         for (int j = 0; j < requestsCount; j++) {
  107.             String toSearch = inputLines[referenceCount + 2 + j];
  108.             searchResult = Arrays.stream(reference).filter(line -> line.toLowerCase()
  109.                     .contains(toSearch.toLowerCase().trim()))
  110.                     .toArray(String[]::new);
  111.  
  112.             requiredTotalMatches += searchResult.length;
  113.  
  114.             List<String> searchedFromOutput = cleanedOutput
  115.                     .subList(0, searchResult.length);
  116.  
  117.             if (!Arrays.equals(searchedFromOutput.toArray(), searchResult)) {
  118.                 return new CheckResult(false, "Search result is not equal to the expected in search iteration " + j);
  119.             }
  120.  
  121.             cleanedOutput.subList(0, searchResult.length).clear();
  122.         }
  123.         if (actuallTotalMatches != requiredTotalMatches) {
  124.             return new CheckResult(false, "wrong number of found matches!");
  125.         }
  126.  
  127.         if(referenceCount != clue.referencesCount){
  128.             return new CheckResult(false, "Reference count is incorrect");
  129.         }
  130.         else if(requestsCount != clue.requestsCount){
  131.             return new CheckResult(false, "Search requests count is incorrect");
  132.         }
  133.         else {
  134.             return CheckResult.TRUE;
  135.         }
  136.     }
  137.  
  138.     public static boolean ContainsItemFromList(String inputStr, String[] items) {
  139.         return Arrays.stream(items).parallel().anyMatch(inputStr::contains);
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement