第四周小组作业

基本任务

(1)Gith hub地址 https://github.com/YinshenYuan/wcPro

(2)PSP表格


(3)接口对接

我编码的模块是main函数,我是将main函数中的逻辑分成两个函数public static String argsInfo(String[] args)和public static String exec(String argsinfo),第一个函数实现的功能是解析从main函数传递给它的args[]字符数组,根据不同的输入返回一个不同的字符串,然后第二个函数再接收这个字符串,通过解析这个字符串来决定使用什么样的功能。

public static String argsInfo(String[] args)
	{
		String argsinfo = null;
		if( args[0]== null )//Lack of input
		{
			argsinfo = "No input";
			return argsinfo;
		}
		if(args.length > 1)//more than one input
		{
			argsinfo = "More than one input";
			return argsinfo;
		}
		if (args[0] == "-x")//when first input is -x
		{
			argsinfo = args[0];
			return argsinfo;
		}
		else if(!args[0].endsWith(".txt"))//file name doesn't end with .txt
		{
			argsinfo = "The file doesn't end with .txt ";
			return argsinfo;
		}
		return args[0];//normal situation return path
	}

这个函数处理了输入为空,输入大于一个,输入为“-x”,输入文件名不以“.txt”结尾,和输入以“.txt”结尾的情况。

public static String exec(String argsinfo)
	{
		String err = null;
		if( argsinfo == null )//Lack of input
		{
			err = "args err";
			return err;
		}
		if(argsinfo == "No input" || argsinfo == "More than one input" || argsinfo == "The file doesn't end with .txt ")
		{
			return argsinfo;
		}//when args  err
		else if(argsinfo == "-x" )//when first input is -x
		{
				Window  guiWnd = new Window();
				guiWnd.showDialog();
		}
		else // when normal situation return path
		{
		String content = getContent(argsinfo);
		if(content==null)
		{
			err = "file does not exist";
			return err;
		}
		Map<String, Integer> map = countWord(content); 
			if(!writeOutput(map)) 
			{
					err = "can't write file";
					return err;
			}
			return "write file finished";	
		}
		return "Window";
	}

这个函数根据argsinfo处理的不同情况,接收其返回的String,然后执行相应功能或输出相应错误。

public static void main(String[] args) throws IOException 
	{
		// TODO integrate all the method to figure out the word counting problem
		String argsinfo = null;
		argsinfo =  argsInfo(args);
		System.out.println(exec(argsinfo));
	}

新建一个argsinfo字符串用于接收argsInfo(args)的返回值,然后运行exec(argsinfo),将结果输出。

(4)测试用例

因为main函数主要逻辑分为两个函数,所以可分别对其测试。
首先是对argsInfo(args)的测试,解析参数主要测试当参数为某些特定情况时argsInfo返回值是否与期望值相等。
我就测了参数分别为多个参数、“-x”,以“.txt”结尾,不以”.txt”结尾,参数为空的情况。
而测exec(argsinfo)时思路也差不多,同样是看当接收到不同的argsinfo时输出与期望输出是否相等。

public class MainTest {
	String args[] = new String[1];
	String Args[] = new String[2];
	String argsinfo ;
	
	@Test
	public void test1() 
	{
		args[0] = "testcase/test.txt";
		assertEquals("testcase/test.txt", WordCountPro.argsInfo(args));
	}
	
	
	@Test
	public void test2() 
	{
		args[0] = "testcase/test1.txt";
		assertEquals("testcase/test1.txt", WordCountPro.argsInfo(args));
	}
@Test
	public void test11() 
	{
		Args[0] = "testcase/test.c";
		Args[1] = "testcase/test.txt";
		assertEquals("More than one input", WordCountPro.argsInfo(Args));
	}
	
	
	@Test
	public void test12() 
	{
		Args[0] = "testcase/test.txt";
		Args[1] = "testcase/test1.txt.";
		assertEquals("More than one input", WordCountPro.argsInfo(Args));
	}

此处只列出几个测试用例。

(5)测试结果

测试全部成功,个人认为测试质量尚可。

(6)贡献分

0.25

扩展任务

(1)代码规范

参考的是阿里巴巴Java开发手册,并且组长在编码前就给我们声明了几点规范。
变量名采用小驼峰
{ 换行
在运算符两旁和逗号后面加上空格
注释使用英文
较长的连续代码适当地加空行

(2)对其他组员的评价

我评价的是队长袁寅申的代码,队长的代码严谨度很好,基本都遵循了Java开发手册中的规范,比如对变量名的命名都遵循了小驼峰,并且实例名也都较好的体现了其具体含义,让人能比较快的读懂代码。

(3)静态工具

我们小组统一使用的是findbugs
下载地址:http://findbugs.sourceforge.net/manual/eclipse.html

上图指出在exec()函数中出现字符串比较的问题,用==会有问题,那么就可以改成equals()。

高级任务:性能测试和优化

我所写的代码块主要是逻辑框架,调用其他模块的方法,涉及到的性能优化较少,所以只是设置了一个压力测试。

@Test
	public void stressTesting()
	{
		String[] lrgFiles = {"testcase/prs/prs1.txt", "testcase/prs/prs2.txt"
				,"testcase/prs/prs3.txt"};
		Instant s, e;
		for(String file: lrgFiles)
		{
			String content = WordCountPro.getContent(file);
			s = Instant.now();
			WordCountPro.countWord(content);
			e = Instant.now();
			long sec = Duration.between(s, e).getSeconds();
			long nano = Duration.between(s, e).getNano();
			double interval = (double)sec + (double)nano / 1e9;
			System.out.println("The time used for file " + file + " is " +
								interval + " seconds");				
		}
	}

Prs1大小10m,prs2大小60m,prs3大小100m,测的最终结果

1和2都可以承受,但是prs3大小过大所以没能读出来。

posted on 2018-04-08 16:13  何啸轩  阅读(225)  评论(0)    收藏  举报

导航