Esempio n. 1
0
        public void ReadTo()
        {
            var buffer = new BufferReaderWriter(new byte[10], 0, 0, false);

            buffer.WriteByte(1);
            buffer.Seek(0, SeekOrigin.End);
            buffer.WriteByte(2);
            buffer.Seek(0, SeekOrigin.Begin);

            var arr = new byte[10];

            buffer.ReadTo(arr, 0, 10);
            Assert.Equal(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 2 }, arr);

            Assert.Throws <InvalidOperationException>(() => buffer.ReadTo(arr, -1, 0));
            Assert.Throws <InvalidOperationException>(() => buffer.ReadTo(arr, 0, -1));

            arr = new byte[9];
            Assert.Throws <InvalidOperationException>(() => buffer.ReadTo(arr, 0, 10));

            arr = new byte[12];
            buffer.Seek(0, SeekOrigin.Begin);
            buffer.ReadTo(arr, 2, 10);
            Assert.Equal(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2 }, arr);
        }
Esempio n. 2
0
        public void CopyTo()
        {
            var buffer = new BufferReaderWriter(new byte[10], 0, 0, false);

            buffer.WriteByte(1);
            buffer.Seek(0, SeekOrigin.End);
            buffer.WriteByte(2);

            var arr = new byte[10];

            buffer.CopyTo(arr, 0);
            Assert.Equal(new byte[] { 1, 0, 0, 0, 0, 0, 0, 0, 0, 2 }, arr);

            Assert.Throws <InvalidOperationException>(() => buffer.CopyTo(arr, -1));

            arr = new byte[9];
            Assert.Throws <InvalidOperationException>(() => buffer.CopyTo(arr, 0));

            arr = new byte[12];
            buffer.CopyTo(arr, 2);
            Assert.Equal(new byte[] { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2 }, arr);

            buffer = new BufferReaderWriter(new byte[10] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10
            });
            arr = new byte[8];
            buffer.CopyTo(arr, 0, 2);
            Assert.Equal(new byte[] { 3, 4, 5, 6, 7, 8, 9, 10 }, arr);
        }
Esempio n. 3
0
        public void ResetLength()
        {
            var buffer = new BufferReaderWriter(new byte[10], 0, 0, false);

            Assert.Equal(10, buffer.Capacity);
            Assert.Equal(0, buffer.Length);
            Assert.Equal(0, buffer.Index);

            buffer.WriteInt(1);
            buffer.WriteInt(2);
            Assert.Equal(10, buffer.Capacity);
            Assert.Equal(8, buffer.Length);
            Assert.Equal(8, buffer.Index);

            buffer.ResetLength();
            Assert.Equal(10, buffer.Capacity);
            Assert.Equal(0, buffer.Length);
            Assert.Equal(0, buffer.Index);

            buffer.WriteInt(1);
            buffer.WriteInt(2);
            buffer.WriteInt(3);
            Assert.Equal(10 + 256, buffer.Capacity);
            Assert.Equal(12, buffer.Length);
            Assert.Equal(12, buffer.Index);

            buffer.ResetLength();
            Assert.Equal(10 + 256, buffer.Capacity);
            Assert.Equal(0, buffer.Length);
            Assert.Equal(0, buffer.Index);
        }
Esempio n. 4
0
        public void LengthExtension()
        {
            var buffer = new BufferReaderWriter(100);

            buffer.WriteInt(0x01020304);
            Assert.Equal(4, buffer.Length);

            buffer.Seek(2, SeekOrigin.Begin);
            buffer.WriteByte(1);
            Assert.Equal(4, buffer.Length);
            Assert.Equal(new byte[] { 1, 2, 1, 4 }, buffer.Copy());

            buffer.Seek(3, SeekOrigin.Begin);
            buffer.WriteShort(0x0104);
            Assert.Equal(5, buffer.Length);
            Assert.Equal(new byte[] { 1, 2, 1, 1, 4 }, buffer.Copy());

            buffer.Seek(5, SeekOrigin.Begin);
            buffer.Write(new byte[] { 0x05, 0x06, 0x07 });
            Assert.Equal(8, buffer.Length);
            Assert.Equal(new byte[] { 1, 2, 1, 1, 4, 5, 6, 7 }, buffer.Copy());

            buffer.Seek(10, SeekOrigin.Begin);
            buffer.WriteByte(1);
            Assert.Equal(11, buffer.Length);
            Assert.Equal(new byte[] { 1, 2, 1, 1, 4, 5, 6, 7, 0, 0, 1 }, buffer.Copy());
        }
