...

Source file src/github.com/redis/go-redis/v9/internal/util_test.go

Documentation: github.com/redis/go-redis/v9/internal

     1  package internal
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  	"testing"
     7  
     8  	. "github.com/bsm/ginkgo/v2"
     9  	. "github.com/bsm/gomega"
    10  )
    11  
    12  func BenchmarkToLowerStd(b *testing.B) {
    13  	str := "AaBbCcDdEeFfGgHhIiJjKk"
    14  	for i := 0; i < b.N; i++ {
    15  		_ = strings.ToLower(str)
    16  	}
    17  }
    18  
    19  // util.ToLower is 3x faster than strings.ToLower.
    20  func BenchmarkToLowerInternal(b *testing.B) {
    21  	str := "AaBbCcDdEeFfGgHhIiJjKk"
    22  	for i := 0; i < b.N; i++ {
    23  		_ = ToLower(str)
    24  	}
    25  }
    26  
    27  func TestToLower(t *testing.T) {
    28  	It("toLower", func() {
    29  		str := "AaBbCcDdEeFfGg"
    30  		Expect(ToLower(str)).To(Equal(strings.ToLower(str)))
    31  
    32  		str = "ABCDE"
    33  		Expect(ToLower(str)).To(Equal(strings.ToLower(str)))
    34  
    35  		str = "ABCDE"
    36  		Expect(ToLower(str)).To(Equal(strings.ToLower(str)))
    37  
    38  		str = "abced"
    39  		Expect(ToLower(str)).To(Equal(strings.ToLower(str)))
    40  	})
    41  }
    42  
    43  func TestIsLower(t *testing.T) {
    44  	It("isLower", func() {
    45  		str := "AaBbCcDdEeFfGg"
    46  		Expect(isLower(str)).To(BeFalse())
    47  
    48  		str = "ABCDE"
    49  		Expect(isLower(str)).To(BeFalse())
    50  
    51  		str = "abcdefg"
    52  		Expect(isLower(str)).To(BeTrue())
    53  	})
    54  }
    55  
    56  func TestGetAddr(t *testing.T) {
    57  	It("getAddr", func() {
    58  		str := "127.0.0.1:1234"
    59  		Expect(GetAddr(str)).To(Equal(str))
    60  
    61  		str = "[::1]:1234"
    62  		Expect(GetAddr(str)).To(Equal(str))
    63  
    64  		str = "[fd01:abcd::7d03]:6379"
    65  		Expect(GetAddr(str)).To(Equal(str))
    66  
    67  		Expect(GetAddr("::1:1234")).To(Equal("[::1]:1234"))
    68  
    69  		Expect(GetAddr("fd01:abcd::7d03:6379")).To(Equal("[fd01:abcd::7d03]:6379"))
    70  
    71  		Expect(GetAddr("127.0.0.1")).To(Equal(""))
    72  
    73  		Expect(GetAddr("127")).To(Equal(""))
    74  	})
    75  }
    76  
    77  func BenchmarkReplaceSpaces(b *testing.B) {
    78  	version := runtime.Version()
    79  	for i := 0; i < b.N; i++ {
    80  		_ = ReplaceSpaces(version)
    81  	}
    82  }
    83  
    84  func ReplaceSpacesUseBuilder(s string) string {
    85  	// Pre-allocate a builder with the same length as s to minimize allocations.
    86  	// This is a basic optimization; adjust the initial size based on your use case.
    87  	var builder strings.Builder
    88  	builder.Grow(len(s))
    89  
    90  	for _, char := range s {
    91  		if char == ' ' {
    92  			// Replace space with a hyphen.
    93  			builder.WriteRune('-')
    94  		} else {
    95  			// Copy the character as-is.
    96  			builder.WriteRune(char)
    97  		}
    98  	}
    99  
   100  	return builder.String()
   101  }
   102  
   103  func BenchmarkReplaceSpacesUseBuilder(b *testing.B) {
   104  	version := runtime.Version()
   105  	for i := 0; i < b.N; i++ {
   106  		_ = ReplaceSpacesUseBuilder(version)
   107  	}
   108  }
   109  

View as plain text