github.com/zhongdalu/gf@v1.0.0/g/util/grand/grand_z_unit_test.go (about)

     1  // Copyright 2018 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*"
     8  
     9  package grand_test
    10  
    11  import (
    12  	"github.com/zhongdalu/gf/g/test/gtest"
    13  	"github.com/zhongdalu/gf/g/util/grand"
    14  	"testing"
    15  )
    16  
    17  func Test_Intn(t *testing.T) {
    18  	gtest.Case(t, func() {
    19  		for i := 0; i < 1000000; i++ {
    20  			n := grand.Intn(100)
    21  			gtest.AssertLT(n, 100)
    22  			gtest.AssertGTE(n, 0)
    23  		}
    24  		for i := 0; i < 1000000; i++ {
    25  			n := grand.Intn(-100)
    26  			gtest.AssertLTE(n, 0)
    27  			gtest.AssertGT(n, -100)
    28  		}
    29  	})
    30  }
    31  
    32  func Test_Meet(t *testing.T) {
    33  	gtest.Case(t, func() {
    34  		for i := 0; i < 100; i++ {
    35  			gtest.Assert(grand.Meet(100, 100), true)
    36  		}
    37  		for i := 0; i < 100; i++ {
    38  			gtest.Assert(grand.Meet(0, 100), false)
    39  		}
    40  		for i := 0; i < 100; i++ {
    41  			gtest.AssertIN(grand.Meet(50, 100), []bool{true, false})
    42  		}
    43  	})
    44  }
    45  
    46  func Test_MeetProb(t *testing.T) {
    47  	gtest.Case(t, func() {
    48  		for i := 0; i < 100; i++ {
    49  			gtest.Assert(grand.MeetProb(1), true)
    50  		}
    51  		for i := 0; i < 100; i++ {
    52  			gtest.Assert(grand.MeetProb(0), false)
    53  		}
    54  		for i := 0; i < 100; i++ {
    55  			gtest.AssertIN(grand.MeetProb(0.5), []bool{true, false})
    56  		}
    57  	})
    58  }
    59  
    60  func Test_N(t *testing.T) {
    61  	gtest.Case(t, func() {
    62  		for i := 0; i < 100; i++ {
    63  			gtest.Assert(grand.N(1, 1), 1)
    64  		}
    65  		for i := 0; i < 100; i++ {
    66  			gtest.Assert(grand.N(0, 0), 0)
    67  		}
    68  		for i := 0; i < 100; i++ {
    69  			gtest.AssertIN(grand.N(1, 2), []int{1, 2})
    70  		}
    71  	})
    72  }
    73  
    74  func Test_Rand(t *testing.T) {
    75  	gtest.Case(t, func() {
    76  		for i := 0; i < 100; i++ {
    77  			gtest.Assert(grand.Rand(1, 1), 1)
    78  		}
    79  		for i := 0; i < 100; i++ {
    80  			gtest.Assert(grand.Rand(0, 0), 0)
    81  		}
    82  		for i := 0; i < 100; i++ {
    83  			gtest.AssertIN(grand.Rand(1, 2), []int{1, 2})
    84  		}
    85  		for i := 0; i < 100; i++ {
    86  			gtest.AssertIN(grand.Rand(-1, 2), []int{-1, 0, 1, 2})
    87  		}
    88  	})
    89  }
    90  
    91  func Test_Str(t *testing.T) {
    92  	gtest.Case(t, func() {
    93  		for i := 0; i < 100; i++ {
    94  			gtest.Assert(len(grand.Str(5)), 5)
    95  		}
    96  	})
    97  }
    98  
    99  func Test_RandStr(t *testing.T) {
   100  	gtest.Case(t, func() {
   101  		for i := 0; i < 100; i++ {
   102  			gtest.Assert(len(grand.RandStr(5)), 5)
   103  		}
   104  	})
   105  }
   106  
   107  func Test_Digits(t *testing.T) {
   108  	gtest.Case(t, func() {
   109  		for i := 0; i < 100; i++ {
   110  			gtest.Assert(len(grand.Digits(5)), 5)
   111  		}
   112  	})
   113  }
   114  
   115  func Test_RandDigits(t *testing.T) {
   116  	gtest.Case(t, func() {
   117  		for i := 0; i < 100; i++ {
   118  			gtest.Assert(len(grand.RandDigits(5)), 5)
   119  		}
   120  	})
   121  }
   122  
   123  func Test_Letters(t *testing.T) {
   124  	gtest.Case(t, func() {
   125  		for i := 0; i < 100; i++ {
   126  			gtest.Assert(len(grand.Letters(5)), 5)
   127  		}
   128  	})
   129  }
   130  
   131  func Test_RandLetters(t *testing.T) {
   132  	gtest.Case(t, func() {
   133  		for i := 0; i < 100; i++ {
   134  			gtest.Assert(len(grand.RandLetters(5)), 5)
   135  		}
   136  	})
   137  }
   138  
   139  func Test_Perm(t *testing.T) {
   140  	gtest.Case(t, func() {
   141  		for i := 0; i < 100; i++ {
   142  			gtest.AssertIN(grand.Perm(5), []int{0, 1, 2, 3, 4})
   143  		}
   144  	})
   145  }