Esempio n. 5
0
        public void ReadingLE()
        {
            var buffer = new BufferReaderWriter(31);

            buffer.Endianness = Endianness.LittleEndian;

            buffer.WriteByte(0x42);
            buffer.WriteShort(0x4243);
            buffer.WriteInt(0x42434445);
            buffer.WriteLong(0x4243444546474849);
            buffer.WriteFloat(42.43f);
            buffer.WriteDouble(42.43);
            buffer.Write(new byte[] { 0x42, 0x43, 0x44, 0x45 });

            buffer.Seek(0, SeekOrigin.Begin);

            Assert.Equal(0x42, buffer.ReadByte());
            Assert.Equal(0x4243, buffer.ReadShort());
            Assert.Equal(0x42434445, buffer.ReadInt());
            Assert.Equal(0x4243444546474849, buffer.ReadLong());
            Assert.Equal(42.43f, buffer.ReadFloat());
            Assert.Equal(42.43, buffer.ReadDouble());
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45 }, buffer.Read(4));

            Assert.Throws <InvalidOperationException>(() => { buffer.Read(1); });

            buffer            = new BufferReaderWriter(Hex.ToByteArray("57 80 06 00 00 00 DB 9B D2 9A D2 F9"));
            buffer.Endianness = Endianness.LittleEndian;

            Assert.Equal(0x8057u, buffer.ReadUShort());
            Assert.Equal(0x00000006u, buffer.ReadUInt());
            Assert.Equal(0x9BDBu, buffer.ReadUShort());
            Assert.Equal(0xF9D29AD2u, buffer.ReadUInt());
        }
Esempio n. 6
0
        public void Writing()
        {
            var buffer = new BufferReaderWriter();

            buffer.WriteByte(0x42);
            Assert.Equal(new byte[] { 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter();
            buffer.WriteShort(0x4243);
            Assert.Equal(new byte[] { 0x42, 0x43 }, buffer.Copy());

            buffer = new BufferReaderWriter();
            buffer.WriteInt(0x42434445);
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45 }, buffer.Copy());

            buffer = new BufferReaderWriter();
            buffer.WriteLong(0x4243444546474849);
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49 }, buffer.Copy());

            buffer = new BufferReaderWriter();
            buffer.WriteFloat(42.43f);
            Assert.Equal(new byte[] { 0x52, 0xB8, 0x29, 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter();
            buffer.WriteDouble(42.43);
            Assert.Equal(new byte[] { 0xD7, 0xA3, 0x70, 0x3D, 0x0A, 0x37, 0x45, 0x40 }, buffer.Copy());

            buffer = new BufferReaderWriter();
            buffer.Write(new byte[] { 0x42, 0x43, 0x44, 0x45 });
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45 }, buffer.Copy());
        }
Esempio n. 7
0
        public void Reading()
        {
            var buffer = new BufferReaderWriter(31);

            buffer.WriteByte(0x42);
            buffer.WriteInt16(0x4243);
            buffer.WriteInt32(0x42434445);
            buffer.WriteInt64(0x4243444546474849);
            buffer.WriteFloat(42.43f);
            buffer.WriteDouble(42.43);
            buffer.Write(new byte[] { 0x42, 0x43, 0x44, 0x45 });

            buffer.Seek(0, SeekOrigin.Begin);

            Assert.Equal(0x42, buffer.ReadByte());
            Assert.Equal(0x4243, buffer.ReadInt16());
            Assert.Equal(0x42434445, buffer.ReadInt32());
            Assert.Equal(0x4243444546474849, buffer.ReadInt64());
            Assert.Equal(42.43f, buffer.ReadFloat());
            Assert.Equal(42.43, buffer.ReadDouble());
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45 }, buffer.Read(4));

            Assert.Throws <InvalidOperationException>(() => { buffer.Read(1); });

            buffer = new BufferReaderWriter(Hex.ToByteArray("57 80 06 00 00 00 DB 9B D2 9A D2 F9"));

            Assert.Equal(0x5780u, buffer.ReadUInt16());
            Assert.Equal(0x06000000u, buffer.ReadUInt32());
            Assert.Equal(0xDB9Bu, buffer.ReadUInt16());
            Assert.Equal(0xD29AD2F9u, buffer.ReadUInt32());
        }
