|
| 1 | +/* |
| 2 | +Copyright 2024 The Vitess Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package hex |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | +) |
| 24 | + |
| 25 | +func TestEncodeBytes(t *testing.T) { |
| 26 | + testCases := []struct { |
| 27 | + input []byte |
| 28 | + want []byte |
| 29 | + }{ |
| 30 | + {[]byte{0xAB, 0xCD, 0xEF}, []byte("ABCDEF")}, |
| 31 | + {[]byte{0x01, 0x23, 0x45}, []byte("012345")}, |
| 32 | + } |
| 33 | + |
| 34 | + for _, tCase := range testCases { |
| 35 | + got := EncodeBytes(tCase.input) |
| 36 | + assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func TestEncodeUint(t *testing.T) { |
| 41 | + testCases := []struct { |
| 42 | + input uint64 |
| 43 | + want []byte |
| 44 | + }{ |
| 45 | + {0, []byte("0")}, |
| 46 | + {123, []byte("7B")}, |
| 47 | + {255, []byte("FF")}, |
| 48 | + {4096, []byte("1000")}, |
| 49 | + } |
| 50 | + |
| 51 | + for _, tCase := range testCases { |
| 52 | + got := EncodeUint(tCase.input) |
| 53 | + assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestDecodeUint(t *testing.T) { |
| 58 | + testCases := []struct { |
| 59 | + input uint64 |
| 60 | + want []byte |
| 61 | + }{ |
| 62 | + {0, []byte{0}}, |
| 63 | + {123, []byte{0x01, 0x23}}, |
| 64 | + {255, []byte{0x02, 0x55}}, |
| 65 | + {4096, []byte{0x40, 0x96}}, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tCase := range testCases { |
| 69 | + got := DecodeUint(tCase.input) |
| 70 | + assert.Equalf(t, tCase.want, got, "got %v, want %v", got, tCase.want) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestDecodedLen(t *testing.T) { |
| 75 | + testCases := []struct { |
| 76 | + input []byte |
| 77 | + want int |
| 78 | + }{ |
| 79 | + {[]byte{0}, 1}, |
| 80 | + {[]byte{0x01, 0x23}, 1}, |
| 81 | + {[]byte("ABCDE"), 3}, |
| 82 | + {[]byte("0123456789ABCDEF"), 8}, |
| 83 | + } |
| 84 | + |
| 85 | + for _, tCase := range testCases { |
| 86 | + got := DecodedLen(tCase.input) |
| 87 | + assert.Equalf(t, tCase.want, got, "got %d, want %d", got, tCase.want) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func TestDecodeBytes(t *testing.T) { |
| 92 | + err := DecodeBytes([]byte("testDst"), []byte("1")) |
| 93 | + assert.NoErrorf(t, err, "expected no error, got %v", err) |
| 94 | + |
| 95 | + err = DecodeBytes([]byte("testDst"), []byte("12")) |
| 96 | + assert.NoErrorf(t, err, "expected no error, got %v", err) |
| 97 | + |
| 98 | + // DecodeBytes should return an error for "é" as |
| 99 | + // hex.decode returns an error for non-ASCII characters |
| 100 | + err = DecodeBytes([]byte("testDst"), []byte("é")) |
| 101 | + assert.Errorf(t, err, "expected error to appear for invalid byte, got %v", err) |
| 102 | +} |
0 commit comments