Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
94 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package test | ||
|
||
// FakeSource implements the rand.Source interface. This is useful for when we | ||
// want to supply dummy/predictable values to test specific scenarios that depend | ||
// on rand.Rand. | ||
type FakeSource struct { | ||
seed int64 | ||
} | ||
|
||
// NewFakeSource returns a new instance of FakeSource. FakeSource does not | ||
// generates random numbers, so the passed in seed number is the returned "random" | ||
// number. | ||
func NewFakeSource(seed int64) *FakeSource { | ||
return &FakeSource{seed: seed} | ||
} | ||
|
||
// Seed sets the internal int to the passed in seed value. This will be the returned | ||
// value when using FakeSource.Int63(). | ||
func (s *FakeSource) Seed(seed int64) { | ||
s.seed = seed | ||
} | ||
|
||
// Int63 returns the dummy seed value. | ||
func (s *FakeSource) Int63() int64 { | ||
return s.seed | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package test | ||
|
||
import "testing" | ||
|
||
func TestFakeGenerator(t *testing.T) { | ||
s := NewFakeSource(100) | ||
|
||
if n := s.Int63(); n != 100 { | ||
t.Errorf("expected 100 but got %d", n) | ||
} | ||
|
||
s.Seed(200) | ||
|
||
if n := s.Int63(); n != 200 { | ||
t.Errorf("expected 200 but got %d", n) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters