2017-08-13 45 views
0

運行程序時打印的值(調試),我謹表示我也有蟒蛇精通但前言只是稍微用java而且難以與JUNT如何通過JUNIT

當蟒蛇我調試通常會print超出我認爲可能是錯誤的值如何通過JUNIT運行腳本時如何在java中執行此操作?

更具體我有一些字符串將符號提供給map的方法和如果它們發生不止一次在mapkeyvalue更遞增。

static Map<Character, Double> getCharFrequancies(String text){ 
    Map<Character, Double> myMap = new HashMap<Character, Double>(); 
    int len = (text.length())/2; 
    for(int i = text.length() - 1; i>=0; --i) { 
     char symbol = text.charAt(i); 
     if(myMap.containsKey(symbol)){ 
      myMap.put(symbol, myMap.get(symbol)+(1/len)); 
     } 
     else { 
      myMap.put(symbol, (double) 1); 
     } 

    } 

    return myMap; 
} 

而且測試腳本:

public void testGetCharFrequancies() { 
    Map<Character,Double> expectedMap = new HashMap<Character,Double>(); 
    String text = "aa, b ccc."; 
    expectedMap.put('a', 2/10.0); 
    expectedMap.put(' ', 2/10.0); 
    expectedMap.put(',', 1/10.0); 
    expectedMap.put('b', 1/10.0); 
    expectedMap.put('c', 3/10.0); 
    expectedMap.put('.', 1/10.0); 

    Map<Character,Double> actualMap = HuffmanTree.getCharFrequancies(text); 
    assertEquals(expectedMap.size(), actualMap.size()); 
    assertEquals(expectedMap.keySet(), actualMap.keySet()); 
    for(Character c:expectedMap.keySet()){ 
     assertEquals(expectedMap.get(c), actualMap.get(c), 0.000000000001); 

的失敗是在assertEquals(expectedMap.get(c), actualMap.get(c), 0.000000000001);所以我想打印出mapvalues發生。我該怎麼做?

ps。我正在使用eclipse氧氣

+2

我不會添加一個print語句 - 通過旋轉Eclipse中的調試器會更快地告訴你。我猜測你對兩個雙重值的比較容忍度太小。 1E-12?太小。嘗試1e-3並逐漸變小,直到失敗。或者將實際的字數統計爲整數並忘記頻率。還有一件事:它是拼寫「頻率」。 – duffymo

+0

那麼,爲什麼這個標記的python? –

+0

@duffymo什麼是'容忍比較'?感謝您發現拼寫,閱讀困難是一個婊子 – Arkan

回答

1

我建議您按照下面的方法getCharFrequencies所示編寫它。這將是很好的熟悉JDK中的新lambda表達式8

package utils; 

import java.util.Arrays; 
import java.util.List; 
import java.util.Map; 
import java.util.TreeMap; 
import java.util.stream.Collectors; 

/** 
* @author Michael 
* @link https://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856 
*/ 
public class StringUtils { 

    private StringUtils() {} 

    public static List<String> tokenize(String str) { 
     String [] tokens = new String[0]; 
     if (isNotBlankOrNull(str)) { 
      str = str.trim(); 
      tokens = str.split("\\s+"); 
     } 
     return Arrays.asList(tokens); 
    } 

    public static boolean isBlankOrNull(String s) { 
     return ((s == null) || (s.trim().length() == 0)); 
    } 

    public static boolean isNotBlankOrNull(String s) { 
     return !isBlankOrNull(s); 
    } 

    public static boolean hasSufficientTokens(int numTokens, String str) { 
     return (numTokens >= 0) && tokenize(str).size() >= numTokens; 
    } 

    public static Map<String, Long> getCharFrequencies(String text) { 
     Map<String, Long> charFrequencies = new TreeMap<>(); 
     if (isNotBlankOrNull(text)) { 
      // https://stackoverflow.com/questions/4363665/hashmap-implementation-to-count-the-occurences-of-each-character 
      charFrequencies = Arrays.stream(text.split("")).collect(Collectors.groupingBy(c -> c, Collectors.counting())); 
     } 
     return charFrequencies; 
    } 
} 

這裏的JUnit測試,證​​明它的工作原理:

package utils; 

import org.junit.Assert; 
import org.junit.Test; 

import java.util.Arrays; 
import java.util.Collections; 
import java.util.List; 
import java.util.Map; 
import java.util.TreeMap; 

/** 
* Created by Michael 
* Creation date 12/6/2016. 
* @link https://stackoverflow.com/questions/41006856/how-do-i-catch-a-nosuchelementexception?noredirect=1#comment69222264_41006856 
*/ 
public class StringUtilsTest { 

    @Test 
    public void testIsNotBlankOrNull_NullString() { 
     Assert.assertFalse(StringUtils.isNotBlankOrNull(null)); 
    } 

    @Test 
    public void testIsNotBlankOrNull_EmptyString() { 
     Assert.assertFalse(StringUtils.isNotBlankOrNull("")); 
    } 

    @Test 
    public void testIsNotBlankOrNull_BlankString() { 
     Assert.assertFalse(StringUtils.isNotBlankOrNull("  ")); 
    } 

    @Test 
    public void testIsNotBlankOrNull_FullString() { 
     Assert.assertTrue(StringUtils.isNotBlankOrNull("I'm not null, blank, or empty")); 
    } 

    @Test 
    public void testTokenize_NullString() { 
     // setup 
     List<String> expected = Collections.EMPTY_LIST; 
     // exercise 
     List<String> actual = StringUtils.tokenize(null); 
     // assert 
     Assert.assertEquals(expected, actual); 
    } 

    @Test 
    public void testTokenize_EmptyString() { 
     // setup 
     List<String> expected = Collections.EMPTY_LIST; 
     // exercise 
     List<String> actual = StringUtils.tokenize(""); 
     // assert 
     Assert.assertEquals(expected, actual); 
    } 

    @Test 
    public void testTokenize_BlankString() { 
     // setup 
     List<String> expected = Collections.EMPTY_LIST; 
     // exercise 
     List<String> actual = StringUtils.tokenize("  "); 
     // assert 
     Assert.assertEquals(expected, actual); 
    } 

    @Test 
    public void testTokenize_FullString() { 
     // setup 
     List<String> expected = Arrays.asList("I'm", "not", "null,", "blank,", "or", "empty"); 
     // exercise 
     List<String> actual = StringUtils.tokenize(" I'm not  null, blank, or empty "); 
     // assert 
     Assert.assertEquals(expected.size(), actual.size()); 
     Assert.assertEquals(expected, actual); 
    } 

    @Test 
    public void hasSufficientTokens_NegativeTokens() { 
     // setup 
     int numTokens = -1; 
     String str = " I'm not  null, blank, or empty "; 
     // exercise 
     // assert 
     Assert.assertFalse(StringUtils.hasSufficientTokens(numTokens, str)); 
    } 

    @Test 
    public void hasSufficientTokens_InsufficientTokens() { 
     // setup 
     String str = " I'm not  null, blank, or empty "; 
     int numTokens = StringUtils.tokenize(str).size() + 1; 
     // exercise 
     // assert 
     Assert.assertFalse(StringUtils.hasSufficientTokens(numTokens, str)); 
    } 

    @Test 
    public void hasSufficientTokens_NullString() { 
     // setup 
     String str = ""; 
     int numTokens = StringUtils.tokenize(str).size(); 
     // exercise 
     // assert 
     Assert.assertTrue(StringUtils.hasSufficientTokens(numTokens, str)); 
    } 

    @Test 
    public void hasSufficientTokens_Success() { 
     // setup 
     String str = " I'm not  null, blank, or empty "; 
     int numTokens = StringUtils.tokenize(str).size(); 
     // exercise 
     // assert 
     Assert.assertTrue(StringUtils.hasSufficientTokens(numTokens, str)); 
    } 

    @Test 
    public void testGetCharFrequencies_NullText() { 
     // setup 
     String text = null; 
     Map<String, Long> expected = new TreeMap<>(); 
     // exercise 
     Map<String, Long> actual = StringUtils.getCharFrequencies(text); 
     // assert 
     Assert.assertEquals(expected, actual); 
    } 

    @Test 
    public void testGetCharFrequencies_BlankText() { 
     // setup 
     String text = "  "; 
     Map<String, Long> expected = new TreeMap<>(); 
     // exercise 
     Map<String, Long> actual = StringUtils.getCharFrequencies(text); 
     // assert 
     Assert.assertEquals(expected, actual); 
    } 

    @Test 
    public void testGetCharFrequencies_Success() { 
     // setup 
     String text = "The quick brown fox jumped over the lazy dog!  "; 
     String expectedString = "{T=1, =16, !=1, a=1, b=1, c=1, d=2, e=4, f=1, g=1, h=2, i=1, j=1, k=1, l=1, m=1, n=1, o=4, p=1, q=1, r=2, t=1, u=2, v=1, w=1, x=1, y=1, z=1}"; 
     // exercise 
     Map<String, Long> actual = StringUtils.getCharFrequencies(text); 
     // assert 
     Assert.assertEquals(expectedString, actual.toString()); 
    } 
}