测试项目
博客文章地址:[http://www.cnblogs.com/jx8zjs/p/5862269.html]
工程地址:https://coding.net/u/jx8zjs/p/wordCount/git
ssh://git@git.coding.net:jx8zjs/wordCount.git
测试用例:
1.
My English is very very pool
2.地址 [http://www.gutenberg.org/files/2600/2600-0.txt]
待测单元1:统计输入文件的词频到目标文件
前四行代码为输入文件和输出文件地址,文件1是测试用例1,文件2是测试用例2.
String filename1 = "D://text/pool.txt";
String filename2 = "D://text/2600-0.txt";
String filenamedes1 = "D://pooltest.txt";
String filenamedes2 = "D://2600-0test.txt";
private static FileWordUtil fu = new FileWordUtil(); public void testPrintSortedWordGroupCountToFileBufferedStringString() {
fu.printSortedWordGroupCountToFile(filename1, filenamedes1);
fu.printSortedWordGroupCountToFile(filename2, filenamedes2);
} public void printSortedWordGroupCountToFile(String filename, String destinationFilename) {
List<String[]> result = getSortedWordGroupCount(filename);
if (result == null) {
System.out.println("no result");
return;
}
try {
FileWriter fr = new FileWriter(destinationFilename);
for (String[] sa : result) {
fr.write(sa[1] + ": " + sa[0] + "\r\n");
}
fr.close();
} catch (IOException e) {
e.printStackTrace();
return;
} }
核心词频统计代码(2016.9.26优化版):
public Map<String, Integer> getWordGroupCountBuffered(String filename) {
try {
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
StringBuffer content = new StringBuffer("");
Map<String, Integer> result = new HashMap<String, Integer>();
char[] ch = new char[128];
int bs = 0;
int idx;
boolean added = false;
boolean split = false;
total = 0;
while ((bs = br.read(ch)) > 0) {
for (idx = 0; idx < bs; idx++) { // char
if (isCharacter(ch[idx]) == 1) {
if (split == false) {
content.append(ch[idx]);
added = false;
} else {
String key = content.toString().toLowerCase();
split = false;
total++;
added = true;
content = new StringBuffer("");
content.append(ch[idx]);
if (result.containsKey(key)) {
result.put(key, result.get(key) + 1);
continue;
} else {
result.put(key, 1);
continue;
}
}
} else if (isCharacter(ch[idx]) == 2) { // digital
if (added == true) {
continue;
} else {
content.append(ch[idx]);
}
} else { // not char or digital
split = true;
continue;
}
}
}
String key = content.toString().toLowerCase();
if (result.containsKey(key)) {
result.put(key, result.get(key) + 1);
} else {
result.put(key, 1);
}
total++;
br.close();
fr.close();
return result;
} catch ( FileNotFoundException e) {
System.out.println("failed to open file:" + filename);
e.printStackTrace();
} catch (Exception e) {
System.out.println("some expection occured");
e.printStackTrace();
}
return null;
}
测试结果:
pooltest.txt
2600-0test.txt
待测单元2:统计输入文件的词频到控制台或终端
测试用例1结果:
单元测试总结:
在单元测试的时候偶然间发现了在上文提到的连接中的分词核心函数在某些情况下回遗漏文章最后一个单词,经过反复改进和思考后重写了分析读出字符的逻辑,使测试结果也能满足于预期结果,更令我意外的是算法的效率也提升了近40%(原版本在本机的执行时间平均在490-550ms,新版本运行时间在276-343ms),原因也是引入了新的boolean变量帮助优化逻辑,也减少了一些判定条件。
代码覆盖率:
测试类:
public class FileWordUtilTest { private static FileWordUtil fu = new FileWordUtil();
String filename1 = "D://text/pool.txt";
String filename2 = "D://text/2600-0.txt";
String filenamedes1 = "D://pooltest.txt";
String filenamedes2 = "D://2600-0test.txt"; @Before
public void setUp() throws Exception {
} @After
public void tearDown() throws Exception {
} @Test
public void testGetSortedWordGroupCountBufferedString() {
fu.getSortedWordGroupCountBuffered(filename1);
fu.getSortedWordGroupCountBuffered(filename2);
} @Test
public void testPrintSortedWordGroupCountToFileBufferedStringString() {
fu.printSortedWordGroupCountToFileBuffered(filename1, filenamedes1);
fu.printSortedWordGroupCountToFileBuffered(filename2, filenamedes2);
} @Test
public void testPrintSortedWordGroupCountBufferedString() {
fu.printSortedWordGroupCountBuffered(filename1);
fu.printSortedWordGroupCountBuffered(filename2);
} @Test
public void testPrintSortedWordGroupCountToFileBufferedFileArrayString() {
fu.printSortedWordGroupCountToFileBuffered(filename1, filenamedes1);
fu.printSortedWordGroupCountToFileBuffered(filename2, filenamedes2);
} }
覆盖率结果:
覆盖率分析:
测试中使用上述两个测试用例来进行的代码行覆盖统计,分别测试了getSortedWordGroupCountBuffered 89.0%,printSortedWordGroupCountToFileBuffered 88.9%,printSortedWordGroupCountBuffered 87.3%。
其中未测试到的部分就是catch块,或者旧版本api,null值检测等。所以所选的测试用例基本可以证明当前代码测试完全。