Beispiel #1
0
        public void TestReadOnlyMemStream()
        {
            var mem = new CMemoryStream(m_buf2, 0, m_buf2.Length, true)
            {
                Position = 0
            };
            var reader = new BinaryReader(mem);
            var x      = reader.ReadInt32();

            mem.Seek(-2, SeekOrigin.Current);

            Assert.AreEqual(true, mem.CanRead, "Should be CanRead");
            Assert.AreEqual(false, mem.CanWrite, "Should not be CanWrite");
            Assert.AreEqual(true, mem.CanSeek, "Should be CanSeek");


            try
            {
                mem.SetLength(4);
                Assert.Fail("Should not be able to set the length of a read-only stream.");
            }
            catch (InvalidOperationException)
            {
                // all is well
            }

            try
            {
                mem.WriteByte(0);
                Assert.Fail("Should not be able to write to a readonly stream");
            }
            catch (InvalidOperationException)
            {
                // all is well
            }

            try
            {
                mem.SetBuffer(m_buf1);
                Assert.Fail("Should not be able to set the underlying buffer in a readonly CMemoryStream.");
            }
            catch (InvalidOperationException)
            {
                // all is well
            }
        }
Beispiel #2
0
        public void TestChangingBuffer()
        {
            var buffer = new byte[BUFFER_SIZE];

            m_memStream = new CMemoryStream(m_buf1);
            m_memStream.Read(buffer, 0, m_buf1.Length);

            Assert.AreEqual <long>(m_buf1.Length, m_memStream.Position, "Position");
            Assert.AreEqual <long>(m_buf1.Length, m_memStream.Length, "Length");
            Assert.AreEqual <long>(0, m_memStream.BytesAvailable, "Bytes Available");

            Assert.AreEqual(0, MemCmp(m_buf1, 0, buffer, 0, m_buf1.Length), "Should have read buf1");

            m_memStream.SetBuffer(m_buf2);
            m_memStream.Read(buffer, 0, m_buf2.Length);

            Assert.AreEqual <long>(m_buf2.Length, m_memStream.Position, "Position2");
            Assert.AreEqual <long>(m_buf2.Length, m_memStream.Length, "Length2");
            Assert.AreEqual <long>(0, m_memStream.BytesAvailable, "Bytes Available2");

            Assert.AreEqual(0, MemCmp(m_buf2, 0, buffer, 0, m_buf2.Length), "Should have read buf2");
        }
Beispiel #3
0
        public void TestInternalOperations()
        {
            m_memStream.SetBuffer(m_buffer, START_INDEX, SAMPLE_SIZE);

            Assert.AreEqual <long>(0, m_memStream.Position, "Position");
            Assert.AreEqual <long>(SAMPLE_SIZE, m_memStream.Length, "Length");
            Assert.AreEqual <long>(SAMPLE_SIZE, m_memStream.BytesAvailable, "Bytes Available");

            var buffer    = new byte[BUFFER_SIZE];
            var bytesRead = m_memStream.Read(buffer, 0, BUFFER_SIZE);

            Assert.AreEqual <long>(SAMPLE_SIZE, bytesRead, "Bytes Read");

            Assert.AreEqual(0,
                            MemCmp(buffer, 0, m_buffer, START_INDEX, SAMPLE_SIZE),
                            "Should have read correct subsection");

            m_memStream.Position = 5;
            Assert.AreEqual <long>(5, m_memStream.Position, "New Position");
            Assert.AreEqual <long>(SAMPLE_SIZE, m_memStream.Length, "New Length");
            Assert.AreEqual <long>(SAMPLE_SIZE - 5, m_memStream.BytesAvailable, "New Bytes Available");
        }