| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 | package networkimport (	"bytes"	"testing")const (	testHex1 = "0x0a 0x0b 0x0c 0x0d"	testHex2 = "0a 0b 0c 0d")var (	testBytes = []byte{0x0a, 0x0b, 0x0c, 0x0d})func TestHex2Bytes(t *testing.T) {	if b, ok := Hex2Bytes(testHex1); !ok {		t.Error("Hex2Bytes failed:", testHex1)		return	} else {		t.Logf("testHex1: %s === %v", testHex1, b)	}	if b, ok := Hex2Bytes(testHex2); !ok {		t.Error("Hex2Bytes failed:", testHex2)		return	} else {		t.Logf("testHex2: %s === %v", testHex2, b)	}}func TestCRC16Modbus(t *testing.T) {	crcResult, ok := Hex2Bytes("FB B6") // 大端模式 251,182	if !ok {		t.Error("build crc result failed:", crcResult)		return	}	crc := CRC16Modbus(testBytes)	if !bytes.Equal(crcResult, BigEndian.PutUint16(crc)) {		t.Errorf("needed: %v, got: %v", crcResult, crc)	}}func TestRemake(t *testing.T) {	old := testBytes[:2] // question: len == 2, cap == 4	b := Remake(old)     // wants: len == 2, cap == 2	if len(b) != cap(b) {		t.Errorf("remake failed: len(%d), cap(%d)", len(b), cap(b))	}}func TestBytesEqual(t *testing.T) {	if !BytesEqual([]byte{0xa, 0xb}, testBytes[:2]) {		t.Error("failed")	}}func TestName(t *testing.T) {	b := []byte{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09}	t.Log(b[:6])}
 |