|
|
@@ -2,9 +2,8 @@ package util
|
|
|
|
|
|
import (
|
|
|
"bytes"
|
|
|
- "io"
|
|
|
"math"
|
|
|
- "math/rand/v2"
|
|
|
+ "math/rand"
|
|
|
"runtime"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
@@ -40,36 +39,6 @@ func TestBufferReadWrite(t *testing.T) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-func TestBufferReadStringBytesMatchesReadString(t *testing.T) {
|
|
|
- buf := NewBuffer()
|
|
|
- buf.WriteString("alpha")
|
|
|
- buf.WriteString("")
|
|
|
- buf.WriteString("β")
|
|
|
-
|
|
|
- readBuf := NewBufferFromBytes(buf.Bytes())
|
|
|
- b0, err := readBuf.ReadStringBytes()
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- if string(b0) != "alpha" {
|
|
|
- t.Fatalf("got %q", b0)
|
|
|
- }
|
|
|
- b1, err := readBuf.ReadStringBytes()
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- if len(b1) != 0 {
|
|
|
- t.Fatalf("expected empty slice")
|
|
|
- }
|
|
|
- b2, err := readBuf.ReadStringBytes()
|
|
|
- if err != nil {
|
|
|
- t.Fatal(err)
|
|
|
- }
|
|
|
- if string(b2) != "β" {
|
|
|
- t.Fatalf("got %q", b2)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
func TestBufferWriteReadBytes(t *testing.T) {
|
|
|
buf := NewBuffer()
|
|
|
|
|
|
@@ -213,8 +182,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()
|
|
|
|
|
|
@@ -223,11 +192,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)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -255,7 +224,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)
|
|
|
}
|
|
|
@@ -314,87 +283,6 @@ 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))
|