Skip to content

Commit

Permalink
fix linter issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ninedraft committed Jul 21, 2024
1 parent 5c7712f commit d5e04b9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
1 change: 1 addition & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ issues:
linters:
- gocognit
- gocyclo
- gosec

exclude-use-default: false # use default exclude patterns
max-issues-per-linter: 0 # maximum issues count per one linter
Expand Down
6 changes: 3 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

func ExampleSample() {
rnd := newRndSeed(100)
rnd := newFixedRnd()

items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
sample := sampler.Sample(rnd, items, 3)
Expand All @@ -18,7 +18,7 @@ func ExampleSample() {
}

func ExampleChoices() {
rnd := newRndSeed(100)
rnd := newFixedRnd()

items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
choices := sampler.Choices(rnd, items, 3)
Expand All @@ -28,7 +28,7 @@ func ExampleChoices() {
}

func ExampleChoice() {
rnd := newRndSeed(100)
rnd := newFixedRnd()
items := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
choice := sampler.Choice(rnd, items)

Expand Down
23 changes: 12 additions & 11 deletions sample_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"golang.org/x/exp/slices"
)

const seed = 100

func BenchmarkAppend(b *testing.B) {
rnd := newRnd()
items := make([]int, 1000)
Expand All @@ -28,12 +30,10 @@ func BenchmarkAppend(b *testing.B) {
}

func TestSampleAppend(t *testing.T) {
const seed = 100

t.Run("n is 0", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()
dst := []int{1, 2, 3}
items := []int{4, 5, 6}

Expand All @@ -45,7 +45,7 @@ func TestSampleAppend(t *testing.T) {
t.Run("dst is nil, n is full shuffle", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()
var dst []int
items := []int{1, 2, 3}

Expand All @@ -58,7 +58,7 @@ func TestSampleAppend(t *testing.T) {
t.Run("dst is nil, n is partial shuffle", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()
var dst []int
items := []int{1, 2, 3}

Expand All @@ -71,7 +71,7 @@ func TestSampleAppend(t *testing.T) {
t.Run("dst is not nil, n is full shuffle", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()
dst := []int{4, 5, 6}
items := []int{1, 2, 3}

Expand All @@ -85,7 +85,7 @@ func TestSampleAppend(t *testing.T) {
t.Run("n is greater than or equal to len(items)", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()

dst := []int{1, 2, 3}
items := []int{4, 5, 6}
Expand All @@ -100,7 +100,7 @@ func TestSampleAppend(t *testing.T) {
t.Run("3*n is greater than len(items)", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()

dst := []int{1, 2, 3}
items := []int{4, 5, 6, 7, 8}
Expand All @@ -115,7 +115,7 @@ func TestSampleAppend(t *testing.T) {
t.Run("n is less than len(items)", func(t *testing.T) {
t.Parallel()

rnd := newRndSeed(seed)
rnd := newFixedRnd()
dst := []int{1, 2, 3}
items := []int{4, 5, 6, 7, 8, 9}

Expand Down Expand Up @@ -188,13 +188,14 @@ func assertPrefix[E comparable](t *testing.T, items, want []E) {

func newRnd() *norand.Rand {
var seed [32]byte
rand.Read(seed[:])
_, _ = rand.Read(seed[:])

src := norand.NewChaCha8(seed)
return norand.New(src)
}

func newRndSeed(seed uint64) *norand.Rand {
// returns a random generator with fixed seed
func newFixedRnd() *norand.Rand {
var s [32]byte
binary.PutUvarint(s[:], seed)

Expand Down

0 comments on commit d5e04b9

Please sign in to comment.