github.com/gofiber/fiber/v2@v2.47.0/utils/bytes_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  	"bytes"
     9  	"testing"
    10  )
    11  
    12  func Test_ToLowerBytes(t *testing.T) {
    13  	t.Parallel()
    14  	res := ToLowerBytes([]byte("/MY/NAME/IS/:PARAM/*"))
    15  	AssertEqual(t, true, bytes.Equal([]byte("/my/name/is/:param/*"), res))
    16  	res = ToLowerBytes([]byte("/MY1/NAME/IS/:PARAM/*"))
    17  	AssertEqual(t, true, bytes.Equal([]byte("/my1/name/is/:param/*"), res))
    18  	res = ToLowerBytes([]byte("/MY2/NAME/IS/:PARAM/*"))
    19  	AssertEqual(t, true, bytes.Equal([]byte("/my2/name/is/:param/*"), res))
    20  	res = ToLowerBytes([]byte("/MY3/NAME/IS/:PARAM/*"))
    21  	AssertEqual(t, true, bytes.Equal([]byte("/my3/name/is/:param/*"), res))
    22  	res = ToLowerBytes([]byte("/MY4/NAME/IS/:PARAM/*"))
    23  	AssertEqual(t, true, bytes.Equal([]byte("/my4/name/is/:param/*"), res))
    24  }
    25  
    26  func Benchmark_ToLowerBytes(b *testing.B) {
    27  	path := []byte(largeStr)
    28  	want := []byte(lowerStr)
    29  	var res []byte
    30  	b.Run("fiber", func(b *testing.B) {
    31  		for n := 0; n < b.N; n++ {
    32  			res = ToLowerBytes(path)
    33  		}
    34  		AssertEqual(b, bytes.Equal(want, res), true)
    35  	})
    36  	b.Run("default", func(b *testing.B) {
    37  		for n := 0; n < b.N; n++ {
    38  			res = bytes.ToLower(path)
    39  		}
    40  		AssertEqual(b, bytes.Equal(want, res), true)
    41  	})
    42  }
    43  
    44  func Test_ToUpperBytes(t *testing.T) {
    45  	t.Parallel()
    46  	res := ToUpperBytes([]byte("/my/name/is/:param/*"))
    47  	AssertEqual(t, true, bytes.Equal([]byte("/MY/NAME/IS/:PARAM/*"), res))
    48  	res = ToUpperBytes([]byte("/my1/name/is/:param/*"))
    49  	AssertEqual(t, true, bytes.Equal([]byte("/MY1/NAME/IS/:PARAM/*"), res))
    50  	res = ToUpperBytes([]byte("/my2/name/is/:param/*"))
    51  	AssertEqual(t, true, bytes.Equal([]byte("/MY2/NAME/IS/:PARAM/*"), res))
    52  	res = ToUpperBytes([]byte("/my3/name/is/:param/*"))
    53  	AssertEqual(t, true, bytes.Equal([]byte("/MY3/NAME/IS/:PARAM/*"), res))
    54  	res = ToUpperBytes([]byte("/my4/name/is/:param/*"))
    55  	AssertEqual(t, true, bytes.Equal([]byte("/MY4/NAME/IS/:PARAM/*"), res))
    56  }
    57  
    58  func Benchmark_ToUpperBytes(b *testing.B) {
    59  	path := []byte(largeStr)
    60  	want := []byte(upperStr)
    61  	var res []byte
    62  	b.Run("fiber", func(b *testing.B) {
    63  		for n := 0; n < b.N; n++ {
    64  			res = ToUpperBytes(path)
    65  		}
    66  		AssertEqual(b, bytes.Equal(want, res), true)
    67  	})
    68  	b.Run("default", func(b *testing.B) {
    69  		for n := 0; n < b.N; n++ {
    70  			res = bytes.ToUpper(path)
    71  		}
    72  		AssertEqual(b, bytes.Equal(want, res), true)
    73  	})
    74  }
    75  
    76  func Test_TrimRightBytes(t *testing.T) {
    77  	t.Parallel()
    78  	res := TrimRightBytes([]byte("/test//////"), '/')
    79  	AssertEqual(t, []byte("/test"), res)
    80  
    81  	res = TrimRightBytes([]byte("/test"), '/')
    82  	AssertEqual(t, []byte("/test"), res)
    83  
    84  	res = TrimRightBytes([]byte(" "), ' ')
    85  	AssertEqual(t, 0, len(res))
    86  
    87  	res = TrimRightBytes([]byte("  "), ' ')
    88  	AssertEqual(t, 0, len(res))
    89  
    90  	res = TrimRightBytes([]byte(""), ' ')
    91  	AssertEqual(t, 0, len(res))
    92  }
    93  
    94  func Benchmark_TrimRightBytes(b *testing.B) {
    95  	var res []byte
    96  
    97  	b.Run("fiber", func(b *testing.B) {
    98  		for n := 0; n < b.N; n++ {
    99  			res = TrimRightBytes([]byte("foobar  "), ' ')
   100  		}
   101  		AssertEqual(b, []byte("foobar"), res)
   102  	})
   103  	b.Run("default", func(b *testing.B) {
   104  		for n := 0; n < b.N; n++ {
   105  			res = bytes.TrimRight([]byte("foobar  "), " ")
   106  		}
   107  		AssertEqual(b, []byte("foobar"), res)
   108  	})
   109  }
   110  
   111  func Test_TrimLeftBytes(t *testing.T) {
   112  	t.Parallel()
   113  	res := TrimLeftBytes([]byte("////test/"), '/')
   114  	AssertEqual(t, []byte("test/"), res)
   115  
   116  	res = TrimLeftBytes([]byte("test/"), '/')
   117  	AssertEqual(t, []byte("test/"), res)
   118  
   119  	res = TrimLeftBytes([]byte(" "), ' ')
   120  	AssertEqual(t, 0, len(res))
   121  
   122  	res = TrimLeftBytes([]byte("  "), ' ')
   123  	AssertEqual(t, 0, len(res))
   124  
   125  	res = TrimLeftBytes([]byte(""), ' ')
   126  	AssertEqual(t, 0, len(res))
   127  }
   128  
   129  func Benchmark_TrimLeftBytes(b *testing.B) {
   130  	var res []byte
   131  
   132  	b.Run("fiber", func(b *testing.B) {
   133  		for n := 0; n < b.N; n++ {
   134  			res = TrimLeftBytes([]byte("  foobar"), ' ')
   135  		}
   136  		AssertEqual(b, []byte("foobar"), res)
   137  	})
   138  	b.Run("default", func(b *testing.B) {
   139  		for n := 0; n < b.N; n++ {
   140  			res = bytes.TrimLeft([]byte("  foobar"), " ")
   141  		}
   142  		AssertEqual(b, []byte("foobar"), res)
   143  	})
   144  }
   145  
   146  func Test_TrimBytes(t *testing.T) {
   147  	t.Parallel()
   148  	res := TrimBytes([]byte("   test  "), ' ')
   149  	AssertEqual(t, []byte("test"), res)
   150  
   151  	res = TrimBytes([]byte("test"), ' ')
   152  	AssertEqual(t, []byte("test"), res)
   153  
   154  	res = TrimBytes([]byte(".test"), '.')
   155  	AssertEqual(t, []byte("test"), res)
   156  
   157  	res = TrimBytes([]byte(" "), ' ')
   158  	AssertEqual(t, 0, len(res))
   159  
   160  	res = TrimBytes([]byte("  "), ' ')
   161  	AssertEqual(t, 0, len(res))
   162  
   163  	res = TrimBytes([]byte(""), ' ')
   164  	AssertEqual(t, 0, len(res))
   165  }
   166  
   167  func Benchmark_TrimBytes(b *testing.B) {
   168  	var res []byte
   169  
   170  	b.Run("fiber", func(b *testing.B) {
   171  		for n := 0; n < b.N; n++ {
   172  			res = TrimBytes([]byte("  foobar   "), ' ')
   173  		}
   174  		AssertEqual(b, []byte("foobar"), res)
   175  	})
   176  	b.Run("default", func(b *testing.B) {
   177  		for n := 0; n < b.N; n++ {
   178  			res = bytes.Trim([]byte("  foobar   "), " ")
   179  		}
   180  		AssertEqual(b, []byte("foobar"), res)
   181  	})
   182  }
   183  
   184  func Benchmark_EqualFoldBytes(b *testing.B) {
   185  	left := []byte(upperStr)
   186  	right := []byte(lowerStr)
   187  	var res bool
   188  	b.Run("fiber", func(b *testing.B) {
   189  		for n := 0; n < b.N; n++ {
   190  			res = EqualFoldBytes(left, right)
   191  		}
   192  		AssertEqual(b, true, res)
   193  	})
   194  	b.Run("default", func(b *testing.B) {
   195  		for n := 0; n < b.N; n++ {
   196  			res = bytes.EqualFold(left, right)
   197  		}
   198  		AssertEqual(b, true, res)
   199  	})
   200  }
   201  
   202  func Test_EqualFoldBytes(t *testing.T) {
   203  	t.Parallel()
   204  	res := EqualFoldBytes([]byte("/MY/NAME/IS/:PARAM/*"), []byte("/my/name/is/:param/*"))
   205  	AssertEqual(t, true, res)
   206  	res = EqualFoldBytes([]byte("/MY1/NAME/IS/:PARAM/*"), []byte("/MY1/NAME/IS/:PARAM/*"))
   207  	AssertEqual(t, true, res)
   208  	res = EqualFoldBytes([]byte("/my2/name/is/:param/*"), []byte("/my2/name"))
   209  	AssertEqual(t, false, res)
   210  	res = EqualFoldBytes([]byte("/dddddd"), []byte("eeeeee"))
   211  	AssertEqual(t, false, res)
   212  	res = EqualFoldBytes([]byte("\na"), []byte("*A"))
   213  	AssertEqual(t, false, res)
   214  	res = EqualFoldBytes([]byte("/MY3/NAME/IS/:PARAM/*"), []byte("/my3/name/is/:param/*"))
   215  	AssertEqual(t, true, res)
   216  	res = EqualFoldBytes([]byte("/MY4/NAME/IS/:PARAM/*"), []byte("/my4/nAME/IS/:param/*"))
   217  	AssertEqual(t, true, res)
   218  }