## Description - Return an exported type rather than an unexported type, from an exported function - Assign error values to _ - Add reason to nolint directive - Remove ineffectual assignment - Remove checked cast to same type as value --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] ~~provided a link to the relevant issue or specification~~ - [ ] ~~reviewed "Files changed" and left comments if necessary~~ - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed all author checklist items have been addressed - [ ] confirmed that this PR does not change production code
55 lines
1.2 KiB
Go
55 lines
1.2 KiB
Go
package conv
|
|
|
|
import (
|
|
"runtime"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
)
|
|
|
|
func TestStringSuite(t *testing.T) {
|
|
suite.Run(t, new(StringSuite))
|
|
}
|
|
|
|
type StringSuite struct{ suite.Suite }
|
|
|
|
func unsafeConvertStr() []byte {
|
|
return UnsafeStrToBytes("abc")
|
|
}
|
|
|
|
func (s *StringSuite) TestUnsafeStrToBytes() {
|
|
// we convert in other function to trigger GC. We want to check that
|
|
// the underlying array in []bytes is accessible after GC will finish swapping.
|
|
for i := 0; i < 5; i++ {
|
|
b := unsafeConvertStr()
|
|
runtime.GC()
|
|
<-time.NewTimer(2 * time.Millisecond).C
|
|
b2 := append(b, 'd')
|
|
s.Equal("abc", string(b))
|
|
s.Equal("abcd", string(b2))
|
|
}
|
|
}
|
|
|
|
func unsafeConvertBytes() string {
|
|
return UnsafeBytesToStr([]byte("abc"))
|
|
}
|
|
|
|
func (s *StringSuite) TestUnsafeBytesToStr() {
|
|
// we convert in other function to trigger GC. We want to check that
|
|
// the underlying array in []bytes is accessible after GC will finish swapping.
|
|
for i := 0; i < 5; i++ {
|
|
str := unsafeConvertBytes()
|
|
runtime.GC()
|
|
<-time.NewTimer(2 * time.Millisecond).C
|
|
s.Equal("abc", str)
|
|
}
|
|
}
|
|
|
|
func BenchmarkUnsafeStrToBytes(b *testing.B) {
|
|
for i := 0; i < b.N; i++ {
|
|
UnsafeStrToBytes(strconv.Itoa(i))
|
|
}
|
|
}
|