buffer_test.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package util
  2. import (
  3. "bytes"
  4. "io"
  5. "math"
  6. "math/rand/v2"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. "time"
  11. )
  12. func TestBufferReadWrite(t *testing.T) {
  13. buf := NewBuffer()
  14. buf.WriteBool(true)
  15. buf.WriteInt(42)
  16. buf.WriteFloat64(3.14)
  17. buf.WriteString("Testing, 1, 2, 3!")
  18. readBuf := NewBufferFromBytes(buf.Bytes())
  19. boolVal := readBuf.ReadBool()
  20. intVal := readBuf.ReadInt()
  21. floatVal := readBuf.ReadFloat64()
  22. stringVal := readBuf.ReadString()
  23. if boolVal != true {
  24. t.Errorf("Expected bool value to be true, got %v", boolVal)
  25. }
  26. if intVal != 42 {
  27. t.Errorf("Expected int value to be 42, got %v", intVal)
  28. }
  29. if floatVal != 3.14 {
  30. t.Errorf("Expected float value to be 3.14, got %v", floatVal)
  31. }
  32. if stringVal != "Testing, 1, 2, 3!" {
  33. t.Errorf("Expected string value to be 'Hello, World!', got %v", stringVal)
  34. }
  35. }
  36. func TestBufferWriteReadBytes(t *testing.T) {
  37. buf := NewBuffer()
  38. bytesToWrite := []byte{0x01, 0x02, 0x03, 0x04}
  39. buf.WriteBytes(bytesToWrite)
  40. readBuf := NewBufferFromBytes(buf.Bytes())
  41. readBytes := readBuf.ReadBytes(len(bytesToWrite))
  42. if !bytes.Equal(readBytes, bytesToWrite) {
  43. t.Errorf("Expected bytes to be %v, got %v", bytesToWrite, readBytes)
  44. }
  45. }
  46. func TestBufferWriteReadUInt64(t *testing.T) {
  47. buf := NewBuffer()
  48. uint64Val := uint64(1234567890)
  49. buf.WriteUInt64(uint64Val)
  50. readBuf := NewBufferFromBytes(buf.Bytes())
  51. readUInt64 := readBuf.ReadUInt64()
  52. if readUInt64 != uint64Val {
  53. t.Errorf("Expected uint64 value to be %v, got %v", uint64Val, readUInt64)
  54. }
  55. }
  56. func TestBufferWriteReadFloat32(t *testing.T) {
  57. buf := NewBuffer()
  58. float32Val := float32(3.14159)
  59. buf.WriteFloat32(float32Val)
  60. readBuf := NewBufferFromBytes(buf.Bytes())
  61. readFloat32 := readBuf.ReadFloat32()
  62. if readFloat32 != float32Val {
  63. t.Errorf("Expected float32 value to be %v, got %v", float32Val, readFloat32)
  64. }
  65. }
  66. func TestBufferWriteReadInt8(t *testing.T) {
  67. buf := NewBuffer()
  68. int8Val := int8(-42)
  69. buf.WriteInt8(int8Val)
  70. readBuf := NewBufferFromBytes(buf.Bytes())
  71. readInt8 := readBuf.ReadInt8()
  72. if readInt8 != int8Val {
  73. t.Errorf("Expected int8 value to be %v, got %v", int8Val, readInt8)
  74. }
  75. }
  76. func TestBufferWriteReadUInt16(t *testing.T) {
  77. buf := NewBuffer()
  78. uint16Val := uint16(65535)
  79. buf.WriteUInt16(uint16Val)
  80. readBuf := NewBufferFromBytes(buf.Bytes())
  81. readUInt16 := readBuf.ReadUInt16()
  82. if readUInt16 != uint16Val {
  83. t.Errorf("Expected uint16 value to be %v, got %v", uint16Val, readUInt16)
  84. }
  85. }
  86. func TestBufferWriteReadInt32(t *testing.T) {
  87. buf := NewBuffer()
  88. int32Val := int32(-1234567890)
  89. buf.WriteInt32(int32Val)
  90. readBuf := NewBufferFromBytes(buf.Bytes())
  91. readInt32 := readBuf.ReadInt32()
  92. if readInt32 != int32Val {
  93. t.Errorf("Expected int32 value to be %v, got %v", int32Val, readInt32)
  94. }
  95. }
  96. func TestBufferWriteReadUInt8(t *testing.T) {
  97. buf := NewBuffer()
  98. uint8Val := uint8(255)
  99. buf.WriteUInt8(uint8Val)
  100. readBuf := NewBufferFromBytes(buf.Bytes())
  101. readUInt8 := readBuf.ReadUInt8()
  102. if readUInt8 != uint8Val {
  103. t.Errorf("Expected uint8 value to be %v, got %v", uint8Val, readUInt8)
  104. }
  105. }
  106. func TestBufferWriteReadInt16(t *testing.T) {
  107. buf := NewBuffer()
  108. int16Val := int16(-32768)
  109. buf.WriteInt16(int16Val)
  110. readBuf := NewBufferFromBytes(buf.Bytes())
  111. readInt16 := readBuf.ReadInt16()
  112. if readInt16 != int16Val {
  113. t.Errorf("Expected int16 value to be %v, got %v", int16Val, readInt16)
  114. }
  115. }
  116. func TestBufferWriteReadUInt32(t *testing.T) {
  117. buf := NewBuffer()
  118. uint32Val := uint32(4294967295)
  119. buf.WriteUInt32(uint32Val)
  120. readBuf := NewBufferFromBytes(buf.Bytes())
  121. readUInt32 := readBuf.ReadUInt32()
  122. if readUInt32 != uint32Val {
  123. t.Errorf("Expected uint32 value to be %v, got %v", uint32Val, readUInt32)
  124. }
  125. }
  126. func TestBufferWriteReadInt64(t *testing.T) {
  127. buf := NewBuffer()
  128. int64Val := int64(-9223372036854775808)
  129. buf.WriteInt64(int64Val)
  130. readBuf := NewBufferFromBytes(buf.Bytes())
  131. readInt64 := readBuf.ReadInt64()
  132. if readInt64 != int64Val {
  133. t.Errorf("Expected int64 value to be %v, got %v", int64Val, readInt64)
  134. }
  135. }
  136. func TestBufferBytes(t *testing.T) {
  137. buf := NewBuffer()
  138. buf.WriteInt(-42)
  139. buf.WriteFloat64(-3.14)
  140. unreadBytes := buf.Bytes()
  141. newBuf := NewBufferFromBytes(unreadBytes)
  142. intVal := newBuf.ReadInt()
  143. floatVal := newBuf.ReadFloat64()
  144. if intVal != -42 {
  145. t.Errorf("Expected int value to be -42, got %v", intVal)
  146. }
  147. if floatVal != -3.14 {
  148. t.Errorf("Expected float value to be -3.14, got %v", floatVal)
  149. }
  150. }
  151. func TestBufferNewBufferFrom(t *testing.T) {
  152. buf := NewBuffer()
  153. buf.WriteInt(42)
  154. buf.WriteFloat64(3.14)
  155. newBuf := NewBufferFrom(buf)
  156. intVal := newBuf.ReadInt()
  157. floatVal := newBuf.ReadFloat64()
  158. if intVal != 42 {
  159. t.Errorf("Expected int value to be 42, got %v", intVal)
  160. }
  161. if floatVal != 3.14 {
  162. t.Errorf("Expected float value to be 3.14, got %v", floatVal)
  163. }
  164. }
  165. const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  166. func generateRandomString(ln int) string {
  167. b := make([]byte, ln)
  168. for i := range b {
  169. b[i] = letters[rand.IntN(len(letters))]
  170. }
  171. return string(b)
  172. }
  173. func memMib() float64 {
  174. var m runtime.MemStats
  175. runtime.ReadMemStats(&m)
  176. return float64(m.Alloc) / 1024.0 / 1024.0
  177. }
  178. func TestStringBytes(t *testing.T) {
  179. baselineMem := memMib()
  180. b := make([]byte, 10<<20)
  181. afterMem := memMib()
  182. delta := afterMem - baselineMem
  183. t.Logf("Allocated %v MiB, Delta: %v MiB", afterMem, delta)
  184. s := "Hello World!"
  185. sl := b[512 : 512+len(s)]
  186. copy(sl, stringToBytes(s))
  187. afterMem = memMib()
  188. delta = afterMem - baselineMem
  189. t.Logf("Allocated %v MiB, Delta: %v MiB", afterMem, delta)
  190. // this should pin the large backing array in memory, preventing it from being GC'd
  191. newS := bytesToString(sl)
  192. runtime.GC()
  193. time.Sleep(time.Second)
  194. afterMem = memMib()
  195. delta = afterMem - baselineMem
  196. t.Logf("S: %s, Allocated %v MiB, Delta: %v MiB", newS, afterMem, delta)
  197. // copy the string into a new string and clear out pinned string
  198. sCopy := strings.Clone(newS)
  199. newS = ""
  200. // Now that we've dropped the reference to the pinned backing array, it should be GC'd
  201. runtime.GC()
  202. time.Sleep(time.Second)
  203. afterMem = memMib()
  204. delta = afterMem - baselineMem
  205. t.Logf("S: %s, Allocated %v MiB, Delta: %v MiB", sCopy, afterMem, delta)
  206. if sCopy != s {
  207. t.Errorf("Expected string to be %v, got %v", s, sCopy)
  208. }
  209. if delta > 0.5 {
  210. t.Errorf("Expected memory delta to be less than 0.5 MiB, got %v MiB", delta)
  211. }
  212. }
  213. type randomByteReader struct {
  214. bytes []byte
  215. pos int
  216. }
  217. func newRandomByteReader(bytes []byte) *randomByteReader {
  218. return &randomByteReader{
  219. bytes: bytes,
  220. pos: 0,
  221. }
  222. }
  223. // reads a random number of bytes from 1-4 each time Read is called.
  224. // simulates partial buffered reads
  225. func (sbr *randomByteReader) Read(b []byte) (int, error) {
  226. if sbr.pos >= len(sbr.bytes) {
  227. return 0, io.EOF
  228. }
  229. toCopy := rand.IntN(4) + 1
  230. if toCopy > len(b) {
  231. toCopy = len(b)
  232. }
  233. var err error
  234. remaining := len(sbr.bytes) - sbr.pos
  235. if toCopy > remaining {
  236. err = io.EOF
  237. toCopy = remaining
  238. }
  239. bytesCopied := copy(b, sbr.bytes[sbr.pos:sbr.pos+toCopy])
  240. sbr.pos += bytesCopied
  241. return bytesCopied, err
  242. }
  243. func TestBufferReaderSupport(t *testing.T) {
  244. buf := NewBuffer()
  245. buf.WriteBool(true)
  246. buf.WriteInt(42)
  247. buf.WriteFloat64(3.14)
  248. buf.WriteString("Testing, 1, 2, 3!")
  249. buf.WriteUInt64(uint64(123456))
  250. buf.WriteInt16(44)
  251. buf.WriteFloat32(float32(5.0))
  252. reader := newRandomByteReader(buf.Bytes())
  253. readerBuff := NewBufferFromReader(reader)
  254. b := readerBuff.ReadBool()
  255. i := readerBuff.ReadInt()
  256. f := readerBuff.ReadFloat64()
  257. s := readerBuff.ReadString()
  258. ui64 := readerBuff.ReadUInt64()
  259. i16 := readerBuff.ReadInt16()
  260. f32 := readerBuff.ReadFloat32()
  261. if !b {
  262. t.Errorf("expected true, got: false")
  263. }
  264. if i != 42 {
  265. t.Errorf("expected 42, got: %d", i)
  266. }
  267. if f != 3.14 {
  268. t.Errorf("expected 3.14, got: %f", f)
  269. }
  270. if s != "Testing, 1, 2, 3!" {
  271. t.Errorf("expected 'Testing, 1, 2, 3!', got: '%s'", s)
  272. }
  273. if ui64 != uint64(123456) {
  274. t.Errorf("expected 123456, got: %d", ui64)
  275. }
  276. if i16 != int16(44) {
  277. t.Errorf("expected 44, got: %d", i16)
  278. }
  279. if f32 != float32(5.0) {
  280. t.Errorf("expected 5.0, got: %f", f32)
  281. }
  282. }
  283. func TestTooLargeStringTruncate(t *testing.T) {
  284. normalStr := generateRandomString(100)
  285. bigStr := generateRandomString(math.MaxUint16 + (math.MaxUint16 / 2))
  286. expectedBigStrRead := bigStr[:math.MaxUint16]
  287. otherBigStr := generateRandomString(math.MaxUint16)
  288. plusOne := generateRandomString(math.MaxUint16 + 1)
  289. expectedPlusOne := plusOne[:math.MaxUint16]
  290. buf := NewBuffer()
  291. buf.WriteInt(42)
  292. buf.WriteFloat64(3.14)
  293. buf.WriteString(normalStr)
  294. buf.WriteString(bigStr)
  295. buf.WriteString(otherBigStr)
  296. buf.WriteString(plusOne)
  297. readBuf := NewBufferFromBytes(buf.Bytes())
  298. intVal := readBuf.ReadInt()
  299. floatVal := readBuf.ReadFloat64()
  300. normalStrRead := readBuf.ReadString()
  301. bigStrRead := readBuf.ReadString()
  302. otherBigStrRead := readBuf.ReadString()
  303. plusOneRead := readBuf.ReadString()
  304. if intVal != 42 {
  305. t.Errorf("Expected int value to be 42, got %v", intVal)
  306. }
  307. if floatVal != 3.14 {
  308. t.Errorf("Expected float value to be 3.14, got %v", floatVal)
  309. }
  310. if normalStrRead != normalStr {
  311. t.Errorf("Expected string value to be %v, got %v", normalStr, normalStrRead)
  312. }
  313. if bigStrRead != expectedBigStrRead {
  314. t.Errorf("Expected large string values to be equivalent!")
  315. }
  316. if otherBigStrRead != otherBigStr {
  317. t.Errorf("Expected large string values to be equivalent!")
  318. }
  319. if plusOneRead != expectedPlusOne {
  320. t.Errorf("Expected large string values to be equivalent!")
  321. }
  322. }