Skip to content

Commit

Permalink
optimize Add unit testing for common (#229)
Browse files Browse the repository at this point in the history
* add unit test

* add unit test

* add unit test

Co-authored-by: miaoxueyu <miaoxueyu@xs901.com>
  • Loading branch information
miaoxueyu and miaoxueyu committed Aug 24, 2022
1 parent 4971578 commit 27ea336
Show file tree
Hide file tree
Showing 2 changed files with 356 additions and 0 deletions.
172 changes: 172 additions & 0 deletions pkg/common/bytes/buf_helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
package bytes

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
)

func TestReadBytes(t *testing.T) {
bytes := []byte{2, 3, 4, 5, 6}
byteBuffer := NewByteBuffer(bytes)
readBytes := ReadBytes(len(bytes), byteBuffer)
assert.Equal(t, bytes, readBytes)
}

func TestReadUint8(t *testing.T) {
bytes := []byte{255}
byteBuffer := NewByteBuffer(bytes)
readUint8 := ReadUint8(byteBuffer)
assert.Equal(t, readUint8, bytes[0])
}

func TestReadUInt16(t *testing.T) {
bytes := []byte{255, 255}
byteBuffer := NewByteBuffer(bytes)
readUint16 := ReadUInt16(byteBuffer)
assert.Equal(t, readUint16, uint16(math.MaxUint16))
}

func TestReadUInt32(t *testing.T) {
bytes := []byte{255, 255, 255, 255}
byteBuffer := NewByteBuffer(bytes)
readUInt32 := ReadUInt32(byteBuffer)
assert.Equal(t, readUInt32, uint32(math.MaxUint32))
}

func TestReadUInt64(t *testing.T) {
bytes := []byte{255, 255, 255, 255, 255, 255, 255, 255}
byteBuffer := NewByteBuffer(bytes)
readUInt64 := ReadUInt64(byteBuffer)
assert.Equal(t, readUInt64, uint64(math.MaxUint64))
}

func TestReadString8(t *testing.T) {
bytes := []byte("A")
byteBuffer := NewByteBuffer(bytes)
readString8 := ReadString8(byteBuffer)
assert.Equal(t, readString8, "A")
}

func TestRead1String16(t *testing.T) {
bytes := []byte("seata_test")
byteBuffer := NewByteBuffer(bytes)
readString16 := Read1String16(byteBuffer)
assert.Equal(t, readString16, "se")
}

func TestReadString32(t *testing.T) {
bytes := []byte("seata_test")
byteBuffer := NewByteBuffer(bytes)
readString32 := ReadString32(byteBuffer)
assert.Equal(t, readString32, "seat")
}

func TestReadString64(t *testing.T) {
bytes := []byte("seata_test")
byteBuffer := NewByteBuffer(bytes)
readString64 := ReadString64(byteBuffer)
assert.Equal(t, readString64, "seata_te")
}

func generateStringByLength(length int) string {
// a-z loop generate
// a == 97
// z == 122
letterCount := 26
bytes := make([]byte, length)
for i := 0; i < length; i++ {
bytes[i] = byte(97 + (i % letterCount))
}
return string(bytes)
}

func ReadStringNLengthTest(
t *testing.T,
length int,
writeLengthFun func(length int, byteBuffer ByteBuffer),
readStringFun func(buf *ByteBuffer) string,
) {
str := generateStringByLength(length)
byteBuffer := NewByteBuffer([]byte{})

// write length and content
writeLengthFun(len(str), *byteBuffer)
_, _ = byteBuffer.WriteString(str)

readString := readStringFun(byteBuffer)
assert.Equal(t, readString, generateStringByLength(length))
}

func TestReadString8Length(t *testing.T) {
writeLengthFun := func(length int, byteBuffer ByteBuffer) {
_ = byteBuffer.WriteByte(byte(length))
}

ReadStringNLengthTest(t, 0, writeLengthFun, ReadString8Length)
ReadStringNLengthTest(t, 255, writeLengthFun, ReadString8Length)
}

func TestReadString16Length(t *testing.T) {
writeFun := func(length int, byteBuffer ByteBuffer) {
_, _ = byteBuffer.WriteUint16(uint16(length))
}

ReadStringNLengthTest(t, 0, writeFun, ReadString16Length)
ReadStringNLengthTest(t, math.MaxInt16, writeFun, ReadString16Length)
}

