// b.Len() == len(b.Bytes()).
func (b *Buffer) Len() int { return len(b.buf) - b.off }
+// Cap returns the capacity of the buffer's underlying byte slice, that is, the
+// total space allocated for the the buffer's data.
+func (b *Buffer) Cap() int { return cap(b.buf) }
+
// Truncate discards all but the first n unread bytes from the buffer.
// It panics if n is negative or greater than the length of the buffer.
func (b *Buffer) Truncate(n int) {
empty(t, "TestMixedReadsAndWrites (2)", &buf, s, make([]byte, buf.Len()))
}
+func TestCapWithPreallocatedSlice(t *testing.T) {
+ buf := NewBuffer(make([]byte, 10))
+ n := buf.Cap()
+ if n != 10 {
+ t.Errorf("expected 10, got %d", n)
+ }
+}
+
+func TestCapWithSliceAndWrittenData(t *testing.T) {
+ buf := NewBuffer(make([]byte, 0, 10))
+ buf.Write([]byte("test"))
+ n := buf.Cap()
+ if n != 10 {
+ t.Errorf("expected 10, got %d", n)
+ }
+}
+
func TestNil(t *testing.T) {
var b *Buffer
if b.String() != "<nil>" {
// Export func for testing
var IndexBytePortable = indexBytePortable
var EqualPortable = equalPortable
-
-func (b *Buffer) Cap() int {
- return cap(b.buf)
-}