mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-07 22:22:29 +08:00
publish lancet
This commit is contained in:
50
random/random.go
Normal file
50
random/random.go
Normal file
@@ -0,0 +1,50 @@
|
||||
// Copyright 2021 dudaodong@gmail.com. All rights reserved.
|
||||
// Use of this source code is governed by MIT license.
|
||||
|
||||
// Package random implements some basic functions to generate random int and string.
|
||||
package random
|
||||
|
||||
import (
|
||||
crand "crypto/rand"
|
||||
"io"
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// RandString generate random string
|
||||
// see https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
|
||||
func RandString(length int) string {
|
||||
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
b := make([]byte, length)
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
for i := range b {
|
||||
b[i] = letters[r.Int63()%int64(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
// RandInt generate random int between min and max, maybe min, not be max
|
||||
func RandInt(min, max int) int {
|
||||
if min == max {
|
||||
return min
|
||||
}
|
||||
if max < min {
|
||||
min, max = max, min
|
||||
}
|
||||
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
return r.Intn(max-min) + min
|
||||
}
|
||||
|
||||
// RandBytes generate random byte slice
|
||||
func RandBytes(length int) []byte {
|
||||
if length < 1 {
|
||||
return nil
|
||||
}
|
||||
b := make([]byte, length)
|
||||
|
||||
if _, err := io.ReadFull(crand.Reader, b); err != nil {
|
||||
return nil
|
||||
}
|
||||
return b
|
||||
}
|
||||
46
random/random_test.go
Normal file
46
random/random_test.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package random
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/duke-git/lancet/utils"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRandString(t *testing.T) {
|
||||
randStr := RandString(6)
|
||||
fmt.Println(randStr)
|
||||
pattern := `^[a-zA-Z]+$`
|
||||
reg := regexp.MustCompile(pattern)
|
||||
|
||||
if len(randStr) != 6 || !reg.MatchString(randStr) {
|
||||
utils.LogFailedTestInfo(t, "RandString", "RandString(6)", "RandString(6) should be 6 letters ", randStr)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandInt(t *testing.T) {
|
||||
randInt := RandInt(1, 10)
|
||||
|
||||
if randInt < 1 || randInt >= 10 {
|
||||
utils.LogFailedTestInfo(t, "RandInt", "RandInt(1, 10)", "RandInt(1, 10) should between [1, 10) ", randInt)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
|
||||
func TestRandBytes(t *testing.T) {
|
||||
randBytes := RandBytes(4)
|
||||
|
||||
if len(randBytes) != 4 {
|
||||
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(4)", "RandBytes(4) should return 4 element of []bytes", randBytes)
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(randBytes)
|
||||
et := v.Type().Elem()
|
||||
if v.Kind() != reflect.Slice || et.Kind() != reflect.Uint8 {
|
||||
utils.LogFailedTestInfo(t, "RandBytes", "RandBytes(4)", "RandBytes(4) should return 4 element of []bytes", randBytes)
|
||||
t.FailNow()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user