wc的改进
学号:201531040455
项目代码
1 要求
- 制定编码规范
- 代码自审并修正
- 代码互审
- 合并代码
- 为已完成的实施单元测试
- 实现扩展功能
- 静态代码审检查
- 性能没能和优化
2 制定要求
单独写了一个md文件,放在git上,快速查阅
3 代码自审并修正
修正代码风格(变量定义、注释等)
算法错误并没有找到算误,至于效率,我就不是很在意了
4 代码互审、合并
由于我没有找到队友(我已经是一个在准备考研的大四老学长),所以我没抽出时间去做这件事
5 为已完成的实施单元测试
我在上一个文档中已经完成这个部分,所采用的是写一个shell文件自动测试并对比结果(linux自带一个wc命令),得出程序是否正确。有关其详细论述可以参照我的上一个readme,这里只把shell文件内容放出来
#!/bin/sh
#Author: seven
#date: 2018/09/27
#aim: an automatic script to test the program
#a function to print test result
#function will receive 2 parameters,
#first is the type of the test,
#second is whether the test tested as expected
test_result_print(){
if [ $2 -eq 1 ]
then
echo `date "+%Y-%m-%d %H:%M:%S"` " $1 test succeeded!" | tee -a test_log.txt
else
echo `date "+%Y-%m-%d %H:%M:%S"` " $1 test met with failure!" | tee -a test_log.txt
fi
}
compare_result_with_standard(){
if [ "$1" = "$2" ]
then
if_test_success=1
else
if_test_success=0
fi
}
test_file_name="input"
#Here is the test for ./wc -h
#the ideal result is the usage of the program
standard_result="Usage: wc.exe [OPTIONS]... [FILE]
--character -c calculate the numbers of characters in the file
--word -w calculate the numbers of words in the file
--line -l calculate the numbers of lines in the file
--outtofile=x -o x transfer the result to the specific file"
result=`./wc -h`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -h" $if_test_success
#Here is the test for ./wc -h input
#the ideal result is to print "-h parameter should work alone!"
standard_result="-h parameter should work alone!"
result=`./wc -h input`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -h print" $if_test_success
#Here is the test for wc -w input
standard_result=`wc -w $test_file_name | sed 's/[^0-9]//g'`
result=`./wc -w $test_file_name | sed 's/[^0-9]//g'`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -w input" $if_test_success
#Here is the test for wc -w nothing
standard_result="Fail to Open the File!"
result=`./wc -w nothing`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -w nothing" $if_test_success
#Here is the test for wc -l nothing
standard_result="Fail to Open the File!"
result=`./wc -l nothing`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -l nothing" $if_test_success
#Here is the test for wc -c input
standard_result=`wc -c $test_file_name | sed 's/[^0-9]//g'`
result=`./wc -c $test_file_name | sed 's/[^0-9]//g'`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -c input" $if_test_success
#Here is the test for wc -c nothing
standard_result="Fail to Open the File!"
result=`./wc -c nothing`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -c nothing" $if_test_success
#Here is the test for wc -w -o result.log input
standard_result=`wc -w $test_file_name | sed 's/[^0-9]//g'`
result=`./wc -w -o result.log $test_file_name | sed 's/[^0-9]//g'`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -w -o result.log input in std output" $if_test_success
result=`cat result.log | sed 's/[^0-9]//g'`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -w -o result.log input in outputfile" $if_test_success
6 实现扩展功能
先看看扩展功能要求:
wc.exe -s //递归处理目录下符合条件的文件
wc.exe -a file.c //返回更复杂的数据(代码行 / 空行 / 注释行)
wc.exe -e stopList.txt // 停用词表,统计文件单词总数时,不统计该表中的单词
[file_name]: 文件或目录名,可以处理一般通配符。
6.0 TDD思想
由于要求使用TDD,所以先写测试用例:
#Here is the test for ./wc -s -w input*
standard_result="input, word number:23\ninput1, word number:4"
result=`./wc -s -w input*`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -c input" $if_test_success
#Here is the test for ./wc -e stop_list.txt input*
standard_result="input, word number:21"
result=`./wc -e stop_list.txt input`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -c input" $if_test_success
#Here is the test for ./wc -a input
standard_result="the lines of input is 12\nthe empty of input is 2\nthe explanatory lines of input is 3"
result=`./wc -a input`
compare_result_with_standard "$result" "$standard_result"
test_result_print "wc -c input" $if_test_success
6.1 关键代码
if(Char == '/'){
if(text[r+1]=='/'){
//判断是否有两个连载一起的"//"
exSign = TRUE;
}
else if(text[r+1]=='*'){
//如果遇到/*则进入注释状态
exBegin = TURE;
r++;
}
}
else if(Char == '*'){
if(text[r+1]=='/'&&exBegin){
exBegin = FALSE;
exEnd = TRUE;
r++;
}
}
else if(Char=='\n'||Char =='\0'){
if(exBegin&&(code==0||code == 1)){
exLine++;
code = 0;
}
else if(exBegin){
codeLine++;
code = 0;
}
else if(exEnd){
codeLine++;
code = 0;
exEnd = false;
}
else if((code==1&&exSign)||(code==0&&exSign)){
//如果该行只有一个或者没有字符并且在"/*"的作用范围内
exLine++;
code = 0;
exSign = FALSE;
}
else if(code==0||code==1){
empLine++;
exSign = FALSE;
code = 0;
}
else{
codeLine++;
exSign = FALSE;
code = 0;
}
lineCount++;
}
6.4 测试
下面是脚 本运行情况
seven@mylab:~/wordcount/bin/linux_amd64$ ./test.sh
2018-10-21 20:06:47 wc -h test succeeded!
2018-10-21 20:06:47 wc -h print test succeeded!
2018-10-21 20:06:47 wc -w input test succeeded!
2018-10-21 20:06:47 wc -w nothing test succeeded!
2018-10-21 20:06:47 wc -l nothing test succeeded!
2018-10-21 20:06:47 wc -c input test succeeded!
2018-10-21 20:06:47 wc -c nothing test succeeded!
2018-10-21 20:06:47 wc -w -o result.log input in std output test succeeded!
2018-10-21 20:06:47 wc -w -o result.log input in outputfile test succeeded!
2018-10-21 20:06:47 wc -s -w input test succeeded!
2018-10-21 20:06:47 wc -e stop_list.txt input test succeeded!
2018-10-21 20:06:47 wc -a input test succeeded!
最后三排说明我写的程序已经通过了!
7 静态代码审检查
用到了一个c 语言的检验工具splint
其运行结果如下
Running from dir:
command:
C:\Users\seven\splint.exe -standard -preproc -warnposix -retvalint -compdef -usedef -exportlocal -unrecog -imptype \
-bufferoverflowhigh -predboolothers -varuse -type -mayaliasunique -boolops -branchstate -nullpass -nullderef -retvalother \
-nullret -formattype -onlytrans -mustfreefresh -globs -isoreserved -namechecks -isolib -exportfcn -paramuse -exportheader \
-declundef -temptrans -nullassign -formatconst -readonlytrans -noeffect -globstate -compdestroy \
============= stdout: =============
============ end stdout =============
============= stderr: =============
================================
Checking finished
这个意思好像是我的程序一点错都没报,那就是说我写得很完美~
8 性能没能和优化
8.1 测试数据集思路:
选用所有单元测试中的等价类和无效等价类进行进行测试,然后将各模块进行联合测试。
8.2优化前后性能指标:
8.2.1 优化前
系统是否满足预期的性能需求。
8.2.2 优化后
判断系统是否满足预期的性能需求;寻找软件系统可能存在的性能问题,定位性能瓶颈并解决问题; 判定软件系统的性能表现,预见系统负载压力承受力,在应用部署之前,评估系统性能。(是否满足上线性能要求?系统极限承载如何?系统稳定性如何?)
8.2.3 优化的设计思路:
减少了库的引用,定位到某个要用的模块,而不是整个引用,优化判断分支,减少了判断次数,提高效率,同时减小重复性。
9 总结
本次写得比较匆忙,没有上一个文档那么认真,主要原因是因为我在准备考研,时间确实紧张,但是我还是改尽我所能多做一些,因为多做课程设计对自己是有帮助的,我也乐意去做一个老师认真布置的作业。