github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/spanz/span_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     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  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package spanz
    15  
    16  import (
    17  	"bytes"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tidb/pkg/tablecodec"
    21  	"github.com/pingcap/tiflow/cdc/processor/tablepb"
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestStartCompare(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	tests := []struct {
    29  		lhs []byte
    30  		rhs []byte
    31  		res int
    32  	}{
    33  		{nil, nil, 0},
    34  		{nil, []byte{}, 0},
    35  		{[]byte{}, nil, 0},
    36  		{[]byte{}, []byte{}, 0},
    37  		{[]byte{1}, []byte{2}, -1},
    38  		{[]byte{2}, []byte{1}, 1},
    39  		{[]byte{3}, []byte{3}, 0},
    40  	}
    41  
    42  	for _, test := range tests {
    43  		require.Equal(t, test.res, StartCompare(test.lhs, test.rhs))
    44  	}
    45  }
    46  
    47  func TestEndCompare(t *testing.T) {
    48  	t.Parallel()
    49  
    50  	tests := []struct {
    51  		lhs []byte
    52  		rhs []byte
    53  		res int
    54  	}{
    55  		{nil, nil, 0},
    56  		{nil, []byte{}, 0},
    57  		{[]byte{}, nil, 0},
    58  		{[]byte{}, []byte{}, 0},
    59  		{[]byte{1}, []byte{2}, -1},
    60  		{[]byte{2}, []byte{1}, 1},
    61  		{[]byte{3}, []byte{3}, 0},
    62  	}
    63  
    64  	for _, test := range tests {
    65  		require.Equal(t, test.res, EndCompare(test.lhs, test.rhs))
    66  	}
    67  }
    68  
    69  func TestIntersect(t *testing.T) {
    70  	t.Parallel()
    71  
    72  	tests := []struct {
    73  		lhs tablepb.Span
    74  		rhs tablepb.Span
    75  		// Set nil for non-intersect
    76  		res *tablepb.Span
    77  	}{
    78  		{
    79  			lhs: tablepb.Span{StartKey: nil, EndKey: []byte{1}},
    80  			rhs: tablepb.Span{StartKey: []byte{1}, EndKey: nil},
    81  			res: nil,
    82  		},
    83  		{
    84  			lhs: tablepb.Span{StartKey: nil, EndKey: nil},
    85  			rhs: tablepb.Span{StartKey: nil, EndKey: nil},
    86  			res: &tablepb.Span{StartKey: nil, EndKey: nil},
    87  		},
    88  		{
    89  			lhs: tablepb.Span{StartKey: nil, EndKey: nil},
    90  			rhs: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}},
    91  			res: &tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}},
    92  		},
    93  		{
    94  			lhs: tablepb.Span{StartKey: []byte{0}, EndKey: []byte{3}},
    95  			rhs: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}},
    96  			res: &tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}},
    97  		},
    98  		{
    99  			lhs: tablepb.Span{StartKey: []byte{0}, EndKey: []byte{2}},
   100  			rhs: tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}},
   101  			res: &tablepb.Span{StartKey: []byte{1}, EndKey: []byte{2}},
   102  		},
   103  	}
   104  
   105  	for _, test := range tests {
   106  		t.Logf("running.., %v", test)
   107  		res, err := Intersect(test.lhs, test.rhs)
   108  		if test.res == nil {
   109  			require.NotNil(t, err)
   110  		} else {
   111  			require.Equal(t, *test.res, res)
   112  		}
   113  
   114  		// Swap lhs and rhs, should get the same result
   115  		res2, err2 := Intersect(test.rhs, test.lhs)
   116  		if test.res == nil {
   117  			require.NotNil(t, err2)
   118  		} else {
   119  			require.Equal(t, *test.res, res2)
   120  		}
   121  	}
   122  }
   123  
   124  func TestGetTableRange(t *testing.T) {
   125  	t.Parallel()
   126  
   127  	startKey, endKey := GetTableRange(123)
   128  	require.Equal(t, -1, bytes.Compare(startKey, endKey))
   129  	prefix := []byte(tablecodec.GenTableRecordPrefix(123))
   130  	require.GreaterOrEqual(t, 0, bytes.Compare(startKey, prefix))
   131  	prefix[len(prefix)-1]++
   132  	require.LessOrEqual(t, 0, bytes.Compare(endKey, prefix))
   133  }