Feat[entropy]: Random slice value
This commit is contained in:
parent
3139db4896
commit
929cdf25bb
|
@ -23,6 +23,14 @@ func RandomStrChoice(choice []string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
// GetRandomSliceValue returns a random item from an input slice of strings.
|
||||
func GetRandomSliceValue(choice ...any) any {
|
||||
if len(choice) > 0 {
|
||||
return choice[RNGUint32()%uint32(len(choice))]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetCryptoSeed returns a random int64 derived from crypto/rand.
|
||||
// This can be used as a seed for the math/rand package.
|
||||
func GetCryptoSeed() int64 {
|
||||
|
|
|
@ -84,15 +84,27 @@ func Test_RandStr_Entropy(t *testing.T) {
|
|||
t.Logf("[ENTROPY] final score (lower is better): %d (RandStr)", totalScore)
|
||||
}
|
||||
|
||||
func Test_RandomStrChoice(t *testing.T) {
|
||||
func sliceTest[T comparable](t *testing.T, do func(...T) T) {
|
||||
var slice []T
|
||||
for n := 0; n != 500; n++ {
|
||||
var item interface{} = RandStr(555)
|
||||
slice = append(slice, item)
|
||||
}
|
||||
check(do(slice), do(slice), t)
|
||||
}
|
||||
|
||||
func Test_RandomSliceValue(t *testing.T) {
|
||||
if RandomStrChoice([]string{}) != "" {
|
||||
t.Fatalf("RandomStrChoice returned a value when given an empty slice")
|
||||
}
|
||||
var slice []string
|
||||
for n := 0; n != 500; n++ {
|
||||
slice = append(slice, RandStr(555))
|
||||
sliceTest(t, GetRandomSliceValue)
|
||||
}
|
||||
|
||||
func Test_RandomStrChoice(t *testing.T) {
|
||||
if GetRandomSliceValue([]interface{}{}) != nil {
|
||||
t.Fatalf("GetRandomSliceValue returned a value when given an empty slice")
|
||||
}
|
||||
check(RandomStrChoice(slice), RandomStrChoice(slice), t)
|
||||
sliceTest(t, RandomStrChoice)
|
||||
}
|
||||
|
||||
func Test_RNGUint32(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue