github.com/binbinly/pkg@v0.0.11-0.20240321014439-f4fbf666eb0f/util/pad_test.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestPad(t *testing.T) { 11 src := "test" 12 tests := []struct { 13 pad, want string 14 n int 15 }{ 16 {"0", src, 3}, 17 {"0", "0" + src, 5}, 18 {"0", "0" + src + "0", 6}, 19 {"0", "00" + src + "0", 7}, 20 {"0", "00" + src + "00", 8}, 21 22 {"123", "1" + src, 5}, 23 {"123", "1" + src + "1", 6}, 24 {"123", "12" + src + "1", 7}, 25 {"123", "12" + src + "12", 8}, 26 {"123", "123" + src + "12", 9}, 27 {"123", "1231" + src + "123", 11}, 28 } 29 for _, v := range tests { 30 assert.Equal(t, v.want, Pad(src, v.pad, v.n)) 31 } 32 } 33 34 func TestLeftPad(t *testing.T) { 35 src := "test" 36 tests := []struct { 37 pad, want string 38 n int 39 }{ 40 {"0", src, 3}, 41 {"0", "0" + src, 5}, 42 {"0", "00" + src, 6}, 43 {"0", "000" + src, 7}, 44 45 {"123", "1" + src, 5}, 46 {"123", "12" + src, 6}, 47 {"123", "123" + src, 7}, 48 {"123", "1231" + src, 8}, 49 {"123", "1231231" + src, 11}, 50 } 51 for _, v := range tests { 52 assert.Equal(t, v.want, LeftPad(src, v.pad, v.n)) 53 } 54 assert.Equal(t, fmt.Sprintf("%32s", src), LeftPad(src, " ", 32)) 55 assert.Equal(t, fmt.Sprintf("%032s", "111"), LeftPad("111", "0", 32)) 56 } 57 58 func TestRightPad(t *testing.T) { 59 src := "test" 60 tests := []struct { 61 pad, want string 62 n int 63 }{ 64 {"0", src, 3}, 65 {"0", src + "0", 5}, 66 {"0", src + "00", 6}, 67 {"0", src + "000", 7}, 68 69 {"123", src + "1", 5}, 70 {"123", src + "12", 6}, 71 {"123", src + "123", 7}, 72 {"123", src + "1231", 8}, 73 {"123", src + "1231231", 11}, 74 } 75 for _, v := range tests { 76 assert.Equal(t, v.want, RightPad(src, v.pad, v.n)) 77 } 78 assert.Equal(t, fmt.Sprintf("%-32s", src), RightPad(src, " ", 32)) 79 assert.Equal(t, fmt.Sprintf("%s%032s", "111", "")[:32], RightPad("111", "0", 32)) 80 } 81 82 func TestPadBytes(t *testing.T) { 83 src := []byte("test") 84 tests := []struct { 85 pad, want []byte 86 n int 87 }{ 88 {[]byte("0"), src, 3}, 89 {[]byte("0"), JoinBytes([]byte("0"), src), 5}, 90 {[]byte("0"), JoinBytes([]byte("0"), src, []byte("0")), 6}, 91 {[]byte("0"), JoinBytes([]byte("00"), src, []byte("0")), 7}, 92 {[]byte("0"), JoinBytes([]byte("00"), src, []byte("00")), 8}, 93 94 {[]byte("123"), JoinBytes([]byte("1"), src), 5}, 95 {[]byte("123"), JoinBytes([]byte("1"), src, []byte("1")), 6}, 96 {[]byte("123"), JoinBytes([]byte("12"), src, []byte("1")), 7}, 97 {[]byte("123"), JoinBytes([]byte("12"), src, []byte("12")), 8}, 98 {[]byte("123"), JoinBytes([]byte("123"), src, []byte("12")), 9}, 99 {[]byte("123"), JoinBytes([]byte("1231"), src, []byte("123")), 11}, 100 } 101 for _, v := range tests { 102 assert.Equal(t, v.want, PadBytes(src, v.pad, v.n)) 103 } 104 } 105 106 func TestLeftPadBytes(t *testing.T) { 107 src := []byte("test") 108 tests := []struct { 109 pad, want []byte 110 n int 111 }{ 112 {[]byte("0"), src, 3}, 113 {[]byte("0"), JoinBytes([]byte("0"), src), 5}, 114 {[]byte("0"), JoinBytes([]byte("00"), src), 6}, 115 {[]byte("0"), JoinBytes([]byte("000"), src), 7}, 116 117 {[]byte("123"), JoinBytes([]byte("1"), src), 5}, 118 {[]byte("123"), JoinBytes([]byte("12"), src), 6}, 119 {[]byte("123"), JoinBytes([]byte("123"), src), 7}, 120 {[]byte("123"), JoinBytes([]byte("1231"), src), 8}, 121 {[]byte("123"), JoinBytes([]byte("1231231"), src), 11}, 122 } 123 for _, v := range tests { 124 assert.Equal(t, v.want, LeftPadBytes(src, v.pad, v.n)) 125 } 126 } 127 128 func TestRightPadBytes(t *testing.T) { 129 src := []byte("test") 130 tests := []struct { 131 pad, want []byte 132 n int 133 }{ 134 {[]byte("0"), src, 3}, 135 {[]byte("0"), JoinBytes(src, []byte("0")), 5}, 136 {[]byte("0"), JoinBytes(src, []byte("00")), 6}, 137 {[]byte("0"), JoinBytes(src, []byte("000")), 7}, 138 139 {[]byte("123"), JoinBytes(src, []byte("1")), 5}, 140 {[]byte("123"), JoinBytes(src, []byte("12")), 6}, 141 {[]byte("123"), JoinBytes(src, []byte("123")), 7}, 142 {[]byte("123"), JoinBytes(src, []byte("1231")), 8}, 143 {[]byte("123"), JoinBytes(src, []byte("1231231")), 11}, 144 } 145 for _, v := range tests { 146 assert.Equal(t, v.want, RightPadBytes(src, v.pad, v.n)) 147 } 148 } 149 150 func BenchmarkPad(b *testing.B) { 151 s := "111" 152 b.Run("LeftPad", func(b *testing.B) { 153 for i := 0; i < b.N; i++ { 154 _ = LeftPad(s, "0", 32) 155 } 156 }) 157 b.Run("fmt.Sprintf", func(b *testing.B) { 158 for i := 0; i < b.N; i++ { 159 _ = fmt.Sprintf("%032s", s) 160 } 161 }) 162 } 163 164 // go test -run=^$ -benchmem -benchtime=1s -count=3 -bench=^BenchmarkPad$ 165 // goos: linux 166 // goarch: amd64 167 // pkg: github.com/fufuok/utils 168 // cpu: Intel(R) Xeon(R) CPU E3-1230 V2 @ 3.30GHz 169 // BenchmarkPad/LeftPad-8 15095967 82.43 ns/op 32 B/op 1 allocs/op 170 // BenchmarkPad/LeftPad-8 15392232 81.87 ns/op 32 B/op 1 allocs/op 171 // BenchmarkPad/LeftPad-8 14027343 85.44 ns/op 32 B/op 1 allocs/op 172 // BenchmarkPad/fmt.Sprintf-8 4820535 289.7 ns/op 48 B/op 2 allocs/op 173 // BenchmarkPad/fmt.Sprintf-8 4064318 327.1 ns/op 48 B/op 2 allocs/op 174 // BenchmarkPad/fmt.Sprintf-8 4150326 279.9 ns/op 48 B/op 2 allocs/op