6 type random_decode_test struct {
11 var random_decode_tests = []random_decode_test{
13 {"i-9223372036854775808e", int64(-9223372036854775808)},
15 {"29:unicode test проверка", "unicode test проверка"},
16 {"d1:ai5e1:b5:helloe", map[string]interface{}{"a": int64(5), "b": "hello"}},
17 {"li5ei10ei15ei20e7:bencodee",
18 []interface{}{int64(5), int64(10), int64(15), int64(20), "bencode"}},
19 {"ldedee", []interface{}{map[string]interface{}{}, map[string]interface{}{}}},
20 {"le", []interface{}{}},
23 func TestRandomDecode(t *testing.T) {
24 for _, test := range random_decode_tests {
26 err := Unmarshal([]byte(test.data), &value)
31 if !reflect.DeepEqual(test.expected, value) {
32 t.Errorf("got: %v (%T), expected: %v (%T)\n",
33 value, value, test.expected, test.expected)
38 func check_error(t *testing.T, err error) {
44 func assert_equal(t *testing.T, x, y interface{}) {
45 if !reflect.DeepEqual(x, y) {
46 t.Errorf("got: %v (%T), expected: %v (%T)\n", x, x, y, y)
50 type unmarshaler_int struct {
54 func (this *unmarshaler_int) UnmarshalBencode(data []byte) error {
55 return Unmarshal(data, &this.x)
58 type unmarshaler_string struct {
62 func (this *unmarshaler_string) UnmarshalBencode(data []byte) error {
67 func TestUnmarshalerBencode(t *testing.T) {
69 var ss []unmarshaler_string
70 check_error(t, Unmarshal([]byte("i71e"), &i))
71 assert_equal(t, i.x, 71)
72 check_error(t, Unmarshal([]byte("l5:hello5:fruit3:waye"), &ss))
73 assert_equal(t, ss[0].x, "5:hello")
74 assert_equal(t, ss[1].x, "5:fruit")
75 assert_equal(t, ss[2].x, "3:way")