github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/codec/codec_test.go (about) 1 // Copyright 2022 zGraph Authors. All rights reserved. 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package codec 16 17 import ( 18 "bytes" 19 "math" 20 "testing" 21 22 "github.com/stretchr/testify/require" 23 ) 24 25 func TestFastSlowFastReverse(t *testing.T) { 26 if !supportsUnaligned { 27 return 28 } 29 b := []byte{1, 2, 3, 4, 5, 6, 7, 8, 255, 0, 0, 0, 0, 0, 0, 0, 0, 247} 30 r1 := b 31 fastReverseBytes(b) 32 r2 := b 33 reverseBytes(r2) 34 require.Equal(t, r1, r2) 35 } 36 37 func TestBytesCodec(t *testing.T) { 38 inputs := []struct { 39 enc []byte 40 dec []byte 41 desc bool 42 }{ 43 {[]byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 247}, false}, 44 {[]byte{}, []byte{255, 255, 255, 255, 255, 255, 255, 255, 8}, true}, 45 {[]byte{0}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 248}, false}, 46 {[]byte{0}, []byte{255, 255, 255, 255, 255, 255, 255, 255, 7}, true}, 47 {[]byte{1, 2, 3}, []byte{1, 2, 3, 0, 0, 0, 0, 0, 250}, false}, 48 {[]byte{1, 2, 3}, []byte{254, 253, 252, 255, 255, 255, 255, 255, 5}, true}, 49 {[]byte{1, 2, 3, 0}, []byte{1, 2, 3, 0, 0, 0, 0, 0, 251}, false}, 50 {[]byte{1, 2, 3, 0}, []byte{254, 253, 252, 255, 255, 255, 255, 255, 4}, true}, 51 {[]byte{1, 2, 3, 4, 5, 6, 7}, []byte{1, 2, 3, 4, 5, 6, 7, 0, 254}, false}, 52 {[]byte{1, 2, 3, 4, 5, 6, 7}, []byte{254, 253, 252, 251, 250, 249, 248, 255, 1}, true}, 53 {[]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}, 54 {[]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}, 55 {[]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}, 56 {[]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}, 57 {[]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}, 58 {[]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}, 59 } 60 61 for _, input := range inputs { 62 require.Len(t, input.dec, EncodedBytesLength(len(input.enc))) 63 64 if input.desc { 65 b := EncodeBytesDesc(nil, input.enc) 66 require.Equal(t, input.dec, b) 67 68 _, d, err := DecodeBytesDesc(b, nil) 69 require.NoError(t, err) 70 require.Equal(t, input.enc, d) 71 } else { 72 b := EncodeBytes(nil, input.enc) 73 require.Equal(t, input.dec, b) 74 75 _, d, err := DecodeBytes(b, nil) 76 require.NoError(t, err) 77 require.Equal(t, input.enc, d) 78 } 79 } 80 81 // Test error decode. 82 errInputs := [][]byte{ 83 {1, 2, 3, 4}, 84 {0, 0, 0, 0, 0, 0, 0, 247}, 85 {0, 0, 0, 0, 0, 0, 0, 0, 246}, 86 {0, 0, 0, 0, 0, 0, 0, 1, 247}, 87 {1, 2, 3, 4, 5, 6, 7, 8, 0}, 88 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1}, 89 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1, 2, 3, 4, 5, 6, 7, 8}, 90 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1, 2, 3, 4, 5, 6, 7, 8, 255}, 91 {1, 2, 3, 4, 5, 6, 7, 8, 255, 1, 2, 3, 4, 5, 6, 7, 8, 0}, 92 } 93 94 for _, input := range errInputs { 95 _, _, err := DecodeBytes(input, nil) 96 require.Error(t, err) 97 } 98 } 99 100 func TestBytesCodecExt(t *testing.T) { 101 inputs := []struct { 102 enc []byte 103 dec []byte 104 }{ 105 {[]byte{}, []byte{0, 0, 0, 0, 0, 0, 0, 0, 247}}, 106 {[]byte{1, 2, 3}, []byte{1, 2, 3, 0, 0, 0, 0, 0, 250}}, 107 {[]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}}, 108 } 109 110 // `assertEqual` is to deal with test case for `[]byte{}` & `[]byte(nil)`. 111 assertEqual := func(expected []byte, acutal []byte) { 112 require.Equal(t, len(expected), len(acutal)) 113 for i := range expected { 114 require.Equal(t, expected[i], acutal[i]) 115 } 116 } 117 118 for _, input := range inputs { 119 assertEqual(input.enc, EncodeBytesExt(nil, input.enc, true)) 120 assertEqual(input.dec, EncodeBytesExt(nil, input.enc, false)) 121 } 122 } 123 124 func TestFloatCodec(t *testing.T) { 125 tblFloat := []float64{ 126 -1, 127 0, 128 1, 129 math.MaxFloat64, 130 math.MaxFloat32, 131 math.SmallestNonzeroFloat32, 132 math.SmallestNonzeroFloat64, 133 math.Inf(-1), 134 math.Inf(1), 135 } 136 137 for _, floatNum := range tblFloat { 138 b := EncodeFloat(nil, floatNum) 139 _, v, err := DecodeFloat(b) 140 require.NoError(t, err) 141 require.Equal(t, floatNum, v) 142 143 b = EncodeFloatDesc(nil, floatNum) 144 _, v, err = DecodeFloatDesc(b) 145 require.NoError(t, err) 146 require.Equal(t, floatNum, v) 147 } 148 149 tblCmp := []struct { 150 Arg1 float64 151 Arg2 float64 152 Ret int 153 }{ 154 {1, -1, 1}, 155 {1, 0, 1}, 156 {0, -1, 1}, 157 {0, 0, 0}, 158 {math.MaxFloat64, 1, 1}, 159 {math.MaxFloat32, math.MaxFloat64, -1}, 160 {math.MaxFloat64, 0, 1}, 161 {math.MaxFloat64, math.SmallestNonzeroFloat64, 1}, 162 {math.Inf(-1), 0, -1}, 163 {math.Inf(1), 0, 1}, 164 {math.Inf(-1), math.Inf(1), -1}, 165 } 166 167 for _, floatNums := range tblCmp { 168 b1 := EncodeFloat(nil, floatNums.Arg1) 169 b2 := EncodeFloat(nil, floatNums.Arg2) 170 171 ret := bytes.Compare(b1, b2) 172 require.Equal(t, floatNums.Ret, ret) 173 174 b1 = EncodeFloatDesc(nil, floatNums.Arg1) 175 b2 = EncodeFloatDesc(nil, floatNums.Arg2) 176 177 ret = bytes.Compare(b1, b2) 178 require.Equal(t, -floatNums.Ret, ret) 179 } 180 }