From 8e1ca1dd7fcf446b2cd30ec6fa6e585f67c7fb65 Mon Sep 17 00:00:00 2001 From: "M. Sz" Date: Sat, 6 Feb 2021 20:43:04 +0100 Subject: [PATCH] stream writer test: added test for pushing bits --- d2common/d2datautils/stream_writer_test.go | 56 ++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/d2common/d2datautils/stream_writer_test.go b/d2common/d2datautils/stream_writer_test.go index 1f7dd95c..cc199739 100644 --- a/d2common/d2datautils/stream_writer_test.go +++ b/d2common/d2datautils/stream_writer_test.go @@ -4,6 +4,62 @@ import ( "testing" ) +func TestStreamWriterBits(t *testing.T) { + sr := CreateStreamWriter() + data := []byte{221, 19} + + for _, i := range data { + sr.PushBits(i, bitsPerByte) + } + + output := sr.GetBytes() + for i, d := range data { + if output[i] != d { + t.Fatalf("sr.PushBits() pushed %X, but wrote %X instead", d, output[i]) + } + } +} + +func TestStreamWriterBits16(t *testing.T) { + sr := CreateStreamWriter() + data := []uint16{1024, 19} + + for _, i := range data { + sr.PushBits16(i, bitsPerByte*bytesPerint16) + } + + output := sr.GetBytes() + + for i, d := range data { + outputInt := uint16(output[bytesPerint16*i]) | uint16(output[bytesPerint16*i+1])<<8 + if outputInt != d { + t.Fatalf("sr.PushBits16() pushed %X, but wrote %X instead", d, output[i]) + } + } +} + +func TestStreamWriterBits32(t *testing.T) { + sr := CreateStreamWriter() + data := []uint32{19324, 87} + + for _, i := range data { + sr.PushBits32(i, bitsPerByte*bytesPerint32) + } + + output := sr.GetBytes() + + for i, d := range data { + outputInt := uint32(output[bytesPerint32*i]) | + uint32(output[bytesPerint32*i+1])<<8 | + uint32(output[bytesPerint32*i+2])<<16 | + uint32(output[bytesPerint32*i+3])<<24 + + if outputInt != d { + t.Fatalf("sr.PushBits32() pushed %X, but wrote %X instead", d, output[i]) + } + } +} + func TestStreamWriterByte(t *testing.T) { sr := CreateStreamWriter() data := []byte{0x12, 0x34, 0x56, 0x78}