trpc.group/trpc-go/trpc-go@v1.0.3/internal/rand/rand_test.go (about) 1 // 2 // 3 // Tencent is pleased to support the open source community by making tRPC available. 4 // 5 // Copyright (C) 2023 THL A29 Limited, a Tencent company. 6 // All rights reserved. 7 // 8 // If you have downloaded a copy of the tRPC source code from Tencent, 9 // please note that tRPC source code is licensed under the Apache 2.0 License, 10 // A copy of the Apache 2.0 License is included in this file. 11 // 12 // 13 14 package rand 15 16 import ( 17 "testing" 18 "time" 19 20 "github.com/stretchr/testify/assert" 21 ) 22 23 var testSafeRand = NewSafeRand(time.Now().UnixNano()) 24 25 func TestIntnFix(t *testing.T) { 26 fixRand := NewSafeRand(1) 27 for i := 0; i < 100; i++ { 28 ranNum := fixRand.Intn(10) 29 assert.Less(t, ranNum, 10) 30 t.Logf("c=%v", ranNum) 31 } 32 } 33 func TestSafeRand_Int63n(t *testing.T) { 34 type args struct { 35 n int64 36 } 37 tests := []struct { 38 name string 39 args args 40 want int64 41 }{ 42 { 43 name: "1", 44 args: args{ 45 n: 5, 46 }, 47 want: 6, 48 }, 49 } 50 for _, tt := range tests { 51 t.Run(tt.name, func(t *testing.T) { 52 if got := testSafeRand.Int63n(tt.args.n); got == tt.want { 53 t.Errorf("Int63n() = %v, want %v", got, tt.want) 54 } 55 }) 56 } 57 } 58 59 func TestSafeRand_Intn(t *testing.T) { 60 type args struct { 61 n int 62 } 63 tests := []struct { 64 name string 65 args args 66 want int 67 }{ 68 { 69 name: "1", 70 args: args{ 71 n: 5, 72 }, 73 want: 6, 74 }, 75 } 76 for _, tt := range tests { 77 t.Run(tt.name, func(t *testing.T) { 78 if got := testSafeRand.Intn(tt.args.n); got == tt.want { 79 t.Errorf("Int63n() = %v, want %v", got, tt.want) 80 } 81 }) 82 } 83 } 84 85 func TestSafeRand_Float64(t *testing.T) { 86 tests := []struct { 87 name string 88 want float64 89 }{ 90 { 91 name: "ok", 92 want: 6, 93 }, 94 } 95 for _, tt := range tests { 96 t.Run(tt.name, func(t *testing.T) { 97 if got := testSafeRand.Float64(); got == tt.want { 98 t.Errorf("Float64() = %v, want %v", got, tt.want) 99 } 100 }) 101 } 102 }