gf/util/grand/grand_z_unit_test.go

200 lines
4.1 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// 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 (
"github.com/gogf/gf/text/gstr"
2019-06-19 09:06:52 +08:00
"testing"
"time"
2019-07-29 21:01:19 +08:00
"github.com/gogf/gf/test/gtest"
"github.com/gogf/gf/util/grand"
)
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
}
})
}
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
}
})
}
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
}
})
}
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
}
})
}
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},
)
}
})
}
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
}
})
}
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
}
})
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
t.Assert(len(grand.S(5, true)), 5)
}
})
}
func Test_B(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
for i := 0; i < 100; i++ {
b := grand.B(5)
t.Assert(len(b), 5)
t.AssertNE(b, make([]byte, 5))
}
})
}
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++ {
t.Assert(len(grand.S(5)), 5)
}
})
}
func Test_RandStr(t *testing.T) {
str := "我爱GoFrame"
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
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
}
})
}
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
}
})
}
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
}
})
}
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
}
})
}
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
}
})
}
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
}
})
}