Skip to content

Commit

Permalink
[chore] [pdata] Add test coverage for slices Sort method (open-teleme…
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitryax authored Feb 4, 2023
1 parent 16c851d commit 0f6a174
Show file tree
Hide file tree
Showing 20 changed files with 294 additions and 1 deletion.
20 changes: 19 additions & 1 deletion pdata/internal/cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,23 @@ func (es ${structName}) Sort(less func(a, b ${elementName}) bool) {
sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) })
}`

// TODO: Use assert.Less once https://github.com/stretchr/testify/pull/1339 is merged.
const slicePtrTestTemplate = `func Test${structName}_Sort(t *testing.T) {
es := generateTest${structName}()
es.Sort(func(a, b ${elementName}) bool {
return uintptr(unsafe.Pointer(a.orig)) < uintptr(unsafe.Pointer(b.orig))
})
for i := 1; i < es.Len(); i++ {
assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) < uintptr(unsafe.Pointer(es.At(i).orig)))
}
es.Sort(func(a, b ${elementName}) bool {
return uintptr(unsafe.Pointer(a.orig)) > uintptr(unsafe.Pointer(b.orig))
})
for i := 1; i < es.Len(); i++ {
assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) > uintptr(unsafe.Pointer(es.At(i).orig)))
}
}`

const sliceValueTemplate = `// CopyTo copies all elements from the current slice overriding the destination.
func (es ${structName}) CopyTo(dest ${structName}) {
srcLen := es.Len()
Expand Down Expand Up @@ -295,7 +312,8 @@ func (ss *sliceOfPtrs) generateStruct(sb *bytes.Buffer) {
}

func (ss *sliceOfPtrs) generateTests(sb *bytes.Buffer) {
sb.WriteString(os.Expand(commonSliceTestTemplate, ss.templateFields()))
sb.WriteString(os.Expand(commonSliceTestTemplate, ss.templateFields()) + newLine + newLine)
sb.WriteString(os.Expand(slicePtrTestTemplate, ss.templateFields()))
}

func (ss *sliceOfPtrs) generateTestValueHelpers(sb *bytes.Buffer) {
Expand Down
1 change: 1 addition & 0 deletions pdata/internal/cmd/pdatagen/internal/plog_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var plog = &Package{
},
testImports: []string{
`"testing"`,
`"unsafe"`,
``,
`"github.com/stretchr/testify/assert"`,
``,
Expand Down
1 change: 1 addition & 0 deletions pdata/internal/cmd/pdatagen/internal/pmetric_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var pmetric = &Package{
},
testImports: []string{
`"testing"`,
`"unsafe"`,
``,
`"github.com/stretchr/testify/assert"`,
``,
Expand Down
1 change: 1 addition & 0 deletions pdata/internal/cmd/pdatagen/internal/ptrace_package.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ var ptrace = &Package{
},
testImports: []string{
`"testing"`,
`"unsafe"`,
``,
`"github.com/stretchr/testify/assert"`,
``,
Expand Down
17 changes: 17 additions & 0 deletions pdata/plog/generated_logrecordslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/plog/generated_resourcelogsslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/plog/generated_scopelogsslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_exponentialhistogramdatapointslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_histogramdatapointslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_metricslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_numberdatapointslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_resourcemetricsslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_scopemetricsslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions pdata/pmetric/generated_summarydatapointslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0f6a174

Please sign in to comment.