| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 | package networkimport (	"bytes"	"testing")const (	testHex1 = "0x0a 0x0b 0x0c 0x0d"	testHex2 = "0a 0b 0c 0d"	test16 = 65535)var (	testBytes   = []byte{0x0a, 0x0b, 0x0c, 0x0d}	testBytes16 = []byte{0xff, 0xff})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 TestByte2Hex(t *testing.T) {	if s := Byte2Hex(testBytes[0]); s != "0a" {		t.Error("Byte2Hex failed:", s)		return	} else {		t.Log(s)	}}func TestBytes2Hex(t *testing.T) {	if s := Bytes2Hex(testBytes); s != testHex2 {		t.Error("Bytes2Hex failed:", s)		return	} else {		t.Log(s)	}}func TestUint16BytesBig(t *testing.T) {	src := Uint16BytesBig(test16)	if src[0] == testBytes16[0] && src[1] == testBytes16[1] {		t.Log(src)		return	}	t.Error("Uint16BytesBig failed:", src)}func TestUint32BytesBig(t *testing.T) {	src := Uint32BytesBig(test16)	if src[2] == testBytes16[0] && src[3] == testBytes16[1] {		t.Log(src)		return	}	t.Error("Uint32BytesBig failed:", src)}func TestUin64Bytes(t *testing.T) {	src := Uin64BytesBig(test16)	if src[6] == testBytes16[0] && src[7] == testBytes16[1] {		t.Log(src)		return	}	t.Error("Uin64BytesBig failed:", src)}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, Uint16BytesBig(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])}
 |