ODPM 266: Go-libmemif + 2 examples.
[govpp.git] / vendor / github.com / google / gopacket / writer_test.go
1 // Copyright 2012 Google, Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree.
6
7 package gopacket
8
9 import (
10         "fmt"
11         "testing"
12 )
13
14 func TestExponentialSizeIncreasePrepend(t *testing.T) {
15         var b serializeBuffer
16         for i, test := range []struct {
17                 prepend, size int
18         }{
19                 {2, 2},
20                 {2, 4},
21                 {2, 8},
22                 {2, 8},
23                 {2, 16},
24                 {2, 16},
25                 {2, 16},
26                 {2, 16},
27                 {2, 32},
28         } {
29                 b.PrependBytes(test.prepend)
30                 if test.size != cap(b.data) {
31                         t.Error(i, "size want", test.size, "got", cap(b.data))
32                 }
33         }
34         b.Clear()
35         if b.start != 32 {
36                 t.Error(b.start)
37         }
38 }
39
40 func TestExponentialSizeIncreaseAppend(t *testing.T) {
41         var b serializeBuffer
42         for i, test := range []struct {
43                 appnd, size int
44         }{
45                 {2, 2},
46                 {2, 4},
47                 {2, 8},
48                 {2, 8},
49                 {2, 16},
50                 {2, 16},
51                 {2, 16},
52                 {2, 16},
53                 {2, 32},
54         } {
55                 b.AppendBytes(test.appnd)
56                 if test.size != cap(b.data) {
57                         t.Error(i, "size want", test.size, "got", cap(b.data))
58                 }
59         }
60         b.Clear()
61         if b.start != 0 {
62                 t.Error(b.start)
63         }
64 }
65
66 func ExampleSerializeBuffer() {
67         b := NewSerializeBuffer()
68         fmt.Println("1:", b.Bytes())
69         bytes, _ := b.PrependBytes(3)
70         copy(bytes, []byte{1, 2, 3})
71         fmt.Println("2:", b.Bytes())
72         bytes, _ = b.AppendBytes(2)
73         copy(bytes, []byte{4, 5})
74         fmt.Println("3:", b.Bytes())
75         bytes, _ = b.PrependBytes(1)
76         copy(bytes, []byte{0})
77         fmt.Println("4:", b.Bytes())
78         bytes, _ = b.AppendBytes(3)
79         copy(bytes, []byte{6, 7, 8})
80         fmt.Println("5:", b.Bytes())
81         b.Clear()
82         fmt.Println("6:", b.Bytes())
83         bytes, _ = b.PrependBytes(2)
84         copy(bytes, []byte{9, 9})
85         fmt.Println("7:", b.Bytes())
86         // Output:
87         // 1: []
88         // 2: [1 2 3]
89         // 3: [1 2 3 4 5]
90         // 4: [0 1 2 3 4 5]
91         // 5: [0 1 2 3 4 5 6 7 8]
92         // 6: []
93         // 7: [9 9]
94 }