public unsafe void MixedWrite()
        {
            byte[] data = new byte[256];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, 256);

                writer.Write("abcABCäöüÄÖÜßáÁàÀ♥♦♣♠");
                writer.Write((byte)66);
                writer.Write(0x48484848);
                writer.Write(0x84848484U);
                writer.Write("abcABCäöüÄÖÜßáÁàÀ♥♦♣♠");
            }

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(ms))
                {
                    Assert.AreEqual(reader.ReadString(), "abcABCäöüÄÖÜßáÁàÀ♥♦♣♠", "BinaryMemoryReader String incompatible to BinaryReader.");
                    Assert.AreEqual(reader.ReadByte(), (byte)66, "BinaryMemoryReader Byte incompatible to BinaryReader.");
                    Assert.AreEqual(reader.ReadInt32(), 0x48484848, "BinaryMemoryReader Int incompatible to BinaryReader.");
                    Assert.AreEqual(reader.ReadUInt32(), 0x84848484U, "BinaryMemoryReader UInt incompatible to BinaryReader.");
                    Assert.AreEqual(reader.ReadString(), "abcABCäöüÄÖÜßáÁàÀ♥♦♣♠", "BinaryMemoryReader 2nd String incompatible to BinaryReader.");
                }
        }
Ejemplo n.º 2
0
        public unsafe void VanillaStringLimits()
        {
            foreach (int size in new int[] { 1, 2, 3, 20, 5000 })
            {
                byte[] data = new byte[size];

                for (int i = 0; i < size; i++)
                    data[i] = 65;

                fixed(byte *pData = data)
                {
                    BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length);

                    try
                    {
                        reader.ReadVanillaString(size);
                    }
                    catch
                    {
                        Assert.Fail("Should not have thrown an Exception.");
                    }

                    BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                    try
                    {
                        writer.WriteVanillaString(new string('A', size));
                    }
                    catch
                    {
                        Assert.Fail("Should not have thrown an Exception.");
                    }
                }
            }
        }
Ejemplo n.º 3
0
        public unsafe void BinaryMemoryWriter_Initialize()
        {
            byte[] data = new byte[1];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, 1);

                writer.Write((byte)0x5A);
            }
        }
Ejemplo n.º 4
0
        public unsafe void BinaryMemoryWriter_Bytes()
        {
            byte[] data = new byte[10240];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, 10240);

                for (int count = 0; count < 10240; count++)
                {
                    writer.Write((byte)0x5A);
                }
            }
        }
Ejemplo n.º 5
0
        public unsafe void BinaryMemoryWriter_Strings()
        {
            byte[] data = new byte[102400];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, 102400);

                for (int count = 0; count < 10240; count++)
                {
                    writer.Write("ABCDEFGHI");
                }
            }
        }
