软件测试第三次作业
合作者:201531060204, 201531060216
代码地址:https://gitee.com/dzl-git/wordcount_extension
作业链接地址:https://edu.cnblogs.com/campus/xnsy/2018softwaretest2398/homework/2187
一 、项目介绍:
本次项目是在上次作业WorldCount的基础上,利用结对编程的思想,完成对WorldCount项目的功能扩展
二 、互审代码情况:
由于这次的项目是在第二次作业WorldCount的基础上进行扩展,而且是两个人共同完成,所以还是遇到了很多问题。
主要问题:
1.代码命名不规范:
2.代码不够整洁;
3.注释不够详细;
解决方案:
1.通过查阅阿里巴巴公司 java 规范制定了统一的命名规则,
2.将代码规范化
3.加入更加详细的注释
三、单元测试:
测试代码:
//测试返回字符总数 @Test public void testCountCharacter() { File f = new File("D:\\Workspaces\\eclipse-workspace\\wordCount\\src\\test.txt"); if (!f.exists()) System.out.println("文件不存在"); int result=0; BufferedReader reader=null; try{ reader = new BufferedReader(new FileReader(f)); String str=null; while((str=reader.readLine())!=null) result+=str.length(); //字符总数就是读到每一行的字符串的长度 } catch (FileNotFoundException e0){ e0.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try{ reader.close(); } catch(IOException e){ e.printStackTrace(); } } System.out.println("字符总数为:"+result); } //测试返回单词总数 @Test public void testCountWordFile() { File f = new File("D:\\Workspaces\\eclipse-workspace\\wordCount\\src\\test.txt"); if(!f.exists()) System.out.println("文件不存在"); int result=0; BufferedReader reader=null; try{ reader=new BufferedReader(new FileReader(f)); int temp,flag=0; while((temp=reader.read())!=-1){ if((char)temp!='\n'&&(char)temp!='\t'&&(char)temp!=','&&(char)temp!=' '&&(char)temp!='\r'){ flag=1; } else if(flag==1&&((char)temp=='\n'||(char)temp=='\t'||(char)temp==','||(char)temp==' '||(char)temp=='\r')){ flag=0; result++; } } result++; } catch (FileNotFoundException e0){ e0.printStackTrace(); }catch (IOException e) { e.printStackTrace(); } finally{ try{ reader.close(); } catch(IOException e){ e.printStackTrace(); } } System.out.println("单词总数为:"+result); } //测试返回总行数 @Test public void testCountLine() { File f = new File("D:\\Workspaces\\eclipse-workspace\\wordCount\\src\\test.txt"); if (!f.exists()) System.out.println("文件不存在"); int result=0; BufferedReader reader=null; try{ reader = new BufferedReader(new FileReader(f)); while(reader.readLine()!=null) result++; } catch (FileNotFoundException e0){ e0.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try{ reader.close(); } catch(IOException e){ e.printStackTrace(); } } System.out.println("总行数为:"+result); } //测试返回代码行,注释行,空行 @Test public void testCountComplex() { File f = new File("D:\\Workspaces\\eclipse-workspace\\wordCount\\src\\test.txt"); int[] arr=new int[3]; int emptyline=0,codeline=0,noteline=0; BufferedReader reader = null; try{ reader = new BufferedReader(new FileReader(f)); String str=null; int flag=0; while((str=reader.readLine())!=null){ str=str.trim(); //去掉两边的空格,这样可以直接通过判断str长度判断是不是空行 if(str.length()==0||str.equals("{")) emptyline++; else if(str.startsWith("//")||str.startsWith("}//")) //如果该行是//开头,那么是注释行,排除了先是代码然后是//的情况。 noteline++; else if(str.contains("/*")){ //如果代码行中存在/* 如果是开头出现,判断是不是在该行结束,如果在中间出现,该行是代码行 if(str.startsWith("/*")||str.startsWith("}/*")){ if(str.contains("*/")){ //如果在本行中存在*/,判断是不是结尾 if(str.endsWith("*/")) noteline++; else codeline++; } else{ //本行不存在*/就把flag置为1,然后找下一行,直到一行包含*/ flag=1; noteline++; } } else{ //不是以/*开头但是包含/*,判断是不是该行结束 if(str.contains("*/")){ //如果本行结束,那么就是代码行 codeline++; } else{ //如果本行没结束,flag置1,本行是代码行 codeline++; flag=1; } } } else if(flag==1){ //如果flag是1,判断本行有没有*/ if(str.contains("*/")){ //如果本行包含*/且flag=1那么表示注释结束,判断是不是结尾 if(str.endsWith("*/")){ //如果*/在结尾,是注释行,不在结尾,是代码行 flag=0; noteline++; } else{ flag=0; codeline++; } } else{ noteline++; } } else{ codeline++; } } } catch(FileNotFoundException e0){ e0.printStackTrace(); }catch(IOException e){ e.printStackTrace(); } finally{ try{ reader.close(); } catch(IOException e){ e.printStackTrace(); } } arr[0]=codeline; arr[1]=emptyline; arr[2]=noteline; System.out.println("代码行/注释行/空行:"+arr[0]+"/"+arr[1]+"/"+arr[2]); }
测试结果:
测试:
测试结果:
四、性能优化:
性能优化工具:JProfiler
简介:JProfiler是一个全功能的 Java 剖析工具( profiler ),专用于分析 J2SE 和 J2EE 应用程序。它把CPU 、执行绪和内存的剖析组合在一个强大的 应用中。 JProfiler 可提供许多 IDE 整合和应用服务器整合用途。 JProfiler直觉式的 GUI 让你可以找到效能瓶颈、抓出内存漏失 (memory leaks) 、并解决执行绪的问题。它让你得以对 heap walker作资源回收器的 root analysis ,可以轻易找出内存溢出; heap 快照( snapshot )模式让未被参照( reference )的对象、稍微被参照的对象、或在终结( finalization )队列的对象 都会被移除;整合精灵以便剖析浏览器的 Java 外挂功能。
在本次实践的过程中,主要使用到了JProfiler的堆遍历器(Heap walker)、类Classes(显示所有类和实例),由于代码量比较小,目录结构的单一性,所以测试结果很理想,性能很好!运行程序的反应时长很短!
五、总结:
经过本次作业,我学会了使用Junit进行单元测试。通过结对编程,我学会了在团队项目中互相帮助解决双方的问题,不仅能够提高效率,还能够及时发现自己的问题并改正。但是结对编程也需要提前交流好,避免命名冲突等问题。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步