github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/random_test.go (about)

     1  package util
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"math/rand"
     7  	"sync"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestRandInt(t *testing.T) {
    14  	t.Parallel()
    15  	assert.Equal(t, true, RandInt(1, 2) == 1)
    16  	assert.Equal(t, true, RandInt(-1, 0) == -1)
    17  	assert.Equal(t, true, RandInt(0, 5) >= 0)
    18  	assert.Equal(t, true, RandInt(0, 5) < 5)
    19  	assert.Equal(t, 2, RandInt(2, 2))
    20  	assert.Equal(t, 2, RandInt(3, 2))
    21  }
    22  
    23  func TestRandUint32(t *testing.T) {
    24  	t.Parallel()
    25  	assert.Equal(t, true, RandUint32(1, 2) == 1)
    26  	assert.Equal(t, true, RandUint32(0, 5) < 5)
    27  	assert.Equal(t, uint32(2), RandUint32(2, 2))
    28  	assert.Equal(t, uint32(2), RandUint32(3, 2))
    29  }
    30  
    31  func TestFastIntn(t *testing.T) {
    32  	t.Parallel()
    33  	for i := 1; i < 10000; i++ {
    34  		assert.Equal(t, true, FastRandn(uint32(i)) < uint32(i))
    35  		assert.Equal(t, true, FastIntn(i) < i)
    36  	}
    37  	assert.Equal(t, 0, FastIntn(-2))
    38  	assert.Equal(t, 0, FastIntn(0))
    39  	assert.Equal(t, true, FastIntn(math.MaxUint32) < math.MaxUint32)
    40  	assert.Equal(t, true, FastIntn(math.MaxInt64) < math.MaxInt64)
    41  }
    42  
    43  func BenchmarkRandInt(b *testing.B) {
    44  	b.Run("RandInt", func(b *testing.B) {
    45  		for i := 1; i < b.N; i++ {
    46  			_ = RandInt(0, i)
    47  		}
    48  	})
    49  	b.Run("RandUint32", func(b *testing.B) {
    50  		for i := 1; i < b.N; i++ {
    51  			_ = RandUint32(0, uint32(i))
    52  		}
    53  	})
    54  	b.Run("FastIntn", func(b *testing.B) {
    55  		for i := 1; i < b.N; i++ {
    56  			_ = FastIntn(i)
    57  		}
    58  	})
    59  	b.Run("Rand.Intn", func(b *testing.B) {
    60  		for i := 1; i < b.N; i++ {
    61  			_ = Rand.Intn(i)
    62  		}
    63  	})
    64  	b.Run("std.rand.Intn", func(b *testing.B) {
    65  		for i := 1; i < b.N; i++ {
    66  			_ = rand.Intn(i)
    67  		}
    68  	})
    69  }
    70  
    71  func BenchmarkRandIntParallel(b *testing.B) {
    72  	b.Run("RandInt", func(b *testing.B) {
    73  		b.RunParallel(func(pb *testing.PB) {
    74  			for pb.Next() {
    75  				_ = RandInt(0, math.MaxInt32)
    76  			}
    77  		})
    78  	})
    79  	b.Run("RandUint32", func(b *testing.B) {
    80  		b.RunParallel(func(pb *testing.PB) {
    81  			for pb.Next() {
    82  				_ = RandUint32(0, math.MaxInt32)
    83  			}
    84  		})
    85  	})
    86  	b.Run("FastIntn", func(b *testing.B) {
    87  		b.RunParallel(func(pb *testing.PB) {
    88  			for pb.Next() {
    89  				_ = FastIntn(math.MaxInt32)
    90  			}
    91  		})
    92  	})
    93  	b.Run("Rand.Intn", func(b *testing.B) {
    94  		b.RunParallel(func(pb *testing.PB) {
    95  			for pb.Next() {
    96  				_ = Rand.Intn(math.MaxInt32)
    97  			}
    98  		})
    99  	})
   100  	var mu sync.Mutex
   101  	b.Run("std.rand.Intn", func(b *testing.B) {
   102  		b.RunParallel(func(pb *testing.PB) {
   103  			for pb.Next() {
   104  				mu.Lock()
   105  				_ = rand.Intn(math.MaxInt32)
   106  				mu.Unlock()
   107  			}
   108  		})
   109  	})
   110  }
   111  
   112  func TestRandString(t *testing.T) {
   113  	t.Parallel()
   114  	a, b := RandString(777), RandString(777)
   115  	assert.Equal(t, 777, len(a))
   116  	assert.Equal(t, false, a == b)
   117  	assert.Equal(t, "", RandString(-1))
   118  }
   119  
   120  func TestRandBytes(t *testing.T) {
   121  	t.Parallel()
   122  	a, err := RandBytes(777)
   123  	assert.Nil(t, err)
   124  	b, err := RandBytes(777)
   125  	assert.Nil(t, err)
   126  	assert.Equal(t, 777, len(a))
   127  	assert.Equal(t, 777, len(b))
   128  	assert.Equal(t, false, bytes.Equal(a, b))
   129  }
   130  
   131  func TestFastRandBytes(t *testing.T) {
   132  	t.Parallel()
   133  	a, b := FastRandBytes(777), FastRandBytes(777)
   134  	assert.Equal(t, 777, len(a))
   135  	assert.Equal(t, 777, len(b))
   136  	assert.Equal(t, false, bytes.Equal(a, b))
   137  }
   138  
   139  func TestRandHex(t *testing.T) {
   140  	t.Parallel()
   141  	assert.Equal(t, 32, len(RandHex(16)))
   142  	assert.Equal(t, 14, len(RandHex(7)))
   143  }
   144  
   145  func BenchmarkRandBytes(b *testing.B) {
   146  	b.Run("RandString", func(b *testing.B) {
   147  		for i := 0; i < b.N; i++ {
   148  			_ = RandString(20)
   149  		}
   150  	})
   151  	b.Run("RandBytes", func(b *testing.B) {
   152  		for i := 0; i < b.N; i++ {
   153  			RandBytes(20)
   154  		}
   155  	})
   156  	b.Run("FastRandBytes", func(b *testing.B) {
   157  		for i := 0; i < b.N; i++ {
   158  			_ = FastRandBytes(20)
   159  		}
   160  	})
   161  }
   162  
   163  func BenchmarkRandBytesParallel(b *testing.B) {
   164  	b.Run("RandString", func(b *testing.B) {
   165  		b.RunParallel(func(pb *testing.PB) {
   166  			for pb.Next() {
   167  				RandString(20)
   168  			}
   169  		})
   170  	})
   171  	b.Run("RandBytes", func(b *testing.B) {
   172  		b.RunParallel(func(pb *testing.PB) {
   173  			for pb.Next() {
   174  				RandBytes(20)
   175  			}
   176  		})
   177  	})
   178  	b.Run("FastRandBytes", func(b *testing.B) {
   179  		b.RunParallel(func(pb *testing.PB) {
   180  			for pb.Next() {
   181  				FastRandBytes(20)
   182  			}
   183  		})
   184  	})
   185  }