github.com/gogf/gf@v1.16.9/util/grand/grand_z_unit_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // go test *.go -bench=".*" 8 9 package grand_test 10 11 import ( 12 "github.com/gogf/gf/text/gstr" 13 "testing" 14 "time" 15 16 "github.com/gogf/gf/test/gtest" 17 "github.com/gogf/gf/util/grand" 18 ) 19 20 func Test_Intn(t *testing.T) { 21 gtest.C(t, func(t *gtest.T) { 22 for i := 0; i < 1000000; i++ { 23 n := grand.Intn(100) 24 t.AssertLT(n, 100) 25 t.AssertGE(n, 0) 26 } 27 for i := 0; i < 1000000; i++ { 28 n := grand.Intn(-100) 29 t.AssertLE(n, 0) 30 t.Assert(n, -100) 31 } 32 }) 33 } 34 35 func Test_Meet(t *testing.T) { 36 gtest.C(t, func(t *gtest.T) { 37 for i := 0; i < 100; i++ { 38 t.Assert(grand.Meet(100, 100), true) 39 } 40 for i := 0; i < 100; i++ { 41 t.Assert(grand.Meet(0, 100), false) 42 } 43 for i := 0; i < 100; i++ { 44 t.AssertIN(grand.Meet(50, 100), []bool{true, false}) 45 } 46 }) 47 } 48 49 func Test_MeetProb(t *testing.T) { 50 gtest.C(t, func(t *gtest.T) { 51 for i := 0; i < 100; i++ { 52 t.Assert(grand.MeetProb(1), true) 53 } 54 for i := 0; i < 100; i++ { 55 t.Assert(grand.MeetProb(0), false) 56 } 57 for i := 0; i < 100; i++ { 58 t.AssertIN(grand.MeetProb(0.5), []bool{true, false}) 59 } 60 }) 61 } 62 63 func Test_N(t *testing.T) { 64 gtest.C(t, func(t *gtest.T) { 65 for i := 0; i < 100; i++ { 66 t.Assert(grand.N(1, 1), 1) 67 } 68 for i := 0; i < 100; i++ { 69 t.Assert(grand.N(0, 0), 0) 70 } 71 for i := 0; i < 100; i++ { 72 t.AssertIN(grand.N(1, 2), []int{1, 2}) 73 } 74 }) 75 } 76 77 func Test_D(t *testing.T) { 78 gtest.C(t, func(t *gtest.T) { 79 for i := 0; i < 100; i++ { 80 t.Assert(grand.D(time.Second, time.Second), time.Second) 81 } 82 for i := 0; i < 100; i++ { 83 t.Assert(grand.D(0, 0), time.Duration(0)) 84 } 85 for i := 0; i < 100; i++ { 86 t.AssertIN( 87 grand.D(1*time.Second, 3*time.Second), 88 []time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second}, 89 ) 90 } 91 }) 92 } 93 94 func Test_Rand(t *testing.T) { 95 gtest.C(t, func(t *gtest.T) { 96 for i := 0; i < 100; i++ { 97 t.Assert(grand.N(1, 1), 1) 98 } 99 for i := 0; i < 100; i++ { 100 t.Assert(grand.N(0, 0), 0) 101 } 102 for i := 0; i < 100; i++ { 103 t.AssertIN(grand.N(1, 2), []int{1, 2}) 104 } 105 for i := 0; i < 100; i++ { 106 t.AssertIN(grand.N(-1, 2), []int{-1, 0, 1, 2}) 107 } 108 }) 109 } 110 111 func Test_S(t *testing.T) { 112 gtest.C(t, func(t *gtest.T) { 113 for i := 0; i < 100; i++ { 114 t.Assert(len(grand.S(5)), 5) 115 } 116 }) 117 gtest.C(t, func(t *gtest.T) { 118 for i := 0; i < 100; i++ { 119 t.Assert(len(grand.S(5, true)), 5) 120 } 121 }) 122 } 123 124 func Test_B(t *testing.T) { 125 gtest.C(t, func(t *gtest.T) { 126 for i := 0; i < 100; i++ { 127 b := grand.B(5) 128 t.Assert(len(b), 5) 129 t.AssertNE(b, make([]byte, 5)) 130 } 131 }) 132 } 133 134 func Test_Str(t *testing.T) { 135 gtest.C(t, func(t *gtest.T) { 136 for i := 0; i < 100; i++ { 137 t.Assert(len(grand.S(5)), 5) 138 } 139 }) 140 } 141 142 func Test_RandStr(t *testing.T) { 143 str := "我爱GoFrame" 144 gtest.C(t, func(t *gtest.T) { 145 for i := 0; i < 10; i++ { 146 s := grand.Str(str, 100000) 147 t.Assert(gstr.Contains(s, "我"), true) 148 t.Assert(gstr.Contains(s, "爱"), true) 149 t.Assert(gstr.Contains(s, "G"), true) 150 t.Assert(gstr.Contains(s, "o"), true) 151 t.Assert(gstr.Contains(s, "F"), true) 152 t.Assert(gstr.Contains(s, "r"), true) 153 t.Assert(gstr.Contains(s, "a"), true) 154 t.Assert(gstr.Contains(s, "m"), true) 155 t.Assert(gstr.Contains(s, "e"), true) 156 t.Assert(gstr.Contains(s, "w"), false) 157 } 158 }) 159 } 160 161 func Test_Digits(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 for i := 0; i < 100; i++ { 164 t.Assert(len(grand.Digits(5)), 5) 165 } 166 }) 167 } 168 169 func Test_RandDigits(t *testing.T) { 170 gtest.C(t, func(t *gtest.T) { 171 for i := 0; i < 100; i++ { 172 t.Assert(len(grand.Digits(5)), 5) 173 } 174 }) 175 } 176 177 func Test_Letters(t *testing.T) { 178 gtest.C(t, func(t *gtest.T) { 179 for i := 0; i < 100; i++ { 180 t.Assert(len(grand.Letters(5)), 5) 181 } 182 }) 183 } 184 185 func Test_RandLetters(t *testing.T) { 186 gtest.C(t, func(t *gtest.T) { 187 for i := 0; i < 100; i++ { 188 t.Assert(len(grand.Letters(5)), 5) 189 } 190 }) 191 } 192 193 func Test_Perm(t *testing.T) { 194 gtest.C(t, func(t *gtest.T) { 195 for i := 0; i < 100; i++ { 196 t.AssertIN(grand.Perm(5), []int{0, 1, 2, 3, 4}) 197 } 198 }) 199 }