Reference() static public method

static public Reference ( byte data ) : MemBlock
data byte
return MemBlock
示例#1
0
 public override bool Equals(object a)
 {
     if (a == null)
     {
         //If the other is null, clearly it's not equal to a non-null
         return(false);
     }
     if (this == a)
     {
         //Clearly we are the Equal to ourselves
         return(true);
     }
     if (a is byte[])
     {
         /**
          * @todo
          * This is very questionable to just treat byte[] as MemBlock,
          * because the hashcodes won't be equal, but we have code
          * that does this.  We should remove the assumption that MemBlock
          * can equal a byte[]
          */
         a = MemBlock.Reference((byte[])a);
     }
     if (this.GetHashCode() != a.GetHashCode())
     {
         //Hashcodes must be equal for the objects to be equal
         return(false);
     }
     else
     {
         return(this.CompareTo(a) == 0);
     }
 }
示例#2
0
        /**
         * Shorter MemBlocks are less than longer ones.  MemBlocks of identical
         * length are compared from first byte to last byte.  The first byte
         * that differs is compared to get the result of the function
         */
        public int CompareTo(object o)
        {
            if (this == o)
            {
                return(0);
            }
            MemBlock other = o as MemBlock;

            if (other == null)
            {
                byte[] data = o as byte[];
                if (data != null)
                {
                    other = MemBlock.Reference(data);
                }
                else
                {
                    //Put us ahead of all other types, this might not be smart
                    return(-1);
                }
            }
            int t_l = this.Length;
            int o_l = other.Length;

            if (t_l == o_l)
            {
                for (int i = 0; i < t_l; i++)
                {
                    byte t_b = this._buffer[this._offset + i];
                    byte o_b = other._buffer[other._offset + i];
                    if (t_b != o_b)
                    {
                        //OKAY! They are different:
                        if (t_b < o_b)
                        {
                            return(-1);
                        }
                        else
                        {
                            return(1);
                        }
                    }
                    else
                    {
                        //This position is equal, go to the next
                    }
                }
                //We must be equal
                return(0);
            }
            else if (t_l < o_l)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }
示例#3
0
        public void StringTests()
        {
            string   test_ascii = "Hello simple test";
            MemBlock b          = MemBlock.Reference(System.Text.Encoding.UTF8.GetBytes(test_ascii));

            Assert.IsTrue(b.IsAscii(0, b.Length), "IsAscii");
            string test_unicode = "la\u00dfen";

            b = MemBlock.Reference(System.Text.Encoding.UTF8.GetBytes(test_unicode));
            Assert.IsFalse(b.IsAscii(0, b.Length), "Unicode not ascii");
        }
示例#4
0
        public void SomeInsanityTests()
        {
            byte[]   data;
            bool     got_x;
            MemBlock b;

            System.Random r = new System.Random();
            for (int i = 0; i < 100; i++)
            {
                int size = r.Next(1024);
                data = new byte[size];
                r.NextBytes(data);
                int overshoot = r.Next(1, 1024);
                got_x = false;
                b     = null;
                try {
                    //Should throw an exception:
                    b = MemBlock.Reference(data, 0, size + overshoot);
                }
                catch {
                    got_x = true;
                }
                Assert.IsNull(b, "Reference failure test");
                Assert.IsTrue(got_x, "Exception catch test");

                overshoot = r.Next(1, 1024);
                got_x     = false;
                b         = MemBlock.Reference(data);
                try {
                    //Should throw an exception:
                    byte tmp = b[size + overshoot];
                }
                catch {
                    got_x = true;
                }
                Assert.IsTrue(got_x, "index out of range exception");
                got_x = false;
                try {
                    //Should throw an exception:
                    byte tmp = b[b.Length];
                }
                catch {
                    got_x = true;
                }
                Assert.IsTrue(got_x, "index out of range exception");
            }
        }
示例#5
0
 public MemBlock ToMemBlock()
 {
     if (_raw_data != null)
     {
         return(_raw_data);
     }
     //Else make it:
     if (IsValidNumeric(_type_num))
     {
         byte[] buf = new byte[1];
         buf[0]    = (byte)_type_num;
         _raw_data = MemBlock.Reference(buf);
     }
     else
     {
         //It's a string type:
         int    l   = NumberSerializer.GetByteCount(_string_rep);
         byte[] buf = new byte[l];
         NumberSerializer.WriteString(_string_rep, buf, 0);
         _raw_data = MemBlock.Reference(buf);
     }
     return(_raw_data);
 }
示例#6
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");
        }
示例#7
0
        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 })));
        }