github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/utils/buffer/buffer_test.go (about)

     1  // Copyright 2021 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 buffer
    16  
    17  import (
    18  	"bytes"
    19  	"math/rand"
    20  	"strconv"
    21  	"testing"
    22  	"time"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestDynamicBuffer(t *testing.T) {
    28  	const blockSize = 53
    29  
    30  	rand := rand.New(rand.NewSource(time.Now().UnixNano()))
    31  	for i := 0; i < 100; i++ {
    32  		n := 1000 + rand.Int63()%10000
    33  		t.Run(strconv.FormatInt(n, 10), func(t *testing.T) {
    34  			data := make([]byte, n)
    35  			read, err := rand.Read(data)
    36  			require.NoError(t, err)
    37  			require.Equal(t, int(n), read)
    38  
    39  			buf := New(blockSize)
    40  			buf.Append(data)
    41  			itr := buf.Close()
    42  
    43  			reassembled := bytes.NewBuffer(nil)
    44  			err = itr.FlushTo(reassembled)
    45  			require.NoError(t, err)
    46  			require.Equal(t, data, reassembled.Bytes())
    47  		})
    48  	}
    49  }