You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

buf_test.go 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package bytes
  18. import (
  19. "math"
  20. "testing"
  21. "github.com/stretchr/testify/assert"
  22. )
  23. func TestBytes(t *testing.T) {
  24. bytes := []byte{2, 3, 4, 5, 6}
  25. byteBuffer := NewByteBuffer(bytes)
  26. assert.Equal(t, byteBuffer.Bytes(), bytes)
  27. }
  28. func TestRead(t *testing.T) {
  29. bytes := []byte{2, 3, 4, 5, 6}
  30. byteBuffer := NewByteBuffer(bytes)
  31. newBytes := make([]byte, len(bytes))
  32. _, _ = byteBuffer.Read(newBytes)
  33. assert.Equal(t, bytes, newBytes)
  34. }
  35. func TestReadByte(t *testing.T) {
  36. bytes := []byte{2, 3, 4, 5, 6}
  37. byteBuffer := NewByteBuffer(bytes)
  38. for _, byteItem := range bytes {
  39. readByte, _ := byteBuffer.ReadByte()
  40. assert.Equal(t, byteItem, readByte)
  41. }
  42. }
  43. func TestReadInt64(t *testing.T) {
  44. byteBuffer := NewByteBuffer([]byte{127, 255, 255, 255, 255, 255, 255, 255})
  45. readInt64, _ := byteBuffer.ReadInt64()
  46. assert.Equal(t, readInt64, int64(math.MaxInt64))
  47. }
  48. func TestReadUint16(t *testing.T) {
  49. byteBuffer := NewByteBuffer([]byte{255, 255})
  50. readInt16, _ := byteBuffer.ReadUint16()
  51. assert.Equal(t, readInt16, uint16(math.MaxUint16))
  52. }
  53. func TestReadUint32(t *testing.T) {
  54. byteBuffer := NewByteBuffer([]byte{255, 255, 255, 255})
  55. readInt32, _ := byteBuffer.ReadUint32()
  56. assert.Equal(t, readInt32, uint32(math.MaxUint32))
  57. }
  58. func TestReadUint64(t *testing.T) {
  59. byteBuffer := NewByteBuffer([]byte{255, 255, 255, 255, 255, 255, 255, 255})
  60. readUint64, _ := byteBuffer.ReadUint64()
  61. assert.Equal(t, readUint64, uint64(math.MaxUint64))
  62. }
  63. func TestWrite(t *testing.T) {
  64. byteBuffer := NewByteBuffer([]byte{})
  65. _, err := byteBuffer.Write([]byte{255, 255})
  66. if err != nil {
  67. t.Error()
  68. }
  69. readUint16, _ := byteBuffer.ReadUint16()
  70. assert.Equal(t, readUint16, uint16(math.MaxUint16))
  71. }
  72. func TestWriteString(t *testing.T) {
  73. byteBuffer := NewByteBuffer([]byte{})
  74. _, err := byteBuffer.WriteString("seata")
  75. if err != nil {
  76. t.Error()
  77. return
  78. }
  79. seataBytes := []byte{115, 101, 97, 116, 97}
  80. for _, byteItem := range seataBytes {
  81. readByte, _ := byteBuffer.ReadByte()
  82. assert.Equal(t, byteItem, readByte)
  83. }
  84. }
  85. func TestWriteByte(t *testing.T) {
  86. byteBuffer := NewByteBuffer([]byte{})
  87. _ = byteBuffer.WriteByte(1)
  88. readByte, _ := byteBuffer.ReadByte()
  89. assert.Equal(t, readByte, byte(1))
  90. }
  91. func TestWriteUint16(t *testing.T) {
  92. byteBuffer := NewByteBuffer([]byte{})
  93. _, _ = byteBuffer.WriteUint16(uint16(math.MaxUint16))
  94. readUint16, _ := byteBuffer.ReadUint16()
  95. assert.Equal(t, uint16(math.MaxUint16), readUint16)
  96. }
  97. func TestWriteUint32(t *testing.T) {
  98. byteBuffer := NewByteBuffer([]byte{})
  99. _, _ = byteBuffer.WriteUint32(uint32(math.MaxUint32))
  100. readUint32, _ := byteBuffer.ReadUint32()
  101. assert.Equal(t, uint32(math.MaxUint32), readUint32)
  102. }
  103. func TestWriteUint64(t *testing.T) {
  104. byteBuffer := NewByteBuffer([]byte{})
  105. _, _ = byteBuffer.WriteUint64(uint64(math.MaxUint64))
  106. readUint64, _ := byteBuffer.ReadUint64()
  107. assert.Equal(t, uint64(math.MaxUint64), readUint64)
  108. }
  109. func TestWriteInt64(t *testing.T) {
  110. byteBuffer := NewByteBuffer([]byte{})
  111. _, _ = byteBuffer.WriteInt64(int64(math.MaxInt64))
  112. readInt64, _ := byteBuffer.ReadInt64()
  113. assert.Equal(t, int64(math.MaxInt64), readInt64)
  114. }
  115. func TestByte2Int64(t *testing.T) {
  116. byte2Int64 := Byte2Int64([]byte{127, 255, 255, 255, 255, 255, 255, 255})
  117. assert.Equal(t, byte2Int64, int64(math.MaxInt64))
  118. }
  119. func TestByte2UInt16(t *testing.T) {
  120. byte2UInt16 := Byte2UInt16([]byte{255, 255})
  121. assert.Equal(t, byte2UInt16, uint16(math.MaxUint16))
  122. }
  123. func TestByte2UInt32(t *testing.T) {
  124. byte2UInt32 := Byte2UInt32([]byte{255, 255, 255, 255})
  125. assert.Equal(t, byte2UInt32, uint32(math.MaxUint32))
  126. }
  127. func TestInt2BytesTo(t *testing.T) {
  128. bytes := make([]byte, 4)
  129. Int2BytesTo(math.MaxInt64, bytes)
  130. assert.Equal(t, bytes, []byte{255, 255, 255, 255})
  131. }
  132. func TestInt2Bytes(t *testing.T) {
  133. bytes := Int2Bytes(math.MaxInt64)
  134. assert.Equal(t, bytes, []byte{255, 255, 255, 255})
  135. }
  136. func TestInt64ToBytesTo(t *testing.T) {
  137. bytes := make([]byte, 8)
  138. Int64ToBytesTo(math.MaxInt64, bytes)
  139. assert.Equal(t, bytes, []byte{127, 255, 255, 255, 255, 255, 255, 255})
  140. }
  141. func TestUint64ToBytesTo(t *testing.T) {
  142. bytes := make([]byte, 8)
  143. Uint64ToBytesTo(math.MaxUint64, bytes)
  144. assert.Equal(t, bytes, []byte{255, 255, 255, 255, 255, 255, 255, 255})
  145. }
  146. func TestInt64ToBytes(t *testing.T) {
  147. bytes := Int64ToBytes(math.MaxInt64)
  148. assert.Equal(t, bytes, []byte{127, 255, 255, 255, 255, 255, 255, 255})
  149. }
  150. func TestUint32ToBytesTo(t *testing.T) {
  151. bytes := make([]byte, 4)
  152. Uint32ToBytesTo(math.MaxUint32, bytes)
  153. assert.Equal(t, bytes, []byte{255, 255, 255, 255})
  154. }
  155. func TestUInt32ToBytes(t *testing.T) {
  156. bytes := UInt32ToBytes(math.MaxUint32)
  157. assert.Equal(t, bytes, []byte{255, 255, 255, 255})
  158. }
  159. func TestUint16ToBytesTo(t *testing.T) {
  160. bytes := make([]byte, 2)
  161. Uint16ToBytesTo(math.MaxUint16, bytes)
  162. assert.Equal(t, bytes, []byte{255, 255})
  163. }
  164. func TestUInt16ToBytes(t *testing.T) {
  165. bytes := UInt16ToBytes(math.MaxUint16)
  166. assert.Equal(t, bytes, []byte{255, 255})
  167. }
  168. func TestUInt64ToBytes(t *testing.T) {
  169. bytes := UInt64ToBytes(math.MaxUint64)
  170. assert.Equal(t, bytes, []byte{255, 255, 255, 255, 255, 255, 255, 255})
  171. }