diff --git a/random_119.go b/random_119.go deleted file mode 100644 index 98d761c..0000000 --- a/random_119.go +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors -// -// SPDX-License-Identifier: MIT - -//go:build !go1.20 -// +build !go1.20 - -package mail - -import ( - "math/rand" - "time" -) - -// randNum returns a random number with a maximum value of maxval. -// -// This function generates a random integer between 0 and maxval (exclusive). It seeds the -// random number generator with the current time in nanoseconds to ensure different results -// each time the function is called. -// -// Parameters: -// - maxval: The upper bound for the random number generation (exclusive). -// -// Returns: -// - A random integer between 0 and maxval. If maxval is less than or equal to 0, it returns 0. -func randNum(maxval int) int { - if maxval <= 0 { - return 0 - } - rand.Seed(time.Now().UnixNano()) - return rand.Intn(maxval) -} diff --git a/random_121.go b/random_121.go deleted file mode 100644 index ea1e399..0000000 --- a/random_121.go +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors -// -// SPDX-License-Identifier: MIT - -//go:build go1.20 && !go1.22 -// +build go1.20,!go1.22 - -package mail - -import ( - "math/rand" -) - -// randNum returns a random number with a maximum value of maxval. -// -// This function generates a random integer between 0 and maxval (exclusive). If maxval is less -// than or equal to 0, it returns 0. The random number generator uses the default seed provided -// by the rand package. -// -// Parameters: -// - maxval: The upper bound for the random number generation (exclusive). -// -// Returns: -// - A random integer between 0 and maxval. If maxval is less than or equal to 0, it returns 0. -func randNum(maxval int) int { - if maxval <= 0 { - return 0 - } - return rand.Intn(maxval) -} diff --git a/random_122.go b/random_122.go deleted file mode 100644 index 27ded3d..0000000 --- a/random_122.go +++ /dev/null @@ -1,30 +0,0 @@ -// SPDX-FileCopyrightText: 2022-2023 The go-mail Authors -// -// SPDX-License-Identifier: MIT - -//go:build go1.22 -// +build go1.22 - -package mail - -import ( - "math/rand/v2" -) - -// randNum returns a random number with a maximum value of maxval. -// -// This function generates a random integer between 0 and maxval (exclusive). It utilizes -// the math/rand/v2 interface for Go 1.22+ and will default to math/rand for older Go versions. -// If maxval is less than or equal to 0, it returns 0. -// -// Parameters: -// - maxval: The upper bound for the random number generation (exclusive). -// -// Returns: -// - A random integer between 0 and maxval. If maxval is less than or equal to 0, it returns 0. -func randNum(maxval int) int { - if maxval <= 0 { - return 0 - } - return rand.IntN(maxval) -} diff --git a/random_test.go b/random_test.go index e69b4e7..a608c2a 100644 --- a/random_test.go +++ b/random_test.go @@ -38,34 +38,12 @@ func TestRandomStringSecure(t *testing.T) { } } -// TestRandomNum tests the randomNum method -func TestRandomNum(t *testing.T) { - tt := []struct { - testName string - max int - }{ - {"Max: 1", 1}, - {"Max: 20", 20}, - {"Max: 50", 50}, - {"Max: 100", 100}, - {"Max: 1000", 1000}, - {"Max: 10000", 10000}, - {"Max: 100000000", 100000000}, - } - - for _, tc := range tt { - t.Run(tc.testName, func(t *testing.T) { - rn := randNum(tc.max) - if rn > tc.max { - t.Errorf("random number generation failed: %d is bigger than given value %d", rn, tc.max) - } - }) - } -} - -func TestRandomNumZero(t *testing.T) { - rn := randNum(0) - if rn != 0 { - t.Errorf("random number generation failed: %d is not zero", rn) +func BenchmarkGenerator_RandomStringSecure(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + _, err := randomStringSecure(22) + if err != nil { + b.Errorf("RandomStringFromCharRange() failed: %s", err) + } } }