github.com/gofiber/fiber/v2@v2.47.0/utils/strings_test.go (about)

     1  // ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
     2  // 🤖 Github Repository: https://github.com/gofiber/fiber
     3  // 📌 API Documentation: https://docs.gofiber.io
     4  
     5  package utils
     6  
     7  import (
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func Test_ToUpper(t *testing.T) {
    13  	t.Parallel()
    14  	res := ToUpper("/my/name/is/:param/*")
    15  	AssertEqual(t, "/MY/NAME/IS/:PARAM/*", res)
    16  }
    17  
    18  const (
    19  	largeStr = "/RePos/GoFiBer/FibEr/iSsues/187643/CoMmEnts/RePos/GoFiBer/FibEr/iSsues/CoMmEnts"
    20  	upperStr = "/REPOS/GOFIBER/FIBER/ISSUES/187643/COMMENTS/REPOS/GOFIBER/FIBER/ISSUES/COMMENTS"
    21  	lowerStr = "/repos/gofiber/fiber/issues/187643/comments/repos/gofiber/fiber/issues/comments"
    22  )
    23  
    24  func Benchmark_ToUpper(b *testing.B) {
    25  	var res string
    26  	b.Run("fiber", func(b *testing.B) {
    27  		for n := 0; n < b.N; n++ {
    28  			res = ToUpper(largeStr)
    29  		}
    30  		AssertEqual(b, upperStr, res)
    31  	})
    32  	b.Run("default", func(b *testing.B) {
    33  		for n := 0; n < b.N; n++ {
    34  			res = strings.ToUpper(largeStr)
    35  		}
    36  		AssertEqual(b, upperStr, res)
    37  	})
    38  }
    39  
    40  func Test_ToLower(t *testing.T) {
    41  	t.Parallel()
    42  	res := ToLower("/MY/NAME/IS/:PARAM/*")
    43  	AssertEqual(t, "/my/name/is/:param/*", res)
    44  	res = ToLower("/MY1/NAME/IS/:PARAM/*")
    45  	AssertEqual(t, "/my1/name/is/:param/*", res)
    46  	res = ToLower("/MY2/NAME/IS/:PARAM/*")
    47  	AssertEqual(t, "/my2/name/is/:param/*", res)
    48  	res = ToLower("/MY3/NAME/IS/:PARAM/*")
    49  	AssertEqual(t, "/my3/name/is/:param/*", res)
    50  	res = ToLower("/MY4/NAME/IS/:PARAM/*")
    51  	AssertEqual(t, "/my4/name/is/:param/*", res)
    52  }
    53  
    54  func Benchmark_ToLower(b *testing.B) {
    55  	var res string
    56  	b.Run("fiber", func(b *testing.B) {
    57  		for n := 0; n < b.N; n++ {
    58  			res = ToLower(largeStr)
    59  		}
    60  		AssertEqual(b, lowerStr, res)
    61  	})
    62  	b.Run("default", func(b *testing.B) {
    63  		for n := 0; n < b.N; n++ {
    64  			res = strings.ToLower(largeStr)
    65  		}
    66  		AssertEqual(b, lowerStr, res)
    67  	})
    68  }
    69  
    70  func Test_TrimRight(t *testing.T) {
    71  	t.Parallel()
    72  	res := TrimRight("/test//////", '/')
    73  	AssertEqual(t, "/test", res)
    74  
    75  	res = TrimRight("/test", '/')
    76  	AssertEqual(t, "/test", res)
    77  
    78  	res = TrimRight(" ", ' ')
    79  	AssertEqual(t, "", res)
    80  
    81  	res = TrimRight("  ", ' ')
    82  	AssertEqual(t, "", res)
    83  
    84  	res = TrimRight("", ' ')
    85  	AssertEqual(t, "", res)
    86  }
    87  
    88  func Benchmark_TrimRight(b *testing.B) {
    89  	var res string
    90  
    91  	b.Run("fiber", func(b *testing.B) {
    92  		for n := 0; n < b.N; n++ {
    93  			res = TrimRight("foobar  ", ' ')
    94  		}
    95  		AssertEqual(b, "foobar", res)
    96  	})
    97  	b.Run("default", func(b *testing.B) {
    98  		for n := 0; n < b.N; n++ {
    99  			res = strings.TrimRight("foobar  ", " ")
   100  		}
   101  		AssertEqual(b, "foobar", res)
   102  	})
   103  }
   104  
   105  func Test_TrimLeft(t *testing.T) {
   106  	t.Parallel()
   107  	res := TrimLeft("////test/", '/')
   108  	AssertEqual(t, "test/", res)
   109  
   110  	res = TrimLeft("test/", '/')
   111  	AssertEqual(t, "test/", res)
   112  
   113  	res = TrimLeft(" ", ' ')
   114  	AssertEqual(t, "", res)
   115  
   116  	res = TrimLeft("  ", ' ')
   117  	AssertEqual(t, "", res)
   118  
   119  	res = TrimLeft("", ' ')
   120  	AssertEqual(t, "", res)
   121  }
   122  
   123  func Benchmark_TrimLeft(b *testing.B) {
   124  	var res string
   125  
   126  	b.Run("fiber", func(b *testing.B) {
   127  		for n := 0; n < b.N; n++ {
   128  			res = TrimLeft("  foobar", ' ')
   129  		}
   130  		AssertEqual(b, "foobar", res)
   131  	})
   132  	b.Run("default", func(b *testing.B) {
   133  		for n := 0; n < b.N; n++ {
   134  			res = strings.TrimLeft("  foobar", " ")
   135  		}
   136  		AssertEqual(b, "foobar", res)
   137  	})
   138  }
   139  
   140  func Test_Trim(t *testing.T) {
   141  	t.Parallel()
   142  	res := Trim("   test  ", ' ')
   143  	AssertEqual(t, "test", res)
   144  
   145  	res = Trim("test", ' ')
   146  	AssertEqual(t, "test", res)
   147  
   148  	res = Trim(".test", '.')
   149  	AssertEqual(t, "test", res)
   150  
   151  	res = Trim(" ", ' ')
   152  	AssertEqual(t, "", res)
   153  
   154  	res = Trim("  ", ' ')
   155  	AssertEqual(t, "", res)
   156  
   157  	res = Trim("", ' ')
   158  	AssertEqual(t, "", res)
   159  }
   160  
   161  func Benchmark_Trim(b *testing.B) {
   162  	var res string
   163  
   164  	b.Run("fiber", func(b *testing.B) {
   165  		for n := 0; n < b.N; n++ {
   166  			res = Trim("  foobar   ", ' ')
   167  		}
   168  		AssertEqual(b, "foobar", res)
   169  	})
   170  	b.Run("default", func(b *testing.B) {
   171  		for n := 0; n < b.N; n++ {
   172  			res = strings.Trim("  foobar   ", " ")
   173  		}
   174  		AssertEqual(b, "foobar", res)
   175  	})
   176  	b.Run("default.trimspace", func(b *testing.B) {
   177  		for n := 0; n < b.N; n++ {
   178  			res = strings.TrimSpace("  foobar   ")
   179  		}
   180  		AssertEqual(b, "foobar", res)
   181  	})
   182  }
   183  
   184  // go test -v -run=^$ -bench=Benchmark_EqualFold -benchmem -count=4
   185  func Benchmark_EqualFold(b *testing.B) {
   186  	var res bool
   187  	b.Run("fiber", func(b *testing.B) {
   188  		for n := 0; n < b.N; n++ {
   189  			res = EqualFold(upperStr, lowerStr)
   190  		}
   191  		AssertEqual(b, true, res)
   192  	})
   193  	b.Run("default", func(b *testing.B) {
   194  		for n := 0; n < b.N; n++ {
   195  			res = strings.EqualFold(upperStr, lowerStr)
   196  		}
   197  		AssertEqual(b, true, res)
   198  	})
   199  }
   200  
   201  func Test_EqualFold(t *testing.T) {
   202  	t.Parallel()
   203  	res := EqualFold("/MY/NAME/IS/:PARAM/*", "/my/name/is/:param/*")
   204  	AssertEqual(t, true, res)
   205  	res = EqualFold("/MY1/NAME/IS/:PARAM/*", "/MY1/NAME/IS/:PARAM/*")
   206  	AssertEqual(t, true, res)
   207  	res = EqualFold("/my2/name/is/:param/*", "/my2/name")
   208  	AssertEqual(t, false, res)
   209  	res = EqualFold("/dddddd", "eeeeee")
   210  	AssertEqual(t, false, res)
   211  	res = EqualFold("\na", "*A")
   212  	AssertEqual(t, false, res)
   213  	res = EqualFold("/MY3/NAME/IS/:PARAM/*", "/my3/name/is/:param/*")
   214  	AssertEqual(t, true, res)
   215  	res = EqualFold("/MY4/NAME/IS/:PARAM/*", "/my4/nAME/IS/:param/*")
   216  	AssertEqual(t, true, res)
   217  }