go.etcd.io/etcd@v3.3.27+incompatible/clientv3/namespace/util_test.go (about)

     1  // Copyright 2017 The etcd Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package namespace
    16  
    17  import (
    18  	"bytes"
    19  	"testing"
    20  )
    21  
    22  func TestPrefixInterval(t *testing.T) {
    23  	tests := []struct {
    24  		pfx string
    25  		key []byte
    26  		end []byte
    27  
    28  		wKey []byte
    29  		wEnd []byte
    30  	}{
    31  		// single key
    32  		{
    33  			pfx: "pfx/",
    34  			key: []byte("a"),
    35  
    36  			wKey: []byte("pfx/a"),
    37  		},
    38  		// range
    39  		{
    40  			pfx: "pfx/",
    41  			key: []byte("abc"),
    42  			end: []byte("def"),
    43  
    44  			wKey: []byte("pfx/abc"),
    45  			wEnd: []byte("pfx/def"),
    46  		},
    47  		// one-sided range
    48  		{
    49  			pfx: "pfx/",
    50  			key: []byte("abc"),
    51  			end: []byte{0},
    52  
    53  			wKey: []byte("pfx/abc"),
    54  			wEnd: []byte("pfx0"),
    55  		},
    56  		// one-sided range, end of keyspace
    57  		{
    58  			pfx: "\xff\xff",
    59  			key: []byte("abc"),
    60  			end: []byte{0},
    61  
    62  			wKey: []byte("\xff\xffabc"),
    63  			wEnd: []byte{0},
    64  		},
    65  	}
    66  	for i, tt := range tests {
    67  		pfxKey, pfxEnd := prefixInterval(tt.pfx, tt.key, tt.end)
    68  		if !bytes.Equal(pfxKey, tt.wKey) {
    69  			t.Errorf("#%d: expected key=%q, got key=%q", i, tt.wKey, pfxKey)
    70  		}
    71  		if !bytes.Equal(pfxEnd, tt.wEnd) {
    72  			t.Errorf("#%d: expected end=%q, got end=%q", i, tt.wEnd, pfxEnd)
    73  		}
    74  	}
    75  }