]> Cypherpunks repositories - gostls13.git/commitdiff
src: Use bytes.Equal instead of bytes.Compare where possible.
authorMatthew Dempsky <mdempsky@google.com>
Sun, 6 Jan 2013 23:03:49 +0000 (10:03 +1100)
committerAndrew Gerrand <adg@golang.org>
Sun, 6 Jan 2013 23:03:49 +0000 (10:03 +1100)
bytes.Equal is simpler to read and should also be faster because
of short-circuiting and assembly implementations.

Change generated automatically using:
  gofmt -r 'bytes.Compare(a, b) == 0 -> bytes.Equal(a, b)'
  gofmt -r 'bytes.Compare(a, b) != 0 -> !bytes.Equal(a, b)'

R=golang-dev, dave, adg, rsc
CC=golang-dev
https://golang.org/cl/7038051

14 files changed:
src/cmd/gofmt/gofmt_test.go
src/cmd/gofmt/long_test.go
src/pkg/bufio/bufio_test.go
src/pkg/crypto/rsa/pkcs1v15_test.go
src/pkg/crypto/rsa/rsa_test.go
src/pkg/encoding/asn1/asn1_test.go
src/pkg/encoding/asn1/marshal_test.go
src/pkg/encoding/hex/hex_test.go
src/pkg/encoding/json/decode_test.go
src/pkg/encoding/json/scanner_test.go
src/pkg/exp/locale/collate/collate_test.go
src/pkg/exp/locale/collate/maketables.go
src/pkg/go/doc/doc_test.go
src/pkg/math/big/int_test.go

index ee943989b62c092eb38e35c6477f9c8500ffc85a..51d16bb648d56c24d1bd87dfa526726958cf1f0f 100644 (file)
@@ -56,7 +56,7 @@ func runTest(t *testing.T, in, out, flags string) {
                return
        }
 
