jl-iot/service/model/dc_test.go

46 lines
1010 B
Go

package model
import (
"testing"
)
func TestEncodeBytes(t *testing.T) {
want := byte(0b11011000)
bits := []bool{false, false, false, true, true, false, true, true}
bs := encodeBytes(bits)
if bs[0] != want {
t.Errorf("encodeBytes(%v) = %v, want %v", bits, bs, want)
}
}
func TestDecodeBools(t *testing.T) {
want := []bool{false, false, false, true, true, false, true, true}
bs := []byte{0xD8}
bits := decodeBools(bs)
if !sliceEquals(bits, want) {
t.Errorf("decodeBits(%v) = %v, want %v", bs, bits, want)
}
}
func TestBytesCopy(t *testing.T) {
want := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
dst := []byte{0x01, 0x02, 0x03, 0x01, 0x02, 0x03, 0x07, 0x08}
src := []byte{0x04, 0x05, 0x06}
copy(dst[3:], src)
if !sliceEquals(dst, want) {
t.Errorf("byteCopy(%v, %v) = %v, want %v", dst, src, dst, want)
}
}
func sliceEquals[T comparable](a, b []T) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}