2016-08-03 09:11:52 +02:00
|
|
|
package bencode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2017-12-29 23:44:45 +01:00
|
|
|
"fmt"
|
2016-08-03 09:11:52 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-02-15 06:57:36 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var marshalTests = []struct {
|
|
|
|
input interface{}
|
|
|
|
expected []string
|
|
|
|
}{
|
|
|
|
{int(42), []string{"i42e"}},
|
|
|
|
{int(-42), []string{"i-42e"}},
|
|
|
|
{uint(43), []string{"i43e"}},
|
|
|
|
{int64(44), []string{"i44e"}},
|
|
|
|
{uint64(45), []string{"i45e"}},
|
|
|
|
{int16(44), []string{"i44e"}},
|
|
|
|
{uint16(45), []string{"i45e"}},
|
|
|
|
|
|
|
|
{"example", []string{"7:example"}},
|
|
|
|
{[]byte("example"), []string{"7:example"}},
|
|
|
|
{30 * time.Minute, []string{"i1800e"}},
|
|
|
|
|
|
|
|
{[]string{"one", "two"}, []string{"l3:one3:twoe", "l3:two3:onee"}},
|
|
|
|
{[]interface{}{"one", "two"}, []string{"l3:one3:twoe", "l3:two3:onee"}},
|
|
|
|
{[]string{}, []string{"le"}},
|
|
|
|
|
|
|
|
{map[string]interface{}{"one": "aa", "two": "bb"}, []string{"d3:one2:aa3:two2:bbe", "d3:two2:bb3:one2:aae"}},
|
|
|
|
{map[string]interface{}{}, []string{"de"}},
|
2016-10-01 14:06:42 +02:00
|
|
|
|
|
|
|
{[]Dict{{"a": "b"}, {"c": "d"}}, []string{"ld1:a1:bed1:c1:dee", "ld1:c1:ded1:a1:bee"}},
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestMarshal(t *testing.T) {
|
2017-12-29 23:44:45 +01:00
|
|
|
for _, tt := range marshalTests {
|
|
|
|
t.Run(fmt.Sprintf("%#v", tt.input), func(t *testing.T) {
|
|
|
|
got, err := Marshal(tt.input)
|
|
|
|
require.Nil(t, err, "marshal should not fail")
|
|
|
|
require.Contains(t, tt.expected, string(got), "the marshaled result should be one of the expected permutations")
|
|
|
|
})
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|