OpenDiablo2/d2common/d2datautils/bitmuncher_test.go

108 lines
2.7 KiB
Go
Raw Normal View History

2021-02-08 07:23:43 +00:00
package d2datautils
2021-03-24 02:38:47 +00:00
import (
"testing"
"github.com/stretchr/testify/assert"
)
2021-02-08 07:23:43 +00:00
2021-03-24 03:40:24 +00:00
var testData = []byte{33, 23, 4, 33, 192, 243} //nolint:gochecknoglobals // just a test
2021-02-08 07:23:43 +00:00
func TestBitmuncherCopy(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm1 := CreateBitMuncher(testData, 0)
2021-02-08 07:23:43 +00:00
bm2 := CopyBitMuncher(bm1)
for i := range bm1.data {
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm1.data[i], bm2.data[i], "original bitmuncher isn't equal to copied")
2021-02-08 07:23:43 +00:00
}
}
func TestBitmuncherSetOffset(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm := CreateBitMuncher(testData, 0)
2021-02-08 07:23:43 +00:00
bm.SetOffset(5)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.Offset(), 5, "Set Offset method didn't set offset to expected number")
2021-02-08 07:23:43 +00:00
}
func TestBitmuncherSteBitsRead(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm := CreateBitMuncher(testData, 0)
2021-02-08 07:23:43 +00:00
bm.SetBitsRead(8)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.BitsRead(), 8, "Set bits read method didn't set bits read to expected value")
2021-02-08 07:23:43 +00:00
}
func TestBitmuncherReadBit(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm := CreateBitMuncher(testData, 0)
2021-02-08 08:55:02 +00:00
2021-02-08 07:23:43 +00:00
var result byte
for i := 0; i < bitsPerByte; i++ {
v := bm.GetBit()
result |= byte(v) << byte(i)
}
2021-03-24 02:38:47 +00:00
assert.Equal(t, result, testData[0], "result of rpeated 8 times get bit didn't return expected byte")
2021-02-08 07:23:43 +00:00
}
func TestBitmuncherGetBits(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm := CreateBitMuncher(testData, 0)
2021-02-08 07:23:43 +00:00
2021-03-24 02:38:47 +00:00
assert.Equal(t, byte(bm.GetBits(bitsPerByte)), testData[0], "get bits didn't return expected value")
2021-02-08 07:23:43 +00:00
}
2021-03-24 02:30:57 +00:00
func TestBitmuncherGetNoBits(t *testing.T) {
bm := CreateBitMuncher(testData, 0)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetBits(0), uint32(0), "get bits didn't return expected value: 0")
2021-03-24 02:30:57 +00:00
}
func TestBitmuncherGetSignedBits(t *testing.T) {
bm := CreateBitMuncher(testData, 0)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetSignedBits(6), -31, "get signed bits didn't return expected value")
2021-03-24 02:30:57 +00:00
}
func TestBitmuncherGetNoSignedBits(t *testing.T) {
bm := CreateBitMuncher(testData, 0)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetSignedBits(0), 0, "get signed bits didn't return expected value")
2021-03-24 02:30:57 +00:00
}
func TestBitmuncherGetOneSignedBit(t *testing.T) {
bm := CreateBitMuncher(testData, 0)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetSignedBits(1), -1, "get signed bits didn't return expected value")
2021-03-24 02:30:57 +00:00
}
2021-02-08 07:23:43 +00:00
func TestBitmuncherSkipBits(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm := CreateBitMuncher(testData, 0)
2021-02-08 07:23:43 +00:00
bm.SkipBits(bitsPerByte)
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetByte(), testData[1], "skipping 8 bits didn't moved bit muncher's position into next byte")
2021-02-08 07:23:43 +00:00
}
func TestBitmuncherGetInt32(t *testing.T) {
2021-03-24 02:30:57 +00:00
bm := CreateBitMuncher(testData, 0)
2021-02-08 07:23:43 +00:00
var testInt int32
for i := 0; i < bytesPerint32; i++ {
2021-03-24 02:30:57 +00:00
testInt |= int32(testData[i]) << int32(bitsPerByte*i)
2021-02-08 07:23:43 +00:00
}
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetInt32(), testInt, "int32 value wasn't returned properly")
2021-02-08 07:23:43 +00:00
}
2021-03-24 02:30:57 +00:00
func TestBitmuncherGetUint32(t *testing.T) {
bm := CreateBitMuncher(testData, 0)
var testUint uint32
for i := 0; i < bytesPerint32; i++ {
testUint |= uint32(testData[i]) << uint32(bitsPerByte*i)
}
2021-03-24 02:38:47 +00:00
assert.Equal(t, bm.GetUInt32(), testUint, "uint32 value wasn't returned properly")
2021-03-24 02:30:57 +00:00
}