github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/base/comparer_test.go (about)

     1  // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors.
     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 base
    16  
    17  import (
    18  	"fmt"
    19  	"sort"
    20  	"testing"
    21  	"time"
    22  
    23  	"golang.org/x/exp/rand"
    24  )
    25  
    26  func TestDefAppendSeparator(t *testing.T) {
    27  	testCases := []struct {
    28  		a, b, want string
    29  	}{
    30  		{"black", "blue", "blb"},
    31  		{"green", "", "green"},
    32  		{"", "2", ""},
    33  		{"1", "2", "1"},
    34  		{"1", "29", "2"},
    35  		{"13", "19", "14"},
    36  		{"13", "99", "2"},
    37  		{"135", "19", "14"},
    38  		{"1357", "19", "14"},
    39  		{"1357", "2", "14"},
    40  		{"13\xff", "14", "13\xff"},
    41  		{"13\xff", "19", "14"},
    42  		{"1\xff\xff", "19", "1\xff\xff"},
    43  		{"1\xff\xff", "2", "1\xff\xff"},
    44  		{"1\xff\xff", "9", "2"},
    45  		{"", "", ""},
    46  		{"1", "", "1"},
    47  		{"11", "", "11"},
    48  		{"11\xff", "", "11\xff"},
    49  		{"1\xff", "", "1\xff"},
    50  		{"1\xff\xff", "", "1\xff\xff"},
    51  		{"\xff", "", "\xff"},
    52  		{"\xff\xff", "", "\xff\xff"},
    53  		{"\xff\xff\xff", "", "\xff\xff\xff"},
    54  	}
    55  	for _, tc := range testCases {
    56  		t.Run("", func(t *testing.T) {
    57  			got := string(DefaultComparer.Separator(nil, []byte(tc.a), []byte(tc.b)))
    58  			if got != tc.want {
    59  				t.Errorf("a, b = %q, %q: got %q, want %q", tc.a, tc.b, got, tc.want)
    60  			}
    61  		})
    62  	}
    63  }
    64  
    65  func TestAbbreviatedKey(t *testing.T) {
    66  	rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano())))
    67  	randBytes := func(size int) []byte {
    68  		data := make([]byte, size)
    69  		for i := range data {
    70  			data[i] = byte(rng.Int() & 0xff)
    71  		}
    72  		return data
    73  	}
    74  
    75  	keys := make([][]byte, 10000)
    76  	for i := range keys {
    77  		keys[i] = randBytes(rng.Intn(16))
    78  	}
    79  	sort.Slice(keys, func(i, j int) bool {
    80  		return DefaultComparer.Compare(keys[i], keys[j]) < 0
    81  	})
    82  
    83  	for i := 1; i < len(keys); i++ {
    84  		last := DefaultComparer.AbbreviatedKey(keys[i-1])
    85  		cur := DefaultComparer.AbbreviatedKey(keys[i])
    86  		cmp := DefaultComparer.Compare(keys[i-1], keys[i])
    87  		if cmp == 0 {
    88  			if last != cur {
    89  				t.Fatalf("expected equal abbreviated keys: %x[%x] != %x[%x]",
    90  					last, keys[i-1], cur, keys[i])
    91  			}
    92  		} else {
    93  			if last > cur {
    94  				t.Fatalf("unexpected abbreviated key ordering: %x[%x] > %x[%x]",
    95  					last, keys[i-1], cur, keys[i])
    96  			}
    97  		}
    98  	}
    99  }
   100  
   101  func BenchmarkAbbreviatedKey(b *testing.B) {
   102  	rng := rand.New(rand.NewSource(1449168817))
   103  	randBytes := func(size int) []byte {
   104  		data := make([]byte, size)
   105  		for i := range data {
   106  			data[i] = byte(rng.Int() & 0xff)
   107  		}
   108  		return data
   109  	}
   110  	keys := make([][]byte, 10000)
   111  	for i := range keys {
   112  		keys[i] = randBytes(8)
   113  	}
   114  
   115  	b.ResetTimer()
   116  	var sum uint64
   117  	for i := 0; i < b.N; i++ {
   118  		j := i % len(keys)
   119  		sum += DefaultComparer.AbbreviatedKey(keys[j])
   120  	}
   121  
   122  	if testing.Verbose() {
   123  		fmt.Println(sum)
   124  	}
   125  }