github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/msgp/write_bytes_test.go (about)

     1  package msgp
     2  
     3  import (
     4  	"bytes"
     5  	"math"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestIssue116(t *testing.T) {
    11  	data := AppendInt64(nil, math.MinInt64)
    12  	i, _, err := nbs.ReadInt64Bytes(data)
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  	if i != math.MinInt64 {
    17  		t.Errorf("put %d in and got %d out", int64(math.MinInt64), i)
    18  	}
    19  
    20  	var buf bytes.Buffer
    21  
    22  	w := NewWriter(&buf)
    23  	w.WriteInt64(math.MinInt64)
    24  	w.Flush()
    25  	i, err = NewReader(&buf).ReadInt64()
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	if i != math.MinInt64 {
    30  		t.Errorf("put %d in and got %d out", int64(math.MinInt64), i)
    31  	}
    32  }
    33  
    34  func TestAppendMapHeader(t *testing.T) {
    35  	szs := []uint32{0, 1, uint32(tint8), uint32(tint16), tuint32}
    36  	var buf bytes.Buffer
    37  	en := NewWriter(&buf)
    38  
    39  	var bts []byte
    40  	for _, sz := range szs {
    41  		buf.Reset()
    42  		en.WriteMapHeader(sz)
    43  		en.Flush()
    44  		bts = AppendMapHeader(bts[0:0], sz)
    45  
    46  		if !bytes.Equal(buf.Bytes(), bts) {
    47  			t.Errorf("for size %d, encoder wrote %q and append wrote %q", sz, buf.Bytes(), bts)
    48  		}
    49  	}
    50  }
    51  
    52  func BenchmarkAppendMapHeader(b *testing.B) {
    53  	buf := make([]byte, 0, 9)
    54  	N := b.N / 4
    55  	b.ReportAllocs()
    56  	b.ResetTimer()
    57  	for i := 0; i < N; i++ {
    58  		AppendMapHeader(buf[:0], 0)
    59  		AppendMapHeader(buf[:0], uint32(tint8))
    60  		AppendMapHeader(buf[:0], tuint16)
    61  		AppendMapHeader(buf[:0], tuint32)
    62  	}
    63  }
    64  
    65  func TestAppendArrayHeader(t *testing.T) {
    66  	szs := []uint32{0, 1, uint32(tint8), uint32(tint16), tuint32}
    67  	var buf bytes.Buffer
    68  	en := NewWriter(&buf)
    69  
    70  	var bts []byte
    71  	for _, sz := range szs {
    72  		buf.Reset()
    73  		en.WriteArrayHeader(sz)
    74  		en.Flush()
    75  		bts = AppendArrayHeader(bts[0:0], sz)
    76  
    77  		if !bytes.Equal(buf.Bytes(), bts) {
    78  			t.Errorf("for size %d, encoder wrote %q and append wrote %q", sz, buf.Bytes(), bts)
    79  		}
    80  	}
    81  }
    82  
    83  func BenchmarkAppendArrayHeader(b *testing.B) {
    84  	buf := make([]byte, 0, 9)
    85  	N := b.N / 4
    86  	b.ReportAllocs()
    87  	b.ResetTimer()
    88  	for i := 0; i < N; i++ {
    89  		AppendArrayHeader(buf[:0], 0)
    90  		AppendArrayHeader(buf[:0], uint32(tint8))
    91  		AppendArrayHeader(buf[:0], tuint16)
    92  		AppendArrayHeader(buf[:0], tuint32)
    93  	}
    94  }
    95  
    96  func TestAppendNil(t *testing.T) {
    97  	var bts []byte
    98  	bts = AppendNil(bts[0:0])
    99  	if bts[0] != mnil {
   100  		t.Fatal("bts[0] is not 'nil'")
   101  	}
   102  }
   103  
   104  func TestAppendFloat64(t *testing.T) {
   105  	f := float64(3.14159)
   106  	var buf bytes.Buffer
   107  	en := NewWriter(&buf)
   108  
   109  	var bts []byte
   110  	en.WriteFloat64(f)
   111  	en.Flush()
   112  	bts = AppendFloat64(bts[0:0], f)
   113  	if !bytes.Equal(buf.Bytes(), bts) {
   114  		t.Errorf("for float %f, encoder wrote %q; append wrote %q", f, buf.Bytes(), bts)
   115  	}
   116  }
   117  
   118  func BenchmarkAppendFloat64(b *testing.B) {
   119  	f := float64(3.14159)
   120  	buf := make([]byte, 0, 9)
   121  	b.SetBytes(9)
   122  	b.ReportAllocs()
   123  	b.ResetTimer()
   124  	for i := 0; i < b.N; i++ {
   125  		AppendFloat64(buf[0:0], f)
   126  	}
   127  }
   128  
   129  func TestAppendFloat32(t *testing.T) {
   130  	f := float32(3.14159)
   131  	var buf bytes.Buffer
   132  	en := NewWriter(&buf)
   133  
   134  	var bts []byte
   135  	en.WriteFloat32(f)
   136  	en.Flush()
   137  	bts = AppendFloat32(bts[0:0], f)
   138  	if !bytes.Equal(buf.Bytes(), bts) {
   139  		t.Errorf("for float %f, encoder wrote %q; append wrote %q", f, buf.Bytes(), bts)
   140  	}
   141  }
   142  
   143  func BenchmarkAppendFloat32(b *testing.B) {
   144  	f := float32(3.14159)
   145  	buf := make([]byte, 0, 5)
   146  	b.SetBytes(5)
   147  	b.ReportAllocs()
   148  	b.ResetTimer()
   149  	for i := 0; i < b.N; i++ {
   150  		AppendFloat32(buf[0:0], f)
   151  	}
   152  }
   153  
   154  func TestAppendInt64(t *testing.T) {
   155  	is := []int64{0, 1, -5, -50, int64(tint16), int64(tint32), int64(tint64)}
   156  	var buf bytes.Buffer
   157  	en := NewWriter(&buf)
   158  
   159  	var bts []byte
   160  	for _, i := range is {
   161  		buf.Reset()
   162  		en.WriteInt64(i)
   163  		en.Flush()
   164  		bts = AppendInt64(bts[0:0], i)
   165  		if !bytes.Equal(buf.Bytes(), bts) {
   166  			t.Errorf("for int64 %d, encoder wrote %q; append wrote %q", i, buf.Bytes(), bts)
   167  		}
   168  	}
   169  }
   170  
   171  func BenchmarkAppendInt64(b *testing.B) {
   172  	is := []int64{0, 1, -5, -50, int64(tint16), int64(tint32), int64(tint64)}
   173  	l := len(is)
   174  	buf := make([]byte, 0, 9)
   175  	b.ReportAllocs()
   176  	b.ResetTimer()
   177  	for i := 0; i < b.N; i++ {
   178  		AppendInt64(buf[0:0], is[i%l])
   179  	}
   180  }
   181  
   182  func TestAppendUint64(t *testing.T) {
   183  	us := []uint64{0, 1, uint64(tuint16), uint64(tuint32), tuint64}
   184  	var buf bytes.Buffer
   185  	en := NewWriter(&buf)
   186  	var bts []byte
   187  
   188  	for _, u := range us {
   189  		buf.Reset()
   190  		en.WriteUint64(u)
   191  		en.Flush()
   192  		bts = AppendUint64(bts[0:0], u)
   193  		if !bytes.Equal(buf.Bytes(), bts) {
   194  			t.Errorf("for uint64 %d, encoder wrote %q; append wrote %q", u, buf.Bytes(), bts)
   195  		}
   196  	}
   197  }
   198  
   199  func BenchmarkAppendUint64(b *testing.B) {
   200  	us := []uint64{0, 1, 15, uint64(tuint16), uint64(tuint32), tuint64}
   201  	buf := make([]byte, 0, 9)
   202  	b.ReportAllocs()
   203  	b.ResetTimer()
   204  	l := len(us)
   205  	for i := 0; i < b.N; i++ {
   206  		AppendUint64(buf[0:0], us[i%l])
   207  	}
   208  }
   209  
   210  func TestAppendBytes(t *testing.T) {
   211  	sizes := []int{0, 1, 225, int(tuint32)}
   212  	var buf bytes.Buffer
   213  	en := NewWriter(&buf)
   214  	var bts []byte
   215  
   216  	for _, sz := range sizes {
   217  		buf.Reset()
   218  		b := RandBytes(sz)
   219  		en.WriteBytes(b)
   220  		en.Flush()
   221  		bts = AppendBytes(b[0:0], b)
   222  		if !bytes.Equal(buf.Bytes(), bts) {
   223  			t.Errorf("for bytes of length %d, encoder wrote %d bytes and append wrote %d bytes", sz, buf.Len(), len(bts))
   224  		}
   225  	}
   226  }
   227  
   228  func benchappendBytes(size uint32, b *testing.B) {
   229  	bts := RandBytes(int(size))
   230  	buf := make([]byte, 0, len(bts)+5)
   231  	b.SetBytes(int64(len(bts) + 5))
   232  	b.ReportAllocs()
   233  	b.ResetTimer()
   234  	for i := 0; i < b.N; i++ {
   235  		AppendBytes(buf[0:0], bts)
   236  	}
   237  }
   238  
   239  func BenchmarkAppend16Bytes(b *testing.B) { benchappendBytes(16, b) }
   240  
   241  func BenchmarkAppend256Bytes(b *testing.B) { benchappendBytes(256, b) }
   242  
   243  func BenchmarkAppend2048Bytes(b *testing.B) { benchappendBytes(2048, b) }
   244  
   245  func TestAppendString(t *testing.T) {
   246  	sizes := []int{0, 1, 225, int(tuint32)}
   247  	var buf bytes.Buffer
   248  	en := NewWriter(&buf)
   249  	var bts []byte
   250  
   251  	for _, sz := range sizes {
   252  		buf.Reset()
   253  		s := string(RandBytes(sz))
   254  		en.WriteString(s)
   255  		en.Flush()
   256  		bts = AppendString(bts[0:0], s)
   257  		if !bytes.Equal(buf.Bytes(), bts) {
   258  			t.Errorf("for string of length %d, encoder wrote %d bytes and append wrote %d bytes", sz, buf.Len(), len(bts))
   259  			t.Errorf("WriteString prefix: %x", buf.Bytes()[0:5])
   260  			t.Errorf("Appendstring prefix: %x", bts[0:5])
   261  		}
   262  	}
   263  }
   264  
   265  func benchappendString(size uint32, b *testing.B) {
   266  	str := string(RandBytes(int(size)))
   267  	buf := make([]byte, 0, len(str)+5)
   268  	b.SetBytes(int64(len(str) + 5))
   269  	b.ReportAllocs()
   270  	b.ResetTimer()
   271  	for i := 0; i < b.N; i++ {
   272  		AppendString(buf[0:0], str)
   273  	}
   274  }
   275  
   276  func BenchmarkAppend16String(b *testing.B) { benchappendString(16, b) }
   277  
   278  func BenchmarkAppend256String(b *testing.B) { benchappendString(256, b) }
   279  
   280  func BenchmarkAppend2048String(b *testing.B) { benchappendString(2048, b) }
   281  
   282  func TestAppendBool(t *testing.T) {
   283  	vs := []bool{true, false}
   284  	var buf bytes.Buffer
   285  	en := NewWriter(&buf)
   286  	var bts []byte
   287  
   288  	for _, v := range vs {
   289  		buf.Reset()
   290  		en.WriteBool(v)
   291  		en.Flush()
   292  		bts = AppendBool(bts[0:0], v)
   293  		if !bytes.Equal(buf.Bytes(), bts) {
   294  			t.Errorf("for %t, encoder wrote %q and append wrote %q", v, buf.Bytes(), bts)
   295  		}
   296  	}
   297  }
   298  
   299  func BenchmarkAppendBool(b *testing.B) {
   300  	vs := []bool{true, false}
   301  	buf := make([]byte, 0, 1)
   302  	b.SetBytes(1)
   303  	b.ReportAllocs()
   304  	b.ResetTimer()
   305  	for i := 0; i < b.N; i++ {
   306  		AppendBool(buf[0:0], vs[i%2])
   307  	}
   308  }
   309  
   310  func BenchmarkAppendTime(b *testing.B) {
   311  	t := time.Now()
   312  	b.SetBytes(15)
   313  	buf := make([]byte, 0, 15)
   314  	b.ReportAllocs()
   315  	b.ResetTimer()
   316  	for i := 0; i < b.N; i++ {
   317  		AppendTime(buf[0:0], t)
   318  	}
   319  }