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 ( import (
"bytes" "bytes"
"encoding/binary" "encoding/binary"
"fmt"
) )
type ( type (
@@ -64,6 +65,9 @@ func (p *PDU_HEADER) Encode(buf *bytes.Buffer) error {
return nil return nil
} }
func (p *PDU_HEADER) Decode(buf *bytes.Buffer) error { 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_length)
binary.Read(buf, binary.BigEndian, &p.command_id) binary.Read(buf, binary.BigEndian, &p.command_id)
binary.Read(buf, binary.BigEndian, &p.command_status) binary.Read(buf, binary.BigEndian, &p.command_status)

View File

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