Fix the rest of pdu tests
All checks were successful
Run Tests / Test (push) Successful in 28s
Benchmark BufferPool / RunBenchmarks (push) Successful in 38s

This commit is contained in:
2024-07-24 19:12:05 +02:00
parent ec04fa1fb6
commit 500cb11235
2 changed files with 141 additions and 138 deletions

View File

@@ -3,6 +3,7 @@ package pdu
import (
"bytes"
"encoding/binary"
"fmt"
)
type (
@@ -64,6 +65,9 @@ func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
return nil
}
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error {
if buf == nil {
return fmt.Errorf("cannot decode nil buffer")
}
binary.Read(buf, binary.BigEndian, &p.command_length)
binary.Read(buf, binary.BigEndian, &p.command_id)
binary.Read(buf, binary.BigEndian, &p.command_status)

View File

@@ -327,166 +327,165 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
// }
// }
// // region decode
// func TestDecodeHandlesShortByteSlice(t *testing.T) {
// var p PDU_HEADER
// data := []byte{0, 0, 0, 10}
// defer func() {
// if r := recover(); r != nil {
// t.Errorf("Decode panicked with short byte slice")
// }
// }()
// err := p.Decode(data)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// }
// region decode
func TestDecodeHandlesShortByteSlice(t *testing.T) {
var p PDU_HEADER
data := []byte{0, 0, 0, 10}
defer func() {
if r := recover(); r != nil {
t.Errorf("Decode panicked with short byte slice")
}
}()
err := p.Decode(bytes.NewBuffer(data))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
// func TestDecodeParsesValidByteSlice(t *testing.T) {
// var p PDU_HEADER
// data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
// err := p.Decode(data)
func TestDecodeParsesValidByteSlice(t *testing.T) {
var p PDU_HEADER
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
err := p.Decode(bytes.NewBuffer(data))
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// if p.command_length != 16 {
// t.Errorf("Expected command_length to be 16, got %d", p.command_length)
// }
// if p.command_id != 1 {
// t.Errorf("Expected command_id to be 1, got %d", p.command_id)
// }
// if p.command_status != 2 {
// t.Errorf("Expected command_status to be 2, got %d", p.command_status)
// }
// if p.sequence_number != 3 {
// t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number)
// }
// }
if p.command_length != 16 {
t.Errorf("Expected command_length to be 16, got %d", p.command_length)
}
if p.command_id != 1 {
t.Errorf("Expected command_id to be 1, got %d", p.command_id)
}
if p.command_status != 2 {
t.Errorf("Expected command_status to be 2, got %d", p.command_status)
}
if p.sequence_number != 3 {
t.Errorf("Expected sequence_number to be 3, got %d", p.sequence_number)
}
}
// func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
// var p PDU_HEADER
// data := make([]byte, 20)
// defer func() {
// if r := recover(); r != nil {
// t.Errorf("Decode panicked with long byte slice")
// }
// }()
// err := p.Decode(data)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// }
func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
var p PDU_HEADER
data := make([]byte, 20)
defer func() {
if r := recover(); r != nil {
t.Errorf("Decode panicked with long byte slice")
}
}()
err := p.Decode(bytes.NewBuffer(data))
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
}
// func TestDecodeHandlesNilDataInput(t *testing.T) {
// var p PDU_HEADER
// err := p.Decode(nil)
func TestDecodeHandlesNilDataInput(t *testing.T) {
var p PDU_HEADER
err := p.Decode(nil)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
if err == nil {
t.Errorf("Expected error, got none")
}
// if p.command_length != 0 {
// t.Errorf("Expected command_length to be 0, got %d", p.command_length)
// }
// if p.command_id != 0 {
// t.Errorf("Expected command_id to be 0, got %d", p.command_id)
// }
// if p.command_status != 0 {
// t.Errorf("Expected command_status to be 0, got %d", p.command_status)
// }
// if p.sequence_number != 0 {
// t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
// }
// }
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
}
if p.command_id != 0 {
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
}
if p.command_status != 0 {
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
}
if p.sequence_number != 0 {
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
}
}
// func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
// var p PDU_HEADER
// data := []byte{}
// err := p.Decode(data)
func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
var p PDU_HEADER
data := []byte{}
err := p.Decode(bytes.NewBuffer(data))
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// if p.command_length != 0 {
// t.Errorf("Expected command_length to be 0, got %d", p.command_length)
// }
// if p.command_id != 0 {
// t.Errorf("Expected command_id to be 0, got %d", p.command_id)
// }
// if p.command_status != 0 {
// t.Errorf("Expected command_status to be 0, got %d", p.command_status)
// }
// if p.sequence_number != 0 {
// t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
// }
// }
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
}
if p.command_id != 0 {
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
}
if p.command_status != 0 {
t.Errorf("Expected command_status to be 0, got %d", p.command_status)
}
if p.sequence_number != 0 {
t.Errorf("Expected sequence_number to be 0, got %d", p.sequence_number)
}
}
// func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
// var p PDU_HEADER
// data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
// originalData := make([]byte, len(data))
// copy(originalData, data)
func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
var p PDU_HEADER
data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
originalData := make([]byte, len(data))
copy(originalData, data)
// err := p.Decode(data)
err := p.Decode(bytes.NewBuffer(data))
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// for i := range data {
// if data[i] != originalData[i] {
// t.Errorf("Expected data at index %d to be %d, got %d", i, originalData[i], data[i])
// }
// }
// }
for i := range data {
if data[i] != originalData[i] {
t.Errorf("Expected data at index %d to be %d, got %d", i, originalData[i], data[i])
}
}
}
// func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
// var p PDU_HEADER
// data := []byte{255, 255, 255, 255, 255, 255, 255, 255}
// err := p.Decode(data)
func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
var p PDU_HEADER
data := []byte{255, 255, 255, 255, 255, 255, 255, 255}
err := p.Decode(bytes.NewBuffer(data))
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// if p.command_length != math.MaxUint32 {
// t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
// }
// if p.command_id != math.MaxUint32 {
// t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id)
// }
// }
if p.command_length != math.MaxUint32 {
t.Errorf("Expected command_length to be %d, got %d", math.MaxUint32, p.command_length)
}
if p.command_id != math.MaxUint32 {
t.Errorf("Expected command_id to be %d, got %d", math.MaxUint32, p.command_id)
}
}
// func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
// var p PDU_HEADER
// data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
// err := p.Decode(data)
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
var p PDU_HEADER
data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
err := p.Decode(bytes.NewBuffer(data))
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// if p.command_length != 0 {
// t.Errorf("Expected command_length to be 0, got %d", p.command_length)
// }
// if p.command_id != 0 {
// t.Errorf("Expected command_id to be 0, got %d", p.command_id)
// }
// }
if p.command_length != 0 {
t.Errorf("Expected command_length to be 0, got %d", p.command_length)
}
if p.command_id != 0 {
t.Errorf("Expected command_id to be 0, got %d", p.command_id)
}
}
// // region size
// func TestSizeReturns16(t *testing.T) {
// var p PDU_HEADER
// if p.Size() != 16 {
// t.Errorf("Expected size to be 16, got %d", p.Size())
// }
// }
// // region benchmarks
// region size
func TestSizeReturns16(t *testing.T) {
var p PDU_HEADER
if p.Size() != 16 {
t.Errorf("Expected size to be 16, got %d", p.Size())
}
}
// region benchmarks
// With buffer pool
func BenchmarkEncodeWithBufferPool(b *testing.B) {
p := &PDU_HEADER{