github.com/kaydxh/golang@v0.0.131/go/math/rand/rand_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package rand_test
    23  
    24  import (
    25  	"fmt"
    26  	"math/rand"
    27  	"strings"
    28  	"testing"
    29  	"time"
    30  
    31  	rand_ "github.com/kaydxh/golang/go/math/rand"
    32  
    33  	"github.com/stretchr/testify/assert"
    34  )
    35  
    36  func TestRand(t *testing.T) {
    37  	s := fmt.Sprintf("%08v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(100000000))
    38  	fmt.Printf("s: %v\n", s)
    39  
    40  	ss := "abc_dvg_123"
    41  	fmt.Printf("ss %v\n", ss)
    42  	ts := strings.TrimPrefix(ss, "abc_dvg")
    43  	fmt.Printf("ts: %v\n", ts)
    44  
    45  	ns := "_abc"
    46  	nns := strings.Split(ns, "_")
    47  	fmt.Printf("nns: %v\n", nns)
    48  
    49  }
    50  
    51  func TestRangeInt(t *testing.T) {
    52  	testCases := []struct {
    53  		min int
    54  		max int
    55  	}{
    56  		{
    57  			min: 10,
    58  			max: 12,
    59  		},
    60  		{
    61  			min: 10000000,
    62  			max: 100000000,
    63  		},
    64  	}
    65  
    66  	for i, testCase := range testCases {
    67  		t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) {
    68  			r, err := rand_.RangeInt(testCase.min, testCase.max)
    69  			if err != nil {
    70  				t.Fatalf("failed to rand int, err: %v", err)
    71  			}
    72  			t.Logf("random: %v", r)
    73  
    74  			assert.GreaterOrEqual(t, r, testCase.min)
    75  			assert.LessOrEqual(t, r, testCase.max)
    76  
    77  		})
    78  	}
    79  }
    80  
    81  func TestRead(t *testing.T) {
    82  	testCases := []struct {
    83  		p []byte
    84  	}{
    85  		{
    86  			p: make([]byte, 10),
    87  		},
    88  		{
    89  			p: make([]byte, 20),
    90  		},
    91  	}
    92  
    93  	for i, testCase := range testCases {
    94  		t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) {
    95  			n, err := rand_.Read(testCase.p)
    96  			if err != nil {
    97  				t.Fatalf("failed to rand int, err: %v", err)
    98  			}
    99  			t.Logf("read n: %v, p: %v", n, testCase.p)
   100  
   101  			assert.Equal(t, len(testCase.p), n)
   102  
   103  		})
   104  	}
   105  }
   106  
   107  func TestRangeString(t *testing.T) {
   108  	testCases := []struct {
   109  		n int
   110  	}{
   111  		{
   112  			n: 0,
   113  		},
   114  		{
   115  			n: 5,
   116  		},
   117  		{
   118  			n: 8,
   119  		},
   120  	}
   121  
   122  	for i, testCase := range testCases {
   123  		t.Run(fmt.Sprintf("test-%v", i), func(t *testing.T) {
   124  			str := rand_.RangeString(testCase.n)
   125  			t.Logf("str: %v", str)
   126  			assert.Equal(t, len(str), testCase.n)
   127  
   128  		})
   129  	}
   130  
   131  }