github.com/Laisky/zap@v1.27.0/buffer/buffer_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package buffer 22 23 import ( 24 "bytes" 25 "strings" 26 "testing" 27 "time" 28 29 "github.com/stretchr/testify/assert" 30 ) 31 32 func TestBufferWrites(t *testing.T) { 33 buf := NewPool().Get() 34 35 tests := []struct { 36 desc string 37 f func() 38 want string 39 }{ 40 {"AppendByte", func() { buf.AppendByte('v') }, "v"}, 41 {"AppendString", func() { buf.AppendString("foo") }, "foo"}, 42 {"AppendIntPositive", func() { buf.AppendInt(42) }, "42"}, 43 {"AppendIntNegative", func() { buf.AppendInt(-42) }, "-42"}, 44 {"AppendUint", func() { buf.AppendUint(42) }, "42"}, 45 {"AppendBool", func() { buf.AppendBool(true) }, "true"}, 46 {"AppendFloat64", func() { buf.AppendFloat(3.14, 64) }, "3.14"}, 47 // Intentionally introduce some floating-point error. 48 {"AppendFloat32", func() { buf.AppendFloat(float64(float32(3.14)), 32) }, "3.14"}, 49 {"AppendWrite", func() { buf.Write([]byte("foo")) }, "foo"}, 50 {"AppendTime", func() { buf.AppendTime(time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC), time.RFC3339) }, "2000-01-02T03:04:05Z"}, 51 {"WriteByte", func() { buf.WriteByte('v') }, "v"}, 52 {"WriteString", func() { buf.WriteString("foo") }, "foo"}, 53 } 54 55 for _, tt := range tests { 56 t.Run(tt.desc, func(t *testing.T) { 57 buf.Reset() 58 tt.f() 59 assert.Equal(t, tt.want, buf.String(), "Unexpected buffer.String().") 60 assert.Equal(t, tt.want, string(buf.Bytes()), "Unexpected string(buffer.Bytes()).") 61 assert.Equal(t, len(tt.want), buf.Len(), "Unexpected buffer length.") 62 // We're not writing more than a kibibyte in tests. 63 assert.Equal(t, _size, buf.Cap(), "Expected buffer capacity to remain constant.") 64 }) 65 } 66 } 67 68 func BenchmarkBuffers(b *testing.B) { 69 // Because we use the strconv.AppendFoo functions so liberally, we can't 70 // use the standard library's bytes.Buffer anyways (without incurring a 71 // bunch of extra allocations). Nevertheless, let's make sure that we're 72 // not losing any precious nanoseconds. 73 str := strings.Repeat("a", 1024) 74 slice := make([]byte, 1024) 75 buf := bytes.NewBuffer(slice) 76 custom := NewPool().Get() 77 b.Run("ByteSlice", func(b *testing.B) { 78 for i := 0; i < b.N; i++ { 79 slice = append(slice, str...) 80 slice = slice[:0] 81 } 82 }) 83 b.Run("BytesBuffer", func(b *testing.B) { 84 for i := 0; i < b.N; i++ { 85 buf.WriteString(str) 86 buf.Reset() 87 } 88 }) 89 b.Run("CustomBuffer", func(b *testing.B) { 90 for i := 0; i < b.N; i++ { 91 custom.AppendString(str) 92 custom.Reset() 93 } 94 }) 95 }