Ejemplo n.º 6
0
        public unsafe void BinaryMemoryWriter_Short()
        {
            byte[] data = new byte[20480];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, 20480);

                for (int count = 0; count < 10240; count++)
                {
                    writer.Write((short)0x5AA5);
                }
            }
        }
        public unsafe void BytesLimits()
        {
            Random rng = new Random();

            byte[] src = new byte[64];

            rng.NextBytes(src);

            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write(src);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length - 1);

                try
                {
                    reader.ReadBytes(src, 0, 64);

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch (OutOfMemoryException) { }
                catch (Exception)
                {
                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length - 1);

                try
                {
                    writer.WriteBytes(src, 0, 64);

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch (OutOfMemoryException) { }
                catch (Exception)
                {
                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
            }
        }
        public unsafe void SevenBitEncodedLimits()
        {
            byte[] data;

            List <KeyValuePair <ulong, int> > pairs = new List <KeyValuePair <ulong, int> >();

            pairs.Add(new KeyValuePair <ulong, int>(0L, 1));

            for (int d = 1; d < 10; d++)
            {
                pairs.Add(new KeyValuePair <ulong, int>((1UL << (d * 7)) - 1, d));
                pairs.Add(new KeyValuePair <ulong, int>(1UL << (d * 7), d + 1));
            }

            pairs.Add(new KeyValuePair <ulong, int>(ulong.MaxValue, 10));

            foreach (KeyValuePair <ulong, int> pair in pairs)
            {
                data = new byte[pair.Value];

                fixed(byte *pData = data)
                {
                    BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, pair.Value);

                    writer.Write7BitEncoded(pair.Key);

                    writer = new BinaryMemoryWriter(pData, pair.Value);

                    try
                    {
                        writer.Write7BitEncoded(pair.Key);

                        Assert.Fail($"Should have thrown an Exception, but didn't with {pair.Key}.");
                    }
                    catch { }

                    BinaryMemoryReader reader = new BinaryMemoryReader(pData, pair.Value);

                    try
                    {
                        Assert.AreEqual(pair.Key, reader.Read7BitEncoded(), $"Didn't read what i've wrote with {pair.Key}.");

                        Assert.Fail($"Should have thrown an Exception, but didn't with {pair.Key}.");
                    }
                    catch { }
                }
            }
        }
        public unsafe void StringLimits()
        {
            foreach (int size in new int[] { 0, 1, 2, 126, 127, 128, 129, 16382, 16383, 16384, 16385, 2097150, 2097151, 2097152, 2097153, 268435454, 268435455, 268435456, 268435457 })
            {
                byte[] data;

                using (MemoryStream ms = new MemoryStream())
                {
                    using (BinaryWriter writer = new BinaryWriter(ms))
                        writer.Write(new string('A', size));

                    data = ms.ToArray();
                }

                fixed(byte *pData = data)
                {
                    BinaryMemoryReader reader = new BinaryMemoryReader(pData, size - 1);

                    try
                    {
                        reader.ReadString();

                        Assert.Fail("Should have thrown an OutOfMemoryException.");
                    }
                    catch (OutOfMemoryException) { }
                    catch (Exception)
                    {
                        Assert.Fail("Should have thrown an OutOfMemoryException.");
                    }

                    BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, size - 1);

                    try
                    {
                        writer.Write(new string('A', size));

                        Assert.Fail("Should have thrown an OutOfMemoryException.");
                    }
                    catch (OutOfMemoryException) { }
                    catch (Exception)
                    {
                        Assert.Fail("Should have thrown an OutOfMemoryException.");
                    }
                }
            }
        }
        public unsafe void StringWrite()
        {
            foreach (int size in new int[] { 1, 127, 128, 16383, 16384, 2097151, 2097152, 268435455, 268435456, 300000000 })
            {
                byte[] data = new byte[size + 8];

                fixed(byte *pData = data)
                {
                    BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                    writer.Write(new string('A', size));
                }

                using (MemoryStream ms = new MemoryStream(data))
                    using (BinaryReader reader = new BinaryReader(ms))
                        Assert.AreEqual(reader.ReadString(), new string('A', size), "BinaryMemoryWriter String incompatible to BinaryWriter.");
            }
        }
        public unsafe void DecimalLimits()
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write(2873645.2345m);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length - 1);

                try
                {
                    reader.ReadDecimal();

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch (OutOfMemoryException) { }
                catch (Exception)
                {
                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length - 1);

                try
                {
                    writer.Write(2873645.2345m);

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch (OutOfMemoryException) { }
                catch (Exception)
                {
                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
            }
        }
        public unsafe void TimeSpanLimits()
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write(DateTime.UtcNow.Ticks);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length - 1);

                try
                {
                    reader.ReadTimeSpan();

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch (OutOfMemoryException) { }
                catch (Exception)
                {
                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length - 1);

                try
                {
                    writer.Write(new TimeSpan(23876482734L));

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch (OutOfMemoryException) { }
                catch (Exception)
                {
                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
            }
        }
        public unsafe void DecimalWrite()
        {
            byte[] data = new byte[4096];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                for (int count = 0; count < 256; count++)
                {
                    writer.Write(count * 12347822345.34m);
                }
            }

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(ms))
                    for (int count = 0; count < 256; count++)
                    {
                        Assert.AreEqual(reader.ReadDecimal(), count * 12347822345.34m, "UnsafeBinaryMemoryWriter Decimal incompatible to BinaryWriter.");
                    }
        }
        public unsafe void DoubleWrite()
        {
            byte[] data = new byte[2048];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                for (int count = 0; count < 256; count++)
                {
                    writer.Write((double)count);
                }
            }

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(ms))
                    for (int count = 0; count < 256; count++)
                    {
                        Assert.AreEqual(reader.ReadDouble(), (double)count, "BinaryMemoryWriter Double incompatible to BinaryWriter.");
                    }
        }
        public unsafe void BooleanWrite()
        {
            byte[] data = new byte[256];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                for (int count = 0; count < 256; count++)
                {
                    writer.Write(count % 2 == 0);
                }
            }

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(ms))
                    for (int count = 0; count < 256; count++)
                    {
                        Assert.AreEqual(reader.ReadBoolean(), count % 2 == 0, "BinaryMemoryWriter Byte incompatible to BinaryWriter.");
                    }
        }
        public unsafe void UShortWrite()
        {
            byte[] data = new byte[512];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                for (int count = 0; count < 256; count++)
                {
                    writer.Write((ushort)count);
                }
            }

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(ms))
                    for (int count = 0; count < 256; count++)
                    {
                        Assert.AreEqual(reader.ReadUInt16(), (ushort)count, "BinaryMemoryWriter UShort incompatible to BinaryWriter.");
                    }
        }
        public unsafe void CutJumpLimits()
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write(new byte[30]);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader    = new BinaryMemoryReader(pData, data.Length);
                BinaryMemoryReader cutReader = reader;

                try
                {
                    cutReader = reader.Cut(31);

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch
                {
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                try
                {
                    cutReader.Jump(31);

                    Assert.Fail("Should have thrown an OutOfMemoryException.");
                }
                catch
                {
                }
            }
        }
        public unsafe void CharWrite()
        {
            byte[] data = new byte[262144];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                for (int count = 0; count < 0xD800; count++)
                {
                    writer.Write((char)count);
                }
            }

            using (MemoryStream ms = new MemoryStream(data))
                using (BinaryReader reader = new BinaryReader(ms))
                    for (int count = 0; count < 0xD800; count++)
                    {
                        char c = reader.ReadChar();

                        Assert.AreEqual(c, (char)count, $"BinaryMemoryWriter Char incompatible to BinaryWriter: 0x{count.ToString("X04")} != 0x{((int)c).ToString("X04")}.");
                    }
        }
Ejemplo n.º 19
0
        public unsafe void DecimalLimits()
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write(1827364.2134324m);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length);

                try
                {
                    reader.ReadDecimal();
                }
                catch
                {
                    Assert.Fail("Should not have thrown an Exception.");
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                try
                {
                    writer.Write(1827364.2134324m);
                }
                catch
                {
                    Assert.Fail("Should not have thrown an Exception.");
                }
            }
        }
Ejemplo n.º 20
0
        public unsafe void DateTimeLimits()
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write(DateTime.UtcNow.Ticks);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length);

                try
                {
                    reader.ReadDateTime();
                }
                catch
                {
                    Assert.Fail("Should not have thrown an Exception.");
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                try
                {
                    writer.Write(DateTime.UtcNow);
                }
                catch
                {
                    Assert.Fail("Should not have thrown an Exception.");
                }
            }
        }
Ejemplo n.º 21
0
        public unsafe void UShortLimits()
        {
            byte[] data;

            using (MemoryStream ms = new MemoryStream())
            {
                using (BinaryWriter writer = new BinaryWriter(ms))
                    writer.Write((ushort)0x5555);

                data = ms.ToArray();
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length);

                try
                {
                    reader.ReadUInt16();
                }
                catch
                {
                    Assert.Fail("Should not have thrown an Exception.");
                }

                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                try
                {
                    writer.Write((ushort)0x5555);
                }
                catch
                {
                    Assert.Fail("Should not have thrown an Exception.");
                }
            }
        }
        public unsafe void BytesWrite()
        {
            Random rng = new Random();

            byte[] src = new byte[1024];
            byte[] chk;

            rng.NextBytes(src);

            byte[] data = new byte[262144];

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                for (int count = 0; count < 256; count++)
                {
                    writer.WriteBytes(src, 0, 1024);
                }
            }

            using (MemoryStream ms = new MemoryStream(data))
            {
                using (BinaryReader reader = new BinaryReader(ms))
                    for (int count = 0; count < 256; count++)
                    {
                        chk = reader.ReadBytes(1024);

                        for (int position = 0; position < 1024; position++)
                        {
                            Assert.AreEqual(chk[position], src[position], $"Invalid content at position {position}.");
                        }
                    }

                data = ms.ToArray();
            }
        }
        public unsafe void CompressedTimeSpanRead()
        {
            int required = 0;

            List <long> timeLongs = new List <long>()
            {
                //Limits 1 Byte -> 3 Byte
                0, 1, -1, 0x1F, -0x1F, 0x20, -0x20,

                0xFF_FF, -0xFF_FF, 0x01_00_00, -0x01_00_00,

                //Limits 3 Byte -> 5 Byte
                0x1F_FF_FF, -0x1F_FF_FF, 0x20_00_00, -0x20_00_00,

                0x01_00_00_00, -0x01_00_00_00,
                0xFF_FF_FF_FF, -0xFF_FF_FF_FF,
                0x01_00_00_00_00, -0x01_00_00_00_00,
                0x01_01_00_00_00, -0x01_01_00_00_00,
                0x01_00_01_00_00, -0x01_00_01_00_00,
                0x01_00_00_01_00, -0x01_00_00_01_00,
                0x01_00_00_00_01, -0x01_00_00_00_01,

                //Limits 5 Byte -> 8 Byte
                0x1F_FF_FF_FF_FF, -0x1F_FF_FF_FF_FF, 0x20_00_00_00_00, -0x20_00_00_00_00,

                0x01_00_00_00_00, -0x01_00_00_00_00,
                0xFF_FF_FF_FF_FF, -0xFF_FF_FF_FF_FF,
                0xFF_00_FF_FF_FF, -0xFF_00_FF_FF_FF,
                0xFF_FF_00_FF_FF, -0xFF_FF_00_FF_FF,
                0xFF_FF_FF_00_FF, -0xFF_FF_FF_00_FF,
                0xFF_FF_FF_FF_00, -0xFF_FF_FF_FF_00,
                0x01_00_00_00_00_00, -0x01_00_00_00_00_00,
                0x01_00_00_01_00_00, -0x01_00_00_01_00_00,
                0x01_00_00_00_01_00, -0x01_00_00_00_01_00,
                0x01_00_00_00_00_01, -0x01_00_00_00_00_01,
                0xFF_FF_FF_FF_FF_FF, -0xFF_FF_FF_FF_FF_FF,
                0x01_00_00_00_00_00_00, -0x01_00_00_00_00_00_00,
                0x01_00_00_00_00_01_00, -0x01_00_00_00_00_01_00,
                0x01_00_00_00_00_00_01, -0x01_00_00_00_00_00_01,
                0xFF_FF_FF_FF_FF_FF_FF, -0xFF_FF_FF_FF_FF_FF_FF,
                0x01_00_00_00_00_00_00_00, -0x01_00_00_00_00_00_00_00,
                0x01_00_00_00_00_00_01_00, -0x01_00_00_00_00_00_01_00,
                0x01_00_00_00_00_00_00_01, -0x01_00_00_00_00_00_00_01,

                //Limits -> 8 Byte
                0x1F_FF_FF_FF_FF_FF_FF_FF, -0x1F_FF_FF_FF_FF_FF_FF_FF
            };

            foreach (long timeLong in timeLongs)
            {
                long checkLong = timeLong;
                if (checkLong < 0)
                {
                    checkLong = -checkLong;
                }

                if (checkLong >= 137438953472)
                {
                    required += 8;
                }
                else if (checkLong >= 2097152)
                {
                    required += 5;
                }
                else if (checkLong >= 32)
                {
                    required += 3;
                }
                else
                {
                    required += 1;
                }
            }

            byte[] data = new byte[required];

            List <TimeSpan> timeSpans = new List <TimeSpan>();

            foreach (long timeLong in timeLongs)
                timeSpans.Add(new TimeSpan(timeLong));

            fixed(byte *pData = data)
            {
                BinaryMemoryWriter writer = new BinaryMemoryWriter(pData, data.Length);

                foreach (TimeSpan timeSpan in timeSpans)
                {
                    writer.WriteCompressed(timeSpan);
                }
            }

            fixed(byte *pData = data)
            {
                BinaryMemoryReader reader = new BinaryMemoryReader(pData, data.Length);

                foreach (TimeSpan timeSpan in timeSpans)
                {
                    Assert.AreEqual(timeSpan, reader.ReadCompressedTimeSpan(), "BinaryMemoryReader TimeSpan Compressed incompatible to BinaryMemoryWriter.");
                }
            }
        }