Skip to content
This repository has been archived by the owner on Feb 26, 2024. It is now read-only.

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku committed Oct 10, 2021
1 parent 0de1e6f commit 39bac8c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 100 deletions.
68 changes: 14 additions & 54 deletions bit_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
}

Expand Down Expand Up @@ -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++
}
}
62 changes: 16 additions & 46 deletions bit_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/binary"
"testing"
"time"

fuzz "github.com/google/gofuzz"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -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++
}
}

0 comments on commit 39bac8c

Please sign in to comment.