|
| 1 | +// Copyright 2024 Dolthub, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package sql |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/stretchr/testify/require" |
| 19 | + "testing" |
| 20 | +) |
| 21 | + |
| 22 | +func TestGrowByteBuffer(t *testing.T) { |
| 23 | + b := NewByteBuffer(10) |
| 24 | + |
| 25 | + // grow less than boundary |
| 26 | + src1 := []byte{1, 1, 1} |
| 27 | + obj1 := append(b.Get(), src1...) |
| 28 | + b.Grow(len(src1)) |
| 29 | + |
| 30 | + require.Equal(t, 10, len(b.buf)) |
| 31 | + require.Equal(t, 3, b.i) |
| 32 | + require.Equal(t, 10, cap(obj1)) |
| 33 | + |
| 34 | + // grow to boundary |
| 35 | + src2 := []byte{0, 0, 0, 0, 0, 0, 0} |
| 36 | + obj2 := append(b.Get(), src2...) |
| 37 | + b.Grow(len(src2)) |
| 38 | + |
| 39 | + require.Equal(t, 20, len(b.buf)) |
| 40 | + require.Equal(t, 10, b.i) |
| 41 | + require.Equal(t, 7, cap(obj2)) |
| 42 | + |
| 43 | + src3 := []byte{2, 2, 2, 2, 2} |
| 44 | + obj3 := append(b.Get(), src3...) |
| 45 | + b.Grow(len(src3)) |
| 46 | + |
| 47 | + require.Equal(t, 20, len(b.buf)) |
| 48 | + require.Equal(t, 15, b.i) |
| 49 | + require.Equal(t, 10, cap(obj3)) |
| 50 | + |
| 51 | + // grow exceeds boundary |
| 52 | + |
| 53 | + src4 := []byte{3, 3, 3, 3, 3, 3, 3, 3} |
| 54 | + obj4 := append(b.Get(), src4...) |
| 55 | + b.Grow(len(src4)) |
| 56 | + |
| 57 | + require.Equal(t, 40, len(b.buf)) |
| 58 | + require.Equal(t, 15, b.i) |
| 59 | + require.Equal(t, 16, cap(obj4)) |
| 60 | + |
| 61 | + // objects are all valid after doubling |
| 62 | + require.Equal(t, src1, obj1) |
| 63 | + require.Equal(t, src2, obj2) |
| 64 | + require.Equal(t, src3, obj3) |
| 65 | + require.Equal(t, src4, obj4) |
| 66 | + |
| 67 | + // reset |
| 68 | + b.Reset() |
| 69 | + require.Equal(t, 40, len(b.buf)) |
| 70 | + require.Equal(t, 0, b.i) |
| 71 | +} |
0 commit comments