mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-15 02:12:55 +01:00
Delete redundant random number generation methods
Removed `randNum` functions and associated tests from `random_119.go`, `random_121.go`, and `random_122.go`. These functions are no longer necessary. Additionally, replaced `TestRandomNum` and `TestRandomNumZero` with benchmarking for `randomStringSecure`.
This commit is contained in:
parent
a94e721161
commit
cdb9463ec8
4 changed files with 7 additions and 121 deletions
|
@ -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)
|
|
||||||
}
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -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)
|
|
||||||
}
|
|
|
@ -38,34 +38,12 @@ func TestRandomStringSecure(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRandomNum tests the randomNum method
|
func BenchmarkGenerator_RandomStringSecure(b *testing.B) {
|
||||||
func TestRandomNum(t *testing.T) {
|
b.ReportAllocs()
|
||||||
tt := []struct {
|
for i := 0; i < b.N; i++ {
|
||||||
testName string
|
_, err := randomStringSecure(22)
|
||||||
max int
|
if err != nil {
|
||||||
}{
|
b.Errorf("RandomStringFromCharRange() failed: %s", err)
|
||||||
{"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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue