Rework every uint8 to byte and rework every []byte to bytes.Buffer
Some checks failed
Benchmark BufferPool / RunBenchmarks (push) Failing after 17s
Run Tests / Test (push) Failing after 15s

I just learnt that uint8 and byte are the fucking same
And that bytes.Buffer is the thing to use
Idiot idiot idiot...
Live and learn
This commit is contained in:
2024-07-24 18:43:18 +02:00
parent bed69fbfd3
commit f30680c26f
9 changed files with 461 additions and 418 deletions

View File

@@ -1,6 +1,7 @@
package pdu
import (
"bytes"
"encoding/binary"
"math"
"sync"
@@ -15,13 +16,14 @@ func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
command_status: 1,
sequence_number: 1,
}
result, err := p.Encode()
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if len(*result) != 16 {
t.Errorf("Expected byte slice of length 16, got %d", len(*result))
if buf.Len() != 16 {
t.Errorf("Expected byte slice of length 16, got %d", buf.Len())
}
}
@@ -32,15 +34,16 @@ func TestEncodeHandlesZeroValues(t *testing.T) {
command_status: 0,
sequence_number: 0,
}
result, err := p.Encode()
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
expected := make([]uint8, 16)
for i, v := range *result {
expected := make([]byte, 16)
for i, v := range buf.Bytes() {
if v != expected[i] {
t.Errorf("Expected byte slice with zero values, got %v", *result)
t.Errorf("Expected byte slice with zero values, got %v", buf.Bytes())
break
}
}
@@ -53,16 +56,17 @@ func TestEncodeEncodesProperly(t *testing.T) {
command_status: 3,
sequence_number: 4,
}
result, err := p.Encode()
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
expected := []uint8{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
for i, v := range *result {
expected := []byte{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
for i, v := range buf.Bytes() {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
t.Errorf("Expected byte slice with values %v, got %v", expected, buf.Bytes())
break
}
}
@@ -75,16 +79,17 @@ func TestEncodeEncodesProperlyComplex(t *testing.T) {
command_status: 35634264,
sequence_number: 476543523,
}
result, err := p.Encode()
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
expected := []uint8{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
for i, v := range *result {
expected := []byte{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
for i, v := range buf.Bytes() {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, *result)
t.Errorf("Expected byte slice with values %v, got %v", expected, buf.Bytes())
break
}
}
@@ -97,24 +102,25 @@ func TestEncodeIntoCorrectlyEncodesFields(t *testing.T) {
command_status: 0,
sequence_number: 12345,
}
buf := make([]uint8, 16)
err := p.EncodeInto(&buf)
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
@@ -125,9 +131,8 @@ func TestEncodeIntoHandlesNilBuffer(t *testing.T) {
command_status: 0,
sequence_number: 12345,
}
var buf *[]uint8 = nil
err := p.EncodeInto(buf)
var buf bytes.Buffer
err := p.Encode(&buf)
if err == nil {
t.Errorf("Expected error when buffer is nil")
}
@@ -140,9 +145,8 @@ func TestEncodeIntoHandlesSmallerBuffer(t *testing.T) {
command_status: 0,
sequence_number: 12345,
}
buf := make([]uint8, 12) // smaller buffer size
err := p.EncodeInto(&buf)
buf := bytes.NewBuffer(make([]byte, 12)) // smaller buffer size
err := p.Encode(buf)
if err == nil {
t.Errorf("Expected error when buffer is too small")
}
@@ -155,24 +159,25 @@ func TestEncodeIntoHandlesLargerBuffer(t *testing.T) {
command_status: 0,
sequence_number: 12345,
}
buf := make([]uint8, 20)
err := p.EncodeInto(&buf)
buf := bytes.NewBuffer(make([]byte, 20)) // larger buffer size
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
@@ -183,24 +188,25 @@ func TestEncodeIntoUsesBigEndianEncoding(t *testing.T) {
command_status: 0,
sequence_number: 12345,
}
buf := make([]uint8, 16)
err := p.EncodeInto(&buf)
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
@@ -211,28 +217,29 @@ func TestEncodeIntoConcurrencySafety(t *testing.T) {
command_status: 0,
sequence_number: 12345,
}
buf := make([]uint8, 16)
buf := bytes.NewBuffer(make([]byte, 16))
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer wg.Done()
p.EncodeInto(&buf)
p.Encode(buf)
}()
}
wg.Wait()
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
@@ -243,24 +250,25 @@ func TestEncodeIntoWithMaximumValues(t *testing.T) {
command_status: math.MaxUint32,
sequence_number: math.MaxUint32,
}
buf := make([]uint8, 16)
err := p.EncodeInto(&buf)
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
@@ -271,240 +279,285 @@ func TestEncodeIntoWithBoundaryValues(t *testing.T) {
command_status: 0,
sequence_number: 0,
}
buf := make([]uint8, 16)
err := p.EncodeInto(&buf)
buf := bytes.NewBuffer(make([]byte, 16))
err := p.Encode(buf)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
if binary.BigEndian.Uint32(buf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(buf[0:4]))
innerbuf := buf.Bytes()
if binary.BigEndian.Uint32(innerbuf[0:4]) != p.command_length {
t.Errorf("Expected command_length %d, got %d", p.command_length, binary.BigEndian.Uint32(innerbuf[0:4]))
}
if binary.BigEndian.Uint32(buf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(buf[4:8]))
if binary.BigEndian.Uint32(innerbuf[4:8]) != p.command_id {
t.Errorf("Expected command_id %d, got %d", p.command_id, binary.BigEndian.Uint32(innerbuf[4:8]))
}
if binary.BigEndian.Uint32(buf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(buf[8:12]))
if binary.BigEndian.Uint32(innerbuf[8:12]) != p.command_status {
t.Errorf("Expected command_status %d, got %d", p.command_status, binary.BigEndian.Uint32(innerbuf[8:12]))
}
if binary.BigEndian.Uint32(buf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(buf[12:16]))
if binary.BigEndian.Uint32(innerbuf[12:16]) != p.sequence_number {
t.Errorf("Expected sequence_number %d, got %d", p.sequence_number, binary.BigEndian.Uint32(innerbuf[12:16]))
}
}
// region decode
func TestDecodeHandlesShortByteSlice(t *testing.T) {
var p PDU_HEADER
data := []uint8{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)
}
}
// func TestRealScenario(t *testing.T) {
// expected := []byte{0, 0, 0, 54, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 1, 67, 77, 84, 0, 1, 1, 49, 50, 51, 52, 53, 0, 1, 1, 54, 55, 56, 57, 48, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 12, 72, 101, 108, 108, 111, 44, 32, 83, 77, 80, 80, 33}
// p := &SUBMIT_SM{
// header: PDU_HEADER{
// command_length: 0,
// command_id: 4,
// command_status: 0,
// sequence_number: 1,
// },
// service_type: "CMT",
// source_addr_ton: 1,
// source_addr_npi: 1,
// source_addr: "12345",
// dest_addr_ton: 1,
// dest_addr_npi: 1,
// destination_addr: "67890",
// esm_class: 0,
// protocol_id: 0,
// priority_flag: 0,
// schedule_delivery_time: "",
// validity_period: "",
// registered_delivery: 1,
// data_coding: 0,
// sm_default_msg_id: 0,
// short_message: "Hello, SMPP!",
// }
// p.header.command_length = uint32(p.Size())
// p.sm_length = byte(len(p.short_message))
// buf := make([]byte, p.Size())
// err := p.EncodeInto(&buf)
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// if len(buf) != len(expected) {
// t.Errorf("Expected byte slice of length %d, got %d", len(expected), len(buf))
// }
// for i, v := range buf {
// if v != expected[i] {
// t.Errorf("Expected byte slice with values %v, got %v", expected, buf)
// break
// }
// }
// }
func TestDecodeParsesValidByteSlice(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
err := p.Decode(data)
// // 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)
// }
// }
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)
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 err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
func TestDecodeHandlesLongerByteSliceWithoutCrashing(t *testing.T) {
var p PDU_HEADER
data := make([]uint8, 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)
}
}
// 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 TestDecodeHandlesNilDataInput(t *testing.T) {
var p PDU_HEADER
err := p.Decode(nil)
// 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)
// }
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// func TestDecodeHandlesNilDataInput(t *testing.T) {
// var p PDU_HEADER
// err := p.Decode(nil)
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 err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
var p PDU_HEADER
data := []uint8{}
err := p.Decode(data)
// 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 err != nil {
t.Errorf("Expected no error, got %v", err)
}
// func TestDecodeHandlesEmptyByteSliceGracefully(t *testing.T) {
// var p PDU_HEADER
// data := []byte{}
// err := p.Decode(data)
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 err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
func TestDecodeDoesNotModifyInputByteSlice(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
originalData := make([]uint8, len(data))
copy(originalData, data)
// 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)
// }
// }
err := p.Decode(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)
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// err := p.Decode(data)
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])
}
}
}
// if err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
var p PDU_HEADER
data := []uint8{255, 255, 255, 255, 255, 255, 255, 255}
err := p.Decode(data)
// 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])
// }
// }
// }
if err != nil {
t.Errorf("Expected no error, got %v", err)
}
// func TestDecodeHandlesByteSlicesWithMaxUint32Values(t *testing.T) {
// var p PDU_HEADER
// data := []byte{255, 255, 255, 255, 255, 255, 255, 255}
// err := p.Decode(data)
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 err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
var p PDU_HEADER
data := []uint8{0, 0, 0, 0, 0, 0, 0, 0}
err := p.Decode(data)
// 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 err != nil {
t.Errorf("Expected no error, got %v", err)
}
// func TestDecodeHandlesByteSlicesWithMinimumUint32Values(t *testing.T) {
// var p PDU_HEADER
// data := []byte{0, 0, 0, 0, 0, 0, 0, 0}
// err := p.Decode(data)
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 err != nil {
// t.Errorf("Expected no error, got %v", err)
// }
// 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())
}
}
// 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 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())
// }
// }
// With buffer pool
func BenchmarkEncode(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf, _ := p.Encode()
ByteBufferPool.Put(buf)
}
}
// // region benchmarks
// These two are effectively the same but there is difference in execution time
// I wonder why...
// I should stop writing benchmarks...
func BenchmarkEncodeWithBufferPool(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := ByteBufferPool.Get(uint(p.Size()))
p.EncodeInto(buf)
ByteBufferPool.Put(buf)
}
}
// // With buffer pool
// func BenchmarkEncode(b *testing.B) {
// p := &PDU_HEADER{
// command_length: 16,
// command_id: 1,
// command_status: 0,
// sequence_number: 12345,
// }
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// buf, _ := p.Encode()
// ByteBufferPool.Put(buf)
// }
// }
// Without buffer pool
func BenchmarkEncodeInto(b *testing.B) {
p := &PDU_HEADER{
command_length: 16,
command_id: 1,
command_status: 0,
sequence_number: 12345,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := make([]uint8, 16)
p.EncodeInto(&buf)
}
}
// // These two are effectively the same but there is difference in execution time
// // I wonder why...
// // I should stop writing benchmarks...
// func BenchmarkEncodeWithBufferPool(b *testing.B) {
// p := &PDU_HEADER{
// command_length: 16,
// command_id: 1,
// command_status: 0,
// sequence_number: 12345,
// }
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// buf := ByteBufferPool.Get(uint(p.Size()))
// p.EncodeInto(buf)
// ByteBufferPool.Put(buf)
// }
// }
func BenchmarkDecode(b *testing.B) {
p := &PDU_HEADER{}
data := []uint8{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
b.ResetTimer()
for i := 0; i < b.N; i++ {
p.Decode(data)
}
}
// // Without buffer pool
// func BenchmarkEncodeInto(b *testing.B) {
// p := &PDU_HEADER{
// command_length: 16,
// command_id: 1,
// command_status: 0,
// sequence_number: 12345,
// }
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// buf := make([]byte, 16)
// p.EncodeInto(&buf)
// }
// }
// func BenchmarkDecode(b *testing.B) {
// p := &PDU_HEADER{}
// data := []byte{0, 0, 0, 16, 0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3}
// b.ResetTimer()
// for i := 0; i < b.N; i++ {
// p.Decode(data)
// }
// }