go-pwgen/helpers_test.go
2023-04-25 15:56:11 +02:00

84 lines
1.6 KiB
Go

package pwgen
import (
"math/rand"
"reflect"
"testing"
"unsafe"
)
func Test_alphabet(t *testing.T) {
type args struct {
start rune
end rune
}
tests := []struct {
name string
args args
wantResult []rune
}{
{
name: "Lowercase runes",
args: args{
start: 'a',
end: 'z',
},
//lowercase runes a to z
wantResult: []rune{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'},
},
{
name: "Uppercase runes",
args: args{
start: 'A',
end: 'Z',
},
wantResult: []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
},
{
name: "Digit runes",
args: args{
start: '0',
end: '9',
},
wantResult: []rune("0123456789"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotResult := alphabet(tt.args.start, tt.args.end); !reflect.DeepEqual(gotResult, tt.wantResult) {
t.Errorf("alphabet() = %v, want %v", gotResult, tt.wantResult)
}
})
}
}
func Fuzz_shuffle(f *testing.F) {
defaultPrng := rand.New(rand.NewSource(1))
f.Add("hello, world")
f.Add("a")
f.Add("aa")
f.Add("abcdef")
f.Add("01234")
f.Fuzz(func(t *testing.T, input string) {
in := []rune(input)
out := shuffle(in, defaultPrng)
if sliceReferenceEqual(in, out) {
t.Logf("returned the same input")
return
}
if len(input) > 1 && string(out) == input {
t.Errorf("shuffle() = %s, input %s", string(out), input)
}
})
}
func sliceReferenceEqual[T any](a, b []T) bool {
ah := (*reflect.SliceHeader)(unsafe.Pointer(&a))
bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
return ah.Data == bh.Data
}