软件质量与测试博客
一、Github地址
https://github.com/kangzhishi/wcTest
二、PSP表格
三、解题思路
分函数实现功能,并实现通过正则表达式来判断词,行,特殊行。
由空格或逗号分割开的都视为单词,且不做单词的有效性校验,例如:thi#,that视为用逗号隔开的2个单词。
自制arg解析,方便命令行指令功能需求。
查阅资料:
http://blog.csdn.net/soszou/article/details/7979060
四、程序设计实现过程
-
解析参数
设计ArgsParser类
-
针对错误设计函数
error
-
针对文件读写设计函数
getFileContents,fileToWord,findfiles
-
针对复杂功能设计函数
countWord,countLine,sumChar
-
整合功能以及输出
countFile
- 五、代码说明
-
public class WordCount {
public static void main(String[] args) throws FileNotFoundException{
String[] args ={"-s","WordCount.java"}
String[] param=args;
int length=param.length;
FileInfo fileInfo=null;
PrintWriter pw=new PrintWriter(new BufferedOutputStream(new FileOutputStream("result.txt",true)));
if(length==2){
Command command=analysisCommand(args[0]);
fileInfo=new FileInfo(Paths.get(args[1]));
execute(command, fileInfo, pw);
pw.flush();
}
else if(length>2){
Command[] commands=new Command[length];
for(int i=0;i<length;i++)
commands[i]=analysisCommand(args[i]);
int index;
if((index=indexOf(Command.OUTPUT, commands))!=-1){
pw=new PrintWriter(new BufferedOutputStream(new FileOutputStream(args[index+1],true)));
commands[index]=null;
}
String name=param[selectFirstNull(commands)];
if((index=indexOf(Command.SALL, commands))==-1){
fileInfo=new FileInfo(Paths.get(name));
for(Command command:commands)
if(command!=null)
execute(command, fileInfo, pw);
}else{
commands[index]=null;
String[] list=new File("").getAbsoluteFile().list(new DirFilter(name));
for(String item:list){
FileInfo fileInfo2=new FileInfo(Paths.get(item));
for(Command command:commands)
if(command!=null)
execute(command, fileInfo2, pw);
}
}
pw.flush();
}
pw.close();
}
private static int selectFirstNull(Command[] commands){
for(int i=1;i<commands.length;i++)
if(commands[i]==null&&commands[i-1]!=null)
return i;
return -1;
}
private static int indexOf(Command command,Command[] commands){
for(int i=0;i<commands.length;i++)
if(command.equals(commands[i]))
return i;
return -1;
}
private static void execute(Command command,FileInfo fileInfo,PrintWriter pw){
switch (command) {
case CHAR:pw.write(fileInfo.path()+", 字符数: "+fileInfo.charNum()+"\r\n");break;
case WORD:pw.write(fileInfo.path()+", 单词数: "+fileInfo.wordNum()+"\r\n");break;
case LINE:pw.write(fileInfo.path()+", 行 数: "+fileInfo.lineNum()+"\r\n");break;
case ALL:fileInfo.toFile(pw);break;
}
}
private static Command analysisCommand(String command){
String string =command.replaceAll("\\s", "");
if(command.equals("-c"))
return Command.CHAR;
else if(command.equals("-w"))
return Command.WORD;
else if(command.equals("-s"))
return Command.SALL;
else if(command.equals("-a"))
return Command.ALL;
else if(command.equals("-e"))
return Command.EXCEPT;
else if(command.equals("-l"))
return Command.LINE;
else if(command.equals("-o"))
return Command.OUTPUT;
else return null;
}
}
class FileInfo{
private int charNum;
private int wordNum;
private int lineSum;
private int spaceSum;
private int codeSum;
private int noteSum;
private Path path;
private boolean isAnalysis=false;
public FileInfo(Path path){
this.path=path;
charNum=0;
wordNum=0;
lineSum=0;
spaceSum=0;
codeSum=0;
noteSum=0;
analysis();
}
public String path(){
return path.toString();
}
public int charNum(){
return charNum;
}
public int wordNum(){
return wordNum;
}
public int lineNum(){
return lineSum;
}
private void analysis(){
if(!isAnalysis){
isAnalysis=true;
String s;
BufferedReader in = null;
try {
in =new BufferedReader(new FileReader(path.toString()));
while((s=in.readLine())!=null){
switch(analysisLine(s)){
case SPACE:spaceSum++;break;
case CODE:codeSum++;break;
case NOTE:noteSum++;break;
}
charNum+=s.replaceAll("\\s", "").length();
wordNum+=s.split("\\W+").length;
//sb.append(s);
lineSum++;
}
} catch (IOException e) {
System.out.println("文件不存在"+path());
}finally{
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void toFile(PrintWriter pw){
pw.write(path.toString()+", 字符数: "+charNum+"\r\n"
+path.toString()+", 单词数: "+wordNum+"\r\n"
+path.toString()+", 行数: "+lineSum+"\r\n"
+path.toString()+", 代码行/空行/注释行: "+codeSum+"/"+spaceSum+"/"+noteSum+"\r\n");
}
public String toString(){
return path.toString()+"charNum="+charNum+" wordNum="+wordNum+" lineSum="+lineSum+" codeSum="+codeSum+" noteSum="+noteSum+" spaceSum="+spaceSum;
}
private Type analysisLine(String line){
return (line.equals("")||Pattern.matches("\\s+", line))?Type.SPACE:(Pattern.matches("//.*", line)?Type.NOTE:Type.CODE);
}
}enum Command{
CHAR,WORD,LINE,ALL,OUTPUT,SALL,EXCEPT;
}enum Type{
SPACE,CODE,NOTE
}class DirFilter implements FilenameFilter{
private Pattern pattern;
public DirFilter(String regex){
pattern = Pattern.compile(regex);
}
public boolean accept(File dir,String name){
return pattern.matcher(name).matches();
}
} - 六、测试设计过程
-
测试过程对于每一项功能单独测试,再进行组合测试,确保覆盖了所有可执行的代码,对文件名输入文件和文件夹也作出了测试,总共设计13个测试用例:
wc.exe wc.exe -a wc.exe -a ../test/test.c wc.exe -l ../test/test.c -o out1.txt wc.exe -w ../test/test.c -o out2.txt wc.exe -a ../test/test.c -o out3.txt wc.exe -c ../test/test.c -o out4.txt wc.exe -l -w -a -c ../test/test.c -o out5.txt wc.exe -a ../test/ -o out6.txt wc.exe -a -s ../test/ -o out7.txt wc.exe -a ../test/*.c -o out8.txt wc.exe -a ../test/*.c -e ../stop.txt -o out9.txt wc.exe -l -w -a -c -s ../test/ -e ../stop.txt -o out10.txt
测试结果见项目下bin
out10.txt out3.txt out5.txt out7.txt out9.txt out1.txt out2.txt out4.txt out6.txt out8.txt result.txt
软件测试工作是一个系统而复杂的工程,软件测试的目的就是确保软件的质量、确认软件以正确的方式做了你所期望的事情,所以工作的主要任务是发现软件的错误、有效定义和实现软件成分由底层到高层的组装过程、验证软件是否满足规格书要求和系统定义文档所规定的技术要求、为软件质量模型的建立提供依据。
而且软件的测试不仅是要确保软件的质量,还要给开发人员提供信息,以方便其为风险评估做相应的准备,以及为其提供分析依据,重要的是要贯穿在整个软件开发的过程中,保证整个 软件开发的过程是高质量的。
软件测试对测试工程师来讲,要求具备较强的专业知识,严谨细心耐心的测试态度,良好的反向思维、发散思维能力、沟通能力等等。七.借鉴了梁锦霖同学的代码,并做了一些改动。
posted on 2018-03-19 12:54 kangzhishi 阅读(232) 评论(3) 编辑 收藏 举报