1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
// ⚡️ 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 (
"fmt"
"math"
"reflect"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
)
var dataTypeExamples []any
// MyStringer for testing fmt.Stringer support.
type MyStringer struct {
value string
}
// CustomType not handled by switch cases.
type CustomType struct {
Field1 string
Field2 int
}
func (ms MyStringer) String() string {
return ms.value
}
func init() {
dataTypeExamples = []any{
42,
int8(42),
int16(42),
int32(42),
int64(42),
uint(42),
uint8(42),
uint16(42),
uint32(42),
uint64(42),
"Hello, World!",
[]byte("Hello, World!"),
true,
float32(3.14),
float64(3.14),
time.Now(),
time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC),
[]string{"Hello", "World"},
[]int{42, 21},
[2]int{42, 21},
[][]int{{42, 21}, {42, 21}},
[]any{[]int{42, 21}, 42, "Hello", true, []string{"Hello", "World"}},
MyStringer{value: "Stringer Value"},
CustomType{Field1: "example", Field2: 42},
}
}
func Test_UnsafeString(t *testing.T) {
t.Parallel()
res := UnsafeString([]byte("Hello, World!"))
require.Equal(t, "Hello, World!", res)
}
func Test_UnsafeBytes(t *testing.T) {
t.Parallel()
res := UnsafeBytes("Hello, World!")
require.Equal(t, []byte("Hello, World!"), res)
}
func Test_CopyString(t *testing.T) {
t.Parallel()
res := CopyString("Hello, World!")
require.Equal(t, "Hello, World!", res)
}
func Test_ToString(t *testing.T) {
t.Parallel()
customInstance := CustomType{Field1: "example", Field2: 42}
timeSample := time.Date(2000, 1, 1, 12, 34, 56, 0, time.UTC)
stringerSample := MyStringer{value: "Stringer Value"}
tests := []struct {
name string
input any
timeFormat []string
expected string
}{
// Primitive types and string
{name: "int", input: int(42), expected: "42"},
{name: "int8", input: int8(42), expected: "42"},
{name: "int16", input: int16(42), expected: "42"},
{name: "int32", input: int32(42), expected: "42"},
{name: "int64", input: int64(42), expected: "42"},
{name: "uint", input: uint(100), expected: "100"},
{name: "uint8", input: uint8(100), expected: "100"},
{name: "uint16", input: uint16(100), expected: "100"},
{name: "uint32", input: uint32(100), expected: "100"},
{name: "uint64", input: uint64(100), expected: "100"},
{name: "string", input: "test string", expected: "test string"},
{name: "[]byte", input: []byte("Hello, World!"), expected: "Hello, World!"},
{name: "bool", input: true, expected: "true"},
{name: "float32", input: float32(3.14), expected: "3.14"},
{name: "float64", input: float64(3.14159), expected: "3.14159"},
// time.Time
{name: "time.Time default format", input: timeSample, expected: "2000-01-01 12:34:56"},
{name: "time.Time custom format", input: timeSample, timeFormat: []string{"Jan 02, 2006"}, expected: "Jan 01, 2000"},
// reflect.Value
{name: "reflect.Value", input: reflect.ValueOf(42), expected: "42"},
// fmt.Stringer
{name: "fmt.Stringer", input: stringerSample, expected: "Stringer Value"},
// Composite types (arrays, slices)
{name: "[]string", input: []string{"Hello", "World"}, expected: "[Hello World]"},
{name: "[]int", input: []int{42, 21}, expected: "[42 21]"},
{name: "[][]int", input: [][]int{{42, 21}, {42, 21}}, expected: "[[42 21] [42 21]]"},
{name: "[]any", input: []any{[]int{42, 21}, 42, "Hello", true, []string{"Hello", "World"}}, expected: "[[42 21] 42 Hello true [Hello World]]"},
// Custom unhandled type
{name: "CustomType", input: customInstance, expected: "{example 42}"},
}
for _, tc := range tests {
tc := tc // capture range variable
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var res string
if len(tc.timeFormat) > 0 {
res = ToString(tc.input, tc.timeFormat...)
} else {
res = ToString(tc.input)
}
require.Equal(t, tc.expected, res)
})
}
// Testing pointer to int
intPtr := 42
testsPtr := []struct {
input any
expected string
}{
{&intPtr, "42"},
}
for _, tc := range testsPtr {
tc := tc
t.Run("pointer to "+reflect.TypeOf(tc.input).Elem().String(), func(t *testing.T) {
t.Parallel()
res := ToString(tc.input)
require.Equal(t, tc.expected, res)
})
}
t.Run("nil", func(t *testing.T) {
t.Parallel()
require.Equal(t, "<nil>", ToString(nil))
})
// Test nil pointer handling - nil pointers return type-specific defaults
t.Run("nil pointer to string", func(t *testing.T) {
t.Parallel()
require.Empty(t, ToString((*string)(nil)))
})
t.Run("nil pointer to int", func(t *testing.T) {
t.Parallel()
require.Equal(t, "0", ToString((*int)(nil)))
})
t.Run("nil pointer to int64", func(t *testing.T) {
t.Parallel()
require.Equal(t, "0", ToString((*int64)(nil)))
})
t.Run("nil pointer to uint64", func(t *testing.T) {
t.Parallel()
require.Equal(t, "0", ToString((*uint64)(nil)))
})
t.Run("nil pointer to float64", func(t *testing.T) {
t.Parallel()
require.Equal(t, "0", ToString((*float64)(nil)))
})
t.Run("nil pointer to bool", func(t *testing.T) {
t.Parallel()
require.Equal(t, "false", ToString((*bool)(nil)))
})
}
func TestCopyBytes(t *testing.T) {
t.Run("empty slice", func(t *testing.T) {
input := []byte{}
copied := CopyBytes(input)
require.Equal(t, input, copied)
})
t.Run("single element", func(t *testing.T) {
input := []byte{42}
copied := CopyBytes(input)
require.Equal(t, input, copied)
input[0] = 0 // Modify the input to ensure the copied slice does not change
require.NotEqual(t, input[0], copied[0])
})
t.Run("multiple elements", func(t *testing.T) {
input := []byte{1, 2, 3, 4, 5}
copied := CopyBytes(input)
require.Equal(t, input, copied)
input[0] = 0 // Modify the input to ensure the copied slice does not change
require.NotEqual(t, input, copied)
})
t.Run("deep copy validation", func(t *testing.T) {
input := []byte{1, 2, 3, 4, 5}
copied := CopyBytes(input)
input[0] = 0 // Modify the input to ensure the copied slice does not change
require.NotEqual(t, input[0], copied[0])
})
t.Run("nil slice", func(t *testing.T) {
copied := CopyBytes(nil)
require.NotNil(t, copied)
require.Empty(t, copied)
require.Equal(t, 0, cap(copied))
})
}
func TestByteSize(t *testing.T) {
tests := []struct {
name string
bytes uint64
expected string
}{
{"Zero Byte", 0, "0B"},
{"One Byte", 1, "1B"},
{"Kilobytes", 1024, "1KB"},
{"Megabytes", 1024 * 1024, "1MB"},
{"Gigabytes", 1024 * 1024 * 1024, "1GB"},
{"Terabytes", 1024 * 1024 * 1024 * 1024, "1TB"},
{"Petabytes", 1024 * 1024 * 1024 * 1024 * 1024, "1PB"},
{"Exabytes", 1024 * 1024 * 1024 * 1024 * 1024 * 1024, "1EB"},
{"Bytes less than KB", 500, "500B"},
{"KB less than MB", 500 * 1024, "500KB"},
{"MB less than GB", 500 * 1024 * 1024, "500MB"},
{"GB less than TB", 500 * 1024 * 1024 * 1024, "500GB"},
{"TB less than PB", 500 * 1024 * 1024 * 1024 * 1024, "500TB"},
{"PB less than EB", 500 * 1024 * 1024 * 1024 * 1024 * 1024, "500PB"},
{"Fractional KB", 1126, "1.1KB"},
{"Fractional MB", 1126 * 1024, "1.1MB"},
{"Fractional GB", 1126 * 1024 * 1024, "1.1GB"},
{"Fractional TB", 1126 * 1024 * 1024 * 1024, "1.1TB"},
{"Fractional PB", 1126 * 1024 * 1024 * 1024 * 1024, "1.1PB"},
{"Fractional EB", 1126 * 1024 * 1024 * 1024 * 1024 * 1024, "1.1EB"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := ByteSize(tt.bytes)
require.Equal(t, tt.expected, result)
})
}
const maxSafe = math.MaxUint64 / 10
t.Run("MaxSafe", func(t *testing.T) {
result := ByteSize(maxSafe)
require.Contains(t, result, "B")
})
t.Run("Overflow", func(t *testing.T) {
result := ByteSize(maxSafe + 1)
require.Contains(t, result, "B")
require.Equal(t, ByteSize(maxSafe), result)
})
}
// Ensure ConvertToBytes caps values exceeding math.MaxInt
func Test_ConvertToBytes_MaxInt(t *testing.T) {
t.Parallel()
big := fmt.Sprintf("%dT", int64(math.MaxInt/1000+1))
require.Equal(t, math.MaxInt, ConvertToBytes(big))
}
// go test -v -run=^$ -bench=ToString -benchmem -count=4
func Benchmark_ToString(b *testing.B) {
for _, value := range dataTypeExamples {
b.Run(reflect.TypeOf(value).String(), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = ToString(value)
}
})
}
}
// go test -v -run=^$ -bench=ToString_concurrency -benchmem -count=4
func Benchmark_ToString_concurrency(b *testing.B) {
for _, value := range dataTypeExamples {
b.Run(reflect.TypeOf(value).String(), func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
_ = ToString(value)
}
})
})
}
}
// 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 i := 0; i < b.N; i++ {
res = UnsafeBytes(hello)
}
require.Equal(b, []byte("Hello, World!"), res)
})
b.Run("default", func(b *testing.B) {
for i := 0; i < b.N; i++ {
res = []byte(hello)
}
require.Equal(b, []byte("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 i := 0; i < b.N; i++ {
res = UnsafeString(hello)
}
require.Equal(b, "Hello, World!", res)
})
b.Run("default", func(b *testing.B) {
for i := 0; i < b.N; i++ {
res = string(hello)
}
require.Equal(b, "Hello, World!", res)
})
}
// go test -v -run=^$ -bench=ByteSize -benchmem -count=4
func Benchmark_ByteSize(b *testing.B) {
testCases := []uint64{
0,
1,
500,
uKilobyte,
1126,
uMegabyte,
1126 * uKilobyte,
uGigabyte,
1126 * uMegabyte,
uTerabyte,
1126 * uGigabyte,
uPetabyte,
1126 * uTerabyte,
uExabyte,
1126 * uPetabyte,
}
for _, bytes := range testCases {
b.Run(strconv.FormatUint(bytes, 10), func(b *testing.B) {
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_ = ByteSize(bytes)
}
})
}
}
|