This repository was archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathencoder_test.go
More file actions
72 lines (60 loc) · 1.44 KB
/
encoder_test.go
File metadata and controls
72 lines (60 loc) · 1.44 KB
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
// Copyright 2015 The Chihaya Authors. All rights reserved.
// Use of this source code is governed by the BSD 2-Clause license,
// which can be found in the LICENSE file.
package bencode
import (
"bytes"
"testing"
"time"
)
var marshalTests = []struct {
input interface{}
expected string
}{
{int(42), "i42e"},
{int(-42), "i-42e"},
{uint(43), "i43e"},
{int64(44), "i44e"},
{uint64(45), "i45e"},
{int16(44), "i44e"},
{uint16(45), "i45e"},
{"example", "7:example"},
{[]byte("example"), "7:example"},
{30 * time.Minute, "i1800e"},
{[]string{"one", "two"}, "l3:one3:twoe"},
{[]interface{}{"one", "two"}, "l3:one3:twoe"},
{[]string{}, "le"},
{map[string]interface{}{"one": "aa", "two": "bb"}, "d3:one2:aa3:two2:bbe"},
{map[string]interface{}{}, "de"},
}
func TestMarshal(t *testing.T) {
for _, test := range marshalTests {
got, err := Marshal(test.input)
if err != nil {
t.Error(err)
} else if string(got) != test.expected {
t.Errorf("\ngot: %s\nexpected: %s", got, test.expected)
}
}
}
func BenchmarkMarshalScalar(b *testing.B) {
buf := &bytes.Buffer{}
encoder := NewEncoder(buf)
for i := 0; i < b.N; i++ {
encoder.Encode("test")
encoder.Encode(123)
}
}
func BenchmarkMarshalLarge(b *testing.B) {
data := map[string]interface{}{
"k1": []string{"a", "b", "c"},
"k2": 42,
"k3": "val",
"k4": uint(42),
}
buf := &bytes.Buffer{}
encoder := NewEncoder(buf)
for i := 0; i < b.N; i++ {
encoder.Encode(data)
}
}