Skip to content
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 95 additions & 73 deletions cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ const commonSliceGenerateTest = `func generateTest${structName}() ${structName}
}
func fillTest${structName}(tv ${structName}) {
tv.Resize(7)
for i := 0; i < tv.Len(); i++ {
fillTest${elementName}(tv.At(i))
l := 7
tv.EnsureCapacity(l)
for i := 0; i < l; i++ {
fillTest${elementName}(tv.AppendEmpty())
}
}`

Expand All @@ -119,7 +120,7 @@ const slicePtrTemplate = `// ${structName} logically represents a slice of ${ele
// Important: zero-initialized instance is not valid for use.
type ${structName} struct {
// orig points to the slice ${originName} field contained somewhere else.
// We use pointer-to-slice to be able to modify it in functions like Resize.
// We use pointer-to-slice to be able to modify it in functions like EnsureCapacity.
orig *[]*${originName}
}
Expand All @@ -128,7 +129,7 @@ func new${structName}(orig *[]*${originName}) ${structName} {
}
// New${structName} creates a ${structName} with 0 elements.
// Can use "Resize" to initialize with a given length.
// Can use "EnsureCapacity" to initialize with a given capacity.
func New${structName}() ${structName} {
orig := []*${originName}(nil)
return ${structName}{&orig}
Expand Down Expand Up @@ -172,6 +173,28 @@ func (es ${structName}) CopyTo(dest ${structName}) {
*dest.orig = wrappers
}
// EnsureCapacity is an operation that ensures the slice has at least the specified capacity.
// 1. If the newCap <= cap then no change in capacity.
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap.
//
// Here is how a new ${structName} can be initialized:
// es := New${structName}()
// es.EnsureCapacity(4)
// for i := 0; i < 4; i++ {
// e := es.AppendEmpty()
// // Here should set all the values for e.
// }
func (es ${structName}) EnsureCapacity(newCap int) {
oldCap := cap(*es.orig)
if newCap <= oldCap {
return
}
newOrig := make([]*${originName}, len(*es.orig), newCap)
copy(newOrig, *es.orig)
*es.orig = newOrig
}
// Resize is an operation that resizes the slice:
// 1. If the newLen <= len then equivalent with slice[0:newLen:cap].
// 2. If the newLen > len then (newLen - cap) empty elements will be appended to the slice.
Expand All @@ -183,20 +206,20 @@ func (es ${structName}) CopyTo(dest ${structName}) {
// e := es.At(i)
// // Here should set all the values for e.
// }
//
// Deprecated: Use EnsureCapacity() and AppendEmpty() instead.
func (es ${structName}) Resize(newLen int) {
oldLen := len(*es.orig)
oldCap := cap(*es.orig)
if newLen <= oldLen {
*es.orig = (*es.orig)[:newLen:oldCap]
return
}
if newLen > oldCap {
newOrig := make([]*${originName}, oldLen, newLen)
copy(newOrig, *es.orig)
*es.orig = newOrig
}
// Add extra empty elements to the array.
extraOrigs := make([]${originName}, newLen-oldLen)
for i := range extraOrigs {
Expand All @@ -209,22 +232,23 @@ func (es ${structName}) Resize(newLen int) {
func (es ${structName}) AppendEmpty() ${elementName} {
*es.orig = append(*es.orig, &${originName}{})
return es.At(es.Len() - 1)
}`
} `

const slicePtrTestTemplate = `func Test${structName}(t *testing.T) {
es := New${structName}()
assert.EqualValues(t, 0, es.Len())
es = new${structName}(&[]*${originName}{})
assert.EqualValues(t, 0, es.Len())
es.Resize(7)
es.EnsureCapacity(7)
emptyVal := new${elementName}(&${originName}{})
testVal := generateTest${elementName}()
assert.EqualValues(t, 7, es.Len())
assert.EqualValues(t, 7, cap(*es.orig))
for i := 0; i < es.Len(); i++ {
assert.EqualValues(t, emptyVal, es.At(i))
fillTest${elementName}(es.At(i))
assert.EqualValues(t, testVal, es.At(i))
el := es.AppendEmpty()
assert.EqualValues(t, emptyVal, el)
fillTest${elementName}(el)
assert.EqualValues(t, testVal, el)
}
}
Expand All @@ -243,46 +267,38 @@ func Test${structName}_CopyTo(t *testing.T) {
assert.EqualValues(t, generateTest${structName}(), dest)
}
func Test${structName}_Resize(t *testing.T) {
func Test${structName}_EnsureCapacity(t *testing.T) {
es := generateTest${structName}()
emptyVal := new${elementName}(&${originName}{})
// Test Resize less elements.
const resizeSmallLen = 4
expectedEs := make(map[*${originName}]bool, resizeSmallLen)
for i := 0; i < resizeSmallLen; i++ {
// Test ensure smaller capacity.
const ensureSmallLen = 4
expectedEs := make(map[*${originName}]bool)
for i := 0; i < es.Len(); i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, resizeSmallLen, len(expectedEs))
es.Resize(resizeSmallLen)
assert.Equal(t, resizeSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, resizeSmallLen)
assert.Equal(t, es.Len(), len(expectedEs))
es.EnsureCapacity(ensureSmallLen)
assert.Less(t, ensureSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, es.Len())
for i := 0; i < es.Len(); i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)
// Test Resize more elements.
const resizeLargeLen = 7
// Test ensure larger capacity
const ensureLargeLen = 9
oldLen := es.Len()
expectedEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, oldLen, len(expectedEs))
es.Resize(resizeLargeLen)
assert.Equal(t, resizeLargeLen, es.Len())
es.EnsureCapacity(ensureLargeLen)
assert.Equal(t, ensureLargeLen, cap(*es.orig))
foundEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)
for i := oldLen; i < resizeLargeLen; i++ {
assert.EqualValues(t, emptyVal, es.At(i))
}
// Test Resize 0 elements.
es.Resize(0)
assert.Equal(t, 0, es.Len())
}`

const sliceValueTemplate = `// ${structName} logically represents a slice of ${elementName}.
Expand All @@ -294,7 +310,7 @@ const sliceValueTemplate = `// ${structName} logically represents a slice of ${e
// Important: zero-initialized instance is not valid for use.
type ${structName} struct {
// orig points to the slice ${originName} field contained somewhere else.
// We use pointer-to-slice to be able to modify it in functions like Resize.
// We use pointer-to-slice to be able to modify it in functions like EnsureCapacity.
orig *[]${originName}
}
Expand All @@ -303,7 +319,7 @@ func new${structName}(orig *[]${originName}) ${structName} {
}
// New${structName} creates a ${structName} with 0 elements.
// Can use "Resize" to initialize with a given length.
// Can use "EnsureCapacity" to initialize with a given capacity.
func New${structName}() ${structName} {
orig := []${originName}(nil)
return ${structName}{&orig}
Expand Down Expand Up @@ -342,6 +358,28 @@ func (es ${structName}) CopyTo(dest ${structName}) {
}
}
// EnsureCapacity is an operation that ensures the slice has at least the specified capacity.
// 1. If the newCap <= cap then no change in capacity.
// 2. If the newCap > cap then the slice capacity will be expanded to equal newCap.
//
// Here is how a new ${structName} can be initialized:
// es := New${structName}()
// es.EnsureCapacity(4)
// for i := 0; i < 4; i++ {
// e := es.AppendEmpty()
// // Here should set all the values for e.
// }
func (es ${structName}) EnsureCapacity(newCap int) {
oldCap := cap(*es.orig)
if newCap <= oldCap {
return
}
newOrig := make([]${originName}, len(*es.orig), newCap)
copy(newOrig, *es.orig)
*es.orig = newOrig
}
// Resize is an operation that resizes the slice:
// 1. If the newLen <= len then equivalent with slice[0:newLen:cap].
// 2. If the newLen > len then (newLen - cap) empty elements will be appended to the slice.
Expand All @@ -353,20 +391,20 @@ func (es ${structName}) CopyTo(dest ${structName}) {
// e := es.At(i)
// // Here should set all the values for e.
// }
//
// Deprecated: Use EnsureCapacity() and AppendEmpty() instead.
func (es ${structName}) Resize(newLen int) {
oldLen := len(*es.orig)
oldCap := cap(*es.orig)
if newLen <= oldLen {
*es.orig = (*es.orig)[:newLen:oldCap]
return
}
if newLen > oldCap {
newOrig := make([]${originName}, oldLen, newLen)
copy(newOrig, *es.orig)
*es.orig = newOrig
}
// Add extra empty elements to the array.
empty := ${originName}{}
for i := oldLen; i < newLen; i++ {
Expand All @@ -387,14 +425,15 @@ const sliceValueTestTemplate = `func Test${structName}(t *testing.T) {
es = new${structName}(&[]${originName}{})
assert.EqualValues(t, 0, es.Len())
es.Resize(7)
es.EnsureCapacity(7)
emptyVal := new${elementName}(&${originName}{})
testVal := generateTest${elementName}()
assert.EqualValues(t, 7, es.Len())
assert.EqualValues(t, 7, cap(*es.orig))
for i := 0; i < es.Len(); i++ {
assert.EqualValues(t, emptyVal, es.At(i))
fillTest${elementName}(es.At(i))
assert.EqualValues(t, testVal, es.At(i))
el := es.AppendEmpty()
assert.EqualValues(t, emptyVal, el)
fillTest${elementName}(el)
assert.EqualValues(t, testVal, el)
}
}
Expand All @@ -413,46 +452,29 @@ func Test${structName}_CopyTo(t *testing.T) {
assert.EqualValues(t, generateTest${structName}(), dest)
}
func Test${structName}_Resize(t *testing.T) {
func Test${structName}_EnsureCapacity(t *testing.T) {
es := generateTest${structName}()
emptyVal := new${elementName}(&${originName}{})
// Test Resize less elements.
const resizeSmallLen = 4
expectedEs := make(map[*${originName}]bool, resizeSmallLen)
for i := 0; i < resizeSmallLen; i++ {
// Test ensure smaller capacity.
const ensureSmallLen = 4
expectedEs := make(map[*${originName}]bool)
for i := 0; i < es.Len(); i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, resizeSmallLen, len(expectedEs))
es.Resize(resizeSmallLen)
assert.Equal(t, resizeSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, resizeSmallLen)
assert.Equal(t, es.Len(), len(expectedEs))
es.EnsureCapacity(ensureSmallLen)
assert.Less(t, ensureSmallLen, es.Len())
foundEs := make(map[*${originName}]bool, es.Len())
for i := 0; i < es.Len(); i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)
// Test Resize more elements.
const resizeLargeLen = 7
// Test ensure larger capacity
const ensureLargeLen = 9
oldLen := es.Len()
expectedEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
expectedEs[es.At(i).orig] = true
}
assert.Equal(t, oldLen, len(expectedEs))
es.Resize(resizeLargeLen)
assert.Equal(t, resizeLargeLen, es.Len())
foundEs = make(map[*${originName}]bool, oldLen)
for i := 0; i < oldLen; i++ {
foundEs[es.At(i).orig] = true
}
assert.EqualValues(t, expectedEs, foundEs)
for i := oldLen; i < resizeLargeLen; i++ {
assert.EqualValues(t, emptyVal, es.At(i))
}
// Test Resize 0 elements.
es.Resize(0)
assert.Equal(t, 0, es.Len())
es.EnsureCapacity(ensureLargeLen)
assert.Equal(t, ensureLargeLen, cap(*es.orig))
}`

type baseSlice interface {
Expand Down
8 changes: 4 additions & 4 deletions exporter/prometheusremotewriteexporter/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ func getTimeSeries(labels []prompb.Label, samples ...prompb.Sample) *prompb.Time

func getQuantiles(bounds []float64, values []float64) pdata.ValueAtQuantileSlice {
quantiles := pdata.NewValueAtQuantileSlice()
quantiles.Resize(len(bounds))
quantiles.EnsureCapacity(len(bounds))

for i := 0; i < len(bounds); i++ {
quantile := quantiles.At(i)
quantile := quantiles.AppendEmpty()
quantile.SetQuantile(bounds[i])
quantile.SetValue(values[i])
}
Expand Down Expand Up @@ -449,9 +449,9 @@ func getMetricsFromMetricList(metricList ...pdata.Metric) pdata.Metrics {

rm := metrics.ResourceMetrics().AppendEmpty()
ilm := rm.InstrumentationLibraryMetrics().AppendEmpty()
ilm.Metrics().Resize(len(metricList))
ilm.Metrics().EnsureCapacity(len(metricList))
for i := 0; i < len(metricList); i++ {
metricList[i].CopyTo(ilm.Metrics().At(i))
metricList[i].CopyTo(ilm.Metrics().AppendEmpty())
}

return metrics
Expand Down
Loading