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
|
// Copyright 2012 Google, Inc. All rights reserved.
//
// Use of this source code is governed by a BSD-style license
// that can be found in the LICENSE file in the root of the source
// tree.
package gopacket
import (
"fmt"
"testing"
)
func TestExponentialSizeIncreasePrepend(t *testing.T) {
var b serializeBuffer
for i, test := range []struct {
prepend, size int
}{
{2, 2},
{2, 4},
{2, 8},
{2, 8},
{2, 16},
{2, 16},
{2, 16},
{2, 16},
{2, 32},
} {
b.PrependBytes(test.prepend)
if test.size != cap(b.data) {
t.Error(i, "size want", test.size, "got", cap(b.data))
}
}
b.Clear()
if b.start != 32 {
t.Error(b.start)
}
}
func TestExponentialSizeIncreaseAppend(t *testing.T) {
var b serializeBuffer
for i, test := range []struct {
appnd, size int
}{
{2, 2},
{2, 4},
{2, 8},
{2, 8},
{2, 16},
{2, 16},
{2, 16},
{2, 16},
{2, 32},
} {
b.AppendBytes(test.appnd)
if test.size != cap(b.data) {
t.Error(i, "size want", test.size, "got", cap(b.data))
}
}
b.Clear()
if b.start != 0 {
t.Error(b.start)
}
}
func ExampleSerializeBuffer() {
b := NewSerializeBuffer()
fmt.Println("1:", b.Bytes())
bytes, _ := b.PrependBytes(3)
copy(bytes, []byte{1, 2, 3})
fmt.Println("2:", b.Bytes())
bytes, _ = b.AppendBytes(2)
copy(bytes, []byte{4, 5})
fmt.Println("3:", b.Bytes())
bytes, _ = b.PrependBytes(1)
copy(bytes, []byte{0})
fmt.Println("4:", b.Bytes())
bytes, _ = b.AppendBytes(3)
copy(bytes, []byte{6, 7, 8})
fmt.Println("5:", b.Bytes())
b.Clear()
fmt.Println("6:", b.Bytes())
bytes, _ = b.PrependBytes(2)
copy(bytes, []byte{9, 9})
fmt.Println("7:", b.Bytes())
// Output:
// 1: []
// 2: [1 2 3]
// 3: [1 2 3 4 5]
// 4: [0 1 2 3 4 5]
// 5: [0 1 2 3 4 5 6 7 8]
// 6: []
// 7: [9 9]
}
|