github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/regionspan/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 regionspan
    15  
    16  import (
    17  	"testing"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  	"github.com/pingcap/tidb/tablecodec"
    22  )
    23  
    24  type spanSuite struct{}
    25  
    26  var _ = check.Suite(&spanSuite{})
    27  
    28  func Test(t *testing.T) { check.TestingT(t) }
    29  
    30  func (s *spanSuite) TestStartCompare(c *check.C) {
    31  	defer testleak.AfterTest(c)()
    32  	tests := []struct {
    33  		lhs []byte
    34  		rhs []byte
    35  		res int
    36  	}{
    37  		{nil, nil, 0},
    38  		{nil, []byte{}, -1},
    39  		{[]byte{}, nil, 1},
    40  		{[]byte{}, []byte{}, 0},
    41  		{[]byte{1}, []byte{2}, -1},
    42  		{[]byte{2}, []byte{1}, 1},
    43  		{[]byte{3}, []byte{3}, 0},
    44  	}
    45  
    46  	for _, t := range tests {
    47  		c.Assert(StartCompare(t.lhs, t.rhs), check.Equals, t.res)
    48  	}
    49  }
    50  
    51  func (s *spanSuite) TestEndCompare(c *check.C) {
    52  	defer testleak.AfterTest(c)()
    53  	tests := []struct {
    54  		lhs []byte
    55  		rhs []byte
    56  		res int
    57  	}{
    58  		{nil, nil, 0},
    59  		{nil, []byte{}, 1},
    60  		{[]byte{}, nil, -1},
    61  		{[]byte{}, []byte{}, 0},
    62  		{[]byte{1}, []byte{2}, -1},
    63  		{[]byte{2}, []byte{1}, 1},
    64  		{[]byte{3}, []byte{3}, 0},
    65  	}
    66  
    67  	for _, t := range tests {
    68  		c.Assert(EndCompare(t.lhs, t.rhs), check.Equals, t.res)
    69  	}
    70  }
    71  
    72  func (s *spanSuite) TestIntersect(c *check.C) {
    73  	defer testleak.AfterTest(c)()
    74  	tests := []struct {
    75  		lhs ComparableSpan
    76  		rhs ComparableSpan
    77  		// Set nil for non-intersect
    78  		res *ComparableSpan
    79  	}{
    80  		{ComparableSpan{nil, []byte{1}}, ComparableSpan{[]byte{1}, nil}, nil},
    81  		{ComparableSpan{nil, nil}, ComparableSpan{nil, nil}, &ComparableSpan{nil, nil}},
    82  		{ComparableSpan{nil, nil}, ComparableSpan{[]byte{1}, []byte{2}}, &ComparableSpan{[]byte{1}, []byte{2}}},
    83  		{ComparableSpan{[]byte{0}, []byte{3}}, ComparableSpan{[]byte{1}, []byte{2}}, &ComparableSpan{[]byte{1}, []byte{2}}},
    84  		{ComparableSpan{[]byte{0}, []byte{2}}, ComparableSpan{[]byte{1}, []byte{2}}, &ComparableSpan{[]byte{1}, []byte{2}}},
    85  	}
    86  
    87  	for _, t := range tests {
    88  		c.Log("running..", t)
    89  		res, err := Intersect(t.lhs, t.rhs)
    90  		if t.res == nil {
    91  			c.Assert(err, check.NotNil)
    92  		} else {
    93  			c.Assert(res, check.DeepEquals, *t.res)
    94  		}
    95  
    96  		// Swap lhs and rhs, should get the same result
    97  		res2, err2 := Intersect(t.rhs, t.lhs)
    98  		if t.res == nil {
    99  			c.Assert(err2, check.NotNil)
   100  		} else {
   101  			c.Assert(res2, check.DeepEquals, *t.res)
   102  		}
   103  	}
   104  }
   105  
   106  func (s *spanSuite) TestGetTableSpan(c *check.C) {
   107  	defer testleak.AfterTest(c)()
   108  	span := GetTableSpan(123)
   109  	c.Assert(span.Start, check.Less, span.End)
   110  	prefix := []byte(tablecodec.GenTableRecordPrefix(123))
   111  	c.Assert(span.Start, check.GreaterEqual, prefix)
   112  	prefix[len(prefix)-1]++
   113  	c.Assert(span.End, check.LessEqual, prefix)
   114  }
   115  
   116  func (s *spanSuite) TestSpanHack(c *check.C) {
   117  	defer testleak.AfterTest(c)()
   118  	testCases := []struct {
   119  		input  Span
   120  		expect Span
   121  	}{
   122  		{Span{nil, nil}, Span{[]byte{}, UpperBoundKey}},
   123  		{Span{nil, []byte{1}}, Span{[]byte{}, []byte{1}}},
   124  		{Span{[]byte{1}, nil}, Span{[]byte{1}, UpperBoundKey}},
   125  		{Span{[]byte{1}, []byte{2}}, Span{[]byte{1}, []byte{2}}},
   126  		{Span{[]byte{}, []byte{}}, Span{[]byte{}, []byte{}}},
   127  	}
   128  
   129  	for _, tc := range testCases {
   130  		c.Assert(tc.input.Hack(), check.DeepEquals, tc.expect)
   131  	}
   132  }
   133  
   134  func (s *spanSuite) TestSpanClone(c *check.C) {
   135  	defer testleak.AfterTest(c)()
   136  	sp := ComparableSpan{
   137  		Start: []byte{1},
   138  		End:   []byte{2},
   139  	}
   140  	sp2 := sp.Clone()
   141  	c.Assert(sp2.String(), check.Equals, "[01, 02)")
   142  	sp2.End[0] = 9
   143  	c.Assert(sp.String(), check.Equals, "[01, 02)")
   144  	c.Assert(sp2.String(), check.Equals, "[01, 09)")
   145  }