github.com/gofiber/fiber/v2@v2.47.0/utils/convert_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 "testing" 9 ) 10 11 func Test_UnsafeString(t *testing.T) { 12 t.Parallel() 13 res := UnsafeString([]byte("Hello, World!")) 14 AssertEqual(t, "Hello, World!", res) 15 } 16 17 // go test -v -run=^$ -bench=UnsafeString -benchmem -count=2 18 19 func Benchmark_UnsafeString(b *testing.B) { 20 hello := []byte("Hello, World!") 21 var res string 22 b.Run("unsafe", func(b *testing.B) { 23 for n := 0; n < b.N; n++ { 24 res = UnsafeString(hello) 25 } 26 AssertEqual(b, "Hello, World!", res) 27 }) 28 b.Run("default", func(b *testing.B) { 29 for n := 0; n < b.N; n++ { 30 res = string(hello) 31 } 32 AssertEqual(b, "Hello, World!", res) 33 }) 34 } 35 36 func Test_UnsafeBytes(t *testing.T) { 37 t.Parallel() 38 res := UnsafeBytes("Hello, World!") 39 AssertEqual(t, []byte("Hello, World!"), res) 40 } 41 42 // go test -v -run=^$ -bench=UnsafeBytes -benchmem -count=4 43 44 func Benchmark_UnsafeBytes(b *testing.B) { 45 hello := "Hello, World!" 46 var res []byte 47 b.Run("unsafe", func(b *testing.B) { 48 for n := 0; n < b.N; n++ { 49 res = UnsafeBytes(hello) 50 } 51 AssertEqual(b, []byte("Hello, World!"), res) 52 }) 53 b.Run("default", func(b *testing.B) { 54 for n := 0; n < b.N; n++ { 55 res = []byte(hello) 56 } 57 AssertEqual(b, []byte("Hello, World!"), res) 58 }) 59 } 60 61 func Test_CopyString(t *testing.T) { 62 t.Parallel() 63 res := CopyString("Hello, World!") 64 AssertEqual(t, "Hello, World!", res) 65 } 66 67 func Test_ToString(t *testing.T) { 68 t.Parallel() 69 res := ToString([]byte("Hello, World!")) 70 AssertEqual(t, "Hello, World!", res) 71 res = ToString(true) 72 AssertEqual(t, "true", res) 73 res = ToString(uint(100)) 74 AssertEqual(t, "100", res) 75 } 76 77 // go test -v -run=^$ -bench=ToString -benchmem -count=2 78 func Benchmark_ToString(b *testing.B) { 79 hello := []byte("Hello, World!") 80 for n := 0; n < b.N; n++ { 81 ToString(hello) 82 } 83 }