lib: Use bytes.Equal instead of bytes.Compare where possible

This commit is contained in:
Jakob Borg 2016-03-31 15:12:46 +00:00 committed by Audrius Butkevicius
parent b6f32b6e45
commit f5f0e46016
8 changed files with 21 additions and 21 deletions

View File

@ -497,10 +497,10 @@ func TestCopy(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
if bytes.Compare(bsOrig, bsChanged) == 0 { if bytes.Equal(bsOrig, bsChanged) {
t.Error("Config should have changed") t.Error("Config should have changed")
} }
if bytes.Compare(bsOrig, bsCopy) != 0 { if !bytes.Equal(bsOrig, bsCopy) {
//ioutil.WriteFile("a", bsOrig, 0644) //ioutil.WriteFile("a", bsOrig, 0644)
//ioutil.WriteFile("b", bsCopy, 0644) //ioutil.WriteFile("b", bsCopy, 0644)
t.Error("Copy should be unchanged") t.Error("Copy should be unchanged")

View File

@ -454,7 +454,7 @@ nextFile:
need := false // If we have a lower version of the file need := false // If we have a lower version of the file
var haveVersion protocol.Vector var haveVersion protocol.Vector
for _, v := range vl.versions { for _, v := range vl.versions {
if bytes.Compare(v.device, device) == 0 { if bytes.Equal(v.device, device) {
have = true have = true
haveVersion = v.version haveVersion = v.version
// XXX: This marks Concurrent (i.e. conflicting) changes as // XXX: This marks Concurrent (i.e. conflicting) changes as
@ -550,7 +550,7 @@ func (db *Instance) dropFolder(folder []byte) {
dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil) dbi := t.NewIterator(util.BytesPrefix([]byte{KeyTypeDevice}), nil)
for dbi.Next() { for dbi.Next() {
itemFolder := db.deviceKeyFolder(dbi.Key()) itemFolder := db.deviceKeyFolder(dbi.Key())
if bytes.Compare(folder, itemFolder) == 0 { if bytes.Equal(folder, itemFolder) {
db.Delete(dbi.Key(), nil) db.Delete(dbi.Key(), nil)
} }
} }
@ -560,7 +560,7 @@ func (db *Instance) dropFolder(folder []byte) {
dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil) dbi = t.NewIterator(util.BytesPrefix([]byte{KeyTypeGlobal}), nil)
for dbi.Next() { for dbi.Next() {
itemFolder := db.globalKeyFolder(dbi.Key()) itemFolder := db.globalKeyFolder(dbi.Key())
if bytes.Compare(folder, itemFolder) == 0 { if bytes.Equal(folder, itemFolder) {
db.Delete(dbi.Key(), nil) db.Delete(dbi.Key(), nil)
} }
} }

View File

@ -23,15 +23,15 @@ func TestDeviceKey(t *testing.T) {
key := db.deviceKey(fld, dev, name) key := db.deviceKey(fld, dev, name)
fld2 := db.deviceKeyFolder(key) fld2 := db.deviceKeyFolder(key)
if bytes.Compare(fld2, fld) != 0 { if !bytes.Equal(fld2, fld) {
t.Errorf("wrong folder %q != %q", fld2, fld) t.Errorf("wrong folder %q != %q", fld2, fld)
} }
dev2 := db.deviceKeyDevice(key) dev2 := db.deviceKeyDevice(key)
if bytes.Compare(dev2, dev) != 0 { if !bytes.Equal(dev2, dev) {
t.Errorf("wrong device %q != %q", dev2, dev) t.Errorf("wrong device %q != %q", dev2, dev)
} }
name2 := db.deviceKeyName(key) name2 := db.deviceKeyName(key)
if bytes.Compare(name2, name) != 0 { if !bytes.Equal(name2, name) {
t.Errorf("wrong name %q != %q", name2, name) t.Errorf("wrong name %q != %q", name2, name)
} }
} }
@ -46,11 +46,11 @@ func TestGlobalKey(t *testing.T) {
key := db.globalKey(fld, name) key := db.globalKey(fld, name)
fld2 := db.globalKeyFolder(key) fld2 := db.globalKeyFolder(key)
if bytes.Compare(fld2, fld) != 0 { if !bytes.Equal(fld2, fld) {
t.Errorf("wrong folder %q != %q", fld2, fld) t.Errorf("wrong folder %q != %q", fld2, fld)
} }
name2 := db.globalKeyName(key) name2 := db.globalKeyName(key)
if bytes.Compare(name2, name) != 0 { if !bytes.Equal(name2, name) {
t.Errorf("wrong name %q != %q", name2, name) t.Errorf("wrong name %q != %q", name2, name)
} }
} }

View File

@ -107,7 +107,7 @@ func (t readWriteTransaction) updateGlobal(folder, device []byte, file protocol.
} }
for i := range fl.versions { for i := range fl.versions {
if bytes.Compare(fl.versions[i].device, device) == 0 { if bytes.Equal(fl.versions[i].device, device) {
if fl.versions[i].version.Equal(file.Version) { if fl.versions[i].version.Equal(file.Version) {
// No need to do anything // No need to do anything
return false return false
@ -213,7 +213,7 @@ func (t readWriteTransaction) removeFromGlobal(folder, device, file []byte, glob
removed := false removed := false
for i := range fl.versions { for i := range fl.versions {
if bytes.Compare(fl.versions[i].device, device) == 0 { if bytes.Equal(fl.versions[i].device, device) {
if i == 0 && globalSize != nil { if i == 0 && globalSize != nil {
f, ok := t.getFile(folder, device, file) f, ok := t.getFile(folder, device, file)
if !ok { if !ok {

View File

@ -174,7 +174,7 @@ func (c *localClient) recvAnnouncements(b beacon.Interface) {
l.Debugf("discover: Received local announcement from %s for %s", addr, protocol.DeviceIDFromBytes(pkt.This.ID)) l.Debugf("discover: Received local announcement from %s for %s", addr, protocol.DeviceIDFromBytes(pkt.This.ID))
var newDevice bool var newDevice bool
if bytes.Compare(pkt.This.ID, c.myID[:]) != 0 { if !bytes.Equal(pkt.This.ID, c.myID[:]) {
newDevice = c.registerDevice(addr, pkt.This) newDevice = c.registerDevice(addr, pkt.This)
} }

View File

@ -97,7 +97,7 @@ func TestRequest(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
if bytes.Compare(bs, []byte("foobar")) != 0 { if !bytes.Equal(bs, []byte("foobar")) {
t.Errorf("Incorrect data from request: %q", string(bs)) t.Errorf("Incorrect data from request: %q", string(bs))
} }
@ -407,13 +407,13 @@ func TestClusterConfig(t *testing.T) {
if l := len(r.Devices); l != 2 { if l := len(r.Devices); l != 2 {
t.Errorf("Incorrect number of devices %d != 2", l) t.Errorf("Incorrect number of devices %d != 2", l)
} }
if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 { if id := r.Devices[0].ID; !bytes.Equal(id, device1[:]) {
t.Errorf("Incorrect device ID %x != %x", id, device1) t.Errorf("Incorrect device ID %x != %x", id, device1)
} }
if r.Devices[0].Flags&protocol.FlagIntroducer == 0 { if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
t.Error("Device1 should be flagged as Introducer") t.Error("Device1 should be flagged as Introducer")
} }
if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 { if id := r.Devices[1].ID; !bytes.Equal(id, device2[:]) {
t.Errorf("Incorrect device ID %x != %x", id, device2) t.Errorf("Incorrect device ID %x != %x", id, device2)
} }
if r.Devices[1].Flags&protocol.FlagIntroducer != 0 { if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {
@ -427,13 +427,13 @@ func TestClusterConfig(t *testing.T) {
if l := len(r.Devices); l != 2 { if l := len(r.Devices); l != 2 {
t.Errorf("Incorrect number of devices %d != 2", l) t.Errorf("Incorrect number of devices %d != 2", l)
} }
if id := r.Devices[0].ID; bytes.Compare(id, device1[:]) != 0 { if id := r.Devices[0].ID; !bytes.Equal(id, device1[:]) {
t.Errorf("Incorrect device ID %x != %x", id, device1) t.Errorf("Incorrect device ID %x != %x", id, device1)
} }
if r.Devices[0].Flags&protocol.FlagIntroducer == 0 { if r.Devices[0].Flags&protocol.FlagIntroducer == 0 {
t.Error("Device1 should be flagged as Introducer") t.Error("Device1 should be flagged as Introducer")
} }
if id := r.Devices[1].ID; bytes.Compare(id, device2[:]) != 0 { if id := r.Devices[1].ID; !bytes.Equal(id, device2[:]) {
t.Errorf("Incorrect device ID %x != %x", id, device2) t.Errorf("Incorrect device ID %x != %x", id, device2)
} }
if r.Devices[1].Flags&protocol.FlagIntroducer != 0 { if r.Devices[1].Flags&protocol.FlagIntroducer != 0 {

View File

@ -66,7 +66,7 @@ func (n DeviceID) Compare(other DeviceID) int {
} }
func (n DeviceID) Equals(other DeviceID) bool { func (n DeviceID) Equals(other DeviceID) bool {
return bytes.Compare(n[:], other[:]) == 0 return bytes.Equal(n[:], other[:])
} }
// Short returns an integer representing bits 0-63 of the device ID. // Short returns an integer representing bits 0-63 of the device ID.

View File

@ -107,7 +107,7 @@ func BlockDiff(src, tgt []protocol.BlockInfo) (have, need []protocol.BlockInfo)
} }
for i := range tgt { for i := range tgt {
if i >= len(src) || bytes.Compare(tgt[i].Hash, src[i].Hash) != 0 { if i >= len(src) || !bytes.Equal(tgt[i].Hash, src[i].Hash) {
// Copy differing block // Copy differing block
need = append(need, tgt[i]) need = append(need, tgt[i])
} else { } else {
@ -132,7 +132,7 @@ func Verify(r io.Reader, blocksize int, blocks []protocol.BlockInfo) error {
hash := hf.Sum(nil) hash := hf.Sum(nil)
hf.Reset() hf.Reset()
if bytes.Compare(hash, block.Hash) != 0 { if !bytes.Equal(hash, block.Hash) {
return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i) return fmt.Errorf("hash mismatch %x != %x for block %d", hash, block.Hash, i)
} }
} }