github.com/whtcorpsinc/milevadb-prod@v0.0.0-20211104133533-f57f4be3b597/soliton/codec/bytes_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 codec 15 16 import ( 17 . "github.com/whtcorpsinc/check" 18 "github.com/whtcorpsinc/milevadb/soliton/testleak" 19 ) 20 21 var _ = Suite(&testBytesSuite{}) 22 23 type testBytesSuite struct { 24 } 25 26 func (s *testBytesSuite) TestFastSlowFastReverse(c *C) { 27 if !supportsUnaligned { 28 return 29 } 30 b := []byte{1, 2, 3, 4, 5, 6, 7, 8, 255, 0, 0, 0, 0, 0, 0, 0, 0, 247} 31 r1 := b 32 fastReverseBytes(b) 33 r2 := b 34 reverseBytes(r2) 35 c.Assert(r1, BytesEquals, r2) 36 } 37 38 func (s *testBytesSuite) TestBytesCodec(c *C) { 39 defer testleak.AfterTest(c)() 40 inputs := []struct { 41 enc []byte 42 dec []byte 43 desc bool 44 }{ 45 {[]byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 247}, false}, 46 {[]byte{}, []byte{255, 255, 255, 255, 255, 255, 255, 255, 8}, true}, 47 {[]byte{0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 248}, false}, 48 {[]byte{0}, []byte{255, 255, 255, 255, 255, 255, 255, 255, 7}, true}, 49 {[]byte{1, 2, 3}, []byte{1, 2, 3, 0, 0, 0, 0, 0, 250}, false}, 50 {[]byte{1, 2, 3}, []byte{254, 253, 252, 255, 255, 255, 255, 255, 5}, true}, 51 {[]byte{1, 2, 3, 0}, []byte{1, 2, 3, 0, 0, 0, 0, 0, 251}, false}, 52 {[]byte{1, 2, 3, 0}, []byte{254, 253, 252, 255, 255, 255, 255, 255, 4}, true}, 53 {[]byte{1, 2, 3, 4, 5, 6, 7}, []byte{1, 2, 3, 4, 5, 6, 7, 0, 254}, false}, 54 {[]byte{1, 2, 3, 4, 5, 6, 7}, []byte{254, 253, 252, 251, 250, 249, 248, 255, 1}, true}, 55 {[]byte{0, 0, 0, 0, 0, 0, 0, 0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 0, 0, 0, 0, 0, 0, 247}, false}, 56 {[]byte{0, 0, 0, 0, 0, 0, 0, 0}, []byte{255, 255, 255, 255, 255, 255, 255, 255, 0, 255, 255, 255, 255, 255, 255, 255, 255, 8}, true}, 57 {[]byte{1, 2, 3, 4, 5, 6, 7, 8}, []byte{1, 2, 3, 4, 5, 6, 7, 8, 255, 0, 0, 0, 0, 0, 0, 0, 0, 247}, false}, 58 {[]byte{1, 2, 3, 4, 5, 6, 7, 8}, []byte{254, 253, 252, 251, 250, 249, 248, 247, 0, 255, 255, 255, 255, 255, 255, 255, 255, 8}, true}, 59 {[]byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{1, 2, 3, 4, 5, 6, 7, 8, 255, 9, 0, 0, 0, 0, 0, 0, 0, 248}, false}, 60 {[]byte{1, 2, 3, 4, 5, 6, 7, 8, 9}, []byte{254, 253, 252, 251, 250, 249, 248, 247, 0, 246, 255, 255, 255, 255, 255, 255, 255, 7}, true}, 61 } 62 63 for _, input := range inputs { 64 65 c.Assert(EncodedBytesLength(len(input.enc)), Equals, len(input.dec)) 66 67 if input.desc { 68 b := EncodeBytesDesc(nil, input.enc) 69 c.Assert(b, BytesEquals, input.dec) 70 _, d, err := DecodeBytesDesc(b, nil) 71 c.Assert(err, IsNil) 72 c.Assert(d, BytesEquals, input.enc) 73 } else { 74 b := EncodeBytes(nil, input.enc) 75 c.Assert(b, BytesEquals, input.dec) 76 _, d, err := DecodeBytes(b, nil) 77 c.Assert(err, IsNil) 78 c.Assert(d, BytesEquals, input.enc) 79 } 80 } 81 82 // Test error decode. 83 errInputs := [][]byte{ 84 {1, 2, 3, 4}, 85 {0, 0, 0, 0, 0, 0, 0, 247}, 86 {0, 0, 0, 0, 0, 0, 0, 0, 246}, 87 {0, 0, 0, 0, 0, 0, 0, 1, 247}, 88 {1, 2, 3, 4, 5, 6, 7, 8, 0}, 89 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1}, 90 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1, 2, 3, 4, 5, 6, 7, 8}, 91 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1, 2, 3, 4, 5, 6, 7, 8, 255}, 92 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1, 2, 3, 4, 5, 6, 7, 8, 0}, 93 } 94 95 for _, input := range errInputs { 96 _, _, err := DecodeBytes(input, nil) 97 c.Assert(err, NotNil) 98 } 99 }