Skip to content

Commit

Permalink
test copyfrom; go work sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jmacd committed Nov 23, 2023
1 parent 4cad612 commit b627cc4
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ module github.com/lightstep/varopt

go 1.21

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/rogpeppe/go-internal v1.10.0 // indirect
github.com/stretchr/testify v1.8.4
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
Expand All @@ -13,18 +12,12 @@ github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsK
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M=
github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18 changes: 18 additions & 0 deletions varopt.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func New[T any](capacity int, rnd *rand.Rand) *Varopt[T] {
return v
}

// Init initializes a Varopt[T] in-place, avoiding an allocation
// compared with New().
func (v *Varopt[T]) Init(capacity int, rnd *rand.Rand) {
*v = Varopt[T]{
capacity: capacity,
Expand All @@ -68,6 +70,22 @@ func (s *Varopt[T]) Reset() {
s.totalWeight = 0
}

// CopyFrom copies the fields of `from` into this Varopt[T].
func (s *Varopt[T]) CopyFrom(from *Varopt[T]) {
// Copy non-slice fields
cpy := *from
// Keep existing slices, reset
cpy.L = s.L[:0]
cpy.T = s.T[:0]
cpy.X = s.X[:0]
// Append to existing slices
cpy.L = append(cpy.L, from.L...)
cpy.T = append(cpy.T, from.T...)
cpy.X = append(cpy.X, from.X...)
// Assign back to `s`
*s = cpy
}

// Add considers a new observation for the sample with given weight.
// If there is an item ejected from the sample as a result, the item
// is returned to allow re-use of memory.
Expand Down
23 changes: 23 additions & 0 deletions varopt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,35 @@ func TestReset(t *testing.T) {
require.Equal(t, sum, v.TotalWeight())
require.Less(t, 0., v.Tau())

var v2 varopt.Varopt[testInt]
v2.Init(capacity, rnd)
v2.CopyFrom(v)

var expect []testInt
for i := 0; i < v.Size(); i++ {
got, _ := v.Get(i)
expect = append(expect, got)
}

v.Reset()

require.Equal(t, 0, v.Size())
require.Equal(t, 0, v.TotalCount())
require.Equal(t, 0., v.TotalWeight())
require.Equal(t, 0., v.Tau())

require.Equal(t, capacity, v2.Size())
require.Equal(t, insert, v2.TotalCount())
require.Equal(t, sum, v2.TotalWeight())
require.Less(t, 0., v2.Tau())

var have []testInt
for i := 0; i < v2.Size(); i++ {
got, _ := v2.Get(i)
have = append(have, got)
}
require.Equal(t, expect, have)

}

func TestEject(t *testing.T) {
Expand Down

0 comments on commit b627cc4

Please sign in to comment.