go.x2ox.com/sorbifolia/random@v0.0.0-20240520090142-6d8be5c4ed59/math_test.go (about)

     1  package random
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  func TestMathRand(t *testing.T) {
     8  	t.Parallel()
     9  
    10  	fr := Math()
    11  	if len(fr.RandString(10)) != 10 {
    12  		t.Error("1")
    13  	}
    14  
    15  	fr = fr.SetRandBytes([]byte("123456"))
    16  	if len(fr.RandString(10)) != 10 {
    17  		t.Error("test fail")
    18  	}
    19  }
    20  
    21  func TestMathRandRepeatable(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	defer func() {
    25  		if e := recover(); e == nil {
    26  			t.Error("test fail")
    27  		}
    28  	}()
    29  
    30  	Math().SetRandBytes([]byte("11")).RandString(1)
    31  }
    32  
    33  func TestMathRandTooLong(t *testing.T) {
    34  	t.Parallel()
    35  
    36  	defer func() {
    37  		if e := recover(); e == nil {
    38  			t.Error("test fail")
    39  		}
    40  	}()
    41  
    42  	Math().SetRandBytes(make([]byte, 257)).RandString(1)
    43  }
    44  
    45  func BenchmarkMathRand(b *testing.B) {
    46  	r := Math()
    47  	for i := 0; i < b.N; i++ {
    48  		r.RandString(1123)
    49  	}
    50  }