本文整理汇总了Golang中bytes.Compare函数的典型用法代码示例。如果您正苦于以下问题:Golang Compare函数的具体用法?Golang Compare怎么用?Golang Compare使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。

示例1: Less

func (self Remote) Less(other Remote) bool {
    val := bytes.Compare(self.Pos, other.Pos)
    if val == 0 {
        val = bytes.Compare([]byte(self.Addr), []byte(other.Addr))
    }
    return val < 0
}

开发者ID:vaudoc,项目名称:god,代码行数:7,代码来源:remote.go

示例2: Less

func (suts *SortableUiTaskSlice) Less(i, j int) bool {

    taskOne := suts.tasks[i]
    taskTwo := suts.tasks[j]

    displayNameOne := taskOne.Task.DisplayName
    displayNameTwo := taskTwo.Task.DisplayName

    if displayNameOne == evergreen.CompileStage {
        return true
    }
    if displayNameTwo == evergreen.CompileStage {
        return false
    }
    if displayNameOne == evergreen.PushStage {
        return false
    }
    if displayNameTwo == evergreen.PushStage {
        return true
    }

    if bytes.Compare([]byte(displayNameOne), []byte(displayNameTwo)) == -1 {
        return true
    }
    if bytes.Compare([]byte(displayNameOne), []byte(displayNameTwo)) == 1 {
        return false
    }
    return false
}

开发者ID:markbenvenuto,项目名称:evergreen,代码行数:29,代码来源:models.go

示例3: authorizeKey

func (ctrl *Controller) authorizeKey(conn ssh.ConnMetadata, key ssh.PublicKey) (
    *ssh.Permissions, error) {

    marshaledKey := key.Marshal()
    for _, authorizedKey := range ctrl.authorizedKeys {
        if bytes.Compare(authorizedKey.Marshal(), marshaledKey) == 0 {
            return &ssh.Permissions{}, nil
        }
    }

    nodes, err := ctrl.cluster.GetDir("console/authorized_keys")
    if err != nil {
        if err == cluster.ErrNotFound {
            return nil, fmt.Errorf("unauthorized")
        }
        return nil, err
    }

    for path, value := range nodes {
        key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(value))
        if err != nil {
            fmt.Printf("bad authorized key from etcd: %s: %s\n", path, err)
        }
        if bytes.Compare(key.Marshal(), marshaledKey) == 0 {
            return &ssh.Permissions{}, nil
        }
    }

    return nil, fmt.Errorf("unauthorized")
}

开发者ID:logan,项目名称:heim,代码行数:30,代码来源:server.go

示例4: Compare

func (oc OfflineComparer) Compare(a, b []byte) int {
    p1, p2, p3 := oc.Split(a)
    p4, p5, p6 := oc.Split(b)

    if p1 == nil || p4 == nil {
        log.Infof("can't find seperate, a:%s b:%s compare bytes...\n", string(a), string(b))
        return bytes.Compare(a, b)
    }

    r1 := bytes.Compare(p1, p4)
    if r1 != 0 {
        return r1
    }

    r2 := bytes.Compare(p2, p5)
    if r2 != 0 {
        return r2
    }

    v1, err1 := strconv.ParseInt(string(p3), 10, 64)
    v2, err2 := strconv.ParseInt(string(p6), 10, 64)
    if err1 != nil || err2 != nil {
        log.Infof("parse int err, a:%s b:%s compare bytes...\n", string(a), string(b))
        return bytes.Compare(p3, p6)
    }

    if v1 < v2 {
        return -1
    } else if v1 == v2 {
        return 0
    } else {
        return 1
    }
}

开发者ID:reasonz,项目名称:im_service,代码行数:34,代码来源:comparer.go

示例5: Less

func (p byKeyValue) Less(i, j int) bool {
    sgn := bytes.Compare(p[i].key, p[j].key)
    if sgn == 0 {
        sgn = bytes.Compare(p[i].value, p[j].value)
    }
    return sgn < 0
}

开发者ID:freeformz,项目名称:go-oauth,代码行数:7,代码来源:oauth.go

示例6: updatePointTimeAndSequence

