github.com/bibaroc/wingman@v0.0.2-0.20200911182922-33c2085136b1/pkg/fst/slice_test.go (about) 1 package fst_test 2 3 import ( 4 "testing" 5 6 "github.com/bibaroc/wingman/pkg/fst" 7 "github.com/google/go-cmp/cmp" 8 ) 9 10 func TestSlice(t *testing.T) { 11 str := "hi there, i'm ur string" 12 want := []byte(str) 13 got := fst.Slice(&str) 14 if diff := cmp.Diff(want, got); diff != "" { 15 t.Errorf("Simple conversion mismatch (-want +got):\n%s", diff) 16 } 17 } 18 19 func BenchmarkSlice32(b *testing.B) { benchmarkSlice(b, str(32)) } 20 func BenchmarkSlice64(b *testing.B) { benchmarkSlice(b, str(64)) } 21 func BenchmarkSlice128(b *testing.B) { benchmarkSlice(b, str(128)) } 22 func BenchmarkSlice256(b *testing.B) { benchmarkSlice(b, str(256)) } 23 func BenchmarkSlc32(b *testing.B) { benchmarkSlc(b, str(32)) } 24 func BenchmarkSlc64(b *testing.B) { benchmarkSlc(b, str(64)) } 25 func BenchmarkSlc128(b *testing.B) { benchmarkSlc(b, str(128)) } 26 func BenchmarkSlc256(b *testing.B) { benchmarkSlc(b, str(256)) } 27 func BenchmarkCastBytea32(b *testing.B) { benchmarkCastBytea(b, str(32)) } 28 func BenchmarkCastBytea64(b *testing.B) { benchmarkCastBytea(b, str(64)) } 29 func BenchmarkCastBytea128(b *testing.B) { benchmarkCastBytea(b, str(128)) } 30 func BenchmarkCastBytea256(b *testing.B) { benchmarkCastBytea(b, str(256)) } 31 32 func benchmarkSlice(b *testing.B, str string) { 33 b.ReportAllocs() 34 b.ResetTimer() 35 for i := 0; i < b.N; i++ { 36 got := fst.Slice(&str) 37 noop(got) 38 } 39 } 40 41 func benchmarkSlc(b *testing.B, str string) { 42 b.ReportAllocs() 43 b.ResetTimer() 44 for i := 0; i < b.N; i++ { 45 got := fst.Slc(&str, len(str)) 46 noop(got) 47 } 48 } 49 50 func benchmarkCastBytea(b *testing.B, str string) { 51 b.ReportAllocs() 52 b.ResetTimer() 53 for i := 0; i < b.N; i++ { 54 got := []byte(str) 55 noop(got) 56 } 57 }