github.com/iDigitalFlame/xmt@v0.5.4/com/packet_test.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package com
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/iDigitalFlame/xmt/device/local"
    24  )
    25  
    26  func TestPacket(t *testing.T) {
    27  	b := Packet{
    28  		ID:     120,
    29  		Job:    29000,
    30  		Device: local.Device.ID,
    31  	}
    32  	b.WriteInt32(0xFF)
    33  	b.WriteFloat32(1.45)
    34  	b.WriteInt8(120)
    35  	b.WriteString("derp123")
    36  	b.WriteUint64(0xFF00FF00FF123)
    37  	b.WriteUint16Pos(0, 0xFF)
    38  	var w bytes.Buffer
    39  	if err := b.Marshal(&w); err != nil {
    40  		t.Fatalf("TestPacket(): Marshal failed with error: %s!", err.Error())
    41  	}
    42  	var r Packet
    43  	if err := r.Unmarshal(bytes.NewReader(w.Bytes())); err != nil {
    44  		t.Fatalf("TestPacket(): Unmarshal failed with error: %s!", err.Error())
    45  	}
    46  	v, err := r.Int32()
    47  	if err != nil {
    48  		t.Fatalf("TestPacket(): Int32 failed with error: %s!", err.Error())
    49  	}
    50  	if v != 0xFF00FF {
    51  		t.Fatalf(`TestPacket(): Int32 result "0x%X" does not match the expected value "0xFF00FF"!`, v)
    52  	}
    53  	f, err := r.Float32()
    54  	if err != nil {
    55  		t.Fatalf("TestPacket(): Float32 failed with error: %s!", err.Error())
    56  	}
    57  	if f != 1.45 {
    58  		t.Fatalf(`TestPacket(): Float32 result "%.2f" does not match the expected value "1.45"!`, f)
    59  	}
    60  	n, err := r.Int8()
    61  	if err != nil {
    62  		t.Fatalf("TestPacket(): Int8 failed with error: %s!", err.Error())
    63  	}
    64  	if n != 120 {
    65  		t.Fatalf(`TestPacket(): Int8 result "%d" does not match the expected value "120"!`, n)
    66  	}
    67  	s, err := r.StringVal()
    68  	if err != nil {
    69  		t.Fatalf("TestPacket(): StringVal failed with error: %s!", err.Error())
    70  	}
    71  	if s != "derp123" {
    72  		t.Fatalf(`TestPacket(): StringVal result "%s" does not match the expected value 120!`, s)
    73  	}
    74  	u, err := r.Uint64()
    75  	if err != nil {
    76  		t.Fatalf("TestPacket(): Uint64 failed with error: %s!", err.Error())
    77  	}
    78  	if u != 0xFF00FF00FF123 {
    79  		t.Fatalf(`TestPacket(): Uint64 result "0x%X" does not match the expected value "0xFF00FF00FF123"!`, u)
    80  	}
    81  }