2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2019-03-18 23:52:25 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
|
|
|
|
// go test *.go -bench=".*"
|
|
|
|
|
|
|
|
package grand_test
|
|
|
|
|
|
|
|
import (
|
2022-07-11 19:33:59 +08:00
|
|
|
"strings"
|
2019-06-19 09:06:52 +08:00
|
|
|
"testing"
|
2021-03-11 20:05:08 +08:00
|
|
|
"time"
|
2019-07-29 21:01:19 +08:00
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2021-11-13 23:30:31 +08:00
|
|
|
"github.com/gogf/gf/v2/text/gstr"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/util/grand"
|
2019-03-18 23:52:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_Intn(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 1000000; i++ {
|
|
|
|
n := grand.Intn(100)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertLT(n, 100)
|
|
|
|
t.AssertGE(n, 0)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 1000000; i++ {
|
|
|
|
n := grand.Intn(-100)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertLE(n, 0)
|
|
|
|
t.Assert(n, -100)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Meet(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.Meet(100, 100), true)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.Meet(0, 100), false)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(grand.Meet(50, 100), []bool{true, false})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_MeetProb(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.MeetProb(1), true)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.MeetProb(0), false)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(grand.MeetProb(0.5), []bool{true, false})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_N(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.N(1, 1), 1)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.N(0, 0), 0)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(grand.N(1, 2), []int{1, 2})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
2021-03-11 20:05:08 +08:00
|
|
|
func Test_D(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
t.Assert(grand.D(time.Second, time.Second), time.Second)
|
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
t.Assert(grand.D(0, 0), time.Duration(0))
|
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
t.AssertIN(
|
|
|
|
grand.D(1*time.Second, 3*time.Second),
|
|
|
|
[]time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-18 23:52:25 +08:00
|
|
|
func Test_Rand(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.N(1, 1), 1)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(grand.N(0, 0), 0)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(grand.N(1, 2), []int{1, 2})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(grand.N(-1, 2), []int{-1, 0, 1, 2})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:05:31 +08:00
|
|
|
func Test_S(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(grand.S(5)), 5)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2020-05-16 16:05:31 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
t.Assert(len(grand.S(5, true)), 5)
|
|
|
|
}
|
|
|
|
})
|
2022-07-11 19:33:59 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(len(grand.S(0)), 0)
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
2020-05-16 16:05:31 +08:00
|
|
|
func Test_B(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-21 00:01:27 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-05-16 16:05:31 +08:00
|
|
|
b := grand.B(5)
|
|
|
|
t.Assert(len(b), 5)
|
|
|
|
t.AssertNE(b, make([]byte, 5))
|
2020-02-21 00:01:27 +08:00
|
|
|
}
|
|
|
|
})
|
2022-07-11 19:33:59 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
b := grand.B(0)
|
|
|
|
t.AssertNil(b)
|
|
|
|
})
|
2020-05-16 16:05:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Str(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-05-16 16:05:31 +08:00
|
|
|
t.Assert(len(grand.S(5)), 5)
|
2020-02-21 00:01:27 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RandStr(t *testing.T) {
|
|
|
|
str := "我爱GoFrame"
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-02-21 00:01:27 +08:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
s := grand.Str(str, 100000)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gstr.Contains(s, "我"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "爱"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "G"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "o"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "F"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "r"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "a"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "m"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "e"), true)
|
|
|
|
t.Assert(gstr.Contains(s, "w"), false)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2022-07-11 19:33:59 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(grand.Str(str, 0), "")
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
list := []string{"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"}
|
|
|
|
str := ""
|
|
|
|
for _, s := range list {
|
|
|
|
tmp := ""
|
|
|
|
for i := 0; i < 15; i++ {
|
|
|
|
tmp += tmp + s
|
|
|
|
}
|
|
|
|
str += tmp
|
|
|
|
}
|
|
|
|
t.Assert(len(grand.Str(str, 300)), 300)
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Digits(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(grand.Digits(5)), 5)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RandDigits(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(grand.Digits(5)), 5)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2022-07-11 19:33:59 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(len(grand.Digits(0)), 0)
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Letters(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(grand.Letters(5)), 5)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RandLetters(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(len(grand.Letters(5)), 5)
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
2022-07-11 19:33:59 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(len(grand.Letters(0)), 0)
|
|
|
|
})
|
2019-03-18 23:52:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Perm(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2019-06-19 09:06:52 +08:00
|
|
|
for i := 0; i < 100; i++ {
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertIN(grand.Perm(5), []int{0, 1, 2, 3, 4})
|
2019-06-19 09:06:52 +08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-07-11 19:33:59 +08:00
|
|
|
|
|
|
|
func Test_Symbols(t *testing.T) {
|
|
|
|
symbols := "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
syms := []byte(grand.Symbols(5))
|
|
|
|
for _, sym := range syms {
|
|
|
|
t.AssertNE(strings.Index(symbols, string(sym)), -1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
t.Assert(grand.Symbols(0), "")
|
|
|
|
})
|
|
|
|
}
|