From bfa9c34ecc8d78321170dcc9070294653c5ee628 Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Tue, 23 Mar 2021 22:30:57 -0400 Subject: [PATCH 1/4] Adds 100% test coverage for BitMuncher --- d2common/d2datautils/bitmuncher_test.go | 94 +++++++++++++++++-------- 1 file changed, 66 insertions(+), 28 deletions(-) diff --git a/d2common/d2datautils/bitmuncher_test.go b/d2common/d2datautils/bitmuncher_test.go index 58ecd4e4..e1393f3b 100644 --- a/d2common/d2datautils/bitmuncher_test.go +++ b/d2common/d2datautils/bitmuncher_test.go @@ -1,17 +1,11 @@ package d2datautils -import ( - "testing" -) +import "testing" -func getTestData() []byte { - result := []byte{33, 23, 4, 33, 192, 243} - - return result -} +var testData = []byte{33, 23, 4, 33, 192, 243} func TestBitmuncherCopy(t *testing.T) { - bm1 := CreateBitMuncher(getTestData(), 0) + bm1 := CreateBitMuncher(testData, 0) bm2 := CopyBitMuncher(bm1) for i := range bm1.data { @@ -22,7 +16,7 @@ func TestBitmuncherCopy(t *testing.T) { } func TestBitmuncherSetOffset(t *testing.T) { - bm := CreateBitMuncher(getTestData(), 0) + bm := CreateBitMuncher(testData, 0) bm.SetOffset(5) if bm.Offset() != 5 { @@ -31,7 +25,7 @@ func TestBitmuncherSetOffset(t *testing.T) { } func TestBitmuncherSteBitsRead(t *testing.T) { - bm := CreateBitMuncher(getTestData(), 0) + bm := CreateBitMuncher(testData, 0) bm.SetBitsRead(8) if bm.BitsRead() != 8 { @@ -40,8 +34,7 @@ func TestBitmuncherSteBitsRead(t *testing.T) { } func TestBitmuncherReadBit(t *testing.T) { - td := getTestData() - bm := CreateBitMuncher(td, 0) + bm := CreateBitMuncher(testData, 0) var result byte @@ -50,46 +43,91 @@ func TestBitmuncherReadBit(t *testing.T) { result |= byte(v) << byte(i) } - if result != td[0] { + if result != testData[0] { t.Fatal("result of rpeated 8 times get bit didn't return expected byte") } } func TestBitmuncherGetBits(t *testing.T) { - td := getTestData() - bm := CreateBitMuncher(td, 0) + bm := CreateBitMuncher(testData, 0) - res := bm.GetBits(bitsPerByte) - - if byte(res) != td[0] { + if byte(bm.GetBits(bitsPerByte)) != testData[0] { t.Fatal("get bits didn't return expected value") } } +func TestBitmuncherGetNoBits(t *testing.T) { + bm := CreateBitMuncher(testData, 0) + + if bm.GetBits(0) != 0 { + t.Fatal("get bits didn't return expected value: 0") + } +} + +func TestBitmuncherGetSignedBits(t *testing.T) { + bm := CreateBitMuncher(testData, 0) + + result := bm.GetSignedBits(6) + expected := -31 + if result != expected { + t.Fatal("get signed bits didn't return expected value", result, expected) + } +} + +func TestBitmuncherGetNoSignedBits(t *testing.T) { + bm := CreateBitMuncher(testData, 0) + + result := bm.GetSignedBits(0) + expected := 0 + if result != expected { + t.Fatal("get signed bits didn't return expected value", result, expected) + } +} + +func TestBitmuncherGetOneSignedBit(t *testing.T) { + bm := CreateBitMuncher(testData, 0) + + result := bm.GetSignedBits(1) + expected := -1 + if result != expected { + t.Fatal("get signed bits didn't return expected value", result, expected) + } +} + func TestBitmuncherSkipBits(t *testing.T) { - td := getTestData() - bm := CreateBitMuncher(td, 0) + bm := CreateBitMuncher(testData, 0) bm.SkipBits(bitsPerByte) - if bm.GetByte() != td[1] { + if bm.GetByte() != testData[1] { t.Fatal("skipping 8 bits didn't moved bit muncher's position into next byte") } } func TestBitmuncherGetInt32(t *testing.T) { - td := getTestData() - bm := CreateBitMuncher(td, 0) + bm := CreateBitMuncher(testData, 0) var testInt int32 for i := 0; i < bytesPerint32; i++ { - testInt |= int32(td[i]) << int32(bitsPerByte*i) + testInt |= int32(testData[i]) << int32(bitsPerByte*i) } - bmInt := bm.GetInt32() - - if bmInt != testInt { + if bm.GetInt32() != testInt { t.Fatal("int32 value wasn't returned properly") } } + +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) + } + + if bm.GetUInt32() != testUint { + t.Fatal("uint32 value wasn't returned properly") + } +} From 3b26d825d254ceae51a7bdea08054d41cd92e8f3 Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Tue, 23 Mar 2021 22:38:47 -0400 Subject: [PATCH 2/4] Replaces comparisons with assertions --- d2common/d2datautils/bitmuncher_test.go | 60 +++++++------------------ 1 file changed, 17 insertions(+), 43 deletions(-) diff --git a/d2common/d2datautils/bitmuncher_test.go b/d2common/d2datautils/bitmuncher_test.go index e1393f3b..f850da23 100644 --- a/d2common/d2datautils/bitmuncher_test.go +++ b/d2common/d2datautils/bitmuncher_test.go @@ -1,6 +1,10 @@ package d2datautils -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/assert" +) var testData = []byte{33, 23, 4, 33, 192, 243} @@ -9,9 +13,7 @@ func TestBitmuncherCopy(t *testing.T) { bm2 := CopyBitMuncher(bm1) for i := range bm1.data { - if bm1.data[i] != bm2.data[i] { - t.Fatal("original bitmuncher isn't equal to copied") - } + assert.Equal(t, bm1.data[i], bm2.data[i], "original bitmuncher isn't equal to copied") } } @@ -19,18 +21,14 @@ func TestBitmuncherSetOffset(t *testing.T) { bm := CreateBitMuncher(testData, 0) bm.SetOffset(5) - if bm.Offset() != 5 { - t.Fatal("Set Offset method didn't set offset to expected number") - } + assert.Equal(t, bm.Offset(), 5, "Set Offset method didn't set offset to expected number") } func TestBitmuncherSteBitsRead(t *testing.T) { bm := CreateBitMuncher(testData, 0) bm.SetBitsRead(8) - if bm.BitsRead() != 8 { - t.Fatal("Set bits read method didn't set bits read to expected value") - } + assert.Equal(t, bm.BitsRead(), 8, "Set bits read method didn't set bits read to expected value") } func TestBitmuncherReadBit(t *testing.T) { @@ -43,55 +41,37 @@ func TestBitmuncherReadBit(t *testing.T) { result |= byte(v) << byte(i) } - if result != testData[0] { - t.Fatal("result of rpeated 8 times get bit didn't return expected byte") - } + assert.Equal(t, result, testData[0], "result of rpeated 8 times get bit didn't return expected byte") } func TestBitmuncherGetBits(t *testing.T) { bm := CreateBitMuncher(testData, 0) - if byte(bm.GetBits(bitsPerByte)) != testData[0] { - t.Fatal("get bits didn't return expected value") - } + assert.Equal(t, byte(bm.GetBits(bitsPerByte)), testData[0], "get bits didn't return expected value") } func TestBitmuncherGetNoBits(t *testing.T) { bm := CreateBitMuncher(testData, 0) - if bm.GetBits(0) != 0 { - t.Fatal("get bits didn't return expected value: 0") - } + assert.Equal(t, bm.GetBits(0), uint32(0), "get bits didn't return expected value: 0") } func TestBitmuncherGetSignedBits(t *testing.T) { bm := CreateBitMuncher(testData, 0) - result := bm.GetSignedBits(6) - expected := -31 - if result != expected { - t.Fatal("get signed bits didn't return expected value", result, expected) - } + assert.Equal(t, bm.GetSignedBits(6), -31, "get signed bits didn't return expected value") } func TestBitmuncherGetNoSignedBits(t *testing.T) { bm := CreateBitMuncher(testData, 0) - result := bm.GetSignedBits(0) - expected := 0 - if result != expected { - t.Fatal("get signed bits didn't return expected value", result, expected) - } + assert.Equal(t, bm.GetSignedBits(0), 0, "get signed bits didn't return expected value") } func TestBitmuncherGetOneSignedBit(t *testing.T) { bm := CreateBitMuncher(testData, 0) - result := bm.GetSignedBits(1) - expected := -1 - if result != expected { - t.Fatal("get signed bits didn't return expected value", result, expected) - } + assert.Equal(t, bm.GetSignedBits(1), -1, "get signed bits didn't return expected value") } func TestBitmuncherSkipBits(t *testing.T) { @@ -99,9 +79,7 @@ func TestBitmuncherSkipBits(t *testing.T) { bm.SkipBits(bitsPerByte) - if bm.GetByte() != testData[1] { - t.Fatal("skipping 8 bits didn't moved bit muncher's position into next byte") - } + assert.Equal(t, bm.GetByte(), testData[1], "skipping 8 bits didn't moved bit muncher's position into next byte") } func TestBitmuncherGetInt32(t *testing.T) { @@ -113,9 +91,7 @@ func TestBitmuncherGetInt32(t *testing.T) { testInt |= int32(testData[i]) << int32(bitsPerByte*i) } - if bm.GetInt32() != testInt { - t.Fatal("int32 value wasn't returned properly") - } + assert.Equal(t, bm.GetInt32(), testInt, "int32 value wasn't returned properly") } func TestBitmuncherGetUint32(t *testing.T) { @@ -127,7 +103,5 @@ func TestBitmuncherGetUint32(t *testing.T) { testUint |= uint32(testData[i]) << uint32(bitsPerByte*i) } - if bm.GetUInt32() != testUint { - t.Fatal("uint32 value wasn't returned properly") - } + assert.Equal(t, bm.GetUInt32(), testUint, "uint32 value wasn't returned properly") } From 34bc9cc697a26b843c53c8c35b28f0a7b3f97d4c Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Tue, 23 Mar 2021 23:37:11 -0400 Subject: [PATCH 3/4] Satisfies golangci-lint --- d2common/d2datautils/bitmuncher_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2common/d2datautils/bitmuncher_test.go b/d2common/d2datautils/bitmuncher_test.go index f850da23..77d68784 100644 --- a/d2common/d2datautils/bitmuncher_test.go +++ b/d2common/d2datautils/bitmuncher_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" ) -var testData = []byte{33, 23, 4, 33, 192, 243} +var testData = []byte{33, 23, 4, 33, 192, 243} //nolint:gochecknoglobals func TestBitmuncherCopy(t *testing.T) { bm1 := CreateBitMuncher(testData, 0) From 804f4148d31da17f852d8cf413166e86d84f519e Mon Sep 17 00:00:00 2001 From: Will Roberts Date: Tue, 23 Mar 2021 23:40:24 -0400 Subject: [PATCH 4/4] Satisfies golangci-lint --- d2common/d2datautils/bitmuncher_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/d2common/d2datautils/bitmuncher_test.go b/d2common/d2datautils/bitmuncher_test.go index 77d68784..d8cfaf57 100644 --- a/d2common/d2datautils/bitmuncher_test.go +++ b/d2common/d2datautils/bitmuncher_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/assert" ) -var testData = []byte{33, 23, 4, 33, 192, 243} //nolint:gochecknoglobals +var testData = []byte{33, 23, 4, 33, 192, 243} //nolint:gochecknoglobals // just a test func TestBitmuncherCopy(t *testing.T) { bm1 := CreateBitMuncher(testData, 0)