github.com/insolar/vanilla@v0.0.0-20201023172447-248fdf805322/longbits/bytestring_test.go (about) 1 // Copyright 2020 Insolar Network Ltd. 2 // All rights reserved. 3 // This material is licensed under the Insolar License version 1.0, 4 // available at https://github.com/insolar/assured-ledger/blob/master/LICENSE.md. 5 6 package longbits 7 8 import ( 9 "io" 10 "testing" 11 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestByteString_ReadAt(t *testing.T) { 16 for _, tc := range []struct { 17 str ByteString 18 input []byte 19 off int64 20 res int 21 output []byte 22 err error 23 }{ 24 { 25 str: ByteString("123"), 26 input: []byte{0x0, 0x0}, 27 output: []byte("23"), 28 off: 1, 29 res: 2, 30 }, 31 { 32 str: ByteString("123"), 33 input: []byte{0x0, 0x0}, 34 output: []byte{0x0, 0x0}, 35 off: 4, 36 err: io.ErrUnexpectedEOF, 37 }, 38 { 39 str: ByteString("123"), 40 input: []byte{}, 41 output: []byte{}, 42 off: 3, 43 }, 44 { 45 str: ByteString("123"), 46 off: 3, 47 }, 48 } { 49 res, err := tc.str.ReadAt(tc.input, tc.off) 50 require.Equal(t, tc.res, res) 51 require.Equal(t, tc.err, err) 52 require.Equal(t, tc.output, tc.input) 53 } 54 } 55 56 func TestByteString_IsEmpty(t *testing.T) { 57 require.Equal(t, true, ByteString("").IsEmpty()) 58 require.Equal(t, false, ByteString("1").IsEmpty()) 59 } 60 61 func TestFill(t *testing.T) { 62 require.Equal(t, EmptyByteString, Fill(0, 0x1)) 63 require.Equal(t, ByteString("\x01"), Fill(1, 0x1)) 64 require.Equal(t, ByteString("\x00"), Fill(1, 0)) 65 } 66 67 func TestByteString_SearchBit(t *testing.T) { 68 for _, tc := range []struct { 69 ByteString ByteString 70 startAt int 71 bit bool 72 res int 73 panic *string 74 }{ 75 { 76 ByteString: ByteString("\x02"), 77 startAt: 1, 78 res: 2, 79 }, 80 { 81 ByteString: ByteString("\x02"), 82 startAt: 8, 83 res: -1, 84 }, 85 { 86 ByteString: ByteString("\x02\x00"), 87 startAt: 9, 88 res: 9, 89 }, 90 { 91 ByteString: ByteString("\xff\x02"), 92 startAt: 0, 93 res: 8, 94 }, 95 { 96 ByteString: ByteString("\x00\x02"), 97 startAt: 9, 98 bit: true, 99 res: 9, 100 }, 101 { 102 ByteString: ByteString("\x00\x02"), 103 startAt: 0, 104 bit: true, 105 res: 9, 106 }, 107 { 108 ByteString: ByteString("\x00\x00"), 109 startAt: 8, 110 bit: true, 111 res: -1, 112 }, 113 { 114 ByteString: ByteString("\xFF\xFF"), 115 startAt: 0, 116 res: -1, 117 }, 118 { 119 ByteString: ByteString("\x01"), 120 startAt: -1, 121 panic: newString("illegal value"), 122 }, 123 } { 124 if tc.panic != nil { 125 require.PanicsWithValue(t, *tc.panic, func() { tc.ByteString.SearchBit(tc.startAt, tc.bit) }, tc.ByteString) 126 continue 127 } 128 require.Equal(t, tc.res, tc.ByteString.SearchBit(tc.startAt, tc.bit), tc.ByteString) 129 } 130 } 131 132 func TestByteString_BitPos(t *testing.T) { 133 for _, tc := range []struct { 134 ByteString ByteString 135 index int 136 bytePos int 137 bitPos uint8 138 panic *string 139 }{ 140 { 141 ByteString: ByteString("\x02"), 142 index: 1, 143 bytePos: 0, 144 bitPos: 0x1, 145 }, 146 { 147 ByteString: ByteString("\x02"), 148 index: 8, 149 panic: newString("out of bounds"), 150 }, 151 { 152 ByteString: ByteString("\x02\x00"), 153 index: 9, 154 bytePos: 1, 155 bitPos: 0x1, 156 }, 157 { 158 ByteString: ByteString("\xff\x02"), 159 index: 0, 160 bytePos: 0, 161 bitPos: 0, 162 }, 163 { 164 ByteString: ByteString("\x01"), 165 index: -1, 166 panic: newString("illegal value"), 167 }, 168 } { 169 if tc.panic != nil { 170 require.Panics(t, func() { tc.ByteString.BitPos(tc.index) }, tc.ByteString) 171 continue 172 } 173 bytePos, bitPos := tc.ByteString.BitPos(tc.index) 174 require.Equal(t, tc.bytePos, bytePos, tc.ByteString) 175 require.Equal(t, tc.bitPos, bitPos, tc.ByteString) 176 } 177 } 178 179 func TestByteString_CutOutBits64(t *testing.T) { 180 for _, tc := range []struct { 181 ByteString ByteString 182 res Bits64 183 }{ 184 { 185 ByteString: ByteString(""), 186 res: Bits64{}, 187 }, 188 { 189 ByteString: ByteString("\x02"), 190 res: Bits64{0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 191 }, 192 { 193 ByteString: ByteString("\x02\x01\x02\x01\x02\x01\x02\x01\x02\x01"), 194 res: Bits64{0x2, 0x1, 0x2, 0x1, 0x1, 0x2, 0x1, 0x1}, 195 }, 196 } { 197 require.Equal(t, tc.res, tc.ByteString.CutOutBits64()) 198 } 199 } 200 201 func TestByteString_FoldToBits64(t *testing.T) { 202 for _, tc := range []struct { 203 ByteString ByteString 204 res Bits64 205 }{ 206 { 207 ByteString: ByteString(""), 208 res: Bits64{}, 209 }, 210 { 211 ByteString: ByteString("\x02"), 212 res: Bits64{0x2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, 213 }, 214 { 215 ByteString: ByteString("\x03\x03\x02\x01\x02\x01\x02\x01\x02\x01"), 216 res: Bits64{0x1, 0x2, 0x2, 0x1, 0x2, 0x1, 0x2, 0x1}, 217 }, 218 } { 219 require.Equal(t, tc.res, tc.ByteString.FoldToBits64()) 220 } 221 } 222 223 func TestByteString_String(t *testing.T) { 224 for _, tc := range []struct { 225 ByteString ByteString 226 res string 227 }{ 228 { 229 ByteString: ByteString(""), 230 res: "bits[0]0x00000000", 231 }, 232 { 233 ByteString: ByteString("\x02"), 234 res: "bits[8]0x00000002", 235 }, 236 { 237 ByteString: ByteString("\x03\x03\x02\x01\x02\x01\x02\x01\x02\x01"), 238 res: "bits[80]0x102010201020201", 239 }, 240 } { 241 require.Equal(t, tc.res, tc.ByteString.String()) 242 } 243 } 244 245 func TestByteStringEqual(t *testing.T) { 246 require.False(t, EmptyByteString.Equal(nil)) 247 require.True(t, EmptyByteString.Equal(EmptyByteString)) 248 require.True(t, EmptyByteString.Equal(WrapBytes([]byte{}))) 249 require.True(t, WrapStr("abc").Equal(WrapBytes([]byte("abc")))) 250 } 251 252 func TestByteStringFold64Unaligned(t *testing.T) { 253 var b [64 * 3 / 8]byte 254 for i := range b { 255 require.Zero(t, CopyBytes(b[:i+1]).FoldToUint64()) 256 require.Zero(t, CopyBytes(b[:i+1]).FoldToBits64()) 257 } 258 } 259 260 func TestByteStringCutOut64Unaligned(t *testing.T) { 261 var b [64 * 3 / 8]byte 262 for i := range b { 263 require.Zero(t, CopyBytes(b[:i+1]).CutOutUint64()) 264 require.Zero(t, CopyBytes(b[:i+1]).CutOutBits64()) 265 } 266 }