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