diff options
| author | Stephen Hassard <steve@hassard.net> | 2018-12-30 05:09:42 -0800 |
|---|---|---|
| committer | Ibrahim Serdar Acikgoz <serdaracikgoz86@gmail.com> | 2018-12-30 13:09:42 +0000 |
| commit | 0b5c8b9cdc35cadc115edfb3e39c02fc894f0e60 (patch) | |
| tree | a00d5e3c577dfc162b04b13044553d0042267020 | |
| parent | hotfix on stashed items vol.3 (diff) | |
| download | gitbatch-0b5c8b9cdc35cadc115edfb3e39c02fc894f0e60.tar.gz | |
Fix RNG seeding and add a test. (#51)
* Fix seeding the RNG.
* Add test for RandomString.
| -rw-r--r-- | pkg/helpers/utils.go | 4 | ||||
| -rw-r--r-- | pkg/helpers/utils_test.go | 10 |
2 files changed, 12 insertions, 2 deletions
diff --git a/pkg/helpers/utils.go b/pkg/helpers/utils.go index 1a3f411..35de2c3 100644 --- a/pkg/helpers/utils.go +++ b/pkg/helpers/utils.go @@ -7,7 +7,7 @@ import ( ) var characterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") -var src = rand.NewSource(time.Now().UnixNano()) +var r = rand.New(rand.NewSource(time.Now().UnixNano())) // TrimTrailingNewline removes the trailing new line form a string. this method // is used mostly on outputs of a command @@ -30,7 +30,7 @@ func Min(x, y int) int { func RandomString(n int) string { b := make([]rune, n) for i := range b { - b[i] = characterRunes[rand.Intn(len(characterRunes))] + b[i] = characterRunes[r.Intn(len(characterRunes))] } return string(b) } diff --git a/pkg/helpers/utils_test.go b/pkg/helpers/utils_test.go new file mode 100644 index 0000000..4b48d3c --- /dev/null +++ b/pkg/helpers/utils_test.go @@ -0,0 +1,10 @@ +package helpers + +import "testing" + +func TestRandomString(t *testing.T) { + var rand_string = RandomString(8) + if len(rand_string) != 8 { + t.Errorf("The length of the string should be equal.") + } +} |
