github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/collation_test.go (about)

     1  // Copyright 2020 WHTCORPS INC, 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 memex
    15  
    16  import (
    17  	. "github.com/whtcorpsinc/check"
    18  	"github.com/whtcorpsinc/BerolinaSQL/charset"
    19  	"github.com/whtcorpsinc/BerolinaSQL/allegrosql"
    20  	"github.com/whtcorpsinc/milevadb/types"
    21  	"github.com/whtcorpsinc/milevadb/soliton/chunk"
    22  	"github.com/whtcorpsinc/milevadb/soliton/defCauslate"
    23  	"github.com/whtcorpsinc/milevadb/soliton/mock"
    24  )
    25  
    26  var _ = SerialSuites(&testDefCauslationSuites{})
    27  
    28  type testDefCauslationSuites struct{}
    29  
    30  func (s *testDefCauslationSuites) TestCompareString(c *C) {
    31  	defCauslate.SetNewDefCauslationEnabledForTest(true)
    32  	defer defCauslate.SetNewDefCauslationEnabledForTest(false)
    33  
    34  	c.Assert(types.CompareString("a", "A", "utf8_general_ci"), Equals, 0)
    35  	c.Assert(types.CompareString("À", "A", "utf8_general_ci"), Equals, 0)
    36  	c.Assert(types.CompareString("😜", "😃", "utf8_general_ci"), Equals, 0)
    37  	c.Assert(types.CompareString("a ", "a  ", "utf8_general_ci"), Equals, 0)
    38  	c.Assert(types.CompareString("ß", "s", "utf8_general_ci"), Equals, 0)
    39  	c.Assert(types.CompareString("ß", "ss", "utf8_general_ci"), Not(Equals), 0)
    40  
    41  	c.Assert(types.CompareString("a", "A", "utf8_unicode_ci"), Equals, 0)
    42  	c.Assert(types.CompareString("À", "A", "utf8_unicode_ci"), Equals, 0)
    43  	c.Assert(types.CompareString("😜", "😃", "utf8_unicode_ci"), Equals, 0)
    44  	c.Assert(types.CompareString("a ", "a  ", "utf8_unicode_ci"), Equals, 0)
    45  	c.Assert(types.CompareString("ß", "s", "utf8_unicode_ci"), Not(Equals), 0)
    46  	c.Assert(types.CompareString("ß", "ss", "utf8_unicode_ci"), Equals, 0)
    47  
    48  	c.Assert(types.CompareString("a", "A", "binary"), Not(Equals), 0)
    49  	c.Assert(types.CompareString("À", "A", "binary"), Not(Equals), 0)
    50  	c.Assert(types.CompareString("😜", "😃", "binary"), Not(Equals), 0)
    51  	c.Assert(types.CompareString("a ", "a  ", "binary"), Not(Equals), 0)
    52  
    53  	ctx := mock.NewContext()
    54  	ft := types.NewFieldType(allegrosql.TypeVarString)
    55  	defCaus1 := &DeferredCauset{
    56  		RetType: ft,
    57  		Index:   0,
    58  	}
    59  	defCaus2 := &DeferredCauset{
    60  		RetType: ft,
    61  		Index:   1,
    62  	}
    63  	chk := chunk.NewChunkWithCapacity([]*types.FieldType{ft, ft}, 4)
    64  	chk.DeferredCauset(0).AppendString("a")
    65  	chk.DeferredCauset(1).AppendString("A")
    66  	chk.DeferredCauset(0).AppendString("À")
    67  	chk.DeferredCauset(1).AppendString("A")
    68  	chk.DeferredCauset(0).AppendString("😜")
    69  	chk.DeferredCauset(1).AppendString("😃")
    70  	chk.DeferredCauset(0).AppendString("a ")
    71  	chk.DeferredCauset(1).AppendString("a  ")
    72  	for i := 0; i < 4; i++ {
    73  		v, isNull, err := CompareStringWithDefCauslationInfo(ctx, defCaus1, defCaus2, chk.GetEvent(0), chk.GetEvent(0), "utf8_general_ci")
    74  		c.Assert(err, IsNil)
    75  		c.Assert(isNull, IsFalse)
    76  		c.Assert(v, Equals, int64(0))
    77  	}
    78  }
    79  
    80  func (s *testDefCauslationSuites) TestDeriveDefCauslationFromExprs(c *C) {
    81  	tInt := types.NewFieldType(allegrosql.TypeLonglong)
    82  	tInt.Charset = charset.CharsetBin
    83  	ctx := mock.NewContext()
    84  
    85  	// no string defCausumn
    86  	chs, defCausl := DeriveDefCauslationFromExprs(ctx, newDeferredCausetWithType(0, tInt), newDeferredCausetWithType(0, tInt), newDeferredCausetWithType(0, tInt))
    87  	c.Assert(chs, Equals, charset.CharsetBin)
    88  	c.Assert(defCausl, Equals, charset.DefCauslationBin)
    89  }