Skip to content

Commit

Permalink
Yay generics!
Browse files Browse the repository at this point in the history
  • Loading branch information
flyingmutant committed Dec 30, 2022
1 parent ba76683 commit d7c95c2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
name: CI
strategy:
matrix:
go: ['1.15', '1.16']
go: ['1.18', '1.19']
os: ['ubuntu-latest', 'windows-latest', 'macOS-latest']
runs-on: ${{ matrix.os }}
steps:
Expand Down
52 changes: 26 additions & 26 deletions digest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ type uniformGen struct {
}

func newUniformGen(t *rapid.T, min float64, max float64) generator {
minR := rapid.Float64Range(min, max).Draw(t, "min param").(float64)
maxR := rapid.Float64Range(minR, max).Draw(t, "max param").(float64)
minR := rapid.Float64Range(min, max).Draw(t, "min param")
maxR := rapid.Float64Range(minR, max).Draw(t, "max param")

return &uniformGen{
Rand: rand.New(rand.NewSource(0)),
Expand All @@ -64,8 +64,8 @@ type normalGen struct {
}

func newNormalGen(t *rapid.T, min float64, max float64) generator {
mean := rapid.Float64Range(min, max).Draw(t, "mean param").(float64)
stddev := rapid.Float64Range(0, (max-min)/2).Draw(t, "stddev param").(float64)
mean := rapid.Float64Range(min, max).Draw(t, "mean param")
stddev := rapid.Float64Range(0, (max-min)/2).Draw(t, "stddev param")

return &normalGen{
Rand: rand.New(rand.NewSource(0)),
Expand All @@ -81,8 +81,8 @@ type logNormalGen struct {
}

func newLogNormalGen(t *rapid.T, min float64, max float64) generator {
mean := rapid.Float64Range(min, max).Draw(t, "mean param").(float64)
stddev := rapid.Float64Range(0, (max-min)/2).Draw(t, "stddev param").(float64)
mean := rapid.Float64Range(min, max).Draw(t, "mean param")
stddev := rapid.Float64Range(0, (max-min)/2).Draw(t, "stddev param")

return &logNormalGen{
Rand: rand.New(rand.NewSource(0)),
Expand All @@ -98,8 +98,8 @@ type paretoGen struct {
}

func newParetoGen(t *rapid.T, min float64, max float64) generator {
m := rapid.Float64Range(min, max).Draw(t, "min param").(float64)
index := rapid.Float64Range(0.1, 10).Draw(t, "index param").(float64)
m := rapid.Float64Range(min, max).Draw(t, "min param")
index := rapid.Float64Range(0.1, 10).Draw(t, "index param")

return &paretoGen{
Rand: rand.New(rand.NewSource(0)),
Expand Down Expand Up @@ -191,7 +191,7 @@ func (d *perfectDigest) Quantile(q float64) float64 {
func TestDigest(t *testing.T) {
t.Parallel()

rapid.Check(t, rapid.Run(&digestMachine{}))
rapid.Check(t, rapid.Run[*digestMachine]())
}

type digestPair struct {
Expand All @@ -216,9 +216,9 @@ func (m *digestMachine) Init(t *rapid.T) {
minErr = 1e-2
}

m.min = rapid.Float64Range(minVal, 1-1e-10).Draw(t, "digest min").(float64)
m.max = rapid.Float64Range(1+1e-10, maxVal).Draw(t, "digest max").(float64)
m.err = rapid.Float64Range(minErr, 1-1e-5).Draw(t, "relative error").(float64)
m.min = rapid.Float64Range(minVal, 1-1e-10).Draw(t, "digest min")
m.max = rapid.Float64Range(1+1e-10, maxVal).Draw(t, "digest max")
m.err = rapid.Float64Range(minErr, 1-1e-5).Draw(t, "relative error")
}

func (m *digestMachine) Check(*rapid.T) {}
Expand All @@ -229,9 +229,9 @@ func (m *digestMachine) AddDigest(t *rapid.T) {
maxSize = 1000
}

gen := rapid.SampledFrom(generatorNames).Draw(t, "generator").(string)
seed := rapid.Int64().Draw(t, "seed").(int64)
count := rapid.IntRange(0, maxSize).Draw(t, "count").(int)
gen := rapid.SampledFrom(generatorNames).Draw(t, "generator")
seed := rapid.Int64().Draw(t, "seed")
count := rapid.IntRange(0, maxSize).Draw(t, "count")

d := &approxDigest{bdigest.NewDigest(m.err)}
r := &perfectDigest{values: make([]float64, 0, count)}
Expand All @@ -254,7 +254,7 @@ func (m *digestMachine) AddDigest(t *rapid.T) {
r.Add(f)
}

q := rapid.Float64Range(0, 1).Draw(t, "quantile").(float64)
q := rapid.Float64Range(0, 1).Draw(t, "quantile")
checkDigest(t, d, r, q, m.err)

m.digests = append(m.digests, digestPair{d, r})
Expand All @@ -265,13 +265,13 @@ func (m *digestMachine) MergeDigests(t *rapid.T) {
t.Skip("nothing to merge")
}

to := rapid.SampledFrom(m.digests).Draw(t, "digest to").(digestPair)
from := rapid.SampledFrom(m.digests).Draw(t, "digest from").(digestPair)
to := rapid.SampledFrom(m.digests).Draw(t, "digest to")
from := rapid.SampledFrom(m.digests).Draw(t, "digest from")

to.d.Merge(from.d)
to.r.Merge(from.r)

q := rapid.Float64Range(0, 1).Draw(t, "quantile").(float64)
q := rapid.Float64Range(0, 1).Draw(t, "quantile")
checkDigest(t, to.d, to.r, q, m.err)
}

Expand Down Expand Up @@ -300,10 +300,10 @@ func TestDigestMarshalBinaryRoundtrip(t *testing.T) {

func testDigestMarshalBinaryRoundtrip(t *rapid.T) {
var (
relErr = rapid.Float64Range(1e-5, 1-1e-5).Draw(t, "relative error").(float64)
seed = rapid.Int64().Draw(t, "seed").(int64)
count = rapid.IntRange(0, 100000).Draw(t, "count").(int)
ctor = rapid.Bool().Draw(t, "use constructor").(bool)
relErr = rapid.Float64Range(1e-5, 1-1e-5).Draw(t, "relative error")
seed = rapid.Int64().Draw(t, "seed")
count = rapid.IntRange(0, 100000).Draw(t, "count")
ctor = rapid.Bool().Draw(t, "use constructor")
)

d1 := logNormalDigest(relErr, seed, count, int32(count)/10)
Expand Down Expand Up @@ -333,9 +333,9 @@ func TestDigest_Reset(t *testing.T) {

rapid.Check(t, func(t *rapid.T) {
var (
err = rapid.Float64Range(1e-5, 1-1e-5).Draw(t, "relative error").(float64)
seed = rapid.Int64().Draw(t, "seed").(int64)
count = rapid.IntRange(0, 100000).Draw(t, "count").(int)
err = rapid.Float64Range(1e-5, 1-1e-5).Draw(t, "relative error")
seed = rapid.Int64().Draw(t, "seed")
count = rapid.IntRange(0, 100000).Draw(t, "count")
)

d := logNormalDigest(err, seed, count, int32(count)/10)
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module pgregory.net/bdigest

go 1.13
go 1.18

require pgregory.net/rapid v0.4.5
require pgregory.net/rapid v0.5.3
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
pgregory.net/rapid v0.4.0 h1:/boyXNQlDs1pmk7g1b9u2KrYqXnqjj0ARUDsZj5kapg=
pgregory.net/rapid v0.4.0/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
pgregory.net/rapid v0.4.5 h1:Dej3+7kpi3p5/9dMouKspRs+K8EALKePW4/UYXKq8/4=
pgregory.net/rapid v0.4.5/go.mod h1:UYpPVyjFHzYBGHIxLFoupi8vwk6rXNzRY9OMvVxFIOU=
pgregory.net/rapid v0.5.3 h1:163N50IHFqr1phZens4FQOdPgfJscR7a562mjQqeo4M=
pgregory.net/rapid v0.5.3/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04=

0 comments on commit d7c95c2

Please sign in to comment.