vail/cmd/vail/message_test.go

51 lines
1.0 KiB
Go
Raw Permalink Normal View History

package main
import (
"bytes"
"testing"
2020-05-30 22:23:53 -06:00
"time"
)
2022-05-15 15:57:12 -06:00
func TestMessageStruct(t *testing.T) {
2022-06-06 09:54:55 -06:00
m := Message{0x1122334455, 0, []uint16{0xaa, 0xbb, 0xcc}}
m2 := Message{12, 0, []uint16{1}}
2020-05-30 22:23:53 -06:00
if !m.Equal(m) {
t.Error("Equal messages did not compare equal")
}
if m.Equal(m2) {
t.Error("Unequal messages compared equal")
}
2022-06-06 09:54:55 -06:00
if m.Equal(Message{m.Timestamp, 0, []uint16{1, 2, 3}}) {
t.Error("Messages with different payloads compared equal")
}
2020-05-30 22:23:53 -06:00
bm, err := m.MarshalBinary()
if err != nil {
t.Error(err)
}
2022-06-06 09:54:55 -06:00
if !bytes.Equal(bm, []byte("\x00\x00\x00\x11\x22\x33\x44\x55\x00\x00\x00\xaa\x00\xbb\x00\xcc")) {
t.Error("Encoded wrong:", bm)
}
2020-05-30 22:23:53 -06:00
if err := m2.UnmarshalBinary(bm); err != nil {
t.Error(err)
}
2020-05-30 22:23:53 -06:00
if !m.Equal(m2) {
t.Error("Decoded wrong", m2)
}
2020-05-30 22:23:53 -06:00
m3 := NewMessage(
time.Unix(
0,
m.Timestamp*time.Millisecond.Nanoseconds(),
),
2022-05-15 17:38:57 -06:00
time.Duration(m.Duration[0])*time.Millisecond,
time.Duration(m.Duration[1])*time.Millisecond,
time.Duration(m.Duration[2])*time.Millisecond,
2020-05-30 22:23:53 -06:00
)
if !m.Equal(m3) {
t.Error("NewMessage didn't work", m, m3)
}
}