From c1ded82b584bb3fd93f677ce6c1499c1e60c21ad Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Thu, 2 Mar 2023 23:57:38 +0800 Subject: [PATCH] Fix --- slice/Slice.go | 2 +- slice/Slice_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/slice/Slice.go b/slice/Slice.go index 900935d..0120c3e 100644 --- a/slice/Slice.go +++ b/slice/Slice.go @@ -71,7 +71,7 @@ func (t *Buf[T]) Append(data []T) error { t.buf = make([]T, len(data), t.maxsize) } } else { - diff := cap(t.buf) - t.bufsize - len(data) + diff := len(t.buf) - t.bufsize - len(data) if diff < 0 { t.buf = append(t.buf, make([]T, -diff)...) } else { diff --git a/slice/Slice_test.go b/slice/Slice_test.go index 435793d..a093095 100644 --- a/slice/Slice_test.go +++ b/slice/Slice_test.go @@ -54,3 +54,28 @@ func TestXxx(t *testing.T) { t.Fatal() } } + +func TestXxx2(t *testing.T) { + var c = New[byte]() + c.Append([]byte("12345")) + c.Append([]byte("67890")) + first := c.GetCopyBuf() + c.Reset() + c.Append([]byte("abc")) + c.Append([]byte("defg")) + second := c.GetCopyBuf() + c.Reset() + c.Append([]byte("akjsdhfaksdjhf")) + c.Append([]byte("9834719203857")) + third := c.GetCopyBuf() + c.Reset() + if !bytes.Equal(first, []byte("1234567890")) { + t.Fatal() + } + if !bytes.Equal(second, []byte("abcdefg")) { + t.Fatal() + } + if !bytes.Equal(third, []byte("akjsdhfaksdjhf9834719203857")) { + t.Fatal() + } +} -- 2.39.2