aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2023-09-15 21:27:55 +0300
committerGitHub <noreply@github.com>2023-09-15 21:27:55 +0300
commit70c09627b307a57a94db64b92e461bb8ad1bc861 (patch)
treeee64098e263e04afac513d9a5d60b4c6eabb128a
parentchange readme for LGPL v2.1 (diff)
parentrevamped bitreader; with new functionality and bug fixes (diff)
downloadbitreader-70c09627b307a57a94db64b92e461bb8ad1bc861.tar.gz
bitreader-70c09627b307a57a94db64b92e461bb8ad1bc861.tar.bz2
bitreader-70c09627b307a57a94db64b92e461bb8ad1bc861.zip
Merge pull request #4 from pektezol/v2
revamped bitreader; with new functionality and bug fixes
-rw-r--r--README.md76
-rw-r--r--bitreader.go419
-rw-r--r--bitreader_test.go1575
3 files changed, 1689 insertions, 381 deletions
diff --git a/README.md b/README.md
index f88c216..6264cd4 100644
--- a/README.md
+++ b/README.md
@@ -1,64 +1,72 @@
1# BitReader [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: LGPL 2.1](https://img.shields.io/badge/License-LGPL_v2.1-blue.svg)](https://github.com/pektezol/bitreader/blob/main/LICENSE) [![Go Reference](https://pkg.go.dev/badge/github.com/pektezol/bitreader.svg)](https://pkg.go.dev/github.com/pektezol/bitreader) 1# BitReader [![Go Reference](https://pkg.go.dev/badge/github.com/pektezol/bitreader.svg)](https://pkg.go.dev/github.com/pektezol/bitreader) [![Go Report Card](https://goreportcard.com/badge/github.com/pektezol/bitreader)](https://goreportcard.com/report/github.com/pektezol/bitreader) [![License: LGPL 2.1](https://img.shields.io/badge/License-LGPL_v2.1-blue.svg)](https://github.com/pektezol/bitreader/blob/main/LICENSE)
2A simple bit reader with big/little-endian support for golang.\ 2A simple bit reader with big/little-endian support for golang.
3Reads stream data from an io.Reader; can read from os.File and a byte array with bytes.NewReader(array).\
4Uses bitwise operations.\
5Support reading up to 64 bits at one time.\
6Includes wrapper functions for most used data types.\
7Error checking on all but wrapper functions.
8 3
9## Installation 4## Installation
10```bash 5```bash
11$ go get github.com/pektezol/bitreader 6$ go get github.com/pektezol/bitreader
12``` 7```
13 8
14## Usage 9## Usage Examples
15 10
16```go 11```go
17import "github.com/pektezol/bitreader" 12import "github.com/pektezol/bitreader"
18 13
19// data: io.Reader Data to read from an io stream 14// ioStream: io.Reader Data to read from an io stream
20// le: bool Little-endian(true) or big-endian(false) state 15// byteStream: []byte Data to read from a byte slice
21reader := bitreader.Reader(data, le) 16// littleEndian: bool Little-endian(true) or big-endian(false) state
17reader := bitreader.NewReader(ioStream, le)
18reader := bitreader.NewReaderFromBytes(byteStream, le)
19
20// Fork Reader, Copies Current Reader
21newReader, err := reader.Fork()
22
23// Read Total Number of Bits Left
24bits, err := reader.ReadRemainingBits()
22 25
23// Read First Bit 26// Read First Bit
24state, err := reader.ReadBool() 27state, err := reader.ReadBool()
25 28
26// Skip Bits/Bytes
27err := reader.SkipBits(8)
28err := reader.SkipBytes(4)
29
30// Read Bits/Bytes 29// Read Bits/Bytes
31value, err := reader.ReadBytes(4) // up to 8 bytes
32value, err := reader.ReadBits(64) // up to 64 bits 30value, err := reader.ReadBits(64) // up to 64 bits
31value, err := reader.ReadBytes(8) // up to 8 bytes
33 32
34// Read String 33// Read String
35text, err := reader.ReadString() // null-terminated 34text, err := reader.ReadString() // null-terminated
36text, err := reader.ReadStringLen(256) // length-specified 35text, err := reader.ReadStringLength(256) // length-specified
37 36
38// Read Bits/Bytes into Slice 37// Read Bits/Bytes into Slice
39arr, err := reader.ReadBitsToSlice(128) 38arr, err := reader.ReadBitsToSlice(128)
40arr, err := reader.ReadBytesToSlice(64) 39arr, err := reader.ReadBytesToSlice(64)
41 40
41// Skip Bits/Bytes
42err := reader.SkipBits(8)
43err := reader.SkipBytes(4)
44
42// Wrapper functions 45// Wrapper functions
43text := reader.TryReadString() // string 46state := reader.TryReadBool() // bool
44text := reader.TryReadStringLen(64) // string 47value := reader.TryReadInt1() // uint8
45arr := reader.ReadBitsToSlice(128) // []byte 48value := reader.TryReadUInt8() // uint8
46arr := reader.ReadBytesToSlice(64) // []byte 49value := reader.TryReadSInt8() // int8
47state := reader.TryReadBool() // bool 50value := reader.TryReadUInt16() // uint16
48value := reader.TryReadInt1() // uint8 51value := reader.TryReadSInt16() // int16
49value := reader.TryReadInt8() // uint8 52value := reader.TryReadUInt32() // uint32
50value := reader.TryReadInt16() // uint16 53value := reader.TryReadSInt32() // int32
51value := reader.TryReadInt32() // uint32 54value := reader.TryReadUInt64() // uint64
52value := reader.TryReadInt64() // uint64 55value := reader.TryReadSInt64() // int64
53value := reader.TryReadFloat32() // float32 56value := reader.TryReadFloat32() // float32
54value := reader.TryReadFloat64() // float64 57value := reader.TryReadFloat64() // float64
55value := reader.TryReadBits(64) // uint64 58value := reader.TryReadBits(64) // uint64
56value := reader.TryReadBytes(8) // uint64 59value := reader.TryReadBytes(8) // uint64
60text := reader.TryReadString() // string
61text := reader.TryReadStringLength(64) // string
62arr := reader.TryReadBitsToSlice(1024) // []byte
63arr := reader.TryReadBytesToSlice(128) // []byte
64bits := reader.TryReadRemainingBits() // uint64
57``` 65```
58 66
59## Error Handling 67## Error Handling
60ReadBits(x), ReadBytes(x), ReadBool(), ReadString(), ReadStringLen(x), ReadBitsToSlice(x), ReadBytesToSlice(x), SkipBits(x) and SkipBytes(x) functions returns an error message when they don't work as expected. It is advised to always handle errors. \ 68All ReadXXX(), SkipXXX() and Fork() functions returns an error message when they don't work as expected. It is advised to always handle errors. \
61Wrapper functions, however, only returns the value and panics if an error is encountered. 69Wrapper functions, however, only returns the value and panics if an error is encountered, for the sake of ease of use.
62 70
63## Bug Report / Feature Request 71## Bug Report / Feature Request
64Using [Github Issues](https://github.com/pektezol/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added. 72Using [Github Issues](https://github.com/pektezol/BitReader/issues/new/choose), you can report a bug that you encountered and/or request a feature that you would like to be added.
diff --git a/bitreader.go b/bitreader.go
index 89c3526..d8f9f89 100644
--- a/bitreader.go
+++ b/bitreader.go
@@ -1,46 +1,72 @@
1// BitReader is a simple bit reader with big/little-endian support for golang. 1// BitReader is a simple bit reader with big/little-endian support for golang.
2// It can read stream data from an io.Reader; can read from os.File and a byte array with bytes.NewReader(array).
3// Uses bitwise operations for v2.
4// Supports reading up to 64 bits at one time.
5// Includes wrapper functions for most used data types.
6// Error checking on all but wrapper functions.
7// Thanks to github.com/mlugg for the big help!
8package bitreader 2package bitreader
9 3
10import ( 4import (
5 "bytes"
6 "errors"
11 "fmt" 7 "fmt"
12 "io" 8 "io"
13 "math" 9 "math"
14) 10)
15 11
16// ReaderType is the main structure of our Reader. 12// Reader is the main structure of our Reader.
17// Whenever index == 0, we need to read a new byte from stream into curByte 13// Whenever index == 0, we need to read a new byte from stream into currentByte
18// 14//
19// stream io.Reader The underlying stream we're reading bytes from 15// stream io.Reader The underlying stream we're reading bytes from
20// index uint18 The current index into the byte [0-7] 16// index uint8 The current index into the byte [0-7]
21// curByte byte The byte we're currently reading from 17// currentByte byte The byte we're currently reading from
22// le bool Whether to read in little-endian order 18// le bool Whether to read in little-endian order or not
23type ReaderType struct { 19type Reader struct {
24 stream io.Reader 20 stream io.Reader
25 index uint8 21 index uint8
26 curByte byte 22 currentByte byte
27 le bool 23 littleEndian bool
28} 24}
29 25
30// Reader is the main constructor that creates the ReaderType object 26// NewReader is the main constructor that creates the Reader object
31// with stream data and little-endian state. 27// with stream reader data and little-endian state.
32func Reader(stream io.Reader, le bool) *ReaderType { 28func NewReader(stream io.Reader, littleEndian bool) *Reader {
33 return &ReaderType{ 29 return &Reader{
34 stream: stream, 30 stream: stream,
35 index: 0, 31 index: 0,
36 curByte: 0, // Initial value doesn't matter, it'll be read as soon as we try to read any bits 32 currentByte: 0,
37 le: le, 33 littleEndian: littleEndian,
38 } 34 }
39} 35}
40 36
41// TryReadBool is a wrapper function that gets the state of 1-bit, 37// NewReaderFromBytes is the main constructor that creates the Reader object
42// returns true if 1, false if 0. Panics on error. 38// with stream byte data and little-endian state.
43func (reader *ReaderType) TryReadBool() bool { 39func NewReaderFromBytes(stream []byte, littleEndian bool) *Reader {
40 return &Reader{
41 stream: bytes.NewReader(stream),
42 index: 0,
43 currentByte: 0,
44 littleEndian: littleEndian,
45 }
46}
47
48// Fork is a function that copies the original reader into a new reader
49// with all of its current values.
50func (reader *Reader) Fork() (*Reader, error) {
51 originalIndex := reader.index
52 originalCurrentByte := reader.currentByte
53 byteStream, err := io.ReadAll(reader.stream)
54 if err != nil {
55 return nil, err // Will only happen when there's no memory, lol
56 }
57 reader.stream = bytes.NewReader(byteStream)
58 return &Reader{
59 stream: bytes.NewReader(byteStream),
60 index: uint8(originalIndex),
61 currentByte: originalCurrentByte,
62 littleEndian: reader.littleEndian,
63 }, nil
64}
65
66// TryReadBool is a wrapper function that gets the state of 1-bit.
67//
68// Returns true if 1, false if 0. Panics on overflow.
69func (reader *Reader) TryReadBool() bool {
44 flag, err := reader.ReadBool() 70 flag, err := reader.ReadBool()
45 if err != nil { 71 if err != nil {
46 panic(err) 72 panic(err)
@@ -49,8 +75,9 @@ func (reader *ReaderType) TryReadBool() bool {
49} 75}
50 76
51// TryReadInt1 is a wrapper function that returns the value of 1-bit. 77// TryReadInt1 is a wrapper function that returns the value of 1-bit.
52// Returns type uint8. Panics on error. 78//
53func (reader *ReaderType) TryReadInt1() uint8 { 79// Returns type uint8. Panics on overflow.
80func (reader *Reader) TryReadInt1() uint8 {
54 value, err := reader.ReadBits(1) 81 value, err := reader.ReadBits(1)
55 if err != nil { 82 if err != nil {
56 panic(err) 83 panic(err)
@@ -58,9 +85,10 @@ func (reader *ReaderType) TryReadInt1() uint8 {
58 return uint8(value) 85 return uint8(value)
59} 86}
60 87
61// TryReadInt8 is a wrapper function that returns the value of 8-bits. 88// TryReadUInt8 is a wrapper function that returns the value of 8-bits.
62// Returns uint8. Panics on error. 89//
63func (reader *ReaderType) TryReadInt8() uint8 { 90// Returns uint8. Panics on overflow.
91func (reader *Reader) TryReadUInt8() uint8 {
64 value, err := reader.ReadBits(8) 92 value, err := reader.ReadBits(8)
65 if err != nil { 93 if err != nil {
66 panic(err) 94 panic(err)
@@ -68,9 +96,21 @@ func (reader *ReaderType) TryReadInt8() uint8 {
68 return uint8(value) 96 return uint8(value)
69} 97}
70 98
71// TryReadInt16 is a wrapper function that returns the value of 16-bits. 99// TryReadSInt8 is a wrapper function that returns the value of 8-bits.
72// Returns uint16. Panics on error. 100//
73func (reader *ReaderType) TryReadInt16() uint16 { 101// Returns int8. Panics on overflow.
102func (reader *Reader) TryReadSInt8() int8 {
103 value, err := reader.ReadBits(8)
104 if err != nil {
105 panic(err)
106 }
107 return int8(value)
108}
109
110// TryReadUInt16 is a wrapper function that returns the value of 16-bits.
111//
112// Returns uint16. Panics on overflow.
113func (reader *Reader) TryReadUInt16() uint16 {
74 value, err := reader.ReadBits(16) 114 value, err := reader.ReadBits(16)
75 if err != nil { 115 if err != nil {
76 panic(err) 116 panic(err)
@@ -78,9 +118,21 @@ func (reader *ReaderType) TryReadInt16() uint16 {
78 return uint16(value) 118 return uint16(value)
79} 119}
80 120
81// TryReadInt32 is a wrapper function that returns the value of 32-bits. 121// TryReadSInt16 is a wrapper function that returns the value of 16-bits.
82// Returns uint32. Panics on error. 122//
83func (reader *ReaderType) TryReadInt32() uint32 { 123// Returns uint16. Panics on overflow.
124func (reader *Reader) TryReadSInt16() int16 {
125 value, err := reader.ReadBits(16)
126 if err != nil {
127 panic(err)
128 }
129 return int16(value)
130}
131
132// TryReadUInt32 is a wrapper function that returns the value of 32-bits.
133//
134// Returns uint32. Panics on overflow.
135func (reader *Reader) TryReadUInt32() uint32 {
84 value, err := reader.ReadBits(32) 136 value, err := reader.ReadBits(32)
85 if err != nil { 137 if err != nil {
86 panic(err) 138 panic(err)
@@ -88,9 +140,21 @@ func (reader *ReaderType) TryReadInt32() uint32 {
88 return uint32(value) 140 return uint32(value)
89} 141}
90 142
91// TryReadInt64 is a wrapper function that returns the value of 64-bits. 143// TryReadSInt32 is a wrapper function that returns the value of 32-bits.
92// Returns uint64. Panics on error. 144//
93func (reader *ReaderType) TryReadInt64() uint64 { 145// Returns int32. Panics on overflow.
146func (reader *Reader) TryReadSInt32() int32 {
147 value, err := reader.ReadBits(32)
148 if err != nil {
149 panic(err)
150 }
151 return int32(value)
152}
153
154// TryReadUInt64 is a wrapper function that returns the value of 64-bits.
155//
156// Returns uint64. Panics on overflow.
157func (reader *Reader) TryReadUInt64() uint64 {
94 value, err := reader.ReadBits(64) 158 value, err := reader.ReadBits(64)
95 if err != nil { 159 if err != nil {
96 panic(err) 160 panic(err)
@@ -98,9 +162,21 @@ func (reader *ReaderType) TryReadInt64() uint64 {
98 return value 162 return value
99} 163}
100 164
165// TryReadSInt64 is a wrapper function that returns the value of 64-bits.
166//
167// Returns int64. Panics on overflow.
168func (reader *Reader) TryReadSInt64() int64 {
169 value, err := reader.ReadBits(64)
170 if err != nil {
171 panic(err)
172 }
173 return int64(value)
174}
175
101// TryReadFloat32 is a wrapper function that returns the value of 32-bits. 176// TryReadFloat32 is a wrapper function that returns the value of 32-bits.
102// Returns float32. Panics on error. 177//
103func (reader *ReaderType) TryReadFloat32() float32 { 178// Returns float32. Panics on overflow.
179func (reader *Reader) TryReadFloat32() float32 {
104 value, err := reader.ReadBits(32) 180 value, err := reader.ReadBits(32)
105 if err != nil { 181 if err != nil {
106 panic(err) 182 panic(err)
@@ -109,8 +185,9 @@ func (reader *ReaderType) TryReadFloat32() float32 {
109} 185}
110 186
111// TryReadFloat64 is a wrapper function that returns the value of 64-bits. 187// TryReadFloat64 is a wrapper function that returns the value of 64-bits.
112// Returns float64. Panics on error. 188//
113func (reader *ReaderType) TryReadFloat64() float64 { 189// Returns float64. Panics on overflow.
190func (reader *Reader) TryReadFloat64() float64 {
114 value, err := reader.ReadBits(64) 191 value, err := reader.ReadBits(64)
115 if err != nil { 192 if err != nil {
116 panic(err) 193 panic(err)
@@ -119,8 +196,9 @@ func (reader *ReaderType) TryReadFloat64() float64 {
119} 196}
120 197
121// TryReadBits is a wrapper function that returns the value of bits specified in the parameter. 198// TryReadBits is a wrapper function that returns the value of bits specified in the parameter.
122// Returns uint64. Panics on error. 199//
123func (reader *ReaderType) TryReadBits(bits int) uint64 { 200// Returns uint64. Panics on overflow.
201func (reader *Reader) TryReadBits(bits int) uint64 {
124 value, err := reader.ReadBits(bits) 202 value, err := reader.ReadBits(bits)
125 if err != nil { 203 if err != nil {
126 panic(err) 204 panic(err)
@@ -129,8 +207,9 @@ func (reader *ReaderType) TryReadBits(bits int) uint64 {
129} 207}
130 208
131// TryReadBytes is a wrapper function that returns the value of bits specified in the parameter. 209// TryReadBytes is a wrapper function that returns the value of bits specified in the parameter.
132// Returns uint64. Panics on error. 210//
133func (reader *ReaderType) TryReadBytes(bytes int) uint64 { 211// Returns uint64. Panics on overflow.
212func (reader *Reader) TryReadBytes(bytes int) uint64 {
134 value, err := reader.ReadBytes(bytes) 213 value, err := reader.ReadBytes(bytes)
135 if err != nil { 214 if err != nil {
136 panic(err) 215 panic(err)
@@ -140,21 +219,33 @@ func (reader *ReaderType) TryReadBytes(bytes int) uint64 {
140 219
141// TryReadString is a wrapper function that returns the string 220// TryReadString is a wrapper function that returns the string
142// that is read until it is null-terminated. 221// that is read until it is null-terminated.
143func (reader *ReaderType) TryReadString() string { 222//
144 text, _ := reader.ReadString() 223// Returns string. Panics on overflow.
224func (reader *Reader) TryReadString() string {
225 text, err := reader.ReadString()
226 if err != nil {
227 panic(err)
228 }
145 return text 229 return text
146} 230}
147 231
148// TryReadStringLen is a wrapper function that returns the string 232// TryReadStringLength is a wrapper function that returns the string
149// that is read until the given length is reached or it is null-terminated. 233// that is read until the given length is reached or it is null-terminated.
150func (reader *ReaderType) TryReadStringLen(length int) string { 234//
151 text, _ := reader.ReadStringLen(length) 235// Returns string. Panics on overflow.
236func (reader *Reader) TryReadStringLength(length int) string {
237 text, err := reader.ReadStringLength(length)
238 if err != nil {
239 panic(err)
240 }
152 return text 241 return text
153} 242}
154 243
155// TryReadBytesToSlice is a wrapper function that reads the specified amount of bits 244// TryReadBytesToSlice is a wrapper function that reads the specified amount of bits
156// from the parameter and puts each bit into a slice and returns this slice. 245// from the parameter and puts each bit into a slice and returns this slice.
157func (reader *ReaderType) TryReadBitsToSlice(bits int) []byte { 246//
247// Returns []byte. Panics on overflow.
248func (reader *Reader) TryReadBitsToSlice(bits int) []byte {
158 bytes := (bits / 8) 249 bytes := (bits / 8)
159 if bits%8 != 0 { 250 if bits%8 != 0 {
160 bytes++ 251 bytes++
@@ -189,7 +280,9 @@ func (reader *ReaderType) TryReadBitsToSlice(bits int) []byte {
189 280
190// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes 281// TryReadBytesToSlice is a wrapper function that reads the specified amount of bytes
191// from the parameter and puts each byte into a slice and returns this slice. 282// from the parameter and puts each byte into a slice and returns this slice.
192func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte { 283//
284// Returns []byte. Panics on overflow.
285func (reader *Reader) TryReadBytesToSlice(bytes int) []byte {
193 var out []byte 286 var out []byte
194 for i := 0; i < bytes; i++ { 287 for i := 0; i < bytes; i++ {
195 val, err := reader.ReadBytes(1) 288 val, err := reader.ReadBytes(1)
@@ -201,42 +294,70 @@ func (reader *ReaderType) TryReadBytesToSlice(bytes int) []byte {
201 return out 294 return out
202} 295}
203 296
204// SkipBits is a function that increases Reader index 297// TryReadBytesToSlice is a wrapper function that reads the remaining bits
205// based on given input bits number. 298// left in the stream and returns the count of bits.
299//
300// Returns uint64. Panics on overflow.
301func (reader *Reader) TryReadRemainingBits() uint64 {
302 bits, err := reader.ReadRemainingBits()
303 if err != nil {
304 panic(err)
305 }
306 return bits
307}
308
309// ReadBool is a function that reads one bit and returns the state, error
310// based on the output. Returns the read value in a bool format.
206// 311//
207// Returns an error if there are no remaining bits. 312// Returns an error if there are no remaining bits.
208func (reader *ReaderType) SkipBits(bits int) error { 313func (reader *Reader) ReadBool() (bool, error) {
209 // Read as many raw bytes as we can 314 val, err := reader.readBit()
210 bytes := bits / 8
211 buf := make([]byte, bytes)
212 _, err := reader.stream.Read(buf)
213 if err != nil { 315 if err != nil {
214 return err 316 return false, err
215 } 317 }
216 // The final read byte should be the new current byte 318 return val == 1, nil
217 if bytes > 0 { 319}
218 reader.curByte = buf[bytes-1] 320
321// ReadBits is a function that reads the specified amount of bits
322// from the parameter and returns the value, error
323// based on the output. It can read up to 64 bits. Returns the read
324// value in type uint64.
325//
326// Returns an error if there are no remaining bits.
327func (reader *Reader) ReadBits(bits int) (uint64, error) {
328 if bits < 1 || bits > 64 {
329 return 0, errors.New("ReadBits(bits) ERROR: Bits number should be between 1 and 64")
219 } 330 }
220 // Read the extra bits 331 var val uint64
221 for i := bytes * 8; i < bits; i++ { 332 for i := 0; i < bits; i++ {
222 _, err := reader.readBit() 333 bit, err := reader.readBit()
223 if err != nil { 334 if err != nil {
224 return err 335 return 0, err
336 }
337 if reader.littleEndian {
338 val |= uint64(bit) << i
339 } else {
340 val |= uint64(bit) << (bits - 1 - i)
225 } 341 }
226 } 342 }
227 return nil 343 return val, nil
228} 344}
229 345
230// SkipBytes is a function that increases Reader index 346// ReadBytes is a function that reads the specified amount of bytes
231// based on given input bytes number. 347// from the parameter and returns the value, error
348// based on the output. It can read up to 8 bytes. Returns the read
349// value in type uint64.
232// 350//
233// Returns an error if there are no remaining bits. 351// Returns an error if there are no remaining bits.
234func (reader *ReaderType) SkipBytes(bytes int) error { 352func (reader *Reader) ReadBytes(bytes int) (uint64, error) {
235 err := reader.SkipBits(bytes * 8) 353 if bytes < 1 || bytes > 8 {
354 return 0, errors.New("ReadBytes(bytes) ERROR: Bytes number should be between 1 and 8")
355 }
356 value, err := reader.ReadBits(bytes * 8)
236 if err != nil { 357 if err != nil {
237 return err 358 return 0, err
238 } 359 }
239 return nil 360 return value, nil
240} 361}
241 362
242// ReadString is a function that reads every byte 363// ReadString is a function that reads every byte
@@ -244,7 +365,7 @@ func (reader *ReaderType) SkipBytes(bytes int) error {
244// string that is read until the null-termination. 365// string that is read until the null-termination.
245// 366//
246// Returns an error if there are no remaining bits. 367// Returns an error if there are no remaining bits.
247func (reader *ReaderType) ReadString() (string, error) { 368func (reader *Reader) ReadString() (string, error) {
248 var out string 369 var out string
249 for { 370 for {
250 value, err := reader.ReadBytes(1) 371 value, err := reader.ReadBytes(1)
@@ -259,13 +380,13 @@ func (reader *ReaderType) ReadString() (string, error) {
259 return out, nil 380 return out, nil
260} 381}
261 382
262// ReadStringLen is a function that reads every byte 383// ReadStringLength is a function that reads every byte
263// until the given length, or it is null-terminated (the byte is 0). 384// until the given length, or it is null-terminated (the byte is 0).
264// Returns the string that is read until the lenth or null-termination. 385// Returns the string that is read until the lenth or null-termination.
265// It will skip the remaining bytes if it is null-terminated. 386// It will skip the remaining bytes if it is null-terminated.
266// 387//
267// Returns an error if there are no remaining bits. 388// Returns an error if there are no remaining bits.
268func (reader *ReaderType) ReadStringLen(length int) (string, error) { 389func (reader *Reader) ReadStringLength(length int) (string, error) {
269 var out string 390 var out string
270 for i := 0; i < length; i++ { 391 for i := 0; i < length; i++ {
271 value, err := reader.ReadBytes(1) 392 value, err := reader.ReadBytes(1)
@@ -281,54 +402,11 @@ func (reader *ReaderType) ReadStringLen(length int) (string, error) {
281 return out, nil 402 return out, nil
282} 403}
283 404
284// ReadBits is a function that reads the specified amount of bits
285// from the parameter and returns the value, error
286// based on the output. It can read up to 64 bits. Returns the read
287// value in type uint64.
288//
289// Returns an error if there are no remaining bits.
290func (reader *ReaderType) ReadBits(bits int) (uint64, error) {
291 if bits < 1 || bits > 64 {
292 return 0, fmt.Errorf("ReadBits(bits) ERROR: Bits number should be between 1 and 64.")
293 }
294 var val uint64
295 for i := 0; i < bits; i++ {
296 bit, err := reader.readBit()
297 if err != nil {
298 return 0, err
299 }
300
301 if reader.le {
302 val |= uint64(bit) << i
303 } else {
304 val |= uint64(bit) << (bits - 1 - i)
305 }
306 }
307 return val, nil
308}
309
310// ReadBytes is a function that reads the specified amount of bytes
311// from the parameter and returns the value, error
312// based on the output. It can read up to 8 bytes. Returns the read
313// value in type uint64.
314//
315// Returns an error if there are no remaining bits.
316func (reader *ReaderType) ReadBytes(bytes int) (uint64, error) {
317 if bytes < 1 || bytes > 8 {
318 return 0, fmt.Errorf("ReadBytes(bytes) ERROR: Bytes number should be between 1 and 8.")
319 }
320 value, err := reader.ReadBits(bytes * 8)
321 if err != nil {
322 return 0, err
323 }
324 return value, nil
325}
326
327// ReadBitsToSlice is a function that reads the specified amount of bits 405// ReadBitsToSlice is a function that reads the specified amount of bits
328// from the parameter and puts each bit into a slice and returns this slice. 406// from the parameter and puts each bit into a slice and returns this slice.
329// 407//
330// Returns an error if there are no remaining bits. 408// Returns an error if there are no remaining bits.
331func (reader *ReaderType) ReadBitsToSlice(bits int) ([]byte, error) { 409func (reader *Reader) ReadBitsToSlice(bits int) ([]byte, error) {
332 bytes := (bits / 8) 410 bytes := (bits / 8)
333 if bits%8 != 0 { 411 if bits%8 != 0 {
334 bytes++ 412 bytes++
@@ -365,7 +443,7 @@ func (reader *ReaderType) ReadBitsToSlice(bits int) ([]byte, error) {
365// from the parameter and puts each byte into a slice and returns this slice. 443// from the parameter and puts each byte into a slice and returns this slice.
366// 444//
367// Returns an error if there are no remaining bytes. 445// Returns an error if there are no remaining bytes.
368func (reader *ReaderType) ReadBytesToSlice(bytes int) ([]byte, error) { 446func (reader *Reader) ReadBytesToSlice(bytes int) ([]byte, error) {
369 var out []byte 447 var out []byte
370 for i := 0; i < bytes; i++ { 448 for i := 0; i < bytes; i++ {
371 val, err := reader.ReadBytes(1) 449 val, err := reader.ReadBytes(1)
@@ -377,35 +455,84 @@ func (reader *ReaderType) ReadBytesToSlice(bytes int) ([]byte, error) {
377 return out, nil 455 return out, nil
378} 456}
379 457
380// ReadBool is a function that reads one bit and returns the state, error 458// SkipBits is a function that increases Reader index
381// based on the output. Returns the read value in a bool format. 459// based on given input bits number.
382// 460//
383// Returns an error if there are no remaining bits. 461// Returns an error if there are no remaining bits.
384func (reader *ReaderType) ReadBool() (bool, error) { 462func (reader *Reader) SkipBits(bits int) error {
385 val, err := reader.readBit() 463 // Read as many raw bytes as we can
464 bytes := bits / 8
465 if bytes > 0 {
466 buf := make([]byte, bytes)
467 _, err := reader.stream.Read(buf)
468 if err != nil {
469 return err
470 }
471 // The final read byte should be the new current byte
472 reader.currentByte = buf[bytes-1]
473 }
474 // Read the extra bits
475 for i := bytes * 8; i < bits; i++ {
476 _, err := reader.readBit()
477 if err != nil {
478 return err
479 }
480 }
481 return nil
482}
483
484// SkipBytes is a function that increases Reader index
485// based on given input bytes number.
486//
487// Returns an error if there are no remaining bits.
488func (reader *Reader) SkipBytes(bytes int) error {
489 err := reader.SkipBits(bytes * 8)
386 if err != nil { 490 if err != nil {
387 return false, err 491 return err
388 } 492 }
389 return val == 1, nil 493 return nil
494}
495
496// ReadRemainingBits is a function that reads the total amount of remaining bits in the stream.
497// It first forks the original reader to check this count, so that it does not interfere with the original stream.
498//
499// Returns an error if there are no remaining bits.
500func (reader *Reader) ReadRemainingBits() (uint64, error) {
501 newReader, err := reader.Fork()
502 if err != nil {
503 return 0, err
504 }
505 var bits uint64 = 0
506 for {
507 err := newReader.SkipBits(1)
508 if err != nil {
509 break // EOF
510 }
511 fmt.Printf("%+v\n", newReader)
512 bits++
513 }
514 return bits, nil
390} 515}
391 516
392// readBit is a private function that reads a single bit from the stream. 517// readBit is a private function that reads a single bit from the stream.
393// This is the main function that makes us read stream data. 518// This is the main function that makes us read stream data.
394func (reader *ReaderType) readBit() (uint8, error) { 519func (reader *Reader) readBit() (uint8, error) {
395 if reader.index == 0 { 520 if reader.index == 0 {
396 // Read a byte from stream into curByte 521 // Read a byte from stream into currentByte
397 buf := make([]byte, 1) 522 buffer := make([]byte, 1)
398 _, err := reader.stream.Read(buf) 523 // We are not checking for the n return value from stream.Read, because we are only reading 1 byte at a time.
524 // Meaning if an EOF happens with a 1 byte read, we dont have any extra byte reading anyways.
525 _, err := reader.stream.Read(buffer)
399 if err != nil { 526 if err != nil {
400 return 0, err 527 return 0, err
401 } 528 }
402 reader.curByte = buf[0] 529 reader.currentByte = buffer[0]
403 } 530 }
404 var val bool 531 var val bool
405 if reader.le { 532 if reader.littleEndian {
406 val = (reader.curByte & (1 << reader.index)) != 0 533 val = (reader.currentByte & (1 << reader.index)) != 0
407 } else { 534 } else {
408 val = (reader.curByte & (1 << (7 - reader.index))) != 0 535 val = (reader.currentByte & (1 << (7 - reader.index))) != 0
409 } 536 }
410 reader.index = (reader.index + 1) % 8 537 reader.index = (reader.index + 1) % 8
411 if val { 538 if val {
diff --git a/bitreader_test.go b/bitreader_test.go
index 2926758..acd980e 100644
--- a/bitreader_test.go
+++ b/bitreader_test.go
@@ -1,212 +1,1385 @@
1// BitReader is a simple bit reader with big/little-endian support for golang.
2// %83.2 coerage
1package bitreader 3package bitreader
2 4
3import ( 5import (
4 "bytes" 6 "bytes"
7 "io"
8 "math"
9 "reflect"
5 "testing" 10 "testing"
6) 11)
7 12
8// TODO: Write better unit tests 13func TestNewReader(t *testing.T) {
14 stream := bytes.NewReader([]byte{0x01, 0x02, 0x03})
15 type args struct {
16 stream io.Reader
17 littleEndian bool
18 }
19 tests := []struct {
20 name string
21 args args
22 want *Reader
23 }{
24 {
25 name: "ReaderLE",
26 args: args{
27 stream: stream,
28 littleEndian: true,
29 },
30 want: &Reader{
31 stream: stream,
32 index: 0,
33 currentByte: 0,
34 littleEndian: true,
35 },
36 },
37 {
38 name: "ReaderBE",
39 args: args{
40 stream: stream,
41 littleEndian: false,
42 },
43 want: &Reader{
44 stream: stream,
45 index: 0,
46 currentByte: 0,
47 littleEndian: false,
48 },
49 },
50 }
51 for _, tt := range tests {
52 t.Run(tt.name, func(t *testing.T) {
53 if got := NewReader(tt.args.stream, tt.args.littleEndian); !reflect.DeepEqual(got, tt.want) {
54 t.Errorf("NewReader() = %+v, want %+v", got, tt.want)
55 }
56 })
57 }
58}
59
60func TestNewReaderFromBytes(t *testing.T) {
61 type args struct {
62 stream []byte
63 littleEndian bool
64 }
65 tests := []struct {
66 name string
67 args args
68 want *Reader
69 }{
70 {
71 name: "ReaderLE",
72 args: args{
73 stream: []byte{0x01, 0x02, 0x03},
74 littleEndian: true,
75 },
76 want: &Reader{
77 stream: bytes.NewReader([]byte{0x01, 0x02, 0x03}),
78 index: 0,
79 currentByte: 0,
80 littleEndian: true,
81 },
82 },
83 {
84 name: "ReaderBE",
85 args: args{
86 stream: []byte{0x01, 0x02, 0x03},
87 littleEndian: false,
88 },
89 want: &Reader{
90 stream: bytes.NewReader([]byte{0x01, 0x02, 0x03}),
91 index: 0,
92 currentByte: 0,
93 littleEndian: false,
94 },
95 },
96 }
97 for _, tt := range tests {
98 t.Run(tt.name, func(t *testing.T) {
99 if got := NewReaderFromBytes(tt.args.stream, tt.args.littleEndian); !reflect.DeepEqual(got, tt.want) {
100 t.Errorf("NewReaderFromBytes() = %+v, want %+v", got, tt.want)
101 }
102 })
103 }
104}
105
106func TestReader_Fork(t *testing.T) {
107 stream := bytes.NewReader([]byte{53})
108 tests := []struct {
109 name string
110 reader *Reader
111 want *Reader
112 wantErr bool
113 }{
114 {
115 name: "Fork",
116 reader: &Reader{
117 stream: stream,
118 index: 4,
119 currentByte: 53,
120 littleEndian: false,
121 },
122 want: &Reader{
123 index: 4,
124 currentByte: 53,
125 littleEndian: false,
126 },
127 wantErr: false,
128 },
129 }
130 for _, tt := range tests {
131 t.Run(tt.name, func(t *testing.T) {
132 got, err := tt.reader.Fork()
133 tt.want.stream = got.stream
134 if (err != nil) != tt.wantErr {
135 t.Errorf("Reader.Fork() error = %v, wantErr %v", err, tt.wantErr)
136 return
137 }
138 if !reflect.DeepEqual(got, tt.want) {
139 t.Errorf("Reader.Fork() = %v, want %v", got, tt.want)
140 }
141 })
142 }
143}
144
145func TestReader_TryReadBool(t *testing.T) {
146 tests := []struct {
147 name string
148 reader *Reader
149 want bool
150 }{
151 {
152 name: "ReadBoolTrueLE",
153 reader: &Reader{
154 stream: bytes.NewReader([]byte{0b00000001}),
155 index: 0,
156 currentByte: 0,
157 littleEndian: true,
158 },
159 want: true,
160 },
161 {
162 name: "ReadBoolTrueBE",
163 reader: &Reader{
164 stream: bytes.NewReader([]byte{0b10000000}),
165 index: 0,
166 currentByte: 0,
167 littleEndian: false,
168 },
169 want: true,
170 },
171 {
172 name: "ReadBoolFalseLE",
173 reader: &Reader{
174 stream: bytes.NewReader([]byte{0b00000010}),
175 index: 0,
176 currentByte: 0,
177 littleEndian: true,
178 },
179 want: false,
180 },
181 {
182 name: "ReadBoolFalseBE",
183 reader: &Reader{
184 stream: bytes.NewReader([]byte{0b01000000}),
185 index: 0,
186 currentByte: 0,
187 littleEndian: false,
188 },
189 want: false,
190 },
191 }
192 for _, tt := range tests {
193 t.Run(tt.name, func(t *testing.T) {
194 if got := tt.reader.TryReadBool(); got != tt.want {
195 t.Errorf("Reader.TryReadBool() = %+v, want %+v", got, tt.want)
196 }
197 })
198 }
199}
200
201func TestReader_TryReadInt1(t *testing.T) {
202 tests := []struct {
203 name string
204 reader *Reader
205 want uint8
206 }{
207 {
208 name: "ReadInt1TrueLE",
209 reader: &Reader{
210 stream: bytes.NewReader([]byte{0b00000001}),
211 index: 0,
212 currentByte: 0,
213 littleEndian: true,
214 },
215 want: 0b1,
216 },
217 {
218 name: "ReadInt1TrueBE",
219 reader: &Reader{
220 stream: bytes.NewReader([]byte{0b10000000}),
221 index: 0,
222 currentByte: 0,
223 littleEndian: false,
224 },
225 want: 0b1,
226 },
227 {
228 name: "ReadInt1FalseLE",
229 reader: &Reader{
230 stream: bytes.NewReader([]byte{0b00000010}),
231 index: 0,
232 currentByte: 0,
233 littleEndian: true,
234 },
235 want: 0b0,
236 },
237 {
238 name: "ReadInt1FalseBE",
239 reader: &Reader{
240 stream: bytes.NewReader([]byte{0b01000000}),
241 index: 0,
242 currentByte: 0,
243 littleEndian: false,
244 },
245 want: 0b0,
246 },
247 }
248 for _, tt := range tests {
249 t.Run(tt.name, func(t *testing.T) {
250 if got := tt.reader.TryReadInt1(); got != tt.want {
251 t.Errorf("Reader.TryReadInt1() = %+v, want %+v", got, tt.want)
252 }
253 })
254 }
255}
256
257func TestReader_TryReadUInt8(t *testing.T) {
258 tests := []struct {
259 name string
260 reader *Reader
261 want uint8
262 }{
263 {
264 name: "ReadUInt8LE",
265 reader: &Reader{
266 stream: bytes.NewReader([]byte{202}),
267 index: 0,
268 currentByte: 0,
269 littleEndian: true,
270 },
271 want: 202,
272 },
273 {
274 name: "ReadUInt8BE",
275 reader: &Reader{
276 stream: bytes.NewReader([]byte{202}),
277 index: 0,
278 currentByte: 0,
279 littleEndian: false,
280 },
281 want: 202,
282 },
283 }
284 for _, tt := range tests {
285 t.Run(tt.name, func(t *testing.T) {
286 if got := tt.reader.TryReadUInt8(); got != tt.want {
287 t.Errorf("Reader.TryReadUInt8() = %+v, want %+v", got, tt.want)
288 }
289 })
290 }
291}
292
293func TestReader_TryReadSInt8(t *testing.T) {
294 tests := []struct {
295 name string
296 reader *Reader
297 want int8
298 }{
299 {
300 name: "ReadSInt8LE",
301 reader: &Reader{
302 stream: bytes.NewReader([]byte{202}),
303 index: 0,
304 currentByte: 0,
305 littleEndian: true,
306 },
307 want: -54,
308 },
309 {
310 name: "ReadSInt8BE",
311 reader: &Reader{
312 stream: bytes.NewReader([]byte{202}),
313 index: 0,
314 currentByte: 0,
315 littleEndian: false,
316 },
317 want: -54,
318 },
319 }
320 for _, tt := range tests {
321 t.Run(tt.name, func(t *testing.T) {
322 if got := tt.reader.TryReadSInt8(); got != tt.want {
323 t.Errorf("Reader.TryReadSInt8() = %+v, want %+v", got, tt.want)
324 }
325 })
326 }
327}
328
329func TestReader_TryReadUInt16(t *testing.T) {
330 tests := []struct {
331 name string
332 reader *Reader
333 want uint16
334 }{
335 {
336 name: "ReadUInt16LE",
337 reader: &Reader{
338 stream: bytes.NewReader([]byte{0b10101010, 0b01010101}),
339 index: 0,
340 currentByte: 0,
341 littleEndian: true,
342 },
343 want: 0b0101010110101010,
344 },
345 {
346 name: "ReadUInt16BE",
347 reader: &Reader{
348 stream: bytes.NewReader([]byte{0b10101010, 0b01010101}),
349 index: 0,
350 currentByte: 0,
351 littleEndian: false,
352 },
353 want: 0b1010101001010101,
354 },
355 }
356 for _, tt := range tests {
357 t.Run(tt.name, func(t *testing.T) {
358 if got := tt.reader.TryReadUInt16(); got != tt.want {
359 t.Errorf("Reader.TryReadUInt16() = %+v, want %+v", got, tt.want)
360 }
361 })
362 }
363}
364
365func TestReader_TryReadSInt16(t *testing.T) {
366 tests := []struct {
367 name string
368 reader *Reader
369 want int16
370 }{
371 {
372 name: "ReadSInt16LE",
373 reader: &Reader{
374 stream: bytes.NewReader([]byte{0b10101010, 0b01010101}),
375 index: 0,
376 currentByte: 0,
377 littleEndian: true,
378 },
379 want: 21930,
380 },
381 {
382 name: "ReadSInt16BE",
383 reader: &Reader{
384 stream: bytes.NewReader([]byte{0b10101010, 0b01010101}),
385 index: 0,
386 currentByte: 0,
387 littleEndian: false,
388 },
389 want: -21931,
390 },
391 }
392 for _, tt := range tests {
393 t.Run(tt.name, func(t *testing.T) {
394 if got := tt.reader.TryReadSInt16(); got != tt.want {
395 t.Errorf("Reader.TryReadSInt16() = %+v, want %+v", got, tt.want)
396 }
397 })
398 }
399}
400
401func TestReader_TryReadUInt32(t *testing.T) {
402 tests := []struct {
403 name string
404 reader *Reader
405 want uint32
406 }{
407 {
408 name: "ReadUInt32LE",
409 reader: &Reader{
410 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111}),
411 index: 0,
412 currentByte: 0,
413 littleEndian: true,
414 },
415 want: 0b00001111111100000101010110101010,
416 },
417 {
418 name: "ReadUInt32BE",
419 reader: &Reader{
420 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111}),
421 index: 0,
422 currentByte: 0,
423 littleEndian: false,
424 },
425 want: 0b10101010010101011111000000001111,
426 },
427 }
428 for _, tt := range tests {
429 t.Run(tt.name, func(t *testing.T) {
430 if got := tt.reader.TryReadUInt32(); got != tt.want {
431 t.Errorf("Reader.TryReadUInt32() = %+v, want %+v", got, tt.want)
432 }
433 })
434 }
435}
436
437func TestReader_TryReadSInt32(t *testing.T) {
438 tests := []struct {
439 name string
440 reader *Reader
441 want int32
442 }{
443 {
444 name: "ReadSInt32LE",
445 reader: &Reader{
446 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111}),
447 index: 0,
448 currentByte: 0,
449 littleEndian: true,
450 },
451 want: 267408810,
452 },
453 {
454 name: "ReadSInt32BE",
455 reader: &Reader{
456 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111}),
457 index: 0,
458 currentByte: 0,
459 littleEndian: false,
460 },
461 want: -1437208561,
462 },
463 }
464 for _, tt := range tests {
465 t.Run(tt.name, func(t *testing.T) {
466 if got := tt.reader.TryReadSInt32(); got != tt.want {
467 t.Errorf("Reader.TryReadSInt32() = %+v, want %+v", got, tt.want)
468 }
469 })
470 }
471}
472
473func TestReader_TryReadUInt64(t *testing.T) {
474 tests := []struct {
475 name string
476 reader *Reader
477 want uint64
478 }{
479 {
480 name: "ReadUInt64LE",
481 reader: &Reader{
482 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111, 0b10101010, 0b01010101, 0b11110000, 0b00001111}),
483 index: 0,
484 currentByte: 0,
485 littleEndian: true,
486 },
487 want: 0b0000111111110000010101011010101000001111111100000101010110101010,
488 },
489 {
490 name: "ReadUInt64BE",
491 reader: &Reader{
492 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111, 0b10101010, 0b01010101, 0b11110000, 0b00001111}),
493 index: 0,
494 currentByte: 0,
495 littleEndian: false,
496 },
497 want: 0b1010101001010101111100000000111110101010010101011111000000001111,
498 },
499 }
500 for _, tt := range tests {
501 t.Run(tt.name, func(t *testing.T) {
502 if got := tt.reader.TryReadUInt64(); got != tt.want {
503 t.Errorf("Reader.TryReadUInt64() = %+v, want %+v", got, tt.want)
504 }
505 })
506 }
507}
508
509func TestReader_TryReadSInt64(t *testing.T) {
510 tests := []struct {
511 name string
512 reader *Reader
513 want int64
514 }{
515 {
516 name: "ReadSInt64LE",
517 reader: &Reader{
518 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111, 0b10101010, 0b01010101, 0b11110000, 0b00001111}),
519 index: 0,
520 currentByte: 0,
521 littleEndian: true,
522 },
523 want: 1148512093879686570,
524 },
525 {
526 name: "ReadSInt64BE",
527 reader: &Reader{
528 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111, 0b10101010, 0b01010101, 0b11110000, 0b00001111}),
529 index: 0,
530 currentByte: 0,
531 littleEndian: false,
532 },
533 want: -6172763764168462321,
534 },
535 }
536 for _, tt := range tests {
537 t.Run(tt.name, func(t *testing.T) {
538 if got := tt.reader.TryReadSInt64(); got != tt.want {
539 t.Errorf("Reader.TryReadSInt64() = %+v, want %+v", got, tt.want)
540 }
541 })
542 }
543}
544
545func TestReader_TryReadFloat32(t *testing.T) {
546 tests := []struct {
547 name string
548 reader *Reader
549 want float32
550 }{
551 {
552 name: "ReadFloat32LE",
553 reader: &Reader{
554 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111}),
555 index: 0,
556 currentByte: 0,
557 littleEndian: true,
558 },
559 want: math.Float32frombits(0b00001111111100000101010110101010),
560 },
561 {
562 name: "ReadFloat32BE",
563 reader: &Reader{
564 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111}),
565 index: 0,
566 currentByte: 0,
567 littleEndian: false,
568 },
569 want: math.Float32frombits(0b10101010010101011111000000001111),
570 },
571 }
572 for _, tt := range tests {
573 t.Run(tt.name, func(t *testing.T) {
574 if got := tt.reader.TryReadFloat32(); got != tt.want {
575 t.Errorf("Reader.TryReadFloat32() = %+v, want %+v", got, tt.want)
576 }
577 })
578 }
579}
9 580
10// 01110001, 00001101, 00000000, 00000000, 10100010, 00011011, 00000000, 00000000, 11001100 581func TestReader_TryReadFloat64(t *testing.T) {
11var TestArray = [...]byte{113, 13, 0, 0, 162, 27, 0, 0, 204} 582 tests := []struct {
583 name string
584 reader *Reader
585 want float64
586 }{
587 {
588 name: "ReadFloat64LE",
589 reader: &Reader{
590 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111, 0b10101010, 0b01010101, 0b11110000, 0b00001111}),
591 index: 0,
592 currentByte: 0,
593 littleEndian: true,
594 },
595 want: math.Float64frombits(0b0000111111110000010101011010101000001111111100000101010110101010),
596 },
597 {
598 name: "ReadFloat64BE",
599 reader: &Reader{
600 stream: bytes.NewReader([]byte{0b10101010, 0b01010101, 0b11110000, 0b00001111, 0b10101010, 0b01010101, 0b11110000, 0b00001111}),
601 index: 0,
602 currentByte: 0,
603 littleEndian: false,
604 },
605 want: math.Float64frombits(0b1010101001010101111100000000111110101010010101011111000000001111),
606 },
607 }
608 for _, tt := range tests {
609 t.Run(tt.name, func(t *testing.T) {
610 if got := tt.reader.TryReadFloat64(); got != tt.want {
611 t.Errorf("Reader.TryReadFloat64() = %+v, want %+v", got, tt.want)
612 }
613 })
614 }
615}
12 616
13func TestTryReadFloat32(t *testing.T) { 617func TestReader_TryReadBits(t *testing.T) {
14 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 618 type args struct {
15 expected := []float32{6.98198182157e+29, -2.10064170919e-18} 619 bits int
16 for i := range expected { 620 }
17 value := bitreader.TryReadFloat32() 621 tests := []struct {
18 if value != expected[i] { 622 name string
19 t.Fatalf("TryReadFloat32 FAIL for index %d: Expected %f, Got %f", i, expected[i], value) 623 reader *Reader
20 } 624 args args
21 } 625 want uint64
22} 626 }{
23 627 {
24func TestTryReadFloat64(t *testing.T) { 628 name: "ReadBitsLE",
25 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 629 reader: &Reader{
26 expected := []float64{3.68828741038253948851462939603e+236} 630 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
27 for i := range expected { 631 index: 0,
28 value := bitreader.TryReadFloat64() 632 currentByte: 0,
29 if value != expected[i] { 633 littleEndian: true,
30 t.Fatalf("TryReadFloat64 FAIL for index %d: Expected %f, Got %f", i, expected[i], value) 634 },
31 } 635 args: args{
32 } 636 bits: 12,
33} 637 },
34 638 want: 0b010111110000,
35func TestTryReadInt8(t *testing.T) { 639 },
36 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 640 {
37 expected := []int{113, 13, 0} 641 name: "ReadBitsBE",
38 for i := range expected { 642 reader: &Reader{
39 value := bitreader.TryReadInt8() 643 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
40 if int(value) != expected[i] { 644 index: 0,
41 t.Fatalf("TryReadInt8 FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 645 currentByte: 0,
42 } 646 littleEndian: false,
43 } 647 },
44} 648 args: args{
45 649 bits: 12,
46func TestTryReadInt16(t *testing.T) { 650 },
47 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 651 want: 0b111100000101,
48 expected := []int{28941, 0, 41499, 0} 652 },
49 for i := range expected { 653 }
50 value := bitreader.TryReadInt16() 654 for _, tt := range tests {
51 if int(value) != expected[i] { 655 t.Run(tt.name, func(t *testing.T) {
52 t.Fatalf("TryReadInt16 FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 656 if got := tt.reader.TryReadBits(tt.args.bits); got != tt.want {
53 } 657 t.Errorf("Reader.TryReadBits() = %+v, want %+v", got, tt.want)
54 } 658 }
55} 659 })
56 660 }
57func TestTryReadInt32(t *testing.T) { 661}
58 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 662
59 expected := []int{1896677376, 2719678464} 663func TestReader_TryReadBytes(t *testing.T) {
60 for i := range expected { 664 type args struct {
61 value := bitreader.TryReadInt32() 665 bytes int
62 if int(value) != expected[i] { 666 }
63 t.Fatalf("TryReadInt32 FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 667 tests := []struct {
64 } 668 name string
65 } 669 reader *Reader
66} 670 args args
67 671 want uint64
68func TestTryReadInt64(t *testing.T) { 672 }{
69 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 673 {
70 expected := []int{8146167303702773760} 674 name: "ReadBytesLE",
71 for i := range expected { 675 reader: &Reader{
72 value := bitreader.TryReadInt64() 676 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
73 if int(value) != expected[i] { 677 index: 0,
74 t.Fatalf("TryReadInt64 FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 678 currentByte: 0,
75 } 679 littleEndian: true,
76 } 680 },
77} 681 args: args{
78 682 bytes: 2,
79func TestReadBit(t *testing.T) { 683 },
80 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 684 want: 0b0101010111110000,
81 expected := []bool{false, true, true, true} 685 },
82 for i := range expected { 686 {
83 value, err := bitreader.ReadBool() 687 name: "ReadBytesBE",
84 if err != nil { 688 reader: &Reader{
85 t.Fatal(err) 689 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
86 } 690 index: 0,
87 if value != expected[i] { 691 currentByte: 0,
88 t.Fatalf("ReadBit FAIL for index %d: Expected %t, Got %t", i, expected[i], value) 692 littleEndian: false,
89 } 693 },
90 } 694 args: args{
91} 695 bytes: 2,
92 696 },
93func TestReadBitLE(t *testing.T) { 697 want: 0b1111000001010101,
94 bitreader := Reader(bytes.NewReader(TestArray[:]), true) 698 },
95 expected := []bool{true, false, false, false} 699 }
96 for i := range expected { 700 for _, tt := range tests {
97 value, err := bitreader.ReadBool() 701 t.Run(tt.name, func(t *testing.T) {
98 if err != nil { 702 if got := tt.reader.TryReadBytes(tt.args.bytes); got != tt.want {
99 t.Fatal(err) 703 t.Errorf("Reader.TryReadBytes() = %+v, want %+v", got, tt.want)
100 } 704 }
101 if value != expected[i] { 705 })
102 t.Fatalf("ReadBitLE FAIL for index %d: Expected %t, Got %t", i, expected[i], value) 706 }
103 } 707}
104 } 708
105} 709func TestReader_TryReadString(t *testing.T) {
106 710 tests := []struct {
107func TestReadBits(t *testing.T) { 711 name string
108 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 712 reader *Reader
109 expected := []int{3793354753, 2288779267} // 11100010000110100000000000000001, 10001000011011000000000000000011 713 want string
110 expectedBool := []bool{false, false} 714 }{
111 for i := range expected { 715 {
112 bool, err := bitreader.ReadBool() 716 name: "ReadStringLE",
113 if bool != expectedBool[i] { 717 reader: &Reader{
114 t.Fatalf("ReadBits ReadBit FAIL for index %d: Expected %t, Got %t", i, expectedBool[i], bool) 718 stream: bytes.NewReader([]byte{'H', 'e', 'l', 'l', 'o', 0, '!'}),
115 } 719 index: 0,
116 if err != nil { 720 currentByte: 0,
117 t.Fatal(err) 721 littleEndian: true,
118 } 722 },
119 value, err := bitreader.ReadBits(32) 723 want: "Hello",
120 if err != nil { 724 },
121 t.Fatal(err) 725 {
122 } 726 name: "ReadStringBE",
123 if int(value) != expected[i] { 727 reader: &Reader{
124 t.Fatalf("ReadBits FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 728 stream: bytes.NewReader([]byte{'W', 'o', 'r', 'l', 'd', 0, '!'}),
125 } 729 index: 0,
126 } 730 currentByte: 0,
127} 731 littleEndian: false,
128 732 },
129func TestReadBitsLE(t *testing.T) { 733 want: "World",
130 bitreader := Reader(bytes.NewReader(TestArray[:]), true) 734 },
131 expected := []int{1720, 1768} // 11010111000, 11011101000 735 }
132 for i := range expected { 736 for _, tt := range tests {
133 bitreader.ReadBool() 737 t.Run(tt.name, func(t *testing.T) {
134 value, err := bitreader.ReadBits(32) 738 if got := tt.reader.TryReadString(); got != tt.want {
135 if err != nil { 739 t.Errorf("Reader.TryReadString() = %+v, want %+v", got, tt.want)
136 t.Fatal(err) 740 }
137 } 741 })
138 if int(value) != expected[i] { 742 }
139 t.Fatalf("ReadBitsLE FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 743}
140 } 744
141 } 745func TestReader_TryReadStringLength(t *testing.T) {
142} 746 type args struct {
143 747 length int
144func TestReadBytes(t *testing.T) { 748 }
145 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 749 tests := []struct {
146 expected := []int{3793354753, 2288779267} // 11100010000110100000000000000001, 10001000011011000000000000000011 750 name string
147 expectedBool := []bool{false, false} 751 reader *Reader
148 for i := range expected { 752 args args
149 bool, err := bitreader.ReadBool() 753 want string
150 if bool != expectedBool[i] { 754 }{
151 t.Fatalf("ReadBytes ReadBit FAIL for index %d: Expected %t, Got %t", i, expectedBool[i], bool) 755 {
152 } 756 name: "ReadStringLengthLE",
153 if err != nil { 757 reader: &Reader{
154 t.Fatal(err) 758 stream: bytes.NewReader([]byte{'H', 'e', 'l', 'l', 'o', 0, '!'}),
155 } 759 index: 0,
156 value, err := bitreader.ReadBytes(4) 760 currentByte: 0,
157 if err != nil { 761 littleEndian: true,
158 t.Fatal(err) 762 },
159 } 763 args: args{
160 if int(value) != expected[i] { 764 length: 4,
161 t.Fatalf("ReadBytes FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 765 },
162 } 766 want: "Hell",
163 } 767 },
164} 768 {
165 769 name: "ReadStringLengthBE",
166func TestReadBytesLE(t *testing.T) { 770 reader: &Reader{
167 bitreader := Reader(bytes.NewReader(TestArray[:]), true) 771 stream: bytes.NewReader([]byte{'W', 'o', 'r', 'l', 'd', '!', '?'}),
168 expected := []int{1720, 1768} // 11010111000, 11011101000 772 index: 0,
169 for i := range expected { 773 currentByte: 0,
170 bitreader.ReadBool() 774 littleEndian: false,
171 value, err := bitreader.ReadBytes(4) 775 },
172 if err != nil { 776 args: args{
173 t.Fatal(err) 777 length: 6,
174 } 778 },
175 if int(value) != expected[i] { 779 want: "World!",
176 t.Fatalf("ReadBytesLE FAIL for index %d: Expected %d, Got %d", i, expected[i], value) 780 },
177 } 781 {
178 } 782 name: "ReadStringLengthNullHitBE",
179} 783 reader: &Reader{
180 784 stream: bytes.NewReader([]byte{'W', 'o', 'r', 'l', 'd', 0, '!', '?'}),
181func TestSkipBits(t *testing.T) { 785 index: 0,
182 bitreader := Reader(bytes.NewReader(TestArray[:]), false) 786 currentByte: 0,
183 expected := []bool{true, true, false, true} //00001101 787 littleEndian: false,
184 err := bitreader.SkipBits(12) 788 },
185 if err != nil { 789 args: args{
186 t.Fatal(err) 790 length: 7,
187 } 791 },
188 for i := range expected { 792 want: "World",
189 value, err := bitreader.ReadBool() 793 },
190 if err != nil { 794 }
191 t.Fatal(err) 795 for _, tt := range tests {
192 } 796 t.Run(tt.name, func(t *testing.T) {
193 if value != expected[i] { 797 if got := tt.reader.TryReadStringLength(tt.args.length); got != tt.want {
194 t.Fatalf("SkipBits ReadBit FAIL for index %d: Expected %t, Got %t", i, expected[i], value) 798 t.Errorf("Reader.TryReadStringLength() = %+v, want %+v", got, tt.want)
195 } 799 }
196 } 800 })
197} 801 }
198 802}
199func TestSkipBitsLE(t *testing.T) { 803
200 bitreader := Reader(bytes.NewReader(TestArray[:]), true) 804func TestReader_TryReadBitsToSlice(t *testing.T) {
201 expected := []bool{false, false, false, false} //10110000 805 type args struct {
202 bitreader.SkipBits(12) 806 bits int
203 for i := range expected { 807 }
204 value, err := bitreader.ReadBool() 808 tests := []struct {
205 if err != nil { 809 name string
206 t.Fatal(err) 810 reader *Reader
207 } 811 args args
208 if value != expected[i] { 812 want []byte
209 t.Fatalf("SkipBitsLE ReadBit FAIL for index %d: Expected %t, Got %t", i, expected[i], value) 813 }{
210 } 814 {
815 name: "ReadBitsToSliceBE",
816 reader: &Reader{
817 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
818 index: 0,
819 currentByte: 0,
820 littleEndian: false,
821 },
822 args: args{
823 bits: 12,
824 },
825 want: []byte{0b11110010, 0b0},
826 },
827 {
828 name: "ReadBitsToSliceLE",
829 reader: &Reader{
830 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
831 index: 0,
832 currentByte: 0,
833 littleEndian: true,
834 },
835 args: args{
836 bits: 12,
837 },
838 want: []byte{0b11110010, 0b00001111},
839 },
840 {
841 name: "ReadBitsToSliceBE",
842 reader: &Reader{
843 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
844 index: 0,
845 currentByte: 0,
846 littleEndian: false,
847 },
848 args: args{
849 bits: 16,
850 },
851 want: []byte{0b11110010, 0b00001111},
852 },
853 {
854 name: "ReadBitsToSliceLE",
855 reader: &Reader{
856 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
857 index: 0,
858 currentByte: 0,
859 littleEndian: true,
860 },
861 args: args{
862 bits: 16,
863 },
864 want: []byte{0b11110010, 0b00001111},
865 },
866 }
867 for _, tt := range tests {
868 t.Run(tt.name, func(t *testing.T) {
869 if got := tt.reader.TryReadBitsToSlice(tt.args.bits); !reflect.DeepEqual(got, tt.want) {
870 t.Errorf("Reader.TryReadBitsToSlice() = %+v, want %+v", got, tt.want)
871 }
872 })
873 }
874}
875
876func TestReader_TryReadBytesToSlice(t *testing.T) {
877 type args struct {
878 bytes int
879 }
880 tests := []struct {
881 name string
882 reader *Reader
883 args args
884 want []byte
885 }{
886 {
887 name: "ReadBytesToSliceBE",
888 reader: &Reader{
889 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
890 index: 0,
891 currentByte: 0,
892 littleEndian: false,
893 },
894 args: args{
895 bytes: 2,
896 },
897 want: []byte{0b11110010, 0b00001111},
898 },
899 {
900 name: "ReadBytesToSliceLE",
901 reader: &Reader{
902 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
903 index: 0,
904 currentByte: 0,
905 littleEndian: true,
906 },
907 args: args{
908 bytes: 2,
909 },
910 want: []byte{0b11110010, 0b00001111},
911 },
912 }
913 for _, tt := range tests {
914 t.Run(tt.name, func(t *testing.T) {
915 if got := tt.reader.TryReadBytesToSlice(tt.args.bytes); !reflect.DeepEqual(got, tt.want) {
916 t.Errorf("Reader.TryReadBytesToSlice() = %+v, want %+v", got, tt.want)
917 }
918 })
919 }
920}
921
922func TestReader_TryReadRemainingBits(t *testing.T) {
923 tests := []struct {
924 name string
925 reader *Reader
926 want uint64
927 wantErr bool
928 }{
929 {
930 name: "ReadRemainingBits",
931 reader: &Reader{
932 stream: bytes.NewReader([]byte{0x11, 0x22}),
933 index: 0,
934 currentByte: 0,
935 littleEndian: false,
936 },
937 want: 16,
938 wantErr: false,
939 },
940 }
941 for _, tt := range tests {
942 t.Run(tt.name, func(t *testing.T) {
943 got := tt.reader.TryReadRemainingBits()
944 if got != tt.want {
945 t.Errorf("Reader.TryReadRemainingBits() = %v, want %v", got, tt.want)
946 }
947 })
948 }
949}
950
951func TestReader_ReadBool(t *testing.T) {
952 tests := []struct {
953 name string
954 reader *Reader
955 want bool
956 wantErr bool
957 }{
958 {
959 name: "ReadBoolTrueLE",
960 reader: &Reader{
961 stream: bytes.NewReader([]byte{0b00000001}),
962 index: 0,
963 currentByte: 0,
964 littleEndian: true,
965 },
966 want: true,
967 },
968 {
969 name: "ReadBoolTrueBE",
970 reader: &Reader{
971 stream: bytes.NewReader([]byte{0b10000000}),
972 index: 0,
973 currentByte: 0,
974 littleEndian: false,
975 },
976 want: true,
977 },
978 {
979 name: "ReadBoolFalseLE",
980 reader: &Reader{
981 stream: bytes.NewReader([]byte{0b00000010}),
982 index: 0,
983 currentByte: 0,
984 littleEndian: true,
985 },
986 want: false,
987 },
988 {
989 name: "ReadBoolFalseBE",
990 reader: &Reader{
991 stream: bytes.NewReader([]byte{0b01000000}),
992 index: 0,
993 currentByte: 0,
994 littleEndian: false,
995 },
996 want: false,
997 },
998 }
999 for _, tt := range tests {
1000 t.Run(tt.name, func(t *testing.T) {
1001 got, err := tt.reader.ReadBool()
1002 if (err != nil) != tt.wantErr {
1003 t.Errorf("Reader.ReadBool() error = %+v, wantErr %+v", err, tt.wantErr)
1004 return
1005 }
1006 if got != tt.want {
1007 t.Errorf("Reader.ReadBool() = %+v, want %+v", got, tt.want)
1008 }
1009 })
1010 }
1011}
1012
1013func TestReader_ReadBits(t *testing.T) {
1014 type args struct {
1015 bits int
1016 }
1017 tests := []struct {
1018 name string
1019 reader *Reader
1020 args args
1021 want uint64
1022 wantErr bool
1023 }{
1024 {
1025 name: "ReadBitsLE",
1026 reader: &Reader{
1027 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
1028 index: 0,
1029 currentByte: 0,
1030 littleEndian: true,
1031 },
1032 args: args{
1033 bits: 12,
1034 },
1035 want: 0b010111110000,
1036 },
1037 {
1038 name: "ReadBitsBE",
1039 reader: &Reader{
1040 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
1041 index: 0,
1042 currentByte: 0,
1043 littleEndian: false,
1044 },
1045 args: args{
1046 bits: 12,
1047 },
1048 want: 0b111100000101,
1049 },
1050 }
1051 for _, tt := range tests {
1052 t.Run(tt.name, func(t *testing.T) {
1053 got, err := tt.reader.ReadBits(tt.args.bits)
1054 if (err != nil) != tt.wantErr {
1055 t.Errorf("Reader.ReadBits() error = %+v, wantErr %+v", err, tt.wantErr)
1056 return
1057 }
1058 if got != tt.want {
1059 t.Errorf("Reader.ReadBits() = %+v, want %+v", got, tt.want)
1060 }
1061 })
1062 }
1063}
1064
1065func TestReader_ReadBytes(t *testing.T) {
1066 type args struct {
1067 bytes int
1068 }
1069 tests := []struct {
1070 name string
1071 reader *Reader
1072 args args
1073 want uint64
1074 wantErr bool
1075 }{
1076 {
1077 name: "ReadBytesLE",
1078 reader: &Reader{
1079 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
1080 index: 0,
1081 currentByte: 0,
1082 littleEndian: true,
1083 },
1084 args: args{
1085 bytes: 2,
1086 },
1087 want: 0b0101010111110000,
1088 },
1089 {
1090 name: "ReadBytesBE",
1091 reader: &Reader{
1092 stream: bytes.NewReader([]byte{0b11110000, 0b01010101}),
1093 index: 0,
1094 currentByte: 0,
1095 littleEndian: false,
1096 },
1097 args: args{
1098 bytes: 2,
1099 },
1100 want: 0b1111000001010101,
1101 },
1102 }
1103 for _, tt := range tests {
1104 t.Run(tt.name, func(t *testing.T) {
1105 got, err := tt.reader.ReadBytes(tt.args.bytes)
1106 if (err != nil) != tt.wantErr {
1107 t.Errorf("Reader.ReadBytes() error = %+v, wantErr %+v", err, tt.wantErr)
1108 return
1109 }
1110 if got != tt.want {
1111 t.Errorf("Reader.ReadBytes() = %+v, want %+v", got, tt.want)
1112 }
1113 })
1114 }
1115}
1116
1117func TestReader_ReadString(t *testing.T) {
1118 tests := []struct {
1119 name string
1120 reader *Reader
1121 want string
1122 wantErr bool
1123 }{
1124 {
1125 name: "ReadStringLE",
1126 reader: &Reader{
1127 stream: bytes.NewReader([]byte{'H', 'e', 'l', 'l', 'o', 0, '!'}),
1128 index: 0,
1129 currentByte: 0,
1130 littleEndian: true,
1131 },
1132 want: "Hello",
1133 },
1134 {
1135 name: "ReadStringBE",
1136 reader: &Reader{
1137 stream: bytes.NewReader([]byte{'W', 'o', 'r', 'l', 'd', 0, '!'}),
1138 index: 0,
1139 currentByte: 0,
1140 littleEndian: false,
1141 },
1142 want: "World",
1143 },
1144 }
1145 for _, tt := range tests {
1146 t.Run(tt.name, func(t *testing.T) {
1147 got, err := tt.reader.ReadString()
1148 if (err != nil) != tt.wantErr {
1149 t.Errorf("Reader.ReadString() error = %+v, wantErr %+v", err, tt.wantErr)
1150 return
1151 }
1152 if got != tt.want {
1153 t.Errorf("Reader.ReadString() = %+v, want %+v", got, tt.want)
1154 }
1155 })
1156 }
1157}
1158
1159func TestReader_ReadStringLength(t *testing.T) {
1160 type args struct {
1161 length int
1162 }
1163 tests := []struct {
1164 name string
1165 reader *Reader
1166 args args
1167 want string
1168 wantErr bool
1169 }{
1170 {
1171 name: "ReadStringLengthLE",
1172 reader: &Reader{
1173 stream: bytes.NewReader([]byte{'H', 'e', 'l', 'l', 'o', 0, '!'}),
1174 index: 0,
1175 currentByte: 0,
1176 littleEndian: true,
1177 },
1178 args: args{
1179 length: 4,
1180 },
1181 want: "Hell",
1182 },
1183 {
1184 name: "ReadStringLengthBE",
1185 reader: &Reader{
1186 stream: bytes.NewReader([]byte{'W', 'o', 'r', 'l', 'd', '!', '?'}),
1187 index: 0,
1188 currentByte: 0,
1189 littleEndian: false,
1190 },
1191 args: args{
1192 length: 6,
1193 },
1194 want: "World!",
1195 },
1196 {
1197 name: "ReadStringLengthNullHitBE",
1198 reader: &Reader{
1199 stream: bytes.NewReader([]byte{'W', 'o', 'r', 'l', 'd', 0, '!', '?'}),
1200 index: 0,
1201 currentByte: 0,
1202 littleEndian: false,
1203 },
1204 args: args{
1205 length: 7,
1206 },
1207 want: "World",
1208 },
1209 }
1210 for _, tt := range tests {
1211 t.Run(tt.name, func(t *testing.T) {
1212 got, err := tt.reader.ReadStringLength(tt.args.length)
1213 if (err != nil) != tt.wantErr {
1214 t.Errorf("Reader.ReadStringLength() error = %+v, wantErr %+v", err, tt.wantErr)
1215 return
1216 }
1217 if got != tt.want {
1218 t.Errorf("Reader.ReadStringLength() = %+v, want %+v", got, tt.want)
1219 }
1220 })
1221 }
1222}
1223
1224func TestReader_ReadBitsToSlice(t *testing.T) {
1225 type args struct {
1226 bits int
1227 }
1228 tests := []struct {
1229 name string
1230 reader *Reader
1231 args args
1232 want []byte
1233 wantErr bool
1234 }{
1235 {
1236 name: "ReadBitsToSliceBE",
1237 reader: &Reader{
1238 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
1239 index: 0,
1240 currentByte: 0,
1241 littleEndian: false,
1242 },
1243 args: args{
1244 bits: 12,
1245 },
1246 want: []byte{0b11110010, 0b0},
1247 },
1248 {
1249 name: "ReadBitsToSliceLE",
1250 reader: &Reader{
1251 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
1252 index: 0,
1253 currentByte: 0,
1254 littleEndian: true,
1255 },
1256 args: args{
1257 bits: 12,
1258 },
1259 want: []byte{0b11110010, 0b00001111},
1260 },
1261 {
1262 name: "ReadBitsToSliceBE",
1263 reader: &Reader{
1264 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
1265 index: 0,
1266 currentByte: 0,
1267 littleEndian: false,
1268 },
1269 args: args{
1270 bits: 16,
1271 },
1272 want: []byte{0b11110010, 0b00001111},
1273 },
1274 {
1275 name: "ReadBitsToSliceLE",
1276 reader: &Reader{
1277 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
1278 index: 0,
1279 currentByte: 0,
1280 littleEndian: true,
1281 },
1282 args: args{
1283 bits: 16,
1284 },
1285 want: []byte{0b11110010, 0b00001111},
1286 },
1287 }
1288 for _, tt := range tests {
1289 t.Run(tt.name, func(t *testing.T) {
1290 got, err := tt.reader.ReadBitsToSlice(tt.args.bits)
1291 if (err != nil) != tt.wantErr {
1292 t.Errorf("Reader.ReadBitsToSlice() error = %+v, wantErr %+v", err, tt.wantErr)
1293 return
1294 }
1295 if !reflect.DeepEqual(got, tt.want) {
1296 t.Errorf("Reader.ReadBitsToSlice() = %+v, want %+v", got, tt.want)
1297 }
1298 })
1299 }
1300}
1301
1302func TestReader_ReadBytesToSlice(t *testing.T) {
1303 type args struct {
1304 bytes int
1305 }
1306 tests := []struct {
1307 name string
1308 reader *Reader
1309 args args
1310 want []byte
1311 wantErr bool
1312 }{
1313 {
1314 name: "ReadBytesToSliceBE",
1315 reader: &Reader{
1316 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
1317 index: 0,
1318 currentByte: 0,
1319 littleEndian: false,
1320 },
1321 args: args{
1322 bytes: 2,
1323 },
1324 want: []byte{0b11110010, 0b00001111},
1325 },
1326 {
1327 name: "ReadBytesToSliceLE",
1328 reader: &Reader{
1329 stream: bytes.NewReader([]byte{0b11110010, 0b00001111}),
1330 index: 0,
1331 currentByte: 0,
1332 littleEndian: true,
1333 },
1334 args: args{
1335 bytes: 2,
1336 },
1337 want: []byte{0b11110010, 0b00001111},
1338 },
1339 }
1340 for _, tt := range tests {
1341 t.Run(tt.name, func(t *testing.T) {
1342 got, err := tt.reader.ReadBytesToSlice(tt.args.bytes)
1343 if (err != nil) != tt.wantErr {
1344 t.Errorf("Reader.ReadBytesToSlice() error = %+v, wantErr %+v", err, tt.wantErr)
1345 return
1346 }
1347 if !reflect.DeepEqual(got, tt.want) {
1348 t.Errorf("Reader.ReadBytesToSlice() = %+v, want %+v", got, tt.want)
1349 }
1350 })
1351 }
1352}
1353
1354func TestReader_ReadRemainingBits(t *testing.T) {
1355 tests := []struct {
1356 name string
1357 reader *Reader
1358 want uint64
1359 wantErr bool
1360 }{
1361 {
1362 name: "ReadRemainingBits",
1363 reader: &Reader{
1364 stream: bytes.NewReader([]byte{0x11, 0x22}),
1365 index: 0,
1366 currentByte: 0,
1367 littleEndian: false,
1368 },
1369 want: 16,
1370 wantErr: false,
1371 },
1372 }
1373 for _, tt := range tests {
1374 t.Run(tt.name, func(t *testing.T) {
1375 got, err := tt.reader.ReadRemainingBits()
1376 if (err != nil) != tt.wantErr {
1377 t.Errorf("Reader.ReadRemainingBits() error = %v, wantErr %v", err, tt.wantErr)
1378 return
1379 }
1380 if got != tt.want {
1381 t.Errorf("Reader.ReadRemainingBits() = %v, want %v", got, tt.want)
1382 }
1383 })
211 } 1384 }
212} 1385}