]> Cypherpunks repositories - gostls13.git/commitdiff
panics: use the new facilities of testing.B instead
authorRob Pike <r@golang.org>
Tue, 20 Dec 2011 18:36:25 +0000 (10:36 -0800)
committerRob Pike <r@golang.org>
Tue, 20 Dec 2011 18:36:25 +0000 (10:36 -0800)
Lots of panics go away.
Also fix a name error in html/template.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5498045

18 files changed:
src/pkg/bytes/bytes_test.go
src/pkg/crypto/aes/aes_test.go
src/pkg/encoding/binary/binary_test.go
src/pkg/encoding/gob/timing_test.go
src/pkg/encoding/json/bench_test.go
src/pkg/html/template/error.go
src/pkg/html/template/escape_test.go
src/pkg/image/draw/bench_test.go
src/pkg/image/jpeg/writer_test.go
src/pkg/image/png/writer_test.go
src/pkg/image/tiff/reader_test.go
src/pkg/math/big/nat_test.go
src/pkg/net/http/serve_test.go
src/pkg/net/rpc/server_test.go
src/pkg/old/regexp/all_test.go
src/pkg/regexp/all_test.go
src/pkg/regexp/exec_test.go
src/pkg/strings/strings_test.go

index a2a08c20db0b83cb02729f73f867e8f372dab75e..2a1d41b910e3f222b091ccefe2eeac734f9edefa 100644 (file)
@@ -289,8 +289,7 @@ func bmIndexByte(b *testing.B, index func([]byte, byte) int, n int) {
        for i := 0; i < b.N; i++ {
                j := index(buf, 'x')
                if j != n-1 {
-                       println("bad index", j)
-                       panic("bad index")
+                       b.Fatal("bad index", j)
                }
        }
        buf[n-1] = '\x00'
@@ -317,7 +316,7 @@ func bmEqual(b *testing.B, equal func([]byte, []byte) bool, n int) {
        for i := 0; i < b.N; i++ {
                eq := equal(buf1, buf2)
                if !eq {
-                       panic("bad equal")
+                       b.Fatal("bad equal")
                }
        }
        buf1[n-1] = '\x00'
@@ -339,8 +338,7 @@ func bmIndex(b *testing.B, index func([]byte, []byte) int, n int) {
        for i := 0; i < b.N; i++ {
                j := index(buf, buf[n-7:])
                if j != n-7 {
-                       println("bad index", j)
-                       panic("bad index")
+                       b.Fatal("bad index", j)
                }
        }
        buf[n-1] = '\x00'
@@ -362,8 +360,7 @@ func bmIndexEasy(b *testing.B, index func([]byte, []byte) int, n int) {
        for i := 0; i < b.N; i++ {
                j := index(buf, buf[n-7:])
                if j != n-7 {
-                       println("bad index", j)
-                       panic("bad index")
+                       b.Fatal("bad index", j)
                }
        }
        buf[n-1] = '\x00'
@@ -385,8 +382,7 @@ func bmCount(b *testing.B, count func([]byte, []byte) int, n int) {
        for i := 0; i < b.N; i++ {
                j := count(buf, buf[n-7:])
                if j != 1 {
-                       println("bad count", j)
-                       panic("bad count")
+                       b.Fatal("bad count", j)
                }
        }
        buf[n-1] = '\x00'
@@ -408,8 +404,7 @@ func bmCountEasy(b *testing.B, count func([]byte, []byte) int, n int) {
        for i := 0; i < b.N; i++ {
                j := count(buf, buf[n-7:])
                if j != 1 {
-                       println("bad count", j)
-                       panic("bad count")
+                       b.Fatal("bad count", j)
                }
        }
        buf[n-1] = '\x00'
index aa1d0df8e93996c5ed249f8a4830296ec5b34dba..e500c666d97ee42b62e4a6cc20d0343068ed649d 100644 (file)
@@ -356,7 +356,7 @@ func BenchmarkEncrypt(b *testing.B) {
        tt := encryptTests[0]
        c, err := NewCipher(tt.key)
        if err != nil {
-               panic("NewCipher")
+               b.Fatal("NewCipher:", err)
        }
        out := make([]byte, len(tt.in))
        b.StartTimer()
index 899505e0a55ad464c983f41f9a7625452c7a0d40..3e7057ea22787066f2054d82f30ef0b92adbc7ff 100644 (file)
@@ -197,7 +197,7 @@ func BenchmarkReadStruct(b *testing.B) {
        }
        b.StopTimer()
        if !reflect.DeepEqual(s, t) {
-               panic("no match")
+               b.Fatal("no match")
        }
 }
 
@@ -251,6 +251,6 @@ func BenchmarkWriteInts(b *testing.B) {
        }
        b.StopTimer()
        if !bytes.Equal(buf.Bytes(), big[:30]) {
-               panic("first half doesn't match")
+               b.Fatalf("first half doesn't match: %x %x", buf.Bytes(), big[:30])
        }
 }
index 47437a607f1a3e9ec05e8da354e22d7de6ee8b5b..1017eb7f51d4100a8df8070360ef208b12b50c9a 100644 (file)
@@ -39,7 +39,7 @@ func benchmarkEndToEnd(r io.Reader, w io.Writer, b *testing.B) {
 func BenchmarkEndToEndPipe(b *testing.B) {
        r, w, err := os.Pipe()
        if err != nil {
-               panic("can't get pipe:" + err.Error())
+               b.Fatal("can't get pipe:", err)
        }
        benchmarkEndToEnd(r, w, b)
 }
index f0c52011a1db594c18eefdee99f698ddca09920e..333c1c0ce9e3d13b63bfa9bd58543a6d5cd6bcad 100644 (file)
@@ -84,7 +84,7 @@ func BenchmarkCodeEncoder(b *testing.B) {
        enc := NewEncoder(ioutil.Discard)
        for i := 0; i < b.N; i++ {
                if err := enc.Encode(&codeStruct); err != nil {
-                       panic(err)
+                       b.Fatal("Encode:", err)
                }
        }
        b.SetBytes(int64(len(codeJSON)))
@@ -98,7 +98,7 @@ func BenchmarkCodeMarshal(b *testing.B) {
        }
        for i := 0; i < b.N; i++ {
                if _, err := Marshal(&codeStruct); err != nil {
-                       panic(err)
+                       b.Fatal("Marshal:", err)
                }
        }
        b.SetBytes(int64(len(codeJSON)))
@@ -120,7 +120,7 @@ func BenchmarkCodeDecoder(b *testing.B) {
                buf.WriteByte('\n')
                buf.WriteByte('\n')
                if err := dec.Decode(&r); err != nil {
-                       panic(err)
+                       b.Fatal("Decode:", err)
                }
        }
        b.SetBytes(int64(len(codeJSON)))
@@ -135,7 +135,7 @@ func BenchmarkCodeUnmarshal(b *testing.B) {
        for i := 0; i < b.N; i++ {
                var r codeResponse
                if err := Unmarshal(codeJSON, &r); err != nil {
-                       panic(err)
+                       b.Fatal("Unmmarshal:", err)
                }
        }
        b.SetBytes(int64(len(codeJSON)))
@@ -150,7 +150,7 @@ func BenchmarkCodeUnmarshalReuse(b *testing.B) {
        var r codeResponse
        for i := 0; i < b.N; i++ {
                if err := Unmarshal(codeJSON, &r); err != nil {
-                       panic(err)
+                       b.Fatal("Unmmarshal:", err)
                }
        }
        b.SetBytes(int64(len(codeJSON)))
index 9622d7e48eed8dff1547a2a6150a3728f5c9a74e..dcac748967691be048c851ea36441fc991cfdd77 100644 (file)
@@ -183,11 +183,11 @@ const (
 
 func (e *Error) Error() string {
        if e.Line != 0 {
-               return fmt.Sprintf("exp/template/html:%s:%d: %s", e.Name, e.Line, e.Description)
+               return fmt.Sprintf("html/template:%s:%d: %s", e.Name, e.Line, e.Description)
        } else if e.Name != "" {
-               return fmt.Sprintf("exp/template/html:%s: %s", e.Name, e.Description)
+               return fmt.Sprintf("html/template:%s: %s", e.Name, e.Description)
        }
-       return "exp/template/html: " + e.Description
+       return "html/template: " + e.Description
 }
 
 // errorf creates an error given a format string f and args.
index 2d15c71844846f03837a00fabd660b36bea2aa1a..7702300ffda4d09f9b335ce9d7d341f907986ba6 100644 (file)
@@ -944,23 +944,23 @@ func TestErrors(t *testing.T) {
                },
                {
                        `<input type=button value=onclick=>`,
-                       `exp/template/html:z: "=" in unquoted attr: "onclick="`,
+                       `html/template:z: "=" in unquoted attr: "onclick="`,
                },
                {
                        `<input type=button value= onclick=>`,
-                       `exp/template/html:z: "=" in unquoted attr: "onclick="`,
+                       `html/template:z: "=" in unquoted attr: "onclick="`,
                },
                {
                        `<input type=button value= 1+1=2>`,
-                       `exp/template/html:z: "=" in unquoted attr: "1+1=2"`,
+                       `html/template:z: "=" in unquoted attr: "1+1=2"`,
                },
                {
                        "<a class=`foo>",
-                       "exp/template/html:z: \"`\" in unquoted attr: \"`foo\"",
+                       "html/template:z: \"`\" in unquoted attr: \"`foo\"",
                },
                {
                        `<a style=font:'Arial'>`,
-                       `exp/template/html:z: "'" in unquoted attr: "font:'Arial'"`,
+                       `html/template:z: "'" in unquoted attr: "font:'Arial'"`,
                },
                {
                        `<a=foo>`,
index 2be91185af2e384c0a048216cc6a2de97faf0dd2..554a0d3fbc7fe301212bbe2496b913b56a27fc01 100644 (file)
@@ -51,7 +51,7 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) {
                }
                dst = dst1
        default:
-               panic("unreachable")
+               b.Fatal("unknown destination color model", dcm)
        }
 
        var src image.Image
@@ -116,7 +116,7 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) {
                        Rect:           image.Rect(0, 0, srcw, srch),
                }
        default:
-               panic("unreachable")
+               b.Fatal("unknown source color model", scm)
        }
 
        var mask image.Image
@@ -137,7 +137,7 @@ func bench(b *testing.B, dcm, scm, mcm color.Model, op Op) {
                }
                mask = mask1
        default:
-               panic("unreachable")
+               b.Fatal("unknown mask color model", mcm)
        }
 
        b.StartTimer()
index 28e87321361618198a8efc04fa6dc8fd489bc614..e4b56d28847d517e7c9ea0eda7d19763623db55f 100644 (file)
@@ -105,7 +105,7 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) {
                }
        }
        if !img.Opaque() {
-               panic("expected image to be opaque")
+               b.Fatal("expected image to be opaque")
        }
        b.SetBytes(640 * 480 * 4)
        b.StartTimer()
index 1757e14cada5107054bb57baf7ee42e427fc16ed..228ecccfb4f76486457d7ab6b81847abcf22229f 100644 (file)
@@ -125,7 +125,7 @@ func BenchmarkEncodeRGBOpaque(b *testing.B) {
                }
        }
        if !img.Opaque() {
-               panic("expected image to be opaque")
+               b.Fatal("expected image to be opaque")
        }
        b.SetBytes(640 * 480 * 4)
        b.StartTimer()
@@ -138,7 +138,7 @@ func BenchmarkEncodeRGBA(b *testing.B) {
        b.StopTimer()
        img := image.NewRGBA(image.Rect(0, 0, 640, 480))
        if img.Opaque() {
-               panic("expected image to not be opaque")
+               b.Fatal("expected image to not be opaque")
        }
        b.SetBytes(640 * 480 * 4)
        b.StartTimer()
index 1a3d23bbd75548b0dd2c0fc9cc551abb45de1308..ee5dafd9962a75ed37b7fb6dc66587169a356b2a 100644 (file)
@@ -113,7 +113,7 @@ func BenchmarkDecode(b *testing.B) {
        for i := 0; i < b.N; i++ {
                _, err := Decode(r)
                if err != nil {
-                       panic(err)
+                       b.Fatal("Decode:", err)
                }
        }
 }
index e3c6552d9fb3de34cb8a75400dbb4da7066c0db2..25e39273c0c8b48313cd5b394b9984645cc49183 100644 (file)
@@ -5,7 +5,6 @@
 package big
 
 import (
-       "fmt"
        "io"
        "strings"
        "testing"
@@ -402,7 +401,7 @@ func ScanHelper(b *testing.B, base int, x, y Word) {
        var s string
        s = z.string(lowercaseDigits[0:base])
        if t := toString(z, lowercaseDigits[0:base]); t != s {
-               panic(fmt.Sprintf("scanning: got %s; want %s", s, t))
+               b.Fatalf("scanning: got %s; want %s", s, t)
        }
        b.StartTimer()
 
index c68e6614b19079f6c5d27f9e275bbd765eff1063..24e6b50dab87e6d74d128927d8c40a72f9a02587 100644 (file)
@@ -1164,15 +1164,15 @@ func BenchmarkClientServer(b *testing.B) {
        for i := 0; i < b.N; i++ {
                res, err := Get(ts.URL)
                if err != nil {
-                       panic("Get: " + err.Error())
+                       b.Fatal("Get:", err)
                }
                all, err := ioutil.ReadAll(res.Body)
                if err != nil {
-                       panic("ReadAll: " + err.Error())
+                       b.Fatal("ReadAll:", err)
                }
                body := string(all)
                if body != "Hello world.\n" {
-                       panic("Got body: " + body)
+                       b.Fatal("Got body:", body)
                }
        }
 
index a52a86e414ea02ebaf0fb95c90e495146e17f358..c1845fa50737bb2f2a72321230b60f20fd58334a 100644 (file)
@@ -516,12 +516,10 @@ func benchmarkEndToEnd(dial func() (*Client, error), b *testing.B) {
                        for atomic.AddInt32(&N, -1) >= 0 {
                                err = client.Call("Arith.Add", args, reply)
                                if err != nil {
-                                       fmt.Printf("Add: expected no error but got string %q", err.Error())
-                                       panic("rpc error")
+                                       b.Fatalf("rpc error: Add: expected no error but got string %q", err.Error())
                                }
                                if reply.C != args.A+args.B {
-                                       fmt.Printf("Add: expected %d got %d", reply.C, args.A+args.B)
-                                       panic("rpc error")
+                                       b.Fatalf("rpc error: Add: expected %d got %d", reply.C, args.A+args.B)
                                }
                        }
                        wg.Done()
@@ -536,8 +534,7 @@ func benchmarkEndToEndAsync(dial func() (*Client, error), b *testing.B) {
        once.Do(startServer)
        client, err := dial()
        if err != nil {
-               fmt.Println("error dialing", err)
-               return
+               b.Fatalf("error dialing:", err)
        }
 
        // Asynchronous calls
@@ -561,12 +558,11 @@ func benchmarkEndToEndAsync(dial func() (*Client, error), b *testing.B) {
                }()
                go func() {
                        for call := range res {
-                               a := call.Args.(*Args).A
-                               b := call.Args.(*Args).B
-                               c := call.Reply.(*Reply).C
-                               if a+b != c {
-                                       fmt.Printf("Add: expected %d got %d", a+b, c)
-                                       panic("incorrect reply")
+                               A := call.Args.(*Args).A
+                               B := call.Args.(*Args).B
+                               C := call.Reply.(*Reply).C
+                               if A+B != C {
+                                       b.Fatalf("incorrect reply: Add: expected %d got %d", A+B, C)
                                }
                                <-gate
                                if atomic.AddInt32(&recv, -1) == 0 {
index 9a04360dd11a719676a8e95275f2e0d8b8d7f552..180dac4d455db6ff0decf8c5df9de52f23c0daf9 100644 (file)
@@ -321,8 +321,7 @@ func BenchmarkLiteral(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatal("no match!")
                }
        }
 }
@@ -334,8 +333,7 @@ func BenchmarkNotLiteral(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatal("no match!")
                }
        }
 }
@@ -347,8 +345,7 @@ func BenchmarkMatchClass(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatal("no match!")
                }
        }
 }
@@ -362,8 +359,7 @@ func BenchmarkMatchClass_InRange(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatal("no match!")
                }
        }
 }
index 8810796daf2d4176dad4e665d5a7044aa06cc94d..e729510b5134da56ca57dd188ca648cea92e34bd 100644 (file)
@@ -324,8 +324,7 @@ func BenchmarkLiteral(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatalf("no match!")
                }
        }
 }
@@ -337,8 +336,7 @@ func BenchmarkNotLiteral(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatalf("no match!")
                }
        }
 }
@@ -350,8 +348,7 @@ func BenchmarkMatchClass(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatalf("no match!")
                }
        }
 }
