github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/logutil/hex_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 logsoliton_test
    15  
    16  import (
    17  	"bytes"
    18  	"encoding/hex"
    19  	"reflect"
    20  
    21  	. "github.com/whtcorpsinc/check"
    22  	"github.com/whtcorpsinc/ekvproto/pkg/spacetimepb"
    23  	"github.com/whtcorpsinc/milevadb/ekv"
    24  	"github.com/whtcorpsinc/milevadb/soliton/logutil"
    25  )
    26  
    27  var _ = Suite(&testHexSuite{})
    28  
    29  type testHexSuite struct{}
    30  
    31  func (s *testHexSuite) SetUpSuite(c *C) {}
    32  
    33  func (s *testHexSuite) SetUpTest(c *C) {}
    34  
    35  func (s *testHexSuite) TestHex(c *C) {
    36  	var region spacetimepb.Region
    37  	region.Id = 6662
    38  	region.StartKey = []byte{'t', 200, '\\', 000, 000, 000, '\\', 000, 000, 000, 37, '-', 000, 000, 000, 000, 000, 000, 000, 37}
    39  	region.EndKey = []byte("3asg3asd")
    40  
    41  	c.Assert(logutil.Hex(&region).String(), Equals, "{Id:6662 StartKey:74c85c0000005c000000252d0000000000000025 EndKey:3361736733617364 RegionEpoch:<nil> Peers:[]}")
    42  }
    43  
    44  func (s *testHexSuite) TestPrettyPrint(c *C) {
    45  	var buf bytes.Buffer
    46  
    47  	byteSlice := []byte("asd2fsdafs中文3af")
    48  	logutil.PrettyPrint(&buf, reflect.ValueOf(byteSlice))
    49  	c.Assert(buf.String(), Equals, "61736432667364616673e4b8ade69687336166")
    50  	c.Assert(buf.String(), Equals, hex.EncodeToString(byteSlice))
    51  	buf.Reset()
    52  
    53  	// Go reflect can't distinguish uint8 from byte!
    54  	intSlice := []uint8{1, 2, 3, uint8('a'), uint8('b'), uint8('c'), uint8('\'')}
    55  	logutil.PrettyPrint(&buf, reflect.ValueOf(intSlice))
    56  	c.Assert(buf.String(), Equals, "01020361626327")
    57  	buf.Reset()
    58  
    59  	var ran ekv.KeyRange
    60  	ran.StartKey = ekv.Key("_txxey23_i263")
    61  	ran.EndKey = nil
    62  	logutil.PrettyPrint(&buf, reflect.ValueOf(ran))
    63  	c.Assert(buf.String(), Equals, "{StartKey:5f747878657932335f69323633 EndKey:}")
    64  }