Exemple #1
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());
        }
Exemple #2
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());
        }
Exemple #3
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());
        }
Exemple #4
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());
        }
        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); });
        }