// depending on the query order (whether it's ascending or not) returns
// the min (or max in case of descending query) of the current
// [timestamp,sequence] and the self's [timestamp,sequence]
//
// This is used to determine what the next point's timestamp
// and sequence number should be.
func (self *rawColumnValue) updatePointTimeAndSequence(currentTimeRaw, currentSequenceRaw []byte, isAscendingQuery bool) ([]byte, []byte) {
    if currentTimeRaw == nil {
        return self.time, self.sequence
    }

    compareValue := 1
    if isAscendingQuery {
        compareValue = -1
    }

    timeCompare := bytes.Compare(self.time, currentTimeRaw)
    if timeCompare == compareValue {
        return self.time, self.sequence
    }

    if timeCompare != 0 {
        return currentTimeRaw, currentSequenceRaw
    }

    if bytes.Compare(self.sequence, currentSequenceRaw) == compareValue {
        return currentTimeRaw, self.sequence
    }

    return currentTimeRaw, currentSequenceRaw
}

开发者ID:neurodrone,项目名称:influxdb,代码行数:31,代码来源:shard_datastore.go

示例7: testBasicEncodeDecode32

func testBasicEncodeDecode32(encFunc func([]byte, uint32) []byte,
    decFunc func([]byte) ([]byte, uint32, error), descending bool, t *testing.T) {
    testCases := []uint32{
        0, 1,
        1<<8 - 1, 1 << 8,
        1<<16 - 1, 1 << 16,
        1<<24 - 1, 1 << 24,
        math.MaxUint32 - 1, math.MaxUint32,
    }

    var lastEnc []byte
    for i, v := range testCases {
        enc := encFunc(nil, v)
        if i > 0 {
            if (descending && bytes.Compare(enc, lastEnc) >= 0) ||
                (!descending && bytes.Compare(enc, lastEnc) < 0) {
                t.Errorf("ordered constraint violated for %d: [% x] vs. [% x]", v, enc, lastEnc)
            }
        }
        b, decode, err := decFunc(enc)
        if err != nil {
            t.Error(err)
            continue
        }
        if len(b) != 0 {
            t.Errorf("leftover bytes: [% x]", b)
        }
        if decode != v {
            t.Errorf("decode yielded different value than input: %d vs. %d", decode, v)
        }
        lastEnc = enc
    }
}

开发者ID:soniabhishek,项目名称:cockroach,代码行数:33,代码来源:encoding_test.go

示例8: Equals

//Equals returns true if r==r2
func (r *Record) Equals(r2 *Record) bool {
    if r.Thread != r2.Thread {
        log.Println("unmatched threadname")
        return false
    }
    if bytes.Compare(r.Sign, r2.Sign) != 0 {
        log.Println("unmatched sign")
        return false
    }
    if !r.Stamp.Equal(r2.Stamp) {
        log.Println("unmatched stamp")
        return false
    }
    if r.self != r2.self {
        log.Println("unmatched self")
        return false
    }
    if len(r.Contents) != len(r2.Contents) {
        log.Println("unmatched contents length")
        return false
    }
    for k, v := range r.Contents {
        v2, exist := r2.Contents[k]
        if !exist || bytes.Compare(v, v2) != 0 {
            log.Println("unmatched contents", k)
            return false
        }
    }
    return true
}

开发者ID:utamaro,项目名称:core,代码行数:31,代码来源:record.go

示例9: TestGetSeedHash

