Skip to content

Commit

Permalink
#69: Change deprecated rand.Seed to NewSource/New
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurkushman committed Mar 28, 2024
1 parent 12dac71 commit 27f76ca
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions math.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ import (
// Rand returns a pseudo-random integer between min and max based on unix-nano time seed
// !! for random numbers suitable for security-sensitive work, use the crypto/rand package instead
func Rand(min, max int64) int64 {
rand.Seed(time.Now().UnixNano())
return rand.Int63n(max-min+1) + min
r := rand.New(rand.NewSource(time.Now().UnixNano()))
return r.Int63n(max-min+1) + min
}
41 changes: 39 additions & 2 deletions str_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ import (
"github.com/arthurkushman/pgo"
)

// TestStrReplace is a unit test function that tests the StrReplace function.
//
// It replaces "fox" with "cat" and "dog" with "fox" in the given subject string.
// The function asserts that the resulting string is equal to the expected result.
//
// Parameters:
//
// t: *testing.T - the testing object used for assertion.
//
// Return type: None.
func TestStrReplace(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"

Expand All @@ -24,6 +34,9 @@ func TestStrReplace(t *testing.T) {
assert.Equalf(t, subject, result, "want %s, got %s", subject, result)
}

// TestStrIReplace tests the StrIReplace function.
//
// This function takes input strings and replaces specified substrings case-insensitively.
func TestStrIReplace(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"

Expand All @@ -36,6 +49,10 @@ func TestStrIReplace(t *testing.T) {
assert.Equalf(t, subject, result, "want %s, got %s", subject, result)
}

// TestStrReplaceCount description of the Go function.
//
// It tests the StrReplace function by replacing a specified number of occurrences of a substring in a given string.
// Error is returned if the replacement fails.
func TestStrReplaceCount(t *testing.T) {
subject := "The quick brown fox jumped over the lazy fox or dog"
str, err := pgo.StrReplace("fox", "cat", subject, 1)
Expand All @@ -45,6 +62,9 @@ func TestStrReplaceCount(t *testing.T) {
assert.Equalf(t, str, result, "want %s, got %s", result, str)
}

// TestStrReplaceArray description of the Go function.
//
// It tests the StrReplace function by replacing occurrences of strings in the subject with new strings.
func TestStrReplaceArray(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"
str, err := pgo.StrReplace([]string{"fox", "dog"}, []string{"cat", "elephant"}, subject)
Expand All @@ -54,6 +74,9 @@ func TestStrReplaceArray(t *testing.T) {
assert.Equalf(t, str, result, "want %s, got %s", result, str)
}

// TestStrReplaceErrs tests the StrReplace function.
//
// It checks the behavior of StrReplace when replacing substrings in a given subject string.
func TestStrReplaceErrs(t *testing.T) {
subject := "The quick brown fox jumped over the lazy dog"

Expand All @@ -62,6 +85,9 @@ func TestStrReplaceErrs(t *testing.T) {
assert.Equalf(t, str, subject, "want %s, got %s", subject, str)
}

// TestHTTPBuildQuery is a Go test function for testing the HTTPBuildQuery function.
//
// It does not take any parameters and does not return any values.
func TestHTTPBuildQuery(t *testing.T) {
queryStr := pgo.HTTPBuildQuery(map[string]interface{}{
"foo": "bar",
Expand All @@ -80,6 +106,9 @@ func TestHTTPBuildQuery(t *testing.T) {
assert.Empty(t, queryStr2, "built str from an empty map must be empty")
}

// TestConcatFast is a Go function to test the ConcatFast function.
//
// It takes a slice of structs with name, s (a slice of strings), and result fields, and it runs tests for the ConcatFast function.
func TestConcatFast(t *testing.T) {
tests := []struct {
name string
Expand Down Expand Up @@ -127,22 +156,30 @@ func BenchmarkConcatFast2(b *testing.B) {
}
}

// generateRandomSliceOfStrings generates a slice of 15 random strings.
//
// No parameters.
// Returns a slice of strings.
func generateRandomSliceOfStrings() []string {
const letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

rand.Seed(time.Now().UnixNano())
r := rand.New(rand.NewSource(time.Now().UnixNano()))
s := make([]string, 15)
for i := range s {
bt := make([]byte, 10)
for j := range bt {
bt[j] = letterBytes[rand.Intn(len(letterBytes))]
bt[j] = letterBytes[r.Intn(len(letterBytes))]
}
s[i] = string(bt)
}

return s
}

// stringBuilder concatenates multiple strings into a single string.
//
// It takes in a variadic parameter `s` of type string.
// Returns a string.
func stringBuilder(s ...string) string {
l := len(s)
if l == 0 {
Expand Down

0 comments on commit 27f76ca

Please sign in to comment.