github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/unsafe/string_test.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package unsafe
    22  
    23  import (
    24  	"bytes"
    25  	"testing"
    26  
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestWithBytesSmallString(t *testing.T) {
    31  	str := "foobarbaz"
    32  	validateWithBytes(t, str)
    33  }
    34  
    35  func TestWithBytesLargeString(t *testing.T) {
    36  	var buf bytes.Buffer
    37  	for i := 0; i < 65536; i++ {
    38  		buf.WriteByte(byte(i % 256))
    39  	}
    40  	str := buf.String()
    41  	validateWithBytes(t, str)
    42  }
    43  
    44  func TestWithBytesAndArgSmallString(t *testing.T) {
    45  	str := "foobarbaz"
    46  	validateWithBytesAndArg(t, str)
    47  }
    48  
    49  func TestWithBytesAndArgLargeString(t *testing.T) {
    50  	var buf bytes.Buffer
    51  	for i := 0; i < 65536; i++ {
    52  		buf.WriteByte(byte(i % 256))
    53  	}
    54  	str := buf.String()
    55  	validateWithBytesAndArg(t, str)
    56  }
    57  
    58  var withBytesBenchSink ImmutableBytes
    59  
    60  func BenchmarkWithBytes(b *testing.B) {
    61  	for i := 0; i < b.N; i++ {
    62  		WithBytes("foobar", func(b ImmutableBytes) {
    63  			withBytesBenchSink = b
    64  		})
    65  	}
    66  }
    67  
    68  func validateWithBytes(t *testing.T, str string) {
    69  	WithBytes(str, func(b ImmutableBytes) {
    70  		require.Equal(t, []byte(str), []byte(b))
    71  		require.Equal(t, len(str), len(b))
    72  		require.Equal(t, len(str), cap(b))
    73  	})
    74  }
    75  
    76  func validateWithBytesAndArg(t *testing.T, str string) {
    77  	WithBytesAndArg(str, "cat", func(data ImmutableBytes, arg interface{}) {
    78  		var buf bytes.Buffer
    79  		for _, b := range data {
    80  			buf.WriteByte(b)
    81  		}
    82  		buf.WriteString(arg.(string))
    83  		require.Equal(t, str+"cat", buf.String())
    84  	})
    85  }