func TestReadString32Length(t *testing.T) {
writeFun := func(length int, byteBuffer ByteBuffer) {
_, _ = byteBuffer.WriteUint32(uint32(length))
}

ReadStringNLengthTest(t, 0, writeFun, ReadString32Length)
ReadStringNLengthTest(t, math.MaxInt8, writeFun, ReadString32Length)
}

func TestReadString64Length(t *testing.T) {
writeFun := func(length int, byteBuffer ByteBuffer) {
_, _ = byteBuffer.WriteUint64(uint64(length))
}

ReadStringNLengthTest(t, 0, writeFun, ReadString64Length)
ReadStringNLengthTest(t, math.MaxInt8, writeFun, ReadString64Length)
}

func WriteStringNLengthTest(
t *testing.T,
length int,
writeStringFun func(value string, buf *ByteBuffer),
readStringFun func(buf *ByteBuffer) string,
) {
str := generateStringByLength(length)
byteBuffer := NewByteBuffer([]byte{})

// write length and content
writeStringFun(str, byteBuffer)

readString := readStringFun(byteBuffer)
assert.Equal(t, readString, generateStringByLength(length))
}

func TestWriteString8Length(t *testing.T) {
WriteStringNLengthTest(t, 0, WriteString8Length, ReadString8Length)
WriteStringNLengthTest(t, math.MaxInt8, WriteString8Length, ReadString8Length)
}

func TestWriteString16Length(t *testing.T) {
WriteStringNLengthTest(t, 0, WriteString16Length, ReadString16Length)
WriteStringNLengthTest(t, math.MaxInt8, WriteString16Length, ReadString16Length)
}

func TestWriteString32Length(t *testing.T) {
WriteStringNLengthTest(t, 0, WriteString32Length, ReadString32Length)
WriteStringNLengthTest(t, math.MaxInt8, WriteString32Length, ReadString32Length)
}

func TestWriteString64Length(t *testing.T) {
WriteStringNLengthTest(t, 0, WriteString64Length, ReadString64Length)
WriteStringNLengthTest(t, math.MaxInt8, WriteString64Length, ReadString64Length)
}
184 changes: 184 additions & 0 deletions pkg/common/bytes/buf_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
package bytes

import (
"math"
"testing"

"github.com/stretchr/testify/assert"
)

func TestBytes(t *testing.T) {
bytes := []byte{2, 3, 4, 5, 6}
byteBuffer := NewByteBuffer(bytes)
assert.Equal(t, byteBuffer.Bytes(), bytes)
}

func TestRead(t *testing.T) {
bytes := []byte{2, 3, 4, 5, 6}
byteBuffer := NewByteBuffer(bytes)
newBytes := make([]byte, len(bytes))
_, _ = byteBuffer.Read(newBytes)
assert.Equal(t, bytes, newBytes)
}

func TestReadByte(t *testing.T) {
bytes := []byte{2, 3, 4, 5, 6}
byteBuffer := NewByteBuffer(bytes)
for _, byteItem := range bytes {
readByte, _ := byteBuffer.ReadByte()
assert.Equal(t, byteItem, readByte)
}
}

func TestReadInt64(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{127, 255, 255, 255, 255, 255, 255, 255})
readInt64, _ := byteBuffer.ReadInt64()
assert.Equal(t, readInt64, int64(math.MaxInt64))
}

func TestReadUint16(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{255, 255})
readInt16, _ := byteBuffer.ReadUint16()
assert.Equal(t, readInt16, uint16(math.MaxUint16))
}

func TestReadUint32(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{255, 255, 255, 255})
readInt32, _ := byteBuffer.ReadUint32()
assert.Equal(t, readInt32, uint32(math.MaxUint32))
}

func TestReadUint64(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{255, 255, 255, 255, 255, 255, 255, 255})
readUint64, _ := byteBuffer.ReadUint64()
assert.Equal(t, readUint64, uint64(math.MaxUint64))
}

func TestWrite(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_, err := byteBuffer.Write([]byte{255, 255})
if err != nil {
t.Error()
}
readUint16, _ := byteBuffer.ReadUint16()
assert.Equal(t, readUint16, uint16(math.MaxUint16))
}