Esempio n. 8
0
        public void Resizing()
        {
            var buffer = new BufferReaderWriter(4);

            Assert.Equal(4, buffer.Capacity);

            buffer.WriteInt(0);
            Assert.Equal(4, buffer.Capacity);

            buffer.WriteInt(0);
            Assert.Equal(260, buffer.Capacity);
        }
Esempio n. 9
0
        public void WritingLE()
        {
            var buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };

            buffer.WriteByte(0x42);
            Assert.Equal(new byte[] { 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };
            buffer.WriteInt16(0x4243);
            Assert.Equal(new byte[] { 0x43, 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };
            buffer.WriteInt32(0x42434445);
            Assert.Equal(new byte[] { 0x45, 0x44, 0x43, 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };
            buffer.WriteInt64(0x4243444546474849);
            Assert.Equal(new byte[] { 0x49, 0x48, 0x47, 0x46, 0x45, 0x44, 0x43, 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };
            buffer.WriteFloat(42.43f);
            Assert.Equal(new byte[] { 0x52, 0xB8, 0x29, 0x42 }, buffer.Copy());

            buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };
            buffer.WriteDouble(42.43);
            Assert.Equal(new byte[] { 0xD7, 0xA3, 0x70, 0x3D, 0x0A, 0x37, 0x45, 0x40 }, buffer.Copy());

            buffer = new BufferReaderWriter()
            {
                Endianness = Endianness.LittleEndian
            };
            buffer.Write(new byte[] { 0x42, 0x43, 0x44, 0x45 });
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45 }, buffer.Copy());
        }
Esempio n. 10
0
        public void FixedLength()
        {
            var buffer = new BufferReaderWriter(new byte[10], 0, 0, false);

            buffer.WriteInt(0x01020304);
            buffer.WriteInt(0x01020304);
            Assert.DoesNotThrow(() => buffer.WriteInt(0x01020304));

            buffer = new BufferReaderWriter(new byte[10], 0, 0, true);

            buffer.WriteInt(0x01020304);
            buffer.WriteInt(0x01020304);
            Assert.Throws <InvalidOperationException>(() => buffer.WriteInt(0x01020304));
        }
Esempio n. 11
0
        public void Seeking()
        {
            var buffer = new BufferReaderWriter(10);

            buffer.Seek(0, SeekOrigin.Begin);
            Assert.Equal(0, buffer.Index);

            buffer.Seek(0, SeekOrigin.End);
            Assert.Equal(9, buffer.Index);

            buffer.Seek(-4, SeekOrigin.Current);
            Assert.Equal(5, buffer.Index);

            buffer.Seek(2, SeekOrigin.Current);
            Assert.Equal(7, buffer.Index);

            Assert.DoesNotThrow(() => { buffer.Seek(2, SeekOrigin.Current); });
            buffer.Seek(-2, SeekOrigin.Current);
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(3, SeekOrigin.Current); });

            Assert.DoesNotThrow(() => { buffer.Seek(-7, SeekOrigin.Current); });
            buffer.Seek(2, SeekOrigin.Current);
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(-3, SeekOrigin.Current); });

            Assert.DoesNotThrow(() => { buffer.Seek(-1, SeekOrigin.End); });
            Assert.DoesNotThrow(() => { buffer.Seek(-5, SeekOrigin.End); });
            Assert.DoesNotThrow(() => { buffer.Seek(-9, SeekOrigin.End); });
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(1, SeekOrigin.End); });
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(10, SeekOrigin.End); });

            Assert.DoesNotThrow(() => { buffer.Seek(1, SeekOrigin.Begin); });
            Assert.DoesNotThrow(() => { buffer.Seek(5, SeekOrigin.Begin); });
            Assert.DoesNotThrow(() => { buffer.Seek(9, SeekOrigin.Begin); });
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(-1, SeekOrigin.Begin); });
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(10, SeekOrigin.Begin); });

            buffer.Seek(5, SeekOrigin.Begin);
            Assert.DoesNotThrow(() => { buffer.Seek(1, SeekOrigin.Current); });
            buffer.Seek(5, SeekOrigin.Begin);
            Assert.DoesNotThrow(() => { buffer.Seek(4, SeekOrigin.Current); });
            buffer.Seek(5, SeekOrigin.Begin);
            Assert.DoesNotThrow(() => { buffer.Seek(-1, SeekOrigin.Current); });
            buffer.Seek(5, SeekOrigin.Begin);
            Assert.DoesNotThrow(() => { buffer.Seek(-5, SeekOrigin.Current); });
            buffer.Seek(5, SeekOrigin.Begin);
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(-6, SeekOrigin.Current); });
            buffer.Seek(5, SeekOrigin.Begin);
            Assert.Throws <InvalidOperationException>(() => { buffer.Seek(5, SeekOrigin.Current); });
        }
