Esempio n. 1
0
        public unsafe void TestMethod()
        {
            MemoryStream        stream = new MemoryStream();
            BinaryStreamWrapper bs     = new BinaryStreamWrapper(stream, false);
            Random rand = new Random();
            int    seed = rand.Next();

            rand = new Random(seed);
            byte[] data  = new byte[16];
            byte[] data2 = new byte[16];

            fixed(byte *lp = data)
            {
                for (int x = 0; x < 10000; x++)
                {
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*lp);
                    }
                    int skip = rand.Next(40) + 1;
                    bs.Position += skip;
                    bs.Position -= rand.Next(skip);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(sbyte *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(short *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(int *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(long *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(ushort *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(uint *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(ulong *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(NextDecimal(data, rand));
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(*(Guid *)lp);
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(NextDate(data, rand));
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(NextSingle(data, rand));
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(NextDouble(data, rand));
                    }
                    rand.NextBytes(data);
                    bool value = *lp != 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write(value);
                    }

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(uint *)lp);
                    }
                    data[3] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(uint *)lp);
                    }
                    data[2] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(uint *)lp);
                    }
                    data[1] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(uint *)lp);
                    }

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[7] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[6] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[5] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[4] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[3] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[2] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }
                    data[1] = 0;
                    while (rand.Next(4) < 2)
                    {
                        bs.Write7Bit(*(ulong *)lp);
                    }

                    rand.NextBytes(data);
                    bs.Write(data, 0, data.Length);

                    while (rand.Next(4) < 2)
                    {
                        if (bs.Position > 100)
                        {
                            bs.Position -= 100;
                            int insertCount = rand.Next(16) + 1;
                            bs.InsertBytes(insertCount, 100);
                            bs.Write(data, 0, insertCount);
                            bs.Position -= insertCount;
                            bs.ReadAll(data2, 0, insertCount);
                            bs.Position -= insertCount;
                            bs.RemoveBytes(insertCount, 100);
                            bs.Position += 100;

                            for (int y = 0; y < insertCount; y++)
                            {
                                if (data[y] != data2[y])
                                {
                                    throw new Exception();
                                }
                            }
                        }
                    }
                }
                rand = new Random(seed);

                bs.Position = 0;

                for (int x = 0; x < 10000; x++)
                {
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadUInt8() != *lp)
                        {
                            throw new Exception();
                        }
                    }
                    int skip = rand.Next(40) + 1;
                    bs.Position += skip;
                    bs.Position -= rand.Next(skip);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadInt8() != *(sbyte *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadInt16() != *(short *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadInt32() != *(int *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadInt64() != *(long *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadUInt16() != *(ushort *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadUInt32() != *(uint *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadDecimal() != NextDecimal(data, rand))
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadGuid() != *(Guid *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadDateTime() != NextDate(data, rand))
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadSingle() != NextSingle(data, rand))
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadDouble() != NextDouble(data, rand))
                        {
                            throw new Exception();
                        }
                    }
                    rand.NextBytes(data);
                    bool b2 = *lp != 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.ReadBoolean() != b2)
                        {
                            throw new Exception();
                        }
                    }

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt32() != *(uint *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[3] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt32() != *(uint *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[2] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt32() != *(uint *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[1] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt32() != *(uint *)lp)
                        {
                            throw new Exception();
                        }
                    }

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[7] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[6] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[5] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[4] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[3] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[2] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }
                    data[1] = 0;
                    while (rand.Next(4) < 2)
                    {
                        if (bs.Read7BitUInt64() != *(ulong *)lp)
                        {
                            throw new Exception();
                        }
                    }

                    rand.NextBytes(data);
                    bs.ReadAll(data2, 0, 16);
                    if (!data2.SequenceEqual(data))
                    {
                        throw new Exception();
                    }

                    while (rand.Next(4) < 2)
                    {
                        if (bs.Position > 100)
                        {
                            int insertCount = rand.Next(16);
                        }
                    }
                }
            }
        }
        public unsafe void TestMethod()
        {
            MemoryStream stream = new MemoryStream();
            BinaryStreamWrapper bs = new BinaryStreamWrapper(stream, false);
            Random rand = new Random();
            int seed = rand.Next();
            rand = new Random(seed);
            byte[] data = new byte[16];
            byte[] data2 = new byte[16];

            fixed (byte* lp = data)
            {
                for (int x = 0; x < 10000; x++)
                {
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(byte*)lp);
                    int skip = rand.Next(40) + 1;
                    bs.Position += skip;
                    bs.Position -= rand.Next(skip);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(sbyte*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(short*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(int*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(long*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(ushort*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(uint*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(ulong*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextDecimal(data, rand));
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(*(Guid*)lp);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextDate(data, rand));
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextSingle(data, rand));
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write(NextDouble(data, rand));
                    rand.NextBytes(data);
                    bool value = (*lp != 0);
                    while (rand.Next(4) < 2) bs.Write(value);

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);
                    data[3] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);
                    data[2] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);
                    data[1] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(uint*)lp);

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[7] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[6] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[5] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[4] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[3] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[2] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);
                    data[1] = 0;
                    while (rand.Next(4) < 2) bs.Write7Bit(*(ulong*)lp);

                    rand.NextBytes(data);
                    bs.Write(data, 0, data.Length);

                    while (rand.Next(4) < 2)
                    {
                        if (bs.Position > 100)
                        {
                            bs.Position -= 100;
                            int insertCount = rand.Next(16) + 1;
                            bs.InsertBytes(insertCount, 100);
                            bs.Write(data, 0, insertCount);
                            bs.Position -= insertCount;
                            bs.ReadAll(data2, 0, insertCount);
                            bs.Position -= insertCount;
                            bs.RemoveBytes(insertCount, 100);
                            bs.Position += 100;

                            for (int y = 0; y < insertCount; y++)
                            {
                                if (data[y] != data2[y])
                                    throw new Exception();
                            }
                        }
                    }
                }
                rand = new Random(seed);

                bs.Position = 0;

                for (int x = 0; x < 10000; x++)
                {
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt8() != (*(byte*)lp)) throw new Exception();
                    int skip = rand.Next(40) + 1;
                    bs.Position += skip;
                    bs.Position -= rand.Next(skip);
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt8() != (*(sbyte*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt16() != (*(short*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt32() != (*(int*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadInt64() != (*(long*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt16() != (*(ushort*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt32() != (*(uint*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadUInt64() != (*(ulong*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadDecimal() != NextDecimal(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadGuid() != (*(Guid*)lp)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadDateTime() != NextDate(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadSingle() != NextSingle(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.ReadDouble() != NextDouble(data, rand)) throw new Exception();
                    rand.NextBytes(data);
                    bool b2 = (*lp != 0);
                    while (rand.Next(4) < 2) if (bs.ReadBoolean() != b2) throw new Exception();

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();
                    data[3] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();
                    data[2] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();
                    data[1] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt32() != (*(uint*)lp)) throw new Exception();

                    rand.NextBytes(data);
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[7] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[6] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[5] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[4] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[3] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[2] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();
                    data[1] = 0;
                    while (rand.Next(4) < 2) if (bs.Read7BitUInt64() != (*(ulong*)lp)) throw new Exception();

                    rand.NextBytes(data);
                    bs.ReadAll(data2, 0, 16);
                    if (!data2.SequenceEqual<byte>(data)) throw new Exception();

                    while (rand.Next(4) < 2)
                    {
                        if (bs.Position > 100)
                        {
                            int insertCount = rand.Next(16);
                        }
                    }
                }
            }
        }