1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
|
package msgp
import (
"bytes"
"math"
"math/rand"
"reflect"
"strings"
"testing"
"time"
)
func TestIssue116(t *testing.T) {
data := AppendInt64(nil, math.MinInt64)
i, _, err := ReadInt64Bytes(data)
if err != nil {
t.Fatal(err)
}
if i != math.MinInt64 {
t.Errorf("put %d in and got %d out", int64(math.MinInt64), i)
}
var buf bytes.Buffer
w := NewWriter(&buf)
w.WriteInt64(math.MinInt64)
w.Flush()
i, err = NewReader(&buf).ReadInt64()
if err != nil {
t.Fatal(err)
}
if i != math.MinInt64 {
t.Errorf("put %d in and got %d out", int64(math.MinInt64), i)
}
}
func TestAppendMapHeader(t *testing.T) {
szs := []uint32{0, 1, uint32(tint8), uint32(tint16), tuint32}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, sz := range szs {
buf.Reset()
en.WriteMapHeader(sz)
en.Flush()
bts = AppendMapHeader(bts[0:0], sz)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for size %d, encoder wrote %q and append wrote %q", sz, buf.Bytes(), bts)
}
}
}
func BenchmarkAppendMapHeader(b *testing.B) {
buf := make([]byte, 0, 9)
N := b.N / 4
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < N; i++ {
AppendMapHeader(buf[:0], 0)
AppendMapHeader(buf[:0], uint32(tint8))
AppendMapHeader(buf[:0], tuint16)
AppendMapHeader(buf[:0], tuint32)
}
}
func TestAppendArrayHeader(t *testing.T) {
szs := []uint32{0, 1, uint32(tint8), uint32(tint16), tuint32}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, sz := range szs {
buf.Reset()
en.WriteArrayHeader(sz)
en.Flush()
bts = AppendArrayHeader(bts[0:0], sz)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for size %d, encoder wrote %q and append wrote %q", sz, buf.Bytes(), bts)
}
}
}
func BenchmarkAppendArrayHeader(b *testing.B) {
buf := make([]byte, 0, 9)
N := b.N / 4
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < N; i++ {
AppendArrayHeader(buf[:0], 0)
AppendArrayHeader(buf[:0], uint32(tint8))
AppendArrayHeader(buf[:0], tuint16)
AppendArrayHeader(buf[:0], tuint32)
}
}
func TestAppendBytesHeader(t *testing.T) {
szs := []uint32{0, 1, uint32(tint8), uint32(tint16), tuint32, math.MaxUint32}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, sz := range szs {
buf.Reset()
en.WriteBytesHeader(sz)
en.Flush()
bts = AppendBytesHeader(bts[0:0], sz)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for size %d, encoder wrote %q and append wrote %q", sz, buf.Bytes(), bts)
}
}
}
func BenchmarkAppendBytesHeader(b *testing.B) {
buf := make([]byte, 0, 9)
N := b.N / 4
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < N; i++ {
AppendBytesHeader(buf[:0], 0)
AppendBytesHeader(buf[:0], uint32(tint8))
AppendBytesHeader(buf[:0], tuint16)
AppendBytesHeader(buf[:0], tuint32)
}
}
func TestAppendNil(t *testing.T) {
var bts []byte
bts = AppendNil(bts[0:0])
if bts[0] != mnil {
t.Fatal("bts[0] is not 'nil'")
}
}
func TestAppendFloat(t *testing.T) {
rng := rand.New(rand.NewSource(0))
const n = 1e7
src := make([]float64, n)
for i := range src {
// ~50% full float64, 50% converted from float32.
if rng.Uint32()&1 == 1 {
src[i] = rng.NormFloat64()
} else {
src[i] = float64(math.MaxFloat32 * (0.5 - rng.Float32()))
}
}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, f := range src {
en.WriteFloat(f)
bts = AppendFloat(bts, f)
}
en.Flush()
if buf.Len() != len(bts) {
t.Errorf("encoder wrote %d; append wrote %d bytes", buf.Len(), len(bts))
}
t.Logf("%f bytes/value", float64(buf.Len())/n)
a, b := bts, buf.Bytes()
for i := range a {
if a[i] != b[i] {
t.Errorf("mismatch at byte %d, %d != %d", i, a[i], b[i])
break
}
}
for i, want := range src {
var got float64
var err error
got, a, err = ReadFloat64Bytes(a)
if err != nil {
t.Fatal(err)
}
if want != got {
t.Errorf("value #%d: want %v; got %v", i, want, got)
}
}
}
func BenchmarkAppendFloat(b *testing.B) {
rng := rand.New(rand.NewSource(0))
const n = 1 << 16
src := make([]float64, n)
for i := range src {
// ~50% full float64, 50% converted from float32.
if rng.Uint32()&1 == 1 {
src[i] = rng.NormFloat64()
} else {
src[i] = float64(math.MaxFloat32 * (0.5 - rng.Float32()))
}
}
buf := make([]byte, 0, 9)
b.SetBytes(8)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendFloat(buf, src[i&(n-1)])
}
}
func TestAppendFloat64(t *testing.T) {
f := float64(3.14159)
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
en.WriteFloat64(f)
en.Flush()
bts = AppendFloat64(bts[0:0], f)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for float %f, encoder wrote %q; append wrote %q", f, buf.Bytes(), bts)
}
}
func BenchmarkAppendFloat64(b *testing.B) {
f := float64(3.14159)
buf := make([]byte, 0, 9)
b.SetBytes(9)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendFloat64(buf[0:0], f)
}
}
func TestAppendFloat32(t *testing.T) {
f := float32(3.14159)
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
en.WriteFloat32(f)
en.Flush()
bts = AppendFloat32(bts[0:0], f)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for float %f, encoder wrote %q; append wrote %q", f, buf.Bytes(), bts)
}
}
func BenchmarkAppendFloat32(b *testing.B) {
f := float32(3.14159)
buf := make([]byte, 0, 5)
b.SetBytes(5)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendFloat32(buf[0:0], f)
}
}
func TestAppendInt64(t *testing.T) {
is := []int64{0, 1, -5, -50, int64(tint16), int64(tint32), int64(tint64)}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, i := range is {
buf.Reset()
en.WriteInt64(i)
en.Flush()
bts = AppendInt64(bts[0:0], i)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for int64 %d, encoder wrote %q; append wrote %q", i, buf.Bytes(), bts)
}
}
}
func BenchmarkAppendInt64(b *testing.B) {
is := []int64{0, 1, -5, -50, int64(tint16), int64(tint32), int64(tint64)}
l := len(is)
buf := make([]byte, 0, 9)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendInt64(buf[0:0], is[i%l])
}
}
func TestAppendUint64(t *testing.T) {
us := []uint64{0, 1, uint64(tuint16), uint64(tuint32), tuint64}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, u := range us {
buf.Reset()
en.WriteUint64(u)
en.Flush()
bts = AppendUint64(bts[0:0], u)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for uint64 %d, encoder wrote %q; append wrote %q", u, buf.Bytes(), bts)
}
}
}
func BenchmarkAppendUint64(b *testing.B) {
us := []uint64{0, 1, 15, uint64(tuint16), uint64(tuint32), tuint64}
buf := make([]byte, 0, 9)
b.ReportAllocs()
b.ResetTimer()
l := len(us)
for i := 0; i < b.N; i++ {
AppendUint64(buf[0:0], us[i%l])
}
}
func TestAppendBytes(t *testing.T) {
sizes := []int{0, 1, 225, int(tuint32)}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, sz := range sizes {
buf.Reset()
b := RandBytes(sz)
en.WriteBytes(b)
en.Flush()
bts = AppendBytes(b[0:0], b)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for bytes of length %d, encoder wrote %d bytes and append wrote %d bytes", sz, buf.Len(), len(bts))
}
}
}
func benchappendBytes(size uint32, b *testing.B) {
bts := RandBytes(int(size))
buf := make([]byte, 0, len(bts)+5)
b.SetBytes(int64(len(bts) + 5))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendBytes(buf[0:0], bts)
}
}
func BenchmarkAppend16Bytes(b *testing.B) { benchappendBytes(16, b) }
func BenchmarkAppend256Bytes(b *testing.B) { benchappendBytes(256, b) }
func BenchmarkAppend2048Bytes(b *testing.B) { benchappendBytes(2048, b) }
func TestAppendString(t *testing.T) {
sizes := []int{0, 1, 225, int(tuint32)}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, sz := range sizes {
buf.Reset()
s := string(RandBytes(sz))
en.WriteString(s)
en.Flush()
bts = AppendString(bts[0:0], s)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for string of length %d, encoder wrote %d bytes and append wrote %d bytes", sz, buf.Len(), len(bts))
t.Errorf("WriteString prefix: %x", buf.Bytes()[0:5])
t.Errorf("Appendstring prefix: %x", bts[0:5])
}
}
}
func benchappendString(size uint32, b *testing.B) {
str := string(RandBytes(int(size)))
buf := make([]byte, 0, len(str)+5)
b.SetBytes(int64(len(str) + 5))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendString(buf[0:0], str)
}
}
func BenchmarkAppend16String(b *testing.B) { benchappendString(16, b) }
func BenchmarkAppend256String(b *testing.B) { benchappendString(256, b) }
func BenchmarkAppend2048String(b *testing.B) { benchappendString(2048, b) }
func TestAppendBool(t *testing.T) {
vs := []bool{true, false}
var buf bytes.Buffer
en := NewWriter(&buf)
var bts []byte
for _, v := range vs {
buf.Reset()
en.WriteBool(v)
en.Flush()
bts = AppendBool(bts[0:0], v)
if !bytes.Equal(buf.Bytes(), bts) {
t.Errorf("for %t, encoder wrote %q and append wrote %q", v, buf.Bytes(), bts)
}
}
}
func BenchmarkAppendBool(b *testing.B) {
vs := []bool{true, false}
buf := make([]byte, 0, 1)
b.SetBytes(1)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendBool(buf[0:0], vs[i%2])
}
}
func BenchmarkAppendTime(b *testing.B) {
t := time.Now()
b.SetBytes(15)
buf := make([]byte, 0, 15)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendTime(buf[0:0], t)
}
}
func BenchmarkAppendTimeExt(b *testing.B) {
t := time.Now()
buf := make([]byte, 0, 15)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
AppendTimeExt(buf[0:0], t)
}
}
// TestEncodeDecode does a back-and-forth test of encoding and decoding and compare the value with a given output.
func TestEncodeDecode(t *testing.T) {
for _, tc := range []struct {
name string
input interface{}
output interface{}
encodeError string
}{
{
name: "nil",
input: nil,
},
{
name: "bool",
input: true,
},
{
name: "int",
input: int64(42),
},
{
name: "float",
input: 3.14159,
},
{
name: "string",
input: "hello",
},
{
name: "bytes",
input: []byte("hello"),
},
{
name: "array-empty",
input: []interface{}{},
},
{
name: "array",
input: []interface{}{int64(1), int64(2), int64(3)},
},
{
name: "map-empty",
input: map[string]interface{}{},
},
{
name: "map",
input: map[string]interface{}{"a": int64(1), "b": int64(2)},
},
{
name: "map-interface",
input: map[string]interface{}{"a": int64(1), "b": "2"},
},
{
name: "map-string",
input: map[string]string{"a": "1", "b": "2"},
output: map[string]interface{}{"a": "1", "b": "2"},
},
{
name: "map-array",
input: map[string][]int64{"a": {1, 2}, "b": {3}},
output: map[string]interface{}{"a": []interface{}{int64(1), int64(2)}, "b": []interface{}{int64(3)}},
},
{
name: "map-map",
input: map[string]map[string]int64{"a": {"a": 1, "b": 2}, "b": {"c": 3}},
output: map[string]interface{}{"a": map[string]interface{}{"a": int64(1), "b": int64(2)}, "b": map[string]interface{}{"c": int64(3)}},
},
{
name: "array-map",
input: []interface{}{map[string]interface{}{"a": int64(1), "b": "2"}, map[string]int64{"c": 3}},
output: []interface{}{map[string]interface{}{"a": int64(1), "b": "2"}, map[string]interface{}{"c": int64(3)}},
},
{
name: "array-array",
input: []interface{}{[]int64{1, 2}, []interface{}{int64(3)}},
output: []interface{}{[]interface{}{int64(1), int64(2)}, []interface{}{int64(3)}},
},
{
name: "array-array-map",
input: []interface{}{[]interface{}{int64(1), int64(2)}, map[string]interface{}{"c": int64(3)}},
},
{
name: "map-array-map",
input: map[string]interface{}{"a": []interface{}{int64(1), int64(2)}, "b": map[string]interface{}{"c": int64(3)}},
},
{
name: "map-invalid-keys",
input: map[interface{}]interface{}{int64(1): int64(2)},
encodeError: "msgp: map keys must be strings",
},
{
name: "map-nested-invalid-keys",
input: map[string]interface{}{"a": map[int64]string{1: "2"}},
encodeError: "msgp: map keys must be strings",
},
{
name: "invalid-type",
input: struct{}{},
encodeError: "msgp: type \"struct {}\" not supported",
},
} {
t.Run(tc.name, func(t *testing.T) {
// If no output is given, use the input as output
if tc.output == nil {
tc.output = tc.input
}
buf, err := AppendIntf(nil, tc.input)
if tc.encodeError != "" {
if err == nil || !strings.Contains(err.Error(), tc.encodeError) {
t.Fatalf("expected encode error '%s' but got '%s'", tc.encodeError, err)
}
return
}
if tc.encodeError == "" && err != nil {
t.Fatalf("expected no encode error but got '%s'", err.Error())
}
out, _, _ := ReadIntfBytes(buf)
if err != nil {
t.Fatalf("expected no decode error but got '%s'", err.Error())
}
if !reflect.DeepEqual(tc.output, out) {
t.Fatalf("expected '%v' but got '%v'", tc.input, out)
}
})
}
}
|