Esempio n. 12
0
        public void Rewriting()
        {
            var buffer = new BufferReaderWriter(10);

            buffer.WriteShort(0);
            buffer.WriteLong(0);

            var index = buffer.Index;

            buffer.Seek(0, SeekOrigin.Begin);

            buffer.WriteShort(0x1111);
            buffer.WriteLong(0x2222222222222222);

            Console.WriteLine("index: " + index);
            Console.WriteLine("capac: " + buffer.Capacity);
            buffer.Seek(index, SeekOrigin.Begin);
            Assert.Equal(9, buffer.Index);

            buffer.Seek(0, SeekOrigin.Begin);
            Assert.Equal(0x1111, buffer.ReadShort());
            Assert.Equal(0x2222222222222222, buffer.ReadLong());
        }
Esempio n. 13
0
        public void Reading()
        {
            var buffer = new BufferReaderWriter(31);

            buffer.WriteByte(0x42);
            buffer.WriteShort(0x4243);
            buffer.WriteInt(0x42434445);
            buffer.WriteLong(0x4243444546474849);
            buffer.WriteFloat(42.43f);
            buffer.WriteDouble(42.43);
            buffer.Write(new byte[] { 0x42, 0x43, 0x44, 0x45 });

            buffer.Seek(0, SeekOrigin.Begin);

            Assert.Equal(0x42, buffer.ReadByte());
            Assert.Equal(0x4243, buffer.ReadShort());
            Assert.Equal(0x42434445, buffer.ReadInt());
            Assert.Equal(0x4243444546474849, buffer.ReadLong());
            Assert.Equal(42.43f, buffer.ReadFloat());
            Assert.Equal(42.43, buffer.ReadDouble());
            Assert.Equal(new byte[] { 0x42, 0x43, 0x44, 0x45 }, buffer.Read(4));

            Assert.Throws <InvalidOperationException>(() => { buffer.Read(1); });
        }
Esempio n. 14
0
        public void VarInt()
        {
            var buffer = new BufferReaderWriter(new byte[10], 0, 0, false);

            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x00000000);
                Assert.Equal(new byte[] { 0x00 }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x00, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x0000007F);
                Assert.Equal(new byte[] { 0x7F }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x7F, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x00000080);
                Assert.Equal(new byte[] { 0x80, 0x01 }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x80, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x00002000);
                Assert.Equal(new byte[] { 0x80, 0x40 }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x2000, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x00003FFF);
                Assert.Equal(new byte[] { 0xFF, 0x7F }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x3FFF, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x00004000);
                Assert.Equal(new byte[] { 0x80, 0x80, 0x01 }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x4000, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x001FFFFF);
                Assert.Equal(new byte[] { 0xFF, 0xFF, 0x7F }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x1FFFFF, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x00200000);
                Assert.Equal(new byte[] { 0x80, 0x80, 0x80, 0x01 }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x200000, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x08000000);
                Assert.Equal(new byte[] { 0x80, 0x80, 0x80, 0x40 }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0x8000000, buffer.ReadVarInt());
            }
            {
                buffer.Seek(0, SeekOrigin.Begin);
                buffer.WriteVarInt(0x0FFFFFFF);
                Assert.Equal(new byte[] { 0xFF, 0xFF, 0xFF, 0x7F }, buffer.Copy());

                buffer.Seek(0, SeekOrigin.Begin);
                Assert.Equal(0xFFFFFFF, buffer.ReadVarInt());
            }
        }