fiber/utils/convert_test.go
Gusted 7b7dcf29f7
♻️ Tidy up the codebase (#1613)
* run gofmt

* add t.Helper()

* Simplify assigns

* Simplify make operation

* Remove unused field in struct

* Fix typo

* Run gofumpt ./

* Consistent spacing

* len(...) can never be negative

* Use ReplaceAll

* Simplify operation

* Remove deadcode

* Fix typo

* Tidy up `} else { if ...`

* Fix AssertEqual

* Remove t.Helper() to fix go1.14.15
2021-11-05 08:00:03 +01:00

64 lines
1.5 KiB
Go

// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import "testing"
func Test_UnsafeString(t *testing.T) {
t.Parallel()
res := UnsafeString([]byte("Hello, World!"))
AssertEqual(t, "Hello, World!", res)
}
// go test -v -run=^$ -bench=UnsafeString -benchmem -count=2
func Benchmark_UnsafeString(b *testing.B) {
hello := []byte("Hello, World!")
var res string
b.Run("unsafe", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = UnsafeString(hello)
}
AssertEqual(b, "Hello, World!", res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = string(hello)
}
AssertEqual(b, "Hello, World!", res)
})
}
func Test_UnsafeBytes(t *testing.T) {
t.Parallel()
res := UnsafeBytes("Hello, World!")
AssertEqual(t, []byte("Hello, World!"), res)
}
// go test -v -run=^$ -bench=UnsafeBytes -benchmem -count=4
func Benchmark_UnsafeBytes(b *testing.B) {
hello := "Hello, World!"
var res []byte
b.Run("unsafe", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = UnsafeBytes(hello)
}
AssertEqual(b, []byte("Hello, World!"), res)
})
b.Run("default", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = []byte(hello)
}
AssertEqual(b, []byte("Hello, World!"), res)
})
}
func Test_CopyString(t *testing.T) {
t.Parallel()
res := CopyString("Hello, World!")
AssertEqual(t, "Hello, World!", res)
}