diff --git a/bit_reader_test.go b/bit_reader_test.go index 50bb83e..7a3898d 100644 --- a/bit_reader_test.go +++ b/bit_reader_test.go @@ -4,59 +4,20 @@ import ( "bytes" "testing" - fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func Test_bitReader_readBit(t *testing.T) { - tests := []struct { - name string - byteToRead byte - want bit - wantErr error - }{ - { - name: "read 00000001", - byteToRead: 0x1, - want: zero, - wantErr: nil, - }, - { - name: "read 11111111", - byteToRead: 0xff, - want: one, - wantErr: nil, - }, - { - name: "read 11000011", - byteToRead: 0xc3, - want: one, - wantErr: nil, - }, - { - name: "read 10000000", - byteToRead: 0x80, - want: one, - wantErr: nil, - }, - { - name: "read 01111111", - byteToRead: 0x7f, - want: zero, - wantErr: nil, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - buf := new(bytes.Buffer) - err := buf.WriteByte(tt.byteToRead) - require.Nil(t, err) - b := newBitReader(buf) - got, err := b.readBit() - assert.Equal(t, tt.wantErr, err) - assert.Equal(t, tt.want, got) - }) + var b byte = 0x1 + for i := 0; i < 256; i++ { + buf := new(bytes.Buffer) + require.Nil(t, buf.WriteByte(b)) + br := newBitReader(buf) + actual, err := br.readBit() + require.Nil(t, err) + assert.Equal(t, bit((b&0x80) != 0), actual) + b++ } } @@ -125,15 +86,14 @@ func Test_bitReader_readBits(t *testing.T) { } func Test_bitReader_readByte(t *testing.T) { - f := fuzz.New().NilChance(0) - for i := 0; i < 100; i++ { - var b byte - f.Fuzz(&b) + var b byte = 0x1 + for i := 0; i < 256; i++ { buf := new(bytes.Buffer) require.Nil(t, buf.WriteByte(b)) br := newBitReader(buf) - got, err := br.readByte() + byt, err := br.readByte() require.Nil(t, err) - assert.Equal(t, b, got) + assert.Equal(t, b, byt) + b++ } } diff --git a/bit_writer_test.go b/bit_writer_test.go index a724595..c4a97f7 100644 --- a/bit_writer_test.go +++ b/bit_writer_test.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/binary" "testing" - "time" fuzz "github.com/google/gofuzz" "github.com/stretchr/testify/assert" @@ -57,60 +56,31 @@ func Test_bitWriter_writeBit(t *testing.T) { } func Test_bitWriter_writeBits(t *testing.T) { - var ( - unix = time.Now().Unix() - ) - type args struct { - u uint64 - nbits int - } - tests := []struct { - name string - args args - }{ - { - name: "write timestamp", - args: args{ - u: uint64(unix), - nbits: 64, - }, - }, - { - name: "write 630", - args: args{ - u: 630, - nbits: 64, - }, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - buf := new(bytes.Buffer) - bw := newBitWriter(buf) + f := fuzz.New().NilChance(0) - err := bw.writeBits(tt.args.u, tt.args.nbits) - require.Nil(t, err) + for i := 0; i < 10; i++ { + var u64 uint64 + f.Fuzz(&u64) - wantBytesLen := tt.args.nbits / 8 - wantBytes := make([]byte, wantBytesLen) - binary.BigEndian.PutUint64(wantBytes, tt.args.u) + buf := new(bytes.Buffer) + bw := newBitWriter(buf) + require.Nil(t, bw.writeBits(u64, 64)) - assert.Equal(t, wantBytesLen, buf.Len()) - assert.Equal(t, wantBytes, buf.Bytes()) - }) + wantBytes := make([]byte, 8) + binary.BigEndian.PutUint64(wantBytes, u64) + + assert.Equal(t, wantBytes, buf.Bytes()) } } func Test_bitWriter_writeByte(t *testing.T) { - f := fuzz.New().NilChance(0) - for i := 0; i < 100; i++ { - var b byte - f.Fuzz(&b) + var b byte = 0x1 + for i := 0; i < 256; i++ { buf := new(bytes.Buffer) + require.Nil(t, buf.WriteByte(b)) bw := newBitWriter(buf) - err := bw.writeByte(b) - require.Nil(t, err) - assert.Equal(t, 1, buf.Len()) + require.Nil(t, bw.writeByte(b)) assert.Equal(t, b, buf.Bytes()[0]) + b++ } }