jl-iot/service/model/dc_test.go

46 lines
1010 B
Go
Raw Normal View History

2023-12-08 18:08:15 +08:00
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)
2023-12-08 18:08:15 +08:00
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)
2023-12-08 18:08:15 +08:00
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
}