-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: litt3 <[email protected]>
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package random | ||
|
||
import ( | ||
"fmt" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
// TestRandom provides all the functionality of math/rand.Rand, plus additional randomness functionality useful for testing | ||
type TestRandom struct { | ||
*rand.Rand | ||
} | ||
|
||
// NewTestRandom creates a new instance of TestRandom | ||
// This method may either be seeded, or not seeded. If no seed is provided, then current unix nano time is used. | ||
func NewTestRandom(fixedSeed ...int64) *TestRandom { | ||
var seed int64 | ||
if len(fixedSeed) == 0 { | ||
seed = time.Now().UnixNano() | ||
} else if len(fixedSeed) == 1 { | ||
seed = fixedSeed[0] | ||
} else { | ||
panic("too many arguments, expected exactly one seed") | ||
} | ||
|
||
fmt.Printf("Random seed: %d\n", seed) | ||
return &TestRandom{ | ||
rand.New(rand.NewSource(seed)), | ||
} | ||
} | ||
|
||
// RandomBytes generates a random byte slice of a given length. | ||
func (r *TestRandom) RandomBytes(length int) []byte { | ||
bytes := make([]byte, length) | ||
_, err := r.Read(bytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return bytes | ||
} | ||
|
||
// RandomTime generates a random time. | ||
func (r *TestRandom) RandomTime() time.Time { | ||
return time.Unix(r.Int63(), r.Int63()) | ||
} | ||
|
||
// RandomString generates a random string out of printable ASCII characters. | ||
func (r *TestRandom) RandomString(length int) string { | ||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" | ||
b := make([]byte, length) | ||
for i := range b { | ||
b[i] = charset[r.Intn(len(charset))] | ||
} | ||
return string(b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package random | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"math/rand" | ||
"testing" | ||
) | ||
|
||
// Tests that random seeding produces random results, and that consistent seeding produces consistent results | ||
func TestSetup(t *testing.T) { | ||
testRandom1 := NewTestRandom() | ||
x := testRandom1.Int() | ||
|
||
testRandom2 := NewTestRandom() | ||
y := testRandom2.Int() | ||
|
||
assert.NotEqual(t, x, y) | ||
|
||
seed := rand.Int63() | ||
testRandom3 := NewTestRandom(seed) | ||
a := testRandom3.Int() | ||
|
||
testRandom4 := NewTestRandom(seed) | ||
b := testRandom4.Int() | ||
|
||
assert.Equal(t, a, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters