<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                在練習 9.4 中你寫了一個叫?[main_oddeven.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_9/main_oddeven.go)?的程序用來測試前 100 個整數是否是偶數。這個函數屬于 even 包。 下面是一種可能的方案: 示例 13.7?[even_main.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_13/even/even_main/even_main.go): ~~~ package main import ( "fmt" "even/even" ) func main() { for i:=0; i<=100; i++ { fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i)) } } ~~~ 上面使用了 even.go 中的 even 包: 示例 13.8?[even/even.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_13/even/even/even.go): ~~~ package even func Even(i int) bool { // Exported function return i%2 == 0 } func Odd(i int) bool { // Exported function return i%2 != 0 } ~~~ 在 even 包的路徑下,我們創建一個名為 oddeven_test.go 的測試程序: 示例 13.9?[even/oddeven_test.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_13/even/even/oddeven_test.go): ~~~ package even import "testing" func TestEven(t *testing.T) { if !Even(10) { t.Log(" 10 must be even!") t.Fail() } if Even(7) { t.Log(" 7 is not even!") t.Fail() } } func TestOdd(t *testing.T) { if !Odd(11) { t.Log(" 11 must be odd!") t.Fail() } if Odd(10) { t.Log(" 10 is not odd!") t.Fail() } } ~~~ 由于測試需要具體的輸入用例且不可能測試到所有的用例(非常像一個無窮的數),所以我們必須對要使用的測試用例思考再三。 至少應該包括: * 正常的用例 * 反面的用例(錯誤的輸入,如用負數或字母代替數字,沒有輸入等) * 邊界檢查用例(如果參數的取值范圍是 0 到 1000,檢查 0 和 1000 的情況) 可以直接執行 go install 安裝 even 或者創建一個 以下內容的 Makefile: ~~~ include $(GOROOT)/src/Make.inc TARG=even GOFILES=\ even.go\ include $(GOROOT)/src/Make.pkg ~~~ 然后執行 make(或 gomake)命令來構建歸檔文件 even.a 測試代碼不能在 GOFILES 參數中引用,因為我們不希望生成的程序中有測試代碼。如果包含了測試代碼,go test 會給出錯誤提示!go test 會生成一個單獨的包含測試代碼的 _test 程序。 現在我們可以用命令:`go test`(或?`make test`)來測試 even 包。 因為示例 13.5 中的測試函數不會調用 t.Log 和 t.Fail,所以會得到一個 PASS 的結果。在這個簡單例子中一切都正常執行。 為了看到失敗時的輸出,把函數 TestEven 改為: ~~~ func TestEven(t *testing.T) { if Even(10) { t.Log(“Everything OK: 10 is even, just a test to see failed output!”) t.Fail() } } ~~~ 現在會調用 t.Log 和 t.Fail,得到的結果如下: ~~~ --- FAIL: even.TestEven (0.00 seconds) Everything OK: 10 is even, just a test to see failed output! FAIL ~~~ **練習 13.4:**[string_reverse_test.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_13/string_reverse_test.go) 為練習 7.11?[string_reverse.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/exercises/chapter_7/string_reverse.go)?寫一個單元測試。 把 string_reverse 放到自己的包 strev 中,只包含一個可導出函數 reverse。 實現并測試它。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看