implement String() method. closes #7 (#8)

* implement String() method. closes #7

* String() - lower memory usage
This commit is contained in:
Kirill Danshin
2016-07-12 09:10:16 +03:00
committed by Aliaksandr Valialkin
parent a517d2d153
commit 10f802b4cd
2 changed files with 40 additions and 0 deletions
+5
View File
@@ -100,6 +100,11 @@ func (b *ByteBuffer) SetString(s string) {
b.B = append(b.B[:0], s...)
}
// String returns string representation of ByteBuffer.B
func (b *ByteBuffer) String() string {
return string(b.B)
}
// Reset makes ByteBuffer.B empty.
func (b *ByteBuffer) Reset() {
b.B = b.B[:0]
+35
View File
@@ -101,3 +101,38 @@ func testByteBufferGetPut(t *testing.T) {
Put(b)
}
}
func testByteBufferGetString(t *testing.T) {
for i := 0; i < 10; i++ {
expectedS := fmt.Sprintf("num %d", i)
b := Get()
b.SetString(expectedS)
if b.String() != expectedS {
t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
}
Put(b)
}
}
func TestByteBufferGetStringSerial(t *testing.T) {
testByteBufferGetString(t)
}
func TestByteBufferGetStringConcurrent(t *testing.T) {
concurrency := 10
ch := make(chan struct{}, concurrency)
for i := 0; i < concurrency; i++ {
go func() {
testByteBufferGetString(t)
ch <- struct{}{}
}()
}
for i := 0; i < concurrency; i++ {
select {
case <-ch:
case <-time.After(time.Second):
t.Fatalf("timeout!")
}
}
}