cosmos-sdk/x/upgrade/internal/conv/string_test.go
vastonus e61ee1d389
refactor: use b.Loop() to simplify the code and improve performance (#25379)
Signed-off-by: vastonus <vastonus@outlook.com>
2025-10-01 14:34:05 +00:00

42 lines
828 B
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()
timer := time.NewTimer(2 * time.Millisecond)
<-timer.C
timer.Stop()
b2 := append(b, 'd')
s.Equal("abc", string(b))
s.Equal("abcd", string(b2))
}
}
func BenchmarkUnsafeStrToBytes(b *testing.B) {
for i := 0; b.Loop(); i++ {
UnsafeStrToBytes(strconv.Itoa(i))
}
}