コード例 #1
0
ファイル: MemBlock.cs プロジェクト: kingctan/brunet
 /**
  * convert the MemBlock to base32 with padding
  */
 public string ToBase32String()
 {
     return(Base32.Encode(_buffer, _offset, _length, true));
 }
コード例 #2
0
        public void Test()
        {
            System.Random r = new System.Random();
            //Test numeric type codes:
            for (int i = 1; i < 32; i++)
            {
                PType    p = new PType(i);
                MemBlock b = p.ToMemBlock();

                byte[] buf = new byte[100];
                r.NextBytes(buf); //Get some junk:
                MemBlock junk = MemBlock.Reference(buf);
                MemBlock b1   = MemBlock.Concat(b, junk);
                MemBlock rest = null;
                PType    pp   = PType.Parse(b1, out rest);

                byte[] buf2 = new byte[1];
                buf2[0] = (byte)i;
                MemBlock b2 = MemBlock.Reference(buf2);

                Assert.AreEqual(p, pp, System.String.Format("Round trip int: {0}", i));
                Assert.AreEqual(b, b2, System.String.Format("Convert to MemBlock int: {0}", i));
                Assert.AreEqual(i, pp.TypeNumber, "Typenumber equality");
                Assert.AreEqual(rest, junk, "rest in int PType");
            }

            //Test string types:
            for (int i = 0; i < 1000; i++)
            {
                //Make a random string:
                //
                byte[] buf = new byte[r.Next(1, 100)];
                r.NextBytes(buf);
                string s  = Base32.Encode(buf);
                PType  p1 = new PType(s);
                r.NextBytes(buf); //Get some junk:
                MemBlock b       = MemBlock.Copy(buf);
                MemBlock combine = MemBlock.Concat(p1.ToMemBlock(), b);
                MemBlock b2      = null;
                PType    p2      = PType.Parse(combine, out b2);

                Assert.AreEqual(p1, p2, "Round trip string: " + s);
                Assert.AreEqual(b, b2, "Round trip rest");
                Assert.AreEqual(s, p2.ToString(), "Round trip to string");
                Assert.AreEqual(s, p1.ToString(), "Round trip to string");
                Assert.AreEqual(p1.TypeNumber, p2.TypeNumber, "RT: TypeNumber test");
            }
            //Test all one byte ascii strings:
            for (byte b = 32; b < ASCII_UPPER_BOUND; b++)
            {
                MemBlock raw = MemBlock.Reference(new byte[] { b, 0 });
                MemBlock rest;
                PType    p1 = PType.Parse(raw, out rest);
                Assert.AreEqual(rest, MemBlock.Null, "Rest is null");
                PType p2 = PType.Parse(raw, out rest);
                Assert.AreEqual(rest, MemBlock.Null, "Rest is null");
                Assert.IsTrue(p1 == p2, "reference equality of single byte type");
                Assert.AreEqual(p1, p2, "equality of single byte type");
                Assert.AreEqual(p1, new PType(p1.ToString()), "Round trip string");
            }
            //Test TypeNumber of string types:
            for (int i = 0; i < 100; i++)
            {
                byte[] buf = new byte[20];
                r.NextBytes(buf);
                for (int j = 1; j < 4; j++)
                {
                    string s    = Base32.Encode(buf).Substring(0, j);
                    PType  p1   = new PType(s);
                    byte[] buf2 = System.Text.Encoding.UTF8.GetBytes(s);
                    int    t    = 0;
                    for (int k = 0; k < buf2.Length; k++)
                    {
                        t   = t | buf2[k];
                        t <<= 8;
                    }
                    Assert.AreEqual(t, p1.TypeNumber, System.String.Format("String type number: {0}, s={1}", t, s));
                }
            }
            //Console.Error.WriteLine("Tested PType");
        }
コード例 #3
0
ファイル: NumberSerializer.cs プロジェクト: pcbing/brunet
        public void Test()
        {
            //8 is long enough to hold all the numbers:
            byte[]        buffer = new byte[8];
            System.Random r      = new System.Random();
            int           tests  = 100;

            //Here are shorts:
            for (int i = 0; i < tests; i++)
            {
                short val = (short)r.Next(Int16.MaxValue);
                WriteShort(val, buffer, 0);
                Assert.AreEqual(val, ReadShort(buffer, 0));
            }
            //Ints:
            for (int i = 0; i < tests; i++)
            {
                int val = r.Next();
                WriteInt(val, buffer, 0);
                Assert.AreEqual(val, ReadInt(buffer, 0));
            }
            //Longs:
            for (int i = 0; i < tests; i++)
            {
                long val = r.Next();
                val <<= 4;
                val  |= r.Next();
                WriteLong(val, buffer, 0);
                Assert.AreEqual(val, ReadLong(buffer, 0));
            }
            //Floats:
            for (int i = 0; i < tests; i++)
            {
                float val = (float)r.NextDouble();
                WriteFloat(val, buffer, 0);
                Assert.AreEqual(val, ReadFloat(buffer, 0));
            }
            //Doubles:
            for (int i = 0; i < tests; i++)
            {
                double val = r.NextDouble();
                WriteDouble(val, buffer, 0);
                Assert.AreEqual(val, ReadDouble(buffer, 0));
            }
            //Strings:
            for (int i = 0; i < tests; i++)
            {
                byte[] bin = new byte[1000];
                r.NextBytes(bin);
                //Here's a random ascii string:
                string s = Base32.Encode(bin);
                if (i % 2 == 0)
                {
                    //Half the time make sure there is some unicode bit:
                    s = s + "la\u00dfen";
                }
                byte[] enc      = Encoding.UTF8.GetBytes(s);
                byte[] enc_null = new byte[enc.Length + 1];
                Array.Copy(enc, 0, enc_null, 0, enc.Length);
                enc_null[enc.Length] = 0;
                int    l;
                string s2 = ReadString(enc_null, 0, out l);
                Assert.AreEqual(s, s2, "byte[] readstring");
                Assert.AreEqual(l, enc_null.Length, "byte[] string length");
                string s3 = ReadString(MemBlock.Reference(enc_null), 0, out l);
                Assert.AreEqual(s, s3, "byte[] readstring");
                Assert.AreEqual(l, enc_null.Length, "byte[] string length");
            }

            /*
             * Round tripping is great, but we still might have some
             * brain damage: we could be cancel out the error when reading
             * the data back in.
             */
            Assert.AreEqual((short)511, ReadShort(new byte[] { 1, 255 }, 0));
            Assert.AreEqual((short)511,
                            ReadShort(new MemoryStream(new byte[] { 1, 255 })));
            Assert.AreEqual((short)1535, ReadShort(new byte[] { 5, 255 }, 0));
            Assert.AreEqual((short)1535,
                            ReadShort(new MemoryStream(new byte[] { 5, 255 })));
            Assert.AreEqual(43046721,
                            ReadInt(new byte[] { 2, 144, 215, 65 }, 0));
            Assert.AreEqual(43046721,
                            ReadInt(new MemoryStream(new byte[] { 2, 144, 215, 65 })));
            Assert.AreEqual(59296646043258912L,
                            ReadLong(new byte[] { 0, 210, 169, 252, 67, 199, 208, 32 }, 0));
            Assert.AreEqual(59296646043258912L,
                            ReadLong(new MemoryStream(new byte[] { 0, 210, 169, 252, 67, 199, 208, 32 })));
        }