-       if got := buf.Bytes(); bytes.Compare(got, expected) != 0 {
+       if got := buf.Bytes(); !bytes.Equal(got, expected) {
                t.Errorf("(gofmt %s) != %s (see %s.gofmt)", in, out, in)
                d, err := diff(expected, got)
                if err == nil {
index edbce606a552a962695d43be679491da05eed75b..862e9d98772c97a1399616e72cf8977ed82027a1 100644 (file)
@@ -84,7 +84,7 @@ func testFile(t *testing.T, b1, b2 *bytes.Buffer, filename string) {
        }
 
        // the first and 2nd result should be identical
-       if bytes.Compare(b1.Bytes(), b2.Bytes()) != 0 {
+       if !bytes.Equal(b1.Bytes(), b2.Bytes()) {
                t.Errorf("gofmt %s not idempotent", filename)
        }
 }
index 564621150ef116e8ceabd45c81b84e35efd3f506..4ffb29eaf09216f6fbdb99beef5bb5c037a171a8 100644 (file)
@@ -748,7 +748,7 @@ func testReadLineNewlines(t *testing.T, input string, expect []readLineResult) {
        b := NewReaderSize(strings.NewReader(input), minReadBufferSize)
        for i, e := range expect {
                line, isPrefix, err := b.ReadLine()
-               if bytes.Compare(line, e.line) != 0 {
+               if !bytes.Equal(line, e.line) {
                        t.Errorf("%q call %d, line == %q, want %q", input, i, line, e.line)
                        return
                }
index 58d5fda197628e55ffcf9c671667e0a20c85ce6c..bf9219bae1b47c36158c58eeb3f8f1c51de10d65 100644 (file)
@@ -57,7 +57,7 @@ func TestDecryptPKCS1v15(t *testing.T) {
                        t.Errorf("#%d error decrypting", i)
                }
                want := []byte(test.out)
-               if bytes.Compare(out, want) != 0 {
+               if !bytes.Equal(out, want) {
                        t.Errorf("#%d got:%#v want:%#v", i, out, want)
                }
        }
@@ -90,7 +90,7 @@ func TestEncryptPKCS1v15(t *testing.T) {
                        return false
                }
 
-               if bytes.Compare(plaintext, in) != 0 {
+               if !bytes.Equal(plaintext, in) {
                        t.Errorf("output mismatch: %#v %#v", plaintext, in)
                        return false
                }
@@ -132,7 +132,7 @@ func TestEncryptPKCS1v15SessionKey(t *testing.T) {
                        t.Errorf("#%d error decrypting", i)
                }
                want := []byte(test.out)
-               if bytes.Compare(key, want) != 0 {
+               if !bytes.Equal(key, want) {
                        t.Errorf("#%d got:%#v want:%#v", i, key, want)
                }
        }
@@ -176,7 +176,7 @@ func TestSignPKCS1v15(t *testing.T) {
                }
 
                expected, _ := hex.DecodeString(test.out)
-               if bytes.Compare(s, expected) != 0 {
+               if !bytes.Equal(s, expected) {
                        t.Errorf("#%d got: %x want: %x", i, s, expected)
                }
        }
index 5fdf0b49146bf948cbd21b5dc05bfd724d75e664..9be22a8f0bfb395c8429472bd3aebb65441bdbb2 100644 (file)
@@ -179,7 +179,7 @@ func TestEncryptOAEP(t *testing.T) {
                        if err != nil {
                                t.Errorf("#%d,%d error: %s", i, j, err)
                        }
-                       if bytes.Compare(out, message.out) != 0 {
+                       if !bytes.Equal(out, message.out) {
                                t.Errorf("#%d,%d bad result: %x (want %x)", i, j, out, message.out)
                        }
                }
@@ -203,7 +203,7 @@ func TestDecryptOAEP(t *testing.T) {
                        out, err := DecryptOAEP(sha1, nil, private, message.out, nil)
                        if err != nil {
                                t.Errorf("#%d,%d error: %s", i, j, err)
-                       } else if bytes.Compare(out, message.in) != 0 {
+                       } else if !bytes.Equal(out, message.in) {
                                t.Errorf("#%d,%d bad result: %#v (want %#v)", i, j, out, message.in)
                        }
 
@@ -211,7 +211,7 @@ func TestDecryptOAEP(t *testing.T) {
                        out, err = DecryptOAEP(sha1, random, private, message.out, nil)
                        if err != nil {
                                t.Errorf("#%d,%d (blind) error: %s", i, j, err)
-                       } else if bytes.Compare(out, message.in) != 0 {
+                       } else if !bytes.Equal(out, message.in) {
                                t.Errorf("#%d,%d (blind) bad result: %#v (want %#v)", i, j, out, message.in)
                        }
                }
index cabdf03b4824fd52e3623579d667c63baee2825b..6e98dcf0b990f7fd6b6628009bf634ae9ac60e62 100644 (file)
@@ -124,7 +124,7 @@ func TestBitString(t *testing.T) {
                        t.Errorf("#%d: Incorrect error result (did fail? %v, expected: %v)", i, err == nil, test.ok)
                }
                if err == nil {
-                       if test.bitLength != ret.BitLength || bytes.Compare(ret.Bytes, test.out) != 0 {
+                       if test.bitLength != ret.BitLength || !bytes.Equal(ret.Bytes, test.out) {
                                t.Errorf("#%d: Bad result: %v (expected %v %v)", i, ret, test.out, test.bitLength)
                        }
                }
@@ -166,7 +166,7 @@ func TestBitStringRightAlign(t *testing.T) {
        for i, test := range bitStringRightAlignTests {
                bs := BitString{test.in, test.inlen}
                out := bs.RightAlign()
-               if bytes.Compare(out, test.out) != 0 {
+               if !bytes.Equal(out, test.out) {
                        t.Errorf("#%d got: %x want: %x", i, out, test.out)
                }
        }
@@ -477,7 +477,7 @@ func TestRawStructs(t *testing.T) {
        if s.A != 0x50 {
                t.Errorf("bad value for A: got %d want %d", s.A, 0x50)
        }
-       if bytes.Compare([]byte(s.Raw), input) != 0 {
+       if !bytes.Equal([]byte(s.Raw), input) {
                t.Errorf("bad value for Raw: got %x want %x", s.Raw, input)
        }
 }
index 55d34a709a486d239477722a1b97c421542138dd..b4dbe71ef3c7fe6403757395f8571124ed0ef6d1 100644 (file)
@@ -132,7 +132,7 @@ func TestMarshal(t *testing.T) {
                        t.Errorf("#%d failed: %s", i, err)
                }
                out, _ := hex.DecodeString(test.out)
-               if bytes.Compare(out, data) != 0 {
+               if !bytes.Equal(out, data) {
                        t.Errorf("#%d got: %x want %x\n\t%q\n\t%q", i, data, out, data, out)
 
                }
index 456f9eac72485ff0ee5eb476d2e061860c6ebf99..356f590f0278e546a83db2ef62bb4331ab479cf8 100644 (file)
@@ -65,7 +65,7 @@ func TestDecodeString(t *testing.T) {
                        t.Errorf("#%d: unexpected err value: %s", i, err)
                        continue
                }
-               if bytes.Compare(dst, test.dec) != 0 {
+               if !bytes.Equal(dst, test.dec) {
                        t.Errorf("#%d: got: %#v want: #%v", i, dst, test.dec)
                }
        }
index 4f334d1347d741efcfb3d130c04c17ee5b3f2c0c..93055abd1c25ad3d03d53f1d1b60c1230b4384df 100644 (file)
@@ -422,7 +422,7 @@ func TestUnmarshalMarshal(t *testing.T) {
        if err != nil {
                t.Fatalf("Marshal: %v", err)
        }
-       if bytes.Compare(jsonBig, b) != 0 {
+       if !bytes.Equal(jsonBig, b) {
                t.Errorf("Marshal jsonBig")
                diff(t, b, jsonBig)
                return
@@ -474,7 +474,7 @@ func TestLargeByteSlice(t *testing.T) {
        if err := Unmarshal(b, &s1); err != nil {
                t.Fatalf("Unmarshal: %v", err)
        }
-       if bytes.Compare(s0, s1) != 0 {
+       if !bytes.Equal(s0, s1) {
                t.Errorf("Marshal large byte slice")
                diff(t, s0, s1)
        }
index 14d850865a673c3d387cd847ee2720a9503a0bff..adb35715b9670ec65ea69261f06315204a1c1d8b 100644 (file)
@@ -92,7 +92,7 @@ func TestCompactBig(t *testing.T) {
                t.Fatalf("Compact: %v", err)
        }
        b := buf.Bytes()
-       if bytes.Compare(b, jsonBig) != 0 {
+       if !bytes.Equal(b, jsonBig) {
                t.Error("Compact(jsonBig) != jsonBig")
                diff(t, b, jsonBig)
                return
@@ -118,7 +118,7 @@ func TestIndentBig(t *testing.T) {
                t.Fatalf("Indent2: %v", err)
        }
        b1 := buf1.Bytes()
-       if bytes.Compare(b1, b) != 0 {
+       if !bytes.Equal(b1, b) {
                t.Error("Indent(Indent(jsonBig)) != Indent(jsonBig)")
                diff(t, b1, b)
                return
@@ -130,7 +130,7 @@ func TestIndentBig(t *testing.T) {
                t.Fatalf("Compact: %v", err)
        }
        b1 = buf1.Bytes()
-       if bytes.Compare(b1, jsonBig) != 0 {
+       if !bytes.Equal(b1, jsonBig) {
                t.Error("Compact(Indent(jsonBig)) != jsonBig")
                diff(t, b1, jsonBig)
                return
index 2889a0601249d63d86ecc74b03a40843849728bb..0b470b07b2bb9adcd5c82eaaedd49631dda1d910 100644 (file)
@@ -388,10 +388,10 @@ func TestKey(t *testing.T) {
        }
        // Separate generation from testing to ensure buffers are not overwritten.
        for i, tt := range keyTests {
-               if bytes.Compare(keys1[i], tt.out) != 0 {
+               if !bytes.Equal(keys1[i], tt.out) {
                        t.Errorf("%d: Key(%q) = %d; want %d", i, tt.in, keys1[i], tt.out)
                }
-               if bytes.Compare(keys2[i], tt.out) != 0 {
+               if !bytes.Equal(keys2[i], tt.out) {
                        t.Errorf("%d: KeyFromString(%q) = %d; want %d", i, tt.in, keys2[i], tt.out)
                }
        }
index 40bf10ab449d36f6f23c63c0c98bf9b153f2f969..42df613e60f65a7aac62a7e16831898e8f89c328 100644 (file)
@@ -674,7 +674,7 @@ func testCollator(c *collate.Collator) {
        for _, str := range testInput.values() {
                k0 := c0.KeyFromString(&buf, str)
                k := c.KeyFromString(&buf, str)
-               if bytes.Compare(k0, k) != 0 {
+               if !bytes.Equal(k0, k) {
                        failOnError(fmt.Errorf("test:%U: keys differ (%x vs %x)", []rune(str), k0, k))
                }
                buf.Reset()
index f957ede4abffc47ffcea0a48e51edb235464ca9f..8043038b4aefdb37efdc8b1d10bdeb1a225e920c 100644 (file)
@@ -123,7 +123,7 @@ func test(t *testing.T, mode Mode) {
                }
 
                // compare
-               if bytes.Compare(got, want) != 0 {
+               if !bytes.Equal(got, want) {
                        t.Errorf("package %s\n\tgot:\n%s\n\twant:\n%s", pkg.Name, got, want)
                }
        }
index fd6d152b39de8516dd40269085bb691b5656aaf5..6c981e7752e462fc82ecffa167912eec25310737 100644 (file)
@@ -643,7 +643,7 @@ func TestSetBytes(t *testing.T) {
 
 func checkBytes(b []byte) bool {
        b2 := new(Int).SetBytes(b).Bytes()
-       return bytes.Compare(b, b2) == 0
+       return bytes.Equal(b, b2)
 }
 
 func TestBytes(t *testing.T) {