github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/dbs/memristed/memex/rand_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package memex 15 16 import ( 17 "time" 18 19 "github.com/whtcorpsinc/check" 20 ) 21 22 func (s *testExpressionSuite) TestRandWithTime(c *check.C) { 23 rng1 := NewWithTime() 24 // NOTE: On windows platform, this Sleep is necessary. Because time.Now() is 25 // imprecise, calling UnixNano() twice returns the same value. We have to make 26 // sure the elapsed time is longer than 1ms to get different values. 27 time.Sleep(time.Millisecond) 28 rng2 := NewWithTime() 29 got1 := rng1.Gen() 30 got2 := rng2.Gen() 31 c.Assert(got1 < 1.0, check.IsTrue) 32 c.Assert(got1 >= 0.0, check.IsTrue) 33 c.Assert(got1 != rng1.Gen(), check.IsTrue) 34 c.Assert(got2 < 1.0, check.IsTrue) 35 c.Assert(got2 >= 0.0, check.IsTrue) 36 c.Assert(got2 != rng2.Gen(), check.IsTrue) 37 c.Assert(got1 != got2, check.IsTrue) 38 } 39 40 func (s *testExpressionSuite) TestRandWithSeed(c *check.C) { 41 tests := [4]struct { 42 seed int64 43 once float64 44 twice float64 45 }{{0, 0.15522042769493574, 0.620881741513388}, 46 {1, 0.40540353712197724, 0.8716141803857071}, 47 {-1, 0.9050373219931845, 0.37014932126752037}, 48 {9223372036854775807, 0.9050373219931845, 0.37014932126752037}} 49 for _, test := range tests { 50 rng := NewWithSeed(test.seed) 51 got1 := rng.Gen() 52 c.Assert(got1 == test.once, check.IsTrue) 53 got2 := rng.Gen() 54 c.Assert(got2 == test.twice, check.IsTrue) 55 } 56 }