Содержит метод и счетчик обновлений - PullRequest
0 голосов
/ 07 апреля 2019

Итак, я заканчиваю свою программу, где тест проходит список слов в тестовой программе и, используя префиксы, я проверяю его и возвращаю только те из них, которые соответствуют.

Вот следующий код для моего метода содержимого и фрагментов метода тестирования. Мне просто интересно, что я делаю не так?

// содержит метод

public boolean containsKey(TrieMapNode current, String curKey) {
    // recursively get the value for the current node
    String value = get(current,curKey);
    // if value if null or empty, key is not found return false
    if(value == null) {
        return false;
    }else if (value.equals("")) {
        return false;
    } else {
        return true;
    }
}

// тестовая программа

    import java.util.HashSet;
    import java.util.ArrayList;

public class TriMapTester{
    public static void main(String[] args){
    HashSet<String> posWords = getPositiveWords();
    HashSet<String> negWords = getNegativeWords();
    TrieMapInterface map = new TrieMap();

    //Add all of the positive words
    for(String word: posWords){
      map.put(word, word);
    }

    int countErrors = 0;
    int totalErrors = 0;

    //Check that the map contains all positive words
    for(String s: posWords){
      if(!map.containsKey(s)){
        countErrors++;
      }
    }
    System.out.println("Number of missed keys after searching all positive words: " + countErrors);

    totalErrors += countErrors;
    countErrors = 0;

    //Check that the correct value is associated with each key
    for(String s: posWords){
      if(!map.containsKey(s) || !map.get(s).equals(s)){
        countErrors++;
      }
    }
    System.out.println("Number of incorrect values for keys: " + countErrors);

    totalErrors += countErrors;
    countErrors = 0;

    //Check negative words. Make sure they do not show up in map if they are not also in positive words.
    for(String s: negWords){
      if(map.containsKey(s) && !posWords.contains(s)){
        countErrors++;
      }
    }
    System.out.println("Number of incorrectly found keys after searching all negative words: " + countErrors);
    totalErrors += countErrors;

    //Check the values returned for a number of prefix values
    //Compares the returned result to the true result
    String[] searchPrefixes = {"add", "bril", "cat", "cri", "derri", "mar", "tra", "lor", "marveled", "rit"};
    for(String prefix: searchPrefixes){
      System.out.println("Getting values for prefix: " + prefix);
      ArrayList<String> realResult = matchPrefix(prefix, posWords);
      ArrayList<String> yourResult = map.getValuesForPrefix(prefix);
      System.out.println("Result should contain: " + realResult);
      System.out.println("    Your map returned: " + yourResult);
      countErrors = countDifference(realResult, yourResult);
      totalErrors += countErrors;
      System.out.println("Error Count: " + countErrors);
    }
    System.out.println("Total Errors: " + totalErrors);
  }

  public static int countDifference(ArrayList<String> realResult, ArrayList<String> yourResult){
    int result = 0;
    for(String s: realResult){
      if(!yourResult.contains(s)){
        result++;
      }
    }

    for(Object s: yourResult){
      if(!realResult.contains((String)s)){
        result++;
      }
    }
    return result;
  }

  public static ArrayList<String> matchPrefix(String prefix, HashSet<String> words){
   ArrayList<String> result = new ArrayList<String>();

    for(String s: words){
      if(s.startsWith(prefix)){
        result.add(s);
      }
    }
   return result;
  }

  public static HashSet<String> getPositiveWords(){
HashSet<String> result = new HashSet<String>();
String[] pos = new String[]{"abound", "abounds", "abundance", "abundant", "accessable", "accessible", "acclaim", "acclaimed", "acclamation"}; 
for(String s: pos){
  result.add(s);
}
return result;
 }

  public static HashSet<String> getNegativeWords(){
HashSet<String> result = new HashSet<String>();
String[] neg = new String[]{"abnormal", "abolish", "abominable", "abominably", "abominate", "abomination", "abort", "aborted", "aborts", "abrade", "abrasive", "abrupt", "abruptly", "abscond", "absence", "absent-minded"}; 
for(String s: neg){
  result.add(s);
}
return result;
  }

Теперь вывод, полученный при запуске теста, является правильным, содержащим слова, однако числоколичество ошибок по-прежнему равно 0, несмотря на метод содержимого, поэтому я не уверен, что делаю неправильно.Любая помощь будет принята с благодарностью

1 Ответ

0 голосов
/ 07 апреля 2019

Проблема с этой строкой

if(map.containsKey(s) && !posWords.contains(s))
   countErrors++;

map & posWords содержит те же данные, и вы однажды проверяете их на истинность, а также проверяете на ложность, что никогда не выполнит countErrors++. Вот почему countErrors равно 0.

...