Exemplo n.º 1
0
        public void StringEnumTest()
        {
            var abc = new[] { "A", "B", "C" };
            var a_c = new[] { "A", null, "C" };

            using (var ms = new NativeMemoryStream(128, 128))
            {
                Assert.That(() => ms.Write(null, StringListPackMethod.Concatenated), Throws.Nothing);

                Assert.That(() => ms.Write(abc, StringListPackMethod.Concatenated), Throws.Nothing);
                var len = 14;
                var pos = len;
                Assert.That(len, Is.EqualTo(ms.Length).And.EqualTo(ms.Position));
                Assert.That(() => ms.Write(abc, StringListPackMethod.Packed), Throws.Nothing);
                Assert.That(len += 12 + IntPtr.Size * 3, Is.EqualTo(ms.Length));
                Assert.That(pos += IntPtr.Size * 3, Is.EqualTo(ms.Position));
                Assert.That(ms.Capacity, Is.EqualTo(128));
                Assert.That(() => ms.Write(a_c, StringListPackMethod.Concatenated), Throws.Exception);
                Assert.That(() => ms.Write(a_c, StringListPackMethod.Packed), Throws.Nothing);
                Assert.That(len += 8 + IntPtr.Size * 3, Is.EqualTo(ms.Length));
                Assert.That(pos += IntPtr.Size * 3, Is.EqualTo(ms.Position));
                Assert.That(ms.Capacity, Is.EqualTo(128));

                var testSz = 10;
                var strSz  = 50;
                var l      = new List <string>(testSz);
                for (var i = 0; i < testSz; i++)
                {
                    l.Add(new string('X', strSz));
                }
                Assert.That(() => ms.Write(l, StringListPackMethod.Concatenated), Throws.Nothing);
                var strLen = ((strSz + 1) * testSz + 1) * StringHelper.GetCharSize();
                Assert.That(len += strLen, Is.EqualTo(ms.Length));
                Assert.That(pos += strLen, Is.EqualTo(ms.Position));
                Assert.That(ms.Capacity, Is.GreaterThan(128));

                Assert.That(() => ms.Write(l, StringListPackMethod.Packed), Throws.Nothing);
                strLen           = (strSz + 1) * testSz * StringHelper.GetCharSize();
                Assert.That(len += strLen + IntPtr.Size * testSz, Is.EqualTo(ms.Length));
                Assert.That(pos += IntPtr.Size * testSz, Is.EqualTo(ms.Position));

                Assert.That(ms.Position, Is.LessThan(ms.Length));
                ms.Flush();
                Assert.That(ms.Position, Is.EqualTo(ms.Length));
                Assert.That(ms.Position, Is.LessThanOrEqualTo(ms.Capacity));

                ms.Position = 0;

                Assert.That(ms.ReadArray(typeof(string), abc.Length, false), Is.EquivalentTo(abc));
                Assert.That(ms.ReadArray(typeof(string), abc.Length, true), Is.EquivalentTo(abc));
                Assert.That(ms.ReadArray(typeof(string), a_c.Length, true), Is.EquivalentTo(a_c));
                Assert.That(ms.ReadArray <string>(l.Count, false), Is.EquivalentTo(l));
                Assert.That(ms.ReadArray <string>(l.Count, true), Is.EquivalentTo(l));

                l.Clear();
            }
        }