func TestWriteString(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_, err := byteBuffer.WriteString("seata")
if err != nil {
t.Error()
return
}
seataBytes := []byte{115, 101, 97, 116, 97}
for _, byteItem := range seataBytes {
readByte, _ := byteBuffer.ReadByte()
assert.Equal(t, byteItem, readByte)
}
}

func TestWriteByte(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_ = byteBuffer.WriteByte(1)
readByte, _ := byteBuffer.ReadByte()
assert.Equal(t, readByte, byte(1))
}

func TestWriteUint16(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_, _ = byteBuffer.WriteUint16(uint16(math.MaxUint16))
readUint16, _ := byteBuffer.ReadUint16()
assert.Equal(t, uint16(math.MaxUint16), readUint16)
}

func TestWriteUint32(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_, _ = byteBuffer.WriteUint32(uint32(math.MaxUint32))
readUint32, _ := byteBuffer.ReadUint32()
assert.Equal(t, uint32(math.MaxUint32), readUint32)
}

func TestWriteUint64(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_, _ = byteBuffer.WriteUint64(uint64(math.MaxUint64))
readUint64, _ := byteBuffer.ReadUint64()
assert.Equal(t, uint64(math.MaxUint64), readUint64)
}

func TestWriteInt64(t *testing.T) {
byteBuffer := NewByteBuffer([]byte{})
_, _ = byteBuffer.WriteInt64(int64(math.MaxInt64))
readInt64, _ := byteBuffer.ReadInt64()
assert.Equal(t, int64(math.MaxInt64), readInt64)
}

func TestByte2Int64(t *testing.T) {
byte2Int64 := Byte2Int64([]byte{127, 255, 255, 255, 255, 255, 255, 255})
assert.Equal(t, byte2Int64, int64(math.MaxInt64))
}

func TestByte2UInt16(t *testing.T) {
byte2UInt16 := Byte2UInt16([]byte{255, 255})
assert.Equal(t, byte2UInt16, uint16(math.MaxUint16))
}

func TestByte2UInt32(t *testing.T) {
byte2UInt32 := Byte2UInt32([]byte{255, 255, 255, 255})
assert.Equal(t, byte2UInt32, uint32(math.MaxUint32))
}

func TestInt2BytesTo(t *testing.T) {
bytes := make([]byte, 4)
Int2BytesTo(math.MaxInt64, bytes)
assert.Equal(t, bytes, []byte{255, 255, 255, 255})
}

func TestInt2Bytes(t *testing.T) {
bytes := Int2Bytes(math.MaxInt64)
assert.Equal(t, bytes, []byte{255, 255, 255, 255})
}

func TestInt64ToBytesTo(t *testing.T) {
bytes := make([]byte, 8)
Int64ToBytesTo(math.MaxInt64, bytes)
assert.Equal(t, bytes, []byte{127, 255, 255, 255, 255, 255, 255, 255})
}

func TestUint64ToBytesTo(t *testing.T) {
bytes := make([]byte, 8)
Uint64ToBytesTo(math.MaxUint64, bytes)
assert.Equal(t, bytes, []byte{255, 255, 255, 255, 255, 255, 255, 255})
}

func TestInt64ToBytes(t *testing.T) {
bytes := Int64ToBytes(math.MaxInt64)
assert.Equal(t, bytes, []byte{127, 255, 255, 255, 255, 255, 255, 255})
}

func TestUint32ToBytesTo(t *testing.T) {
bytes := make([]byte, 4)
Uint32ToBytesTo(math.MaxUint32, bytes)
assert.Equal(t, bytes, []byte{255, 255, 255, 255})
}

func TestUInt32ToBytes(t *testing.T) {
bytes := UInt32ToBytes(math.MaxUint32)
assert.Equal(t, bytes, []byte{255, 255, 255, 255})
}

func TestUint16ToBytesTo(t *testing.T) {
bytes := make([]byte, 2)
Uint16ToBytesTo(math.MaxUint16, bytes)
assert.Equal(t, bytes, []byte{255, 255})
}

func TestUInt16ToBytes(t *testing.T) {
bytes := UInt16ToBytes(math.MaxUint16)
assert.Equal(t, bytes, []byte{255, 255})
}

func TestUInt64ToBytes(t *testing.T) {
bytes := UInt64ToBytes(math.MaxUint64)
assert.Equal(t, bytes, []byte{255, 255, 255, 255, 255, 255, 255, 255})
}

0 comments on commit 27ea336

Please sign in to comment.