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

            Assert.AreEqual(source1, "de");

            //Test2
            DictNode dict2 = new DictNode();

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

            Assert.AreEqual(source2, "d3:agei25e4:eyes4:bluee");


            //Test3
            DictNode dh31 = new DictNode();

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

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

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

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

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

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

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

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

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

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

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

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

            Assert.AreEqual(source1, "le");

            //Test2
            ListNode list2 = new ListNode();

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

            Assert.AreEqual(source2, "li1ei2ei3ee");

            //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 = BEncodingFactory.StringEncode(list3);

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

            Assert.AreEqual(source1, "i4e");
            //Assert.AreEqual(ih1.OutputBufferSize, 3);

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

            Assert.AreEqual(source2, "i1234567890e");

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

            Assert.AreEqual(source3, "i0e");

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

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