第二週做業 WordCount

PSP2.1 PSP 階段 預估耗時 (分鐘) 實際耗時 (分鐘)
Planning 計劃 25 30
· Estimate · 估計這個任務須要多少時間 20 30
Development 開發 130 270
· Analysis · 需求分析 (包括學習新技術) 15 20
· Design Spec · 生成設計文檔 0 0
· Design Review · 設計複審 (和同事審覈設計文檔) 0 0
· Coding Standard · 代碼規範 (爲目前的開發制定合適的規範) 2 2
· Design · 具體設計 20 30
· Coding · 具體編碼 120 150
· Code Review · 代碼複審 20 10
· Test · 測試(自我測試,修改代碼,提交修改) 10 60
Reporting 報告 60 30
· Test Report · 測試報告 10 5
· Size Measurement · 計算工做量 10 5
· Postmortem & Process Improvement Plan · 過後總結, 並提出過程改進計劃 20 20
合計 462 642

GitHub

https://github.com/dashmrl/WC.gitgit

解題思路

將不一樣的選項參數交由不一樣的函數處理。github

空格、逗號和句號做爲分隔符,連續空格視爲一個空格。app

支持的參數使用枚舉類來定義,過濾掉不支持的參數。函數

程序設計實現

  • 參數枚舉類單元測試

    enum class Options(val value: String) {
        C("-c"),// 字符數
        W("-w"),// 單詞數
        L("-l"),// 行數
        O("-o")// 輸出文件
    }
  • 參數檢查函數學習

    fun checkArgs(pindex: Int, pc: Int, msg: String): Boolean {
        if (pindex <= -1) return false
        if (pindex + 1 == pc) {
            throw IllegalArgumentException(msg)
        }
    
        return true
    }
  • 單詞統計函數測試

    fun calWordCount(ifile: File): Int {
        println("start calculating word count")
        return ifile.readLines().sumBy {
            it.split(
                    ' ', ',', '.',
                    ignoreCase = false,
                    limit = 0).filter { it.isNotEmpty() }.size
        }
    }
  • 輸出結果函數ui

    fun outputResult(input: String, cc: Int, wc: Int, lc: Int, of: File) {
        val sb = StringBuilder(input)
        if (cc != -1) {
            sb.append(",").append(Options.C.value).append(":").append(cc)
        }
        if (wc != -1) {
            sb.append(",").append(Options.W.value).append(":").append(wc)
        }
        if (lc != -1) {
            sb.append(",").append(Options.L.value).append(":").append(lc)
        }
        if (of.exists() && of.isFile) {
            of.delete()
        }
    
        println("write the result to ${of.absolutePath}")
        of.writeText(sb.toString())
    }

測試 bat 腳本

WordCount.exe -c input.txt
WordCount.exe -w input.txt
WordCount.exe -l -o result.txt input.txt
WordCount.exe -c -w -l -o result.txt input.txt

總結

系統測試在測試中佔比很是小的部分,收益遠沒有單元測試高。編碼

jar 包打包成 exe 不論對於測試和做業,都顯得很是多餘,找打包軟件花費至少半天時間。設計

相關文章
相關標籤/搜索