2016-08-03 09:11:52 +02:00
|
|
|
package bencode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2017-02-15 06:57:36 +01:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-08-03 09:11:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
var unmarshalTests = []struct {
|
|
|
|
input string
|
|
|
|
expected interface{}
|
|
|
|
}{
|
|
|
|
{"i42e", int64(42)},
|
|
|
|
{"i-42e", int64(-42)},
|
|
|
|
|
|
|
|
{"7:example", "example"},
|
|
|
|
|
|
|
|
{"l3:one3:twoe", List{"one", "two"}},
|
|
|
|
{"le", List{}},
|
|
|
|
|
|
|
|
{"d3:one2:aa3:two2:bbe", Dict{"one": "aa", "two": "bb"}},
|
|
|
|
{"de", Dict{}},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshal(t *testing.T) {
|
|
|
|
for _, tt := range unmarshalTests {
|
2017-12-29 23:44:45 +01:00
|
|
|
t.Run(tt.input, func(t *testing.T) {
|
|
|
|
got, err := Unmarshal([]byte(tt.input))
|
|
|
|
require.Nil(t, err, "unmarshal should not fail")
|
|
|
|
require.Equal(t, got, tt.expected, "unmarshalled values should match the expected results")
|
|
|
|
})
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bufferLoop struct {
|
|
|
|
val string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *bufferLoop) Read(b []byte) (int, error) {
|
|
|
|
n := copy(b, r.val)
|
|
|
|
return n, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUnmarshalScalar(b *testing.B) {
|
|
|
|
d1 := NewDecoder(&bufferLoop{"7:example"})
|
|
|
|
d2 := NewDecoder(&bufferLoop{"i42e"})
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-15 19:31:14 +01:00
|
|
|
_, _ = d1.Decode()
|
|
|
|
_, _ = d2.Decode()
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestUnmarshalLarge(t *testing.T) {
|
|
|
|
data := Dict{
|
|
|
|
"k1": List{"a", "b", "c"},
|
|
|
|
"k2": int64(42),
|
|
|
|
"k3": "val",
|
|
|
|
"k4": int64(-42),
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, _ := Marshal(data)
|
|
|
|
dec := NewDecoder(&bufferLoop{string(buf)})
|
|
|
|
|
|
|
|
got, err := dec.Decode()
|
2017-02-15 06:57:36 +01:00
|
|
|
require.Nil(t, err, "decode should not fail")
|
|
|
|
require.Equal(t, got, data, "encoding and decoding should equal the original value")
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkUnmarshalLarge(b *testing.B) {
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"k1": []string{"a", "b", "c"},
|
|
|
|
"k2": 42,
|
|
|
|
"k3": "val",
|
|
|
|
"k4": uint(42),
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, _ := Marshal(data)
|
|
|
|
dec := NewDecoder(&bufferLoop{string(buf)})
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
2022-01-15 19:31:14 +01:00
|
|
|
_, _ = dec.Decode()
|
2016-08-03 09:11:52 +02:00
|
|
|
}
|
|
|
|
}
|