From cdb9463ec8bed94ffcbcc49826014ed62d4f4a77 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Mon, 7 Oct 2024 15:02:49 +0200 Subject: [PATCH] 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`. --- random_119.go | 32 -------------------------------- random_121.go | 30 ------------------------------ random_122.go | 30 ------------------------------ random_test.go | 36 +++++++----------------------------- 4 files changed, 7 insertions(+), 121 deletions(-) delete mode 100644 random_119.go delete mode 100644 random_121.go delete mode 100644 random_122.go 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) + } } }