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