Пример #1
0
        public void TestEncodeDictionary1()
        {
            //Test1
            DictNode dict1   = new DictNode();
            string   source1 = BEncode.StringEncode(dict1);

            Assert.AreEqual(source1, "[]");

            //Test2
            DictNode dict2 = new DictNode();

            dict2.Add("age", 25);
            dict2.Add("eyes", "blue");
            string source2 = BEncode.StringEncode(dict2);

            Assert.AreEqual(source2, "[3:agei25e4:eyes4:blue]");


            //Test3
            DictNode dh31 = new DictNode();

            dh31.Add(Encoding.Default.GetBytes("author"), "Alice");
            dh31.Add("length", 1048576);
            DictNode dict3 = new DictNode();

            dict3.Add("spam.mp3", dh31);
            string source3 = BEncode.StringEncode(dict3);

            Assert.AreEqual(source3, "[8:spam.mp3[6:author5:Alice6:lengthi1048576e]]");
            Assert.AreEqual(dict3.ToString(), "[8:spam.mp3[6:author5:Alice6:lengthi1048576e]]");
        }
Пример #2
0
        public void TestEncodeByteArray1()
        {
            //Test1标点符号
            BytesNode bah1    = new BytesNode("~!@#$%^&*()_+|`-=\\{}:\"<>?[];',./");
            string    source1 = BEncode.StringEncode(bah1);

            Assert.AreEqual("32:~!@#$%^&*()_+|`-=\\{}:\"<>?[];',./", source1);

            //Test2空字符
            BytesNode bah2    = new BytesNode("");
            string    source2 = BEncode.StringEncode(bah2);

            Assert.AreEqual(source2, "0:");

            //Test3英文字母与数字
            BytesNode bah3    = new BytesNode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
            string    source3 = BEncode.StringEncode(bah3);

            Assert.AreEqual(source3, "62:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");

            //Test4中文字体与全角标点符号
            BytesNode bah4    = new BytesNode("微软公司,广州大学");
            string    source4 = BEncode.StringEncode(bah4);

            Assert.AreEqual("18:微软公司,广州大学", source4);

            //Test5全角的数字与英文字母
            BytesNode bah5    = new BytesNode("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
            string    source5 = BEncode.StringEncode(bah5);

            Assert.AreEqual("124:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", source5);
        }
Пример #3
0
        public void TestEncodeList1()
        {
            //Test1
            ListNode list1   = new ListNode();
            string   source1 = BEncode.StringEncode(list1);

            Assert.AreEqual("{}", source1);

            //Test2
            ListNode list2 = new ListNode();

            for (int i = 1; i <= 3; i++)
            {
                list2.Add(i);
            }
            string source2 = BEncode.StringEncode(list2);

            Assert.AreEqual(source2, "{i1ei2ei3e}");

            //Test3
            ListNode lh31 = new ListNode();

            lh31.Add("Alice");
            lh31.Add("Bob");
            ListNode lh32 = new ListNode();

            lh32.Add(2);
            lh32.Add(3);
            ListNode list3   = new ListNode(new List <BEncodedNode>(new BEncodedNode[] { lh31, lh32 }));
            string   source3 = BEncode.StringEncode(list3);

            Assert.AreEqual(source3, "{{5:Alice3:Bob}{i2ei3e}}");
        }
Пример #4
0
        public void TestEncodeInteger1()
        {
            //Test1测试用例为4
            IntNode ih1     = new IntNode(4);
            string  source1 = BEncode.StringEncode(ih1);

            Assert.AreEqual(source1, "i4e");

            //Test2测试用例为1234567890
            IntNode ih2     = new IntNode(1234567890);
            string  source2 = BEncode.StringEncode(ih2);

            Assert.AreEqual(source2, "i1234567890e");

            //Test3测试用例为0
            IntNode ih3     = new IntNode(0);
            string  source3 = BEncode.StringEncode(ih3);

            Assert.AreEqual(source3, "i0e");

            //Test4测试用例为-10
            IntNode ih4     = new IntNode(-10);
            string  source4 = BEncode.StringEncode(ih4);

            Assert.AreEqual(source4, "i-10e");
        }