|
|
@@ -2,8 +2,9 @@ package util
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
+ "io"
|
|
|
"math"
|
|
|
- "math/rand"
|
|
|
+ "math/rand/v2"
|
|
|
"runtime"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
@@ -182,8 +183,8 @@ func TestBufferWriteReadInt64(t *testing.T) {
|
|
|
func TestBufferBytes(t *testing.T) {
|
|
|
buf := NewBuffer()
|
|
|
|
|
|
- buf.WriteInt(42)
|
|
|
- buf.WriteFloat64(3.14)
|
|
|
+ buf.WriteInt(-42)
|
|
|
+ buf.WriteFloat64(-3.14)
|
|
|
|
|
|
unreadBytes := buf.Bytes()
|
|
|
|
|
|
@@ -192,11 +193,11 @@ func TestBufferBytes(t *testing.T) {
|
|
|
intVal := newBuf.ReadInt()
|
|
|
floatVal := newBuf.ReadFloat64()
|
|
|
|
|
|
- if intVal != 42 {
|
|
|
- t.Errorf("Expected int value to be 42, got %v", intVal)
|
|
|
+ if intVal != -42 {
|
|
|
+ t.Errorf("Expected int value to be -42, got %v", intVal)
|
|
|
}
|
|
|
- if floatVal != 3.14 {
|
|
|
- t.Errorf("Expected float value to be 3.14, got %v", floatVal)
|
|
|
+ if floatVal != -3.14 {
|
|
|
+ t.Errorf("Expected float value to be -3.14, got %v", floatVal)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -224,7 +225,7 @@ const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
func generateRandomString(ln int) string {
|
|
|
b := make([]byte, ln)
|
|
|
for i := range b {
|
|
|
- b[i] = letters[rand.Intn(len(letters))]
|
|
|
+ b[i] = letters[rand.IntN(len(letters))]
|
|
|
}
|
|
|
return string(b)
|
|
|
}
|
|
|
@@ -283,6 +284,87 @@ func TestStringBytes(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+type randomByteReader struct {
|
|
|
+ bytes []byte
|
|
|
+ pos int
|
|
|
+}
|
|
|
+
|
|
|
+func newRandomByteReader(bytes []byte) *randomByteReader {
|
|
|
+ return &randomByteReader{
|
|
|
+ bytes: bytes,
|
|
|
+ pos: 0,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// reads a random number of bytes from 1-4 each time Read is called.
|
|
|
+// simulates partial buffered reads
|
|
|
+func (sbr *randomByteReader) Read(b []byte) (int, error) {
|
|
|
+ if sbr.pos >= len(sbr.bytes) {
|
|
|
+ return 0, io.EOF
|
|
|
+ }
|
|
|
+
|
|
|
+ toCopy := rand.IntN(4) + 1
|
|
|
+ if toCopy > len(b) {
|
|
|
+ toCopy = len(b)
|
|
|
+ }
|
|
|
+
|
|
|
+ var err error
|
|
|
+ remaining := len(sbr.bytes) - sbr.pos
|
|
|
+ if toCopy > remaining {
|
|
|
+ err = io.EOF
|
|
|
+ toCopy = remaining
|
|
|
+ }
|
|
|
+
|
|
|
+ bytesCopied := copy(b, sbr.bytes[sbr.pos:sbr.pos+toCopy])
|
|
|
+ sbr.pos += bytesCopied
|
|
|
+
|
|
|
+ return bytesCopied, err
|
|
|
+}
|
|
|
+
|
|
|
+func TestBufferReaderSupport(t *testing.T) {
|
|
|
+ buf := NewBuffer()
|
|
|
+ buf.WriteBool(true)
|
|
|
+ buf.WriteInt(42)
|
|
|
+ buf.WriteFloat64(3.14)
|
|
|
+ buf.WriteString("Testing, 1, 2, 3!")
|
|
|
+ buf.WriteUInt64(uint64(123456))
|
|
|
+ buf.WriteInt16(44)
|
|
|
+ buf.WriteFloat32(float32(5.0))
|
|
|
+
|
|
|
+ reader := newRandomByteReader(buf.Bytes())
|
|
|
+ readerBuff := NewBufferFromReader(reader)
|
|
|
+
|
|
|
+ b := readerBuff.ReadBool()
|
|
|
+ i := readerBuff.ReadInt()
|
|
|
+ f := readerBuff.ReadFloat64()
|
|
|
+ s := readerBuff.ReadString()
|
|
|
+ ui64 := readerBuff.ReadUInt64()
|
|
|
+ i16 := readerBuff.ReadInt16()
|
|
|
+ f32 := readerBuff.ReadFloat32()
|
|
|
+
|
|
|
+ if !b {
|
|
|
+ t.Errorf("expected true, got: false")
|
|
|
+ }
|
|
|
+ if i != 42 {
|
|
|
+ t.Errorf("expected 42, got: %d", i)
|
|
|
+ }
|
|
|
+ if f != 3.14 {
|
|
|
+ t.Errorf("expected 3.14, got: %f", f)
|
|
|
+ }
|
|
|
+ if s != "Testing, 1, 2, 3!" {
|
|
|
+ t.Errorf("expected 'Testing, 1, 2, 3!', got: '%s'", s)
|
|
|
+ }
|
|
|
+ if ui64 != uint64(123456) {
|
|
|
+ t.Errorf("expected 123456, got: %d", ui64)
|
|
|
+ }
|
|
|
+ if i16 != int16(44) {
|
|
|
+ t.Errorf("expected 44, got: %d", i16)
|
|
|
+ }
|
|
|
+ if f32 != float32(5.0) {
|
|
|
+ t.Errorf("expected 5.0, got: %f", f32)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func TestTooLargeStringTruncate(t *testing.T) {
|
|
|
normalStr := generateRandomString(100)
|
|
|
bigStr := generateRandomString(math.MaxUint16 + (math.MaxUint16 / 2))
|