From 1de2e2ceddb853e24a5788a9ebcf214550cddc96 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 3 Jan 2023 15:10:42 +0800 Subject: [PATCH] test: add examples for random package --- README.md | 34 ++++++--- README_zh-CN.md | 34 ++++++--- random/random.go | 30 +++++--- random/random_example_test.go | 126 ++++++++++++++++++++++++++++++++++ random/random_test.go | 1 + 5 files changed, 200 insertions(+), 25 deletions(-) create mode 100644 random/random_example_test.go diff --git a/README.md b/README.md index feafad4..a222e6e 100644 --- a/README.md +++ b/README.md @@ -537,14 +537,32 @@ import "github.com/duke-git/lancet/v2/random" #### Function list: -- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes) -- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt) -- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString) -- [RandUpper](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandUpper) -- [RandLower](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower) -- [RandNumeral](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral) -- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeralOrLetter) -- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4) +- **RandBytes** : generate random byte slice. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandBytes)] + [[play](https://go.dev/play/p/EkiLESeXf8d)] +- **RandInt** : generate random int number between min and max. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)] + [[play](https://go.dev/play/p/pXyyAAI5YxD)] +- **RandString** : generate random string of specified length. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)] + [[play](https://go.dev/play/p/W2xvRUXA7Mi)] +- **RandUpper** : generate a random upper case string. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandUpper)] + [[play](https://go.dev/play/p/29QfOh0DVuh)] +- **RandLower** : generate a random lower case string. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)] + [[play](https://go.dev/play/p/XJtZ471cmtI)] +- **RandNumeral** : generate a random numeral string of specified length. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)] + [[play](https://go.dev/play/p/g4JWVpHsJcf)] +- **RandNumeralOrLetter** : generate a random numeral or letter string. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeralOrLetter)] + [[play](https://go.dev/play/p/19CEQvpx2jD)] +- **UUIdV4** : generate a random UUID of version 4 according to RFC 4122. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4)] + [[play](https://go.dev/play/p/_Z9SFmr28ft)] + + ### 15. Retry package is for executing a function repeatedly until it was successful or canceled by the context. diff --git a/README_zh-CN.md b/README_zh-CN.md index 1f2aa73..d01a166 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -536,14 +536,32 @@ import "github.com/duke-git/lancet/v2/random" #### 函数列表: -- [RandBytes](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes) -- [RandInt](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt) -- [RandString](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString) -- [RandUpper](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandUpper) -- [RandLower](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandLower) -- [RandNumeral](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeral) -- [RandNumeralOrLetter](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeralOrLetter) -- [UUIdV4](https://github.com/duke-git/lancet/blob/main/docs/random.md#UUIdV4) +- **RandBytes** : 生成随机字节切片。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandBytes)] + [[play](https://go.dev/play/p/EkiLESeXf8d)] +- **RandInt** : 生成随机int, 范围[min, max)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandInt)] + [[play](https://go.dev/play/p/pXyyAAI5YxD)] +- **RandString** : 生成给定长度的随机字符串,只包含字母(a-zA-Z)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandString)] + [[play](https://go.dev/play/p/W2xvRUXA7Mi)] +- **RandUpper** : 生成给定长度的随机大写字母字符串(A-Z)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandUpper)] + [[play](https://go.dev/play/p/29QfOh0DVuh)] +- **RandLower** : 生成给定长度的随机小写字母字符串(a-z)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandLower)] + [[play](https://go.dev/play/p/XJtZ471cmtI)] +- **RandNumeral** : 生成给定长度的随机数字字符串(0-9)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeral)] + [[play](https://go.dev/play/p/g4JWVpHsJcf)] +- **RandNumeralOrLetter** : 生成给定长度的随机字符串(数字+字母)。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#RandNumeralOrLetter)] + [[play](https://go.dev/play/p/19CEQvpx2jD)] +- **UUIdV4** : 生成UUID v4字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/random_zh-CN.md#UUIdV4)] + [[play](https://go.dev/play/p/_Z9SFmr28ft)] + + ### 15. retry 重试执行函数直到函数运行成功或被 context cancel。 diff --git a/random/random.go b/random/random.go index d3158b8..8cf31af 100644 --- a/random/random.go +++ b/random/random.go @@ -19,7 +19,8 @@ const ( LETTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" ) -// RandInt generate random int between min and max, maybe min, not be max +// RandInt generate random int between min and max, maybe min, not be max. +// Play: https://go.dev/play/p/pXyyAAI5YxD func RandInt(min, max int) int { if min == max { return min @@ -28,10 +29,12 @@ func RandInt(min, max int) int { min, max = max, min } r := rand.New(rand.NewSource(time.Now().UnixNano())) + return r.Intn(max-min) + min } -// RandBytes generate random byte slice +// RandBytes generate random byte slice. +// Play: https://go.dev/play/p/EkiLESeXf8d func RandBytes(length int) []byte { if length < 1 { return []byte{} @@ -41,45 +44,54 @@ func RandBytes(length int) []byte { if _, err := io.ReadFull(crand.Reader, b); err != nil { return nil } + return b } -// RandString generate random string +// RandString generate random string of specified length. +// Play: https://go.dev/play/p/W2xvRUXA7Mi func RandString(length int) string { return random(LETTERS, length) } -// RandUpper generate a random upper case string +// RandUpper generate a random upper case string. +// Play: https://go.dev/play/p/29QfOh0DVuh func RandUpper(length int) string { return random(UPPER_LETTERS, length) } -// RandLower generate a random lower case string +// RandLower generate a random lower case string. +// Play: https://go.dev/play/p/XJtZ471cmtI func RandLower(length int) string { return random(LOWER_LETTERS, length) } -// RandNumeral generate a random numeral string +// RandNumeral generate a random numeral string of specified length. +// Play: https://go.dev/play/p/g4JWVpHsJcf func RandNumeral(length int) string { return random(NUMERAL, length) } -// RandNumeralOrLetter generate a random numeral or letter string +// RandNumeralOrLetter generate a random numeral or letter string. +// Play: https://go.dev/play/p/19CEQvpx2jD func RandNumeralOrLetter(length int) string { return random(NUMERAL+LETTERS, length) } -// random generate a random string based on given string range +// random generate a random string based on given string range. func random(s string, length int) string { b := make([]byte, length) r := rand.New(rand.NewSource(time.Now().UnixNano())) + for i := range b { b[i] = s[r.Int63()%int64(len(s))] } + return string(b) } -// UUIdV4 generate a random UUID of version 4 according to RFC 4122 +// UUIdV4 generate a random UUID of version 4 according to RFC 4122. +// Play: https://go.dev/play/p/_Z9SFmr28ft func UUIdV4() (string, error) { uuid := make([]byte, 16) diff --git a/random/random_example_test.go b/random/random_example_test.go new file mode 100644 index 0000000..42ae146 --- /dev/null +++ b/random/random_example_test.go @@ -0,0 +1,126 @@ +package random + +import ( + "fmt" + "regexp" +) + +func ExampleRandInt() { + + result := RandInt(1, 10) + + if result >= 1 && result < 10 { + fmt.Println("ok") + } + + // Output: + // ok +} + +func ExampleRandBytes() { + bytes := RandBytes(4) + + fmt.Println(len(bytes)) + + // Output: + // 4 +} + +func ExampleRandString() { + pattern := `^[a-zA-Z]+$` + reg := regexp.MustCompile(pattern) + + s := RandString(6) + + result1 := reg.MatchString(s) + result2 := len(s) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // 6 +} + +func ExampleRandUpper() { + pattern := `^[A-Z]+$` + reg := regexp.MustCompile(pattern) + + s := RandUpper(6) + + result1 := reg.MatchString(s) + result2 := len(s) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // 6 +} + +func ExampleRandLower() { + pattern := `^[a-z]+$` + reg := regexp.MustCompile(pattern) + + s := RandLower(6) + + result1 := reg.MatchString(s) + result2 := len(s) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // 6 +} + +func ExampleRandNumeral() { + pattern := `^[0-9]+$` + reg := regexp.MustCompile(pattern) + + s := RandNumeral(6) + + result1 := reg.MatchString(s) + result2 := len(s) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // 6 +} + +func ExampleRandNumeralOrLetter() { + pattern := `^[0-9a-zA-Z]+$` + reg := regexp.MustCompile(pattern) + + s := RandNumeralOrLetter(6) + + result1 := reg.MatchString(s) + result2 := len(s) + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // true + // 6 +} + +func ExampleUUIdV4() { + pattern := `^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$` + reg := regexp.MustCompile(pattern) + + s, _ := UUIdV4() + + result := reg.MatchString(s) + + fmt.Println(result) + + // Output: + // true +} diff --git a/random/random_test.go b/random/random_test.go index e4bd030..7e4627e 100644 --- a/random/random_test.go +++ b/random/random_test.go @@ -68,6 +68,7 @@ func TestRandInt(t *testing.T) { assert := internal.NewAssert(t, "TestRandInt") r1 := RandInt(1, 10) + t.Log(r1) assert.GreaterOrEqual(r1, 1) assert.Less(r1, 10)