示例#1
0
        public unsafe void ReadPastEnd()
        {
            var s = new ReusableStream(16);

            s.Write((ulong)3);

            var buffer = new byte[16];

            fixed(byte *p = buffer)
            {
                var bufferPtr = p;

                Assert.AreEqual(-1, s.ReadByte());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadBoolean());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt8());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt8());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt16());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt16());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt32());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt32());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadInt64());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadUInt64());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadSingle());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadDouble());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadDateTime());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadGuid());
                Assert.Throws <IndexOutOfRangeException>(() => s.ReadString(true));

                Assert.AreEqual(0, s.Read(buffer, 0, 1));
                Assert.AreEqual(0, s.Read(bufferPtr, 1));

                s.ResetForReading();
                Assert.AreEqual(8, s.Read(buffer, 0, 9));

                s.ResetForReading();
                Assert.AreEqual(8, s.Read(bufferPtr, 9));
            }
        }
示例#2
0
        public void ReadWritePrimitives()
        {
            var s   = new ReusableStream(100);
            var rng = new Random();

            var b   = (byte)rng.Next(255);
            var sb  = (sbyte)rng.Next(255);
            var sh  = (short)rng.Next(short.MinValue, short.MaxValue);
            var ush = (ushort)rng.Next(ushort.MaxValue);
            var i   = rng.Next(int.MinValue, int.MaxValue);
            var ui  = (uint)rng.Next(int.MinValue, int.MaxValue);
            var l   = (long)RandomULong(rng);
            var ul  = RandomULong(rng);
            var f   = (float)rng.NextDouble();
            var d   = rng.NextDouble();
            var c   = (char)rng.Next(char.MinValue, char.MaxValue);
            var t   = DateTime.UtcNow;
            var g   = Guid.NewGuid();

            var expectedLength = 0;

            s.Write(b);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(sb);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(sh);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(ush);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(i);
            expectedLength += 4;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(ui);
            expectedLength += 4;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(l);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(ul);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(f);
            expectedLength += 4;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(d);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(c);
            expectedLength += 2;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(false);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(true);
            expectedLength += 1;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(t);
            expectedLength += 8;
            Assert.AreEqual(expectedLength, s.Length);
            s.Write(g);
            expectedLength += 16;
            Assert.AreEqual(expectedLength, s.Length);

            s.ResetForReading();

            Assert.AreEqual(b, s.ReadUInt8());
            Assert.AreEqual(sb, s.ReadInt8());
            Assert.AreEqual(sh, s.ReadInt16());
            Assert.AreEqual(ush, s.ReadUInt16());
            Assert.AreEqual(i, s.ReadInt32());
            Assert.AreEqual(ui, s.ReadUInt32());
            Assert.AreEqual(l, s.ReadInt64());
            Assert.AreEqual(ul, s.ReadUInt64());
            Assert.AreEqual(f, s.ReadSingle());
            Assert.AreEqual(d, s.ReadDouble());
            Assert.AreEqual(c, s.ReadChar());
            Assert.AreEqual(false, s.ReadBoolean());
            Assert.AreEqual(true, s.ReadBoolean());
            Assert.AreEqual(t, s.ReadDateTime());
            Assert.AreEqual(g, s.ReadGuid());

            // verify that we read to the end
            Assert.AreEqual(s.Length, s.Position);

            s.ResetForReading();
            Assert.AreEqual((int)b, s.ReadByte());
        }