@@ -365,8 +362,7 @@ func BenchmarkMatchClass_InRange(b *testing.B) {
        b.StartTimer()
        for i := 0; i < b.N; i++ {
                if !re.MatchString(x) {
-                       println("no match!")
-                       break
+                       b.Fatalf("no match!")
                }
        }
 }
index 312bf0275fd49de4940b01f6eb2399bf4c3d43d8..e668574a514389377c9baf2e95cc204799b91c46 100644 (file)
@@ -673,7 +673,7 @@ func benchmark(b *testing.B, re string, n int) {
        b.SetBytes(int64(n))
        for i := 0; i < b.N; i++ {
                if r.Match(t) {
-                       panic("match!")
+                       b.Fatal("match!")
                }
        }
 }
index 8866d220c00d92b8362e314a8b37f2a7ba0d0646..54046d68aa0073dea15d6f1cc47e44d6cb8d36dc 100644 (file)
@@ -8,7 +8,6 @@ import (
        "bytes"
        "io"
        "reflect"
-       "strconv"
        . "strings"
        "testing"
        "unicode"
@@ -143,7 +142,7 @@ const benchmarkString = "some_text=some☺value"
 
 func BenchmarkIndexRune(b *testing.B) {
        if got := IndexRune(benchmarkString, '☺'); got != 14 {
-               panic("wrong index: got=" + strconv.Itoa(got))
+               b.Fatalf("wrong index: expected 14, got=%d", got)
        }
        for i := 0; i < b.N; i++ {
                IndexRune(benchmarkString, '☺')
@@ -152,7 +151,7 @@ func BenchmarkIndexRune(b *testing.B) {
 
 func BenchmarkIndexRuneFastPath(b *testing.B) {
        if got := IndexRune(benchmarkString, 'v'); got != 17 {
-               panic("wrong index: got=" + strconv.Itoa(got))
+               b.Fatalf("wrong index: expected 17, got=%d", got)
        }
        for i := 0; i < b.N; i++ {
                IndexRune(benchmarkString, 'v')
@@ -161,7 +160,7 @@ func BenchmarkIndexRuneFastPath(b *testing.B) {
 
 func BenchmarkIndex(b *testing.B) {
        if got := Index(benchmarkString, "v"); got != 17 {
-               panic("wrong index: got=" + strconv.Itoa(got))
+               b.Fatalf("wrong index: expected 17, got=%d", got)
        }
        for i := 0; i < b.N; i++ {
                Index(benchmarkString, "v")