Exemplo n.º 2
0
        public void DisposedTest()
        {
            var ms = new NativeMemoryStream(20);

            ms.Dispose();
            Assert.That(ms.CanRead, Is.False);
            Assert.That(ms.CanSeek, Is.False);
            Assert.That(ms.CanWrite, Is.False);
            Assert.That(() => ms.Capacity > 0, Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Read <int>(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Read(typeof(int)), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Read(new byte[3], 0, 3), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadByte(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadArray(typeof(byte), 1, false), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadReference <int>(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.ReadReference <string>(), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Seek(0, SeekOrigin.Begin), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.SetLength(20), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write("X"), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write("X", CharSet.Ansi), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(new[] { "X", "Y" }), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(new byte[3], 0, 3), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(new[] { 1, 2 }), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.Write(256), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteByte(1), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteObject(Guid.NewGuid()), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteReference("X"), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteReference(256), Throws.InstanceOf <ObjectDisposedException>());
            Assert.That(() => ms.WriteReferenceObject(Guid.NewGuid()), Throws.InstanceOf <ObjectDisposedException>());
        }
Exemplo n.º 3
0
        public void StructEnumTest()
        {
            using (var ms = new NativeMemoryStream(48, 16))
            {
                var arr = new[] { 1L, 2L, 3L, 4L, 5L, 6L };
                Assert.That(() => ms.Write(arr), Throws.Nothing);
                var sz = sizeof(long) * arr.Length;
                Assert.That(ms.Length, Is.EqualTo(sz));
                Assert.That(ms.Position, Is.EqualTo(sz));
                Assert.That(ms.Capacity, Is.EqualTo(48));

                var list = new List <int> {
                    1, 2, 3
                };
                Assert.That(() => ms.Write(list), Throws.Nothing);
                sz += sizeof(int) * list.Count;
                Assert.That(ms.Length, Is.EqualTo(sz));
                Assert.That(ms.Position, Is.EqualTo(sz));
                Assert.That(ms.Capacity, Is.EqualTo(64));

                var pos = ms.Position;
                Assert.That(() => ms.Write(arr, true), Throws.Nothing);
                sz  += (sizeof(long) + IntPtr.Size) * arr.Length;
                pos += IntPtr.Size * arr.Length;
                Assert.That(ms.Length, Is.EqualTo(sz));
                Assert.That(ms.Position, Is.EqualTo(pos));
                Assert.That(ms.Capacity, Is.EqualTo(144));

                Assert.That(() => ms.Write(list, true), Throws.Nothing);
                sz  += (sizeof(int) + IntPtr.Size) * list.Count;
                pos += IntPtr.Size * list.Count;
                Assert.That(ms.Length, Is.EqualTo(sz));
                Assert.That(ms.Position, Is.EqualTo(pos));
                Assert.That(ms.Capacity, Is.EqualTo(160));

                ms.Flush();
                ms.Position = 0;

                Assert.That(() => ms.ReadArray(null, 0, false), Throws.ArgumentNullException);
                Assert.That(() => ms.ReadArray(typeof(int), -1, false), Throws.InstanceOf <ArgumentOutOfRangeException>());
                Assert.That(() => ms.ReadArray(typeof(int), 0, false), Throws.Nothing);
                Assert.That(ms.ReadArray(typeof(int), 0, false).Length, Is.Zero);
                Assert.That(() => ms.ReadArray(typeof(Guid), 100, false), Throws.InstanceOf <ArgumentOutOfRangeException>());

                Assert.That(ms.ReadArray(typeof(long), arr.Length, false), Is.EquivalentTo(arr));
                Assert.That(ms.ReadArray <int>(list.Count, false), Is.EquivalentTo(list));
                Assert.That(ms.ReadArray <long>(arr.Length, true), Is.EquivalentTo(arr));
                Assert.That(ms.ReadArray(typeof(int), list.Count, true), Is.EquivalentTo(list));
            }
        }
Exemplo n.º 4
0
        public void ReadWriteOnlyTest()
        {
            using (var m = new SafeHGlobalHandle(10))
                using (var ms = new NativeMemoryStream((IntPtr)m, m.Size))
                {
                    Assert.That(ms.CanWrite, Is.False);
                    Assert.That(ms.CanSeek, Is.True);
                    Assert.That(ms.CanRead, Is.True);

                    Assert.That(() => ms.Write("X"), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write("X", CharSet.Ansi), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(new[] { "X", "Y" }), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(new byte[3], 0, 3), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(new[] { 1, 2 }), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.Write(256), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteByte(1), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteObject(Guid.NewGuid()), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteReference("X"), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteReference(256), Throws.InstanceOf <NotSupportedException>());
                    Assert.That(() => ms.WriteReferenceObject(Guid.NewGuid()), Throws.InstanceOf <NotSupportedException>());
                }

            using (var ms = new NativeMemoryStream(10, 10, 20, FileAccess.Write))
            {
                Assert.That(ms.CanWrite, Is.True);
                Assert.That(ms.CanSeek, Is.True);
                Assert.That(ms.CanRead, Is.False);

                Assert.That(() => ms.Read <int>(), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.Read(typeof(int)), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.Read(new byte[3], 0, 3), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadByte(), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadArray(typeof(byte), 1, false), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadReference <int>(), Throws.InstanceOf <NotSupportedException>());
                Assert.That(() => ms.ReadReference <string>(), Throws.InstanceOf <NotSupportedException>());
            }
        }