github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/utils/random/concurrent_test.go (about) 1 /* 2 * Copyright (C) 2021 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package random 19 20 import ( 21 "math/rand" 22 "testing" 23 ) 24 25 type mockRandomSource int64 26 27 func (s *mockRandomSource) Seed(seed int64) { 28 *s = mockRandomSource(seed) 29 } 30 31 func (s *mockRandomSource) Int63() int64 { 32 return int64(*s) 33 } 34 35 type mockRandomSource64 int64 36 37 func (s *mockRandomSource64) Seed(seed int64) { 38 *s = mockRandomSource64(seed) 39 } 40 41 func (s *mockRandomSource64) Int63() int64 { 42 return int64(*s) 43 } 44 45 func (s *mockRandomSource64) Uint64() uint64 { 46 return uint64(*s) 47 } 48 49 func TestVirtualConstructor(t *testing.T) { 50 src := new(mockRandomSource) 51 src64 := new(mockRandomSource64) 52 53 cSrc := NewConcurrentRandomSource(src) 54 cSrc64 := NewConcurrentRandomSource(src64) 55 56 // Generated by fair dice roll 57 var seed int64 = 4 58 cSrc.Seed(seed) 59 cSrc64.Seed(seed) 60 61 _, ok := cSrc.(rand.Source64) 62 if ok { 63 t.Errorf("%T exposes unexpected interface", cSrc) 64 } 65 66 cSrc64Extended, ok := cSrc64.(rand.Source64) 67 if !ok { 68 t.Errorf("%T exposes unexpected interface", cSrc64) 69 } 70 71 if cSrc.Int63() != seed { 72 t.Errorf("Wrapped %T returned unexpected value", cSrc) 73 } 74 75 if cSrc64.Int63() != seed { 76 t.Errorf("Wrapped %T returned unexpected value", cSrc) 77 } 78 79 if cSrc64Extended.Uint64() != uint64(seed) { 80 t.Errorf("Wrapped %T returned unexpected value", cSrc64Extended) 81 } 82 }