第四周作业 WC优化
## 一、GitHub地址
https://github.com/donghaixiang/WC-1
## 二、PSP表格
| **PSP2.1** | **PSP****阶段** | **预估耗时****(分钟)** | **实际耗时****(分钟)** |
| --------------------------------------- | --------------------------------------- | ------------------------ | ------------------------ |
| Planning | 计划 | 30 | 20 |
| · Estimate | · 估计这个任务需要多少时间 | 30 | 20 |
| Development | 开发 | 470 | 550 |
| · Analysis | · 需求分析 (包括学习新技术) | 30 | 20 |
| · Design Spec | · 生成设计文档 | 20 | 20 |
| · Design Review | · 设计复审 (和同事审核设计文档) | 30 | 20 |
| · Coding Standard | · 代码规范 (为目前的开发制定合适的规范) | 30 | 30 |
| · Design | · 具体设计 | 30 | 40 |
| · Coding | · 具体编码 | 180 | 240 |
| · Code Review | · 代码复审 | 30 | 30 |
| · Test | · 测试(自我测试,修改代码,提交修改) | 120 | 150 |
| Reporting | 报告 | 90 | 90 |
| · Test Report | · 测试报告 | 30 | 30 |
| · Size Measurement | · 计算工作量 | 30 | 30 |
| · Postmortem & Process Improvement Plan | · 事后总结, 并提出过程改进计划 | 30 | 30 |
| | 合计 | 590 | 660 |
## 三.模块接口设计
处于任务分配的考量,我们小组将整个任务分成了`输入控制` `内容读取` `分析核心` `输出控制` 几个功能模块,同时在组长17002的建议下,我们采用了Kotlin来进行模块的编写,因为Kotlin简洁,同时兼容Java,可以用Junit进行单元测试。
我负责的模块是 输出控制 以及Linecounter的测试
输出控制:
fun com.dashmrl.wc.outputResult(input: String, cc: Int, wc: Int, lc: Int, of: File) {
val sb = StringBuilder(input)
if (cc != -1) {
sb.append(",").append(com.dashmrl.wc.Options.C.value).append(":").append(cc)
}
if (wc != -1) {
sb.append(",").append(com.dashmrl.wc.Options.W.value).append(":").append(wc)
}
if (lc != -1) {
sb.append(",").append(com.dashmrl.wc.Options.L.value).append(":").append(lc)
}
if (of.exists() && of.isFile) {
of.delete()
}
println("write the result to ${of.absolutePath}")
of.writeText(sb.toString())
}
出于对我代码能力及未来发展方向的充分考虑,组长和其它组员决定让我担任最简单的部分代码设计,我在组间同时也帮助交流的进行;
## 四、设计测试用例与测试
对于LineCounter(),使用了Junit对其进行了单元测试,测试的文本是某网页上的若干段文字(位于类CounterTest中),通过文本软件获得了测试文本的字符数,然后编辑其测试程序
/**
* Author dashmrl
* Time 22:01
* Date 15/03/2018
* Email xinliugm@gmail.com
*/
fun main(args: Array<String>) {
val checker = ArgChecker()
val pc = args.size
if (pc < 2) {
throw IllegalArgumentException("no enough args!!")
}
var cc = -1
var wc = -1
var lc = -1
val ifile = File(args[pc - 1])
if (!ifile.exists() || ifile.isDirectory) {
throw IllegalArgumentException("invalid input file(${ifile.absolutePath}),not exits or not a file")
}
for (value in Options.values()) {
val index = args.indexOf(value.value)
if (index == -1) {
continue
}
when (value) {
Options.C -> {
checker.check(index, pc, "-c error")
cc = CharCounter().count(ifile)
}
Options.W -> {
checker.check(index, pc, "-w error")
wc = WordCounter().count(ifile)
}
Options.L -> {
checker.check(index, pc, "-l error")
lc = LineCounter().count(ifile)
}
else -> {
}
}
}
val oindex = args.indexOf(Options.O.value)
val of: File
if (checker.check(oindex - 1, pc, "-c error")) {
checker.checkOutputArg(oindex, args)
of = File(args[oindex + 1])
} else {
of = File("result.txt")
}
FilePrinter().print(ifile.name, cc, wc, lc, of)
}
package test.com.dashmrl.wc.counter
import com.dashmrl.wc.counter.LineCounter
import org.junit.Test
import kotlin.test.assertEquals
/**
* Author dashmrl
* Time 10:25
* Date 2018/4/8
* Email xinliugm@gmail.com
*/
class LineCounterTest : CounterTest() {
private val results = arrayOf(
4,
1,
2,
1,
2,
3,
4,
2,
2,
1,
1,
1,
5,
3,
1,
3,
1,
1,
1,
4)
@Test
fun count() {
inputs.forEach {
val c = LineCounter().count(createIntputFile(it, "inputs.txt"))
assertEquals(results[inputs.indexOf(it)],c,"error,exit!!")
}
}
}
## 五、小组贡献分
0.22
## 六、参考链接
http://www.cnblogs.com/ningjing-zhiyuan/p/8654132.html
[邹欣 代码规范与代码复审](http://www.cnblogs.com/xinz/archive/2011/11/20/2255971.html)

浙公网安备 33010602011771号