func TestGetSeedHash(t *testing.T) {
    seed0, err := GetSeedHash(0)
    if err != nil {
        t.Errorf("Failed to get seedHash for block 0: %v", err)
    }
    if bytes.Compare(seed0, make([]byte, 32)) != 0 {
        log.Printf("seedHash for block 0 should be 0s, was: %v\n", seed0)
    }
    seed1, err := GetSeedHash(30000)
    if err != nil {
        t.Error(err)
    }

    // From python:
    // > from pyethash import get_seedhash
    // > get_seedhash(30000)
    expectedSeed1, err := hex.DecodeString("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
    if err != nil {
        t.Error(err)
    }

    if bytes.Compare(seed1, expectedSeed1) != 0 {
        log.Printf("seedHash for block 1 should be: %v,\nactual value: %v\n", expectedSeed1, seed1)
    }

}

开发者ID:CedarLogic,项目名称:go-ethereum,代码行数:26,代码来源:ethash_test.go

示例10: Test_Blocks

func Test_Blocks(t *testing.T) {
    byteIt := make(chan byte)
    go func() {
        for _, b := range []byte{67, 85, 70, 134, 87, 38, 85, 194, 119, 50, 6, 18, 6, 103, 38, 246, 246, 66, 7, 118, 134, 242, 7, 38, 86, 22, 198, 199, 146, 6, 182, 230, 247, 119, 50, 7, 118, 134, 87, 38, 82, 6, 134, 151, 50, 7, 70, 247, 118, 86, 194, 6, 151, 50, 16, 236, 17, 236, 17, 236, 17, 236} {
            byteIt <- b
        }
        close(byteIt)
    }()
    vi := &versionInfo{5, Q, 18, 2, 15, 2, 16}

    data := splitToBlocks(byteIt, vi).interleave(vi)
    if bytes.Compare(data, []byte{67, 246, 182, 70, 85, 246, 230, 247, 70, 66, 247, 118, 134, 7, 119, 86, 87, 118, 50, 194, 38, 134, 7, 6, 85, 242, 118, 151, 194, 7, 134, 50, 119, 38, 87, 16, 50, 86, 38, 236, 6, 22, 82, 17, 18, 198, 6, 236, 6, 199, 134, 17, 103, 146, 151, 236, 38, 6, 50, 17, 7, 236, 213, 87, 148, 235, 199, 204, 116, 159, 11, 96, 177, 5, 45, 60, 212, 173, 115, 202, 76, 24, 247, 182, 133, 147, 241, 124, 75, 59, 223, 157, 242, 33, 229, 200, 238, 106, 248, 134, 76, 40, 154, 27, 195, 255, 117, 129, 230, 172, 154, 209, 189, 82, 111, 17, 10, 2, 86, 163, 108, 131, 161, 163, 240, 32, 111, 120, 192, 178, 39, 133, 141, 236}) != 0 {
        t.Fail()
    }

    byteIt2 := make(chan byte)
    go func() {
        for _, b := range []byte{67, 85, 70, 134, 87, 38, 85, 194, 119, 50, 6, 18, 6, 103, 38, 246, 246, 66, 7, 118, 134, 242, 7, 38, 86, 22, 198, 199, 146, 6, 182, 230, 247, 119, 50, 7, 118, 134, 87, 38, 82, 6, 134, 151, 50, 7, 70, 247, 118, 86, 194, 6, 151, 50, 16, 236, 17, 236, 17, 236, 17, 236} {
            byteIt2 <- b
        }
        close(byteIt2)
    }()
    vi = &versionInfo{5, Q, 18, 2, 16, 2, 15}

    data = splitToBlocks(byteIt2, vi).interleave(vi)
    if bytes.Compare(data, []byte{67, 246, 247, 247, 85, 66, 119, 118, 70, 7, 50, 86, 134, 118, 7, 194, 87, 134, 118, 6, 38, 242, 134, 151, 85, 7, 87, 50, 194, 38, 38, 16, 119, 86, 82, 236, 50, 22, 6, 17, 6, 198, 134, 236, 18, 199, 151, 17, 6, 146, 50, 236, 103, 6, 7, 17, 38, 182, 70, 236, 246, 230, 71, 101, 27, 62, 13, 91, 166, 86, 138, 16, 78, 229, 102, 11, 199, 107, 2, 182, 132, 103, 89, 66, 136, 69, 78, 255, 116, 129, 126, 163, 219, 234, 158, 216, 42, 234, 97, 62, 186, 59, 123, 148, 220, 191, 254, 145, 82, 95, 129, 79, 236, 254, 30, 174, 228, 50, 181, 110, 150, 205, 34, 235, 242, 0, 115, 147, 58, 243, 28, 140, 221, 219}) != 0 {
        t.Fail()
    }
}

开发者ID:pid-t,项目名称:go-qr-generation,代码行数:29,代码来源:blocks_test.go

示例11: ldbDropRepo

func ldbDropRepo(db *leveldb.DB, repo []byte) {
    defer runtime.GC()

    snap, err := db.GetSnapshot()
    if err != nil {
        panic(err)
    }
    defer snap.Release()

    // Remove all items related to the given repo from the node->file bucket
    start := []byte{keyTypeNode}
    limit := []byte{keyTypeNode + 1}
    dbi := snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
    for dbi.Next() {
        itemRepo := nodeKeyRepo(dbi.Key())
        if bytes.Compare(repo, itemRepo) == 0 {
            db.Delete(dbi.Key(), nil)
        }
    }
    dbi.Release()

    // Remove all items related to the given repo from the global bucket
    start = []byte{keyTypeGlobal}
    limit = []byte{keyTypeGlobal + 1}
    dbi = snap.NewIterator(&util.Range{Start: start, Limit: limit}, nil)
    for dbi.Next() {
        itemRepo := globalKeyRepo(dbi.Key())
        if bytes.Compare(repo, itemRepo) == 0 {
            db.Delete(dbi.Key(), nil)
        }
    }
    dbi.Release()
}

开发者ID:neuroradiology,项目名称:syncthing,代码行数:33,代码来源:leveldb.go

示例12: TestCreateAttributesMetadataObjectFromCert

func TestCreateAttributesMetadataObjectFromCert(t *testing.T) {
    tcert, preK0, err := loadTCertAndPreK0()
    if err != nil {
        t.Error(err)
    }

    metadata := []byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}
    attributeKeys := []string{"position"}
    metadataObj := CreateAttributesMetadataObjectFromCert(tcert, metadata, preK0, attributeKeys)
    if bytes.Compare(metadataObj.Metadata, metadata) != 0 {
        t.Errorf("Invalid metadata result %v but expected %v", metadataObj.Metadata, metadata)
    }

    entries := metadataObj.GetEntries()
    if len(entries) != 2 {
        t.Errorf("Invalid entries in metadata result %v but expected %v", len(entries), 3)
    }

    firstEntry := entries[0]
    if firstEntry.AttributeName != "position" {
        t.Errorf("Invalid first attribute name, this has to be %v but is %v", "position", firstEntry.AttributeName)
    }
    firstKey, err := GetKForAttribute("position", preK0, tcert)
    if err != nil {
        t.Error(err)
    }

    if bytes.Compare(firstKey, firstEntry.AttributeKey) != 0 {
        t.Errorf("Invalid K for first attribute expected %v but returned %v", firstKey, firstEntry.AttributeKey)
    }
}

开发者ID:hyperledger,项目名称:fabric,代码行数:31,代码来源:attributes_test.go

示例13: TestGetAttributesMetadata

func TestGetAttributesMetadata(t *testing.T) {
    metadata := []byte{255, 255, 255, 255}
    entries := make([]*pb.AttributesMetadataEntry, 1)
    var entry pb.AttributesMetadataEntry
    entry.AttributeName = "position"
    entry.AttributeKey = []byte{0, 0, 0, 0}
    entries[0] = &entry
    attributesMetadata := pb.AttributesMetadata{Metadata: metadata, Entries: entries}
    raw, err := proto.Marshal(&attributesMetadata)
    if err != nil {
        t.Error(err)
    }
    resultMetadata, err := GetAttributesMetadata(raw)
    if err != nil {
        t.Error(err)
    }
    if bytes.Compare(resultMetadata.Metadata, attributesMetadata.Metadata) != 0 {
        t.Fatalf("Invalid metadata expected %v result %v", attributesMetadata.Metadata, resultMetadata.Metadata)
    }
    if resultMetadata.Entries[0].AttributeName != attributesMetadata.Entries[0].AttributeName {
        t.Fatalf("Invalid first entry attribute name expected %v result %v", attributesMetadata.Entries[0].AttributeName, resultMetadata.Entries[0].AttributeName)
    }
    if bytes.Compare(resultMetadata.Entries[0].AttributeKey, attributesMetadata.Entries[0].AttributeKey) != 0 {
        t.Fatalf("Invalid first entry attribute key expected %v result %v", attributesMetadata.Entries[0].AttributeKey, resultMetadata.Entries[0].AttributeKey)
    }
}

开发者ID:hyperledger,项目名称:fabric,代码行数:26,代码来源:attributes_test.go

示例14: TestPutSerialized

func TestPutSerialized(t *testing.T) {
    // Create a volume with I/O serialization enabled.
    v := TempUnixVolume(t, true)
    defer _teardown(v)

    sem := make(chan int)
    go func(sem chan int) {
        err := v.Put(TEST_HASH, TEST_BLOCK)
        if err != nil {
            t.Errorf("err1: %v", err)
        }
        sem <- 1
    }(sem)

    go func(sem chan int) {
        err := v.Put(TEST_HASH_2, TEST_BLOCK_2)
        if err != nil {
            t.Errorf("err2: %v", err)
        }
        sem <- 1
    }(sem)

    go func(sem chan int) {
        err := v.Put(TEST_HASH_3, TEST_BLOCK_3)
        if err != nil {
            t.Errorf("err3: %v", err)
        }
        sem <- 1
    }(sem)

    // Wait for all goroutines to finish
    for done := 0; done < 2; {
        done += <-sem
    }

    // Double check that we actually wrote the blocks we expected to write.
    buf, err := v.Get(TEST_HASH)
    if err != nil {
        t.Errorf("Get #1: %v", err)
    }
    if bytes.Compare(buf, TEST_BLOCK) != 0 {
        t.Errorf("Get #1: expected %s, got %s", string(TEST_BLOCK), string(buf))
    }

    buf, err = v.Get(TEST_HASH_2)
    if err != nil {
        t.Errorf("Get #2: %v", err)
    }
    if bytes.Compare(buf, TEST_BLOCK_2) != 0 {
        t.Errorf("Get #2: expected %s, got %s", string(TEST_BLOCK_2), string(buf))
    }

    buf, err = v.Get(TEST_HASH_3)
    if err != nil {
        t.Errorf("Get #3: %v", err)
    }
    if bytes.Compare(buf, TEST_BLOCK_3) != 0 {
        t.Errorf("Get #3: expected %s, got %s", string(TEST_BLOCK_3), string(buf))
    }
}

开发者ID:ntijanic,项目名称:arvados,代码行数:60,代码来源:volume_unix_test.go

示例15: TestBMPString

func TestBMPString(t *testing.T) {
    str, err := bmpString("")
    if bytes.Compare(str, []byte{0, 0}) != 0 {
        t.Errorf("expected empty string to return double 0, but found: % x", str)
    }
    if err != nil {
        t.Errorf("err: %v", err)
    }

    // Example from https://tools.ietf.org/html/rfc7292#appendix-B
    str, err = bmpString("Beavis")
    if bytes.Compare(str, []byte{0x00, 0x42, 0x00, 0x65, 0x00, 0x61, 0x00, 0x0076, 0x00, 0x69, 0x00, 0x73, 0x00, 0x00}) != 0 {
        t.Errorf("expected 'Beavis' to return 0x00 0x42 0x00 0x65 0x00 0x61 0x00 0x76 0x00 0x69 0x00 0x73 0x00 0x00, but found: % x", str)
    }
    if err != nil {
        t.Errorf("err: %v", err)
    }

    // some characters from the "Letterlike Symbols Unicode block"
    tst := "\u2115 - Double-struck N"
    str, err = bmpString(tst)
    if bytes.Compare(str, []byte{0x21, 0x15, 0x00, 0x20, 0x00, 0x2d, 0x00, 0x20, 0x00, 0x44, 0x00, 0x6f, 0x00, 0x75, 0x00, 0x62, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x2d, 0x00, 0x73, 0x00, 0x74, 0x00, 0x72, 0x00, 0x75, 0x00, 0x63, 0x00, 0x6b, 0x00, 0x20, 0x00, 0x4e, 0x00, 0x00}) != 0 {
        t.Errorf("expected '%s' to return 0x21 0x15 0x00 0x20 0x00 0x2d 0x00 0x20 0x00 0x44 0x00 0x6f 0x00 0x75 0x00 0x62 0x00 0x6c 0x00 0x65 0x00 0x2d 0x00 0x73 0x00 0x74 0x00 0x72 0x00 0x75 0x00 0x63 0x00 0x6b 0x00 0x20 0x00 0x4e 0x00 0x00, but found: % x", tst, str)
    }
    if err != nil {
        t.Errorf("err: %v", err)
    }

    // some character outside the BMP should error
    tst = "\U0001f000 East wind (Mahjong)"
    str, err = bmpString(tst)
    if err == nil {
        t.Errorf("expected '%s' to throw error because the first character is not in the BMP", tst)
    }
}

开发者ID:rnaveiras,项目名称:packer,代码行数:35,代码来源:bmp-string_test.go