Skip to content

Commit

Permalink
rework random slice and map making functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adamluzsi committed Feb 23, 2023
1 parent 51d4de1 commit e557510
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 22 deletions.
30 changes: 17 additions & 13 deletions random/Make.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@ func (r *Random) Make(T any) any {
return r.Factory.Make(r, T)
}

func MakeSlice[T any](rnd *Random, length int) []T {
var (
typ T
vs []T
)
func Slice[T any](length int, mk func() T) []T {
var vs []T
for i := 0; i < length; i++ {
vs = append(vs, rnd.Make(typ).(T))
vs = append(vs, mk())
}
return vs
}

func MakeMap[K comparable, V any](rnd *Random, length int) map[K]V {
func Map[K comparable, V any](length int, mk func() (K, V)) map[K]V {
var (
kT K
vT V
vs = make(map[K]V)
vs = make(map[K]V)
collisionRetries = 42
)
for i := 0; i < length; i++ {
k := rnd.Make(kT).(K)
v := rnd.Make(vT).(V)
k, v := mk()
if _, ok := vs[k]; ok {
i++
if 0 < collisionRetries {
collisionRetries--
i--
}
continue
}
vs[k] = v
}
return vs
}

func KV[K comparable, V any](mkK func() K, mkV func() V) func() (K, V) {
return func() (K, V) {
return mkK(), mkV()
}
}
34 changes: 28 additions & 6 deletions random/Make_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package random_test

import (
"github.com/adamluzsi/testcase/pp"
"math/rand"
"testing"
"time"
Expand Down Expand Up @@ -283,12 +284,13 @@ func TestMake(t *testing.T) {
})
}

func TestMakeSlice_smoke(t *testing.T) {
func TestSlice_smoke(t *testing.T) {
it := assert.MakeIt(t)
eventually := assert.EventuallyWithin(5 * time.Second)
rnd := random.New(random.CryptoSeed{})
length := rnd.IntB(1, 5)
slice1 := random.MakeSlice[int](rnd, length)
slice1 := random.Slice[int](length, rnd.Int)
pp.PP(slice1)
it.Must.Equal(length, len(slice1))
it.Must.NotEmpty(slice1)
it.Must.AnyOf(func(a *assert.AnyOf) {
Expand All @@ -299,18 +301,20 @@ func TestMakeSlice_smoke(t *testing.T) {
}
})
eventually.Assert(t, func(it assert.It) {
slice2 := random.MakeSlice[int](rnd, length)
slice2 := random.Slice[int](length, rnd.Int)
it.Must.Equal(len(slice1), len(slice2))
it.Must.NotEqual(slice1, slice2)
})
}

func TestMakeMap_smoke(t *testing.T) {
func TestMap_smoke(t *testing.T) {
it := assert.MakeIt(t)
eventually := assert.EventuallyWithin(5 * time.Second)
rnd := random.New(random.CryptoSeed{})
length := rnd.IntB(1, 5)
map1 := random.MakeMap[string, int](rnd, length)
map1 := random.Map[string, int](length, func() (string, int) {
return rnd.String(), rnd.Int()
})
it.Must.Equal(length, len(map1))
it.Must.NotEmpty(map1)
it.Must.AnyOf(func(a *assert.AnyOf) {
Expand All @@ -322,8 +326,26 @@ func TestMakeMap_smoke(t *testing.T) {
}
})
eventually.Assert(t, func(it assert.It) {
map2 := random.MakeMap[string, int](rnd, length)
map2 := random.Map[string, int](length, random.KV(rnd.String, rnd.Int))
it.Must.Equal(len(map1), len(map2))
it.Must.NotEqual(map1, map2)
})
}

func TestMap_whenNotEnoughUniqueKeyCanBeGenerated_thenItReturnsWithLess(t *testing.T) {
it := assert.MakeIt(t)
rnd := random.New(random.CryptoSeed{})
map1 := random.Map[string, int](10, func() (string, int) {
keys := []string{"foo", "bar", "baz"}
return rnd.SliceElement(keys).(string), rnd.Int()
})
it.Must.NotEmpty(map1)
it.Must.AnyOf(func(a *assert.AnyOf) {
for k, v := range map1 {
a.Test(func(it assert.It) {
it.Must.NotEmpty(k)
it.Must.NotEmpty(v)
})
}
})
}
4 changes: 2 additions & 2 deletions random/examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ func TestExampleRandomError(t *testing.T) {
func ExampleMakeSlice() {
rnd := random.New(random.CryptoSeed{})

pp.PP(random.MakeSlice[int](rnd, 3)) // []int slice with 3 values
pp.PP(random.Slice[int](3, rnd.Int)) // []int slice with 3 values
}

func ExampleMakeMap() {
rnd := random.New(random.CryptoSeed{})

pp.PP(random.MakeMap[string, int](rnd, 3)) // map[string]int slice with 3 key-value pairs
pp.PP(random.Map[string, int](3, random.KV(rnd.String, rnd.Int))) // map[string]int slice with 3 key-value pairs
}

func ExampleRandom_Repeat() {
Expand Down
1 change: 0 additions & 1 deletion random/name.go

This file was deleted.

0 comments on commit e557510

Please sign in to comment.