...

Source file src/github.com/launchdarkly/go-server-sdk-evaluation/v2/internal/local_buffer_test.go

Documentation: github.com/launchdarkly/go-server-sdk-evaluation/v2/internal

     1  package internal
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestLocalBuffer(t *testing.T) {
    11  	for _, preallocated := range []bool{false, true} {
    12  		t.Run(fmt.Sprintf("preallocated=%t", preallocated), func(t *testing.T) {
    13  			var buf LocalBuffer
    14  			if preallocated {
    15  				buf.Data = make([]byte, 0, 100)
    16  			}
    17  
    18  			buf.AppendString("abc")
    19  			buf.AppendByte('.')
    20  			buf.Append([]byte("def"))
    21  			buf.AppendInt(123)
    22  			assert.Equal(t, "abc.def123", string(buf.Data))
    23  
    24  			if preallocated {
    25  				assert.Equal(t, 100, cap(buf.Data))
    26  			}
    27  		})
    28  	}
    29  
    30  	t.Run("reallocation", func(t *testing.T) {
    31  		buf := LocalBuffer{Data: make([]byte, 0, 5)}
    32  
    33  		// by default, the capacity doubles when we reallocate...
    34  		buf.AppendString("abc")
    35  		buf.AppendString("def")
    36  		assert.Equal(t, "abcdef", string(buf.Data))
    37  		assert.Equal(t, 10, cap(buf.Data)) // capacity was doubled
    38  
    39  		// ...unless that would not be enough to hold the new data, in which case we allocate
    40  		// double the total new length
    41  		buf.AppendString("ghijklmnopqrstuvwxyz")
    42  		assert.Equal(t, "abcdefghijklmnopqrstuvwxyz", string(buf.Data))
    43  		assert.Equal(t, 52, cap(buf.Data)) //
    44  	})
    45  }
    46  

View as plain text