Add tests for pdu encoding

This commit is contained in:
PhatPhuckDave
2024-07-22 21:56:14 +02:00
parent 8ebca73181
commit 7f52eccc6c

67
pdu/pdu_test.go Normal file
View File

@@ -0,0 +1,67 @@
package pdu
import "testing"
func TestEncodeReturnsByteSliceOfLength16(t *testing.T) {
p := &PDU_HEADER{
command_length: 1,
command_id: 1,
command_status: 1,
sequence_number: 1,
}
result := p.Encode()
if len(result) != 16 {
t.Errorf("Expected byte slice of length 16, got %d", len(result))
}
}
func TestEncodeHandlesZeroValues(t *testing.T) {
p := &PDU_HEADER{
command_length: 0,
command_id: 0,
command_status: 0,
sequence_number: 0,
}
result := p.Encode()
expected := make([]byte, 16)
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected byte slice with zero values, got %v", result)
break
}
}
}
func TestEncodeEncodesProperly(t *testing.T) {
p := &PDU_HEADER{
command_length: 1,
command_id: 2,
command_status: 3,
sequence_number: 4,
}
result := p.Encode()
expected := []byte{0, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 4}
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, result)
break
}
}
}
func TestEncodeEncodesProperlyComplex(t *testing.T) {
p := &PDU_HEADER{
command_length: 13426724,
command_id: 254352,
command_status: 35634264,
sequence_number: 476543523,
}
result := p.Encode()
expected := []byte{0, 204, 224, 36, 0, 3, 225, 144, 2, 31, 188, 88, 28, 103, 122, 35}
for i, v := range result {
if v != expected[i] {
t.Errorf("Expected byte slice with values %v, got %v", expected, result)
break
}
}
}