github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package grand_test
    10  
    11  import (
    12  	"strings"
    13  	"testing"
    14  	"time"
    15  
    16  	"github.com/wangyougui/gf/v2/test/gtest"
    17  	"github.com/wangyougui/gf/v2/text/gstr"
    18  	"github.com/wangyougui/gf/v2/util/grand"
    19  )
    20  
    21  func Test_Intn(t *testing.T) {
    22  	gtest.C(t, func(t *gtest.T) {
    23  		for i := 0; i < 1000000; i++ {
    24  			n := grand.Intn(100)
    25  			t.AssertLT(n, 100)
    26  			t.AssertGE(n, 0)
    27  		}
    28  		for i := 0; i < 1000000; i++ {
    29  			n := grand.Intn(-100)
    30  			t.AssertLE(n, 0)
    31  			t.Assert(n, -100)
    32  		}
    33  	})
    34  }
    35  
    36  func Test_Meet(t *testing.T) {
    37  	gtest.C(t, func(t *gtest.T) {
    38  		for i := 0; i < 100; i++ {
    39  			t.Assert(grand.Meet(100, 100), true)
    40  		}
    41  		for i := 0; i < 100; i++ {
    42  			t.Assert(grand.Meet(0, 100), false)
    43  		}
    44  		for i := 0; i < 100; i++ {
    45  			t.AssertIN(grand.Meet(50, 100), []bool{true, false})
    46  		}
    47  	})
    48  }
    49  
    50  func Test_MeetProb(t *testing.T) {
    51  	gtest.C(t, func(t *gtest.T) {
    52  		for i := 0; i < 100; i++ {
    53  			t.Assert(grand.MeetProb(1), true)
    54  		}
    55  		for i := 0; i < 100; i++ {
    56  			t.Assert(grand.MeetProb(0), false)
    57  		}
    58  		for i := 0; i < 100; i++ {
    59  			t.AssertIN(grand.MeetProb(0.5), []bool{true, false})
    60  		}
    61  	})
    62  }
    63  
    64  func Test_N(t *testing.T) {
    65  	gtest.C(t, func(t *gtest.T) {
    66  		for i := 0; i < 100; i++ {
    67  			t.Assert(grand.N(1, 1), 1)
    68  		}
    69  		for i := 0; i < 100; i++ {
    70  			t.Assert(grand.N(0, 0), 0)
    71  		}
    72  		for i := 0; i < 100; i++ {
    73  			t.AssertIN(grand.N(1, 2), []int{1, 2})
    74  		}
    75  	})
    76  }
    77  
    78  func Test_D(t *testing.T) {
    79  	gtest.C(t, func(t *gtest.T) {
    80  		for i := 0; i < 100; i++ {
    81  			t.Assert(grand.D(time.Second, time.Second), time.Second)
    82  		}
    83  		for i := 0; i < 100; i++ {
    84  			t.Assert(grand.D(0, 0), time.Duration(0))
    85  		}
    86  		for i := 0; i < 100; i++ {
    87  			t.AssertIN(
    88  				grand.D(1*time.Second, 3*time.Second),
    89  				[]time.Duration{1 * time.Second, 2 * time.Second, 3 * time.Second},
    90  			)
    91  		}
    92  	})
    93  }
    94  
    95  func Test_Rand(t *testing.T) {
    96  	gtest.C(t, func(t *gtest.T) {
    97  		for i := 0; i < 100; i++ {
    98  			t.Assert(grand.N(1, 1), 1)
    99  		}
   100  		for i := 0; i < 100; i++ {
   101  			t.Assert(grand.N(0, 0), 0)
   102  		}
   103  		for i := 0; i < 100; i++ {
   104  			t.AssertIN(grand.N(1, 2), []int{1, 2})
   105  		}
   106  		for i := 0; i < 100; i++ {
   107  			t.AssertIN(grand.N(-1, 2), []int{-1, 0, 1, 2})
   108  		}
   109  	})
   110  }
   111  
   112  func Test_S(t *testing.T) {
   113  	gtest.C(t, func(t *gtest.T) {
   114  		for i := 0; i < 100; i++ {
   115  			t.Assert(len(grand.S(5)), 5)
   116  		}
   117  	})
   118  	gtest.C(t, func(t *gtest.T) {
   119  		for i := 0; i < 100; i++ {
   120  			t.Assert(len(grand.S(5, true)), 5)
   121  		}
   122  	})
   123  	gtest.C(t, func(t *gtest.T) {
   124  		t.Assert(len(grand.S(0)), 0)
   125  	})
   126  }
   127  
   128  func Test_B(t *testing.T) {
   129  	gtest.C(t, func(t *gtest.T) {
   130  		for i := 0; i < 100; i++ {
   131  			b := grand.B(5)
   132  			t.Assert(len(b), 5)
   133  			t.AssertNE(b, make([]byte, 5))
   134  		}
   135  	})
   136  	gtest.C(t, func(t *gtest.T) {
   137  		b := grand.B(0)
   138  		t.AssertNil(b)
   139  	})
   140  }
   141  
   142  func Test_Str(t *testing.T) {
   143  	gtest.C(t, func(t *gtest.T) {
   144  		for i := 0; i < 100; i++ {
   145  			t.Assert(len(grand.S(5)), 5)
   146  		}
   147  	})
   148  }
   149  
   150  func Test_RandStr(t *testing.T) {
   151  	str := "我爱GoFrame"
   152  	gtest.C(t, func(t *gtest.T) {
   153  		for i := 0; i < 10; i++ {
   154  			s := grand.Str(str, 100000)
   155  			t.Assert(gstr.Contains(s, "我"), true)
   156  			t.Assert(gstr.Contains(s, "爱"), true)
   157  			t.Assert(gstr.Contains(s, "G"), true)
   158  			t.Assert(gstr.Contains(s, "o"), true)
   159  			t.Assert(gstr.Contains(s, "F"), true)
   160  			t.Assert(gstr.Contains(s, "r"), true)
   161  			t.Assert(gstr.Contains(s, "a"), true)
   162  			t.Assert(gstr.Contains(s, "m"), true)
   163  			t.Assert(gstr.Contains(s, "e"), true)
   164  			t.Assert(gstr.Contains(s, "w"), false)
   165  		}
   166  	})
   167  	gtest.C(t, func(t *gtest.T) {
   168  		t.Assert(grand.Str(str, 0), "")
   169  	})
   170  	gtest.C(t, func(t *gtest.T) {
   171  		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"}
   172  		str := ""
   173  		for _, s := range list {
   174  			tmp := ""
   175  			for i := 0; i < 15; i++ {
   176  				tmp += tmp + s
   177  			}
   178  			str += tmp
   179  		}
   180  		t.Assert(len(grand.Str(str, 300)), 300)
   181  	})
   182  }
   183  
   184  func Test_Digits(t *testing.T) {
   185  	gtest.C(t, func(t *gtest.T) {
   186  		for i := 0; i < 100; i++ {
   187  			t.Assert(len(grand.Digits(5)), 5)
   188  		}
   189  	})
   190  }
   191  
   192  func Test_RandDigits(t *testing.T) {
   193  	gtest.C(t, func(t *gtest.T) {
   194  		for i := 0; i < 100; i++ {
   195  			t.Assert(len(grand.Digits(5)), 5)
   196  		}
   197  	})
   198  	gtest.C(t, func(t *gtest.T) {
   199  		t.Assert(len(grand.Digits(0)), 0)
   200  	})
   201  }
   202  
   203  func Test_Letters(t *testing.T) {
   204  	gtest.C(t, func(t *gtest.T) {
   205  		for i := 0; i < 100; i++ {
   206  			t.Assert(len(grand.Letters(5)), 5)
   207  		}
   208  	})
   209  }
   210  
   211  func Test_RandLetters(t *testing.T) {
   212  	gtest.C(t, func(t *gtest.T) {
   213  		for i := 0; i < 100; i++ {
   214  			t.Assert(len(grand.Letters(5)), 5)
   215  		}
   216  	})
   217  	gtest.C(t, func(t *gtest.T) {
   218  		t.Assert(len(grand.Letters(0)), 0)
   219  	})
   220  }
   221  
   222  func Test_Perm(t *testing.T) {
   223  	gtest.C(t, func(t *gtest.T) {
   224  		for i := 0; i < 100; i++ {
   225  			t.AssertIN(grand.Perm(5), []int{0, 1, 2, 3, 4})
   226  		}
   227  	})
   228  }
   229  
   230  func Test_Symbols(t *testing.T) {
   231  	symbols := "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
   232  	gtest.C(t, func(t *gtest.T) {
   233  		for i := 0; i < 100; i++ {
   234  			syms := []byte(grand.Symbols(5))
   235  			for _, sym := range syms {
   236  				t.AssertNE(strings.Index(symbols, string(sym)), -1)
   237  			}
   238  		}
   239  	})
   240  	gtest.C(t, func(t *gtest.T) {
   241  		t.Assert(grand.Symbols(0), "")
   242  	})
   243  }