]> Sergey Matveev's repositories - btrtrc.git/blob - bencode/encode_test.go
Initial commit.
[btrtrc.git] / bencode / encode_test.go
1 package bencode
2
3 import "testing"
4 import "bytes"
5
6 type random_encode_test struct {
7         value    interface{}
8         expected string
9 }
10
11 type random_struct struct {
12         ABC         int    `bencode:"abc"`
13         SkipThisOne string `bencode:"-"`
14         CDE         string
15 }
16
17 var random_encode_tests = []random_encode_test{
18         {int(10), "i10e"},
19         {uint(10), "i10e"},
20         {"hello, world", "12:hello, world"},
21         {true, "i1e"},
22         {false, "i0e"},
23         {int8(-8), "i-8e"},
24         {int16(-16), "i-16e"},
25         {int32(32), "i32e"},
26         {int64(-64), "i-64e"},
27         {uint8(8), "i8e"},
28         {uint16(16), "i16e"},
29         {uint32(32), "i32e"},
30         {uint64(64), "i64e"},
31         {random_struct{123, "nono", "hello"}, "d3:CDE5:hello3:abci123ee"},
32         {map[string]string{"a": "b", "c": "d"}, "d1:a1:b1:c1:de"},
33         {[]byte{1, 2, 3, 4}, "4:\x01\x02\x03\x04"},
34         {[4]byte{1, 2, 3, 4}, "li1ei2ei3ei4ee"},
35         {nil, ""},
36         {[]byte{}, "0:"},
37         {"", "0:"},
38         {[]int{}, "le"},
39         {map[string]int{}, "de"},
40 }
41
42 func TestRandomEncode(t *testing.T) {
43         for _, test := range random_encode_tests {
44                 data, err := Marshal(test.value)
45                 if err != nil {
46                         t.Fatal(err)
47                 }
48                 if !bytes.Equal(data, []byte(test.expected)) {
49                         t.Errorf("got: %s, expected: %s\n",
50                                 string(data), string(test.expected))
51                 }
52         }
53 }