Esempio n. 1
0
        private void BinaryReaderReadString_ValidStream_ReturnsExpectedValues(Object[] expectedStrings)
#endif
        {
            // Arrange
            MemoryStream stream = new MemoryStream();

            foreach (String expectedString in expectedStrings)
            {
                Byte[] expectedStringBytes = System.Text.Encoding.UTF8.GetBytes(expectedString);

                UInt32 val = (UInt32)expectedStringBytes.Length;
                while (val >= 0x80)
                {
                    stream.WriteByte((Byte)(val | 0x80));
                    val = val >> 7;
                }
                stream.WriteByte((Byte)val);
                stream.Write(expectedStringBytes, 0, expectedStringBytes.Length);
            }
            stream.Position = 0;
            StaMa.BinaryReader reader = new StaMa.BinaryReader(stream, System.Text.Encoding.UTF8);

            foreach (String expectedString in expectedStrings)
            {
                // Act
                String result = reader.ReadString();

                // Assert
                Assert.That(result, Is.EqualTo(expectedString));
            }

            // Assert
            Assert.That(delegate() { reader.ReadString(); }, Throws.TypeOf(typeof(IOException)));
        }
Esempio n. 2
0
        private void BinaryReaderReadInt16_ValidStream_ReturnsExpectedValues(Int16[] expectedInt16s)
#endif
        {
            // Arrange
            MemoryStream stream = new MemoryStream();

            for (int i = 0; i < expectedInt16s.Length; i++)
            {
                stream.WriteByte((Byte)expectedInt16s[i]);
                stream.WriteByte((Byte)(expectedInt16s[i] >> 8));
            }
            stream.Position = 0;
            StaMa.BinaryReader reader = new StaMa.BinaryReader(stream, System.Text.Encoding.UTF8);

            for (int i = 0; i < expectedInt16s.Length; i++)
            {
                // Act
                Int16 result = reader.ReadInt16();

                // Assert
                Assert.That(result, Is.EqualTo(expectedInt16s[i]));
            }

            // Assert
            Assert.That(delegate() { reader.ReadInt16(); }, Throws.TypeOf(typeof(IOException)));
        }
Esempio n. 3
0
        public void BinaryReaderConstructor_ValidArguments_IsProperlyInitialized()
        {
            // Act
            StaMa.BinaryReader reader = new StaMa.BinaryReader(new MemoryStream(new Byte[] { }), System.Text.Encoding.UTF8);

            // Assert
            Assert.That(delegate() { reader.ReadByte(); }, Throws.TypeOf(typeof(IOException)));
            Assert.That(delegate() { reader.Dispose(); }, Throws.Nothing);
            Assert.That(delegate() { reader.ReadByte(); }, Throws.TypeOf(typeof(ObjectDisposedException)));
            Assert.That(delegate() { reader.ReadInt16(); }, Throws.TypeOf(typeof(ObjectDisposedException)));
            Assert.That(delegate() { reader.ReadString(); }, Throws.TypeOf(typeof(ObjectDisposedException)));
            Assert.That(delegate() { reader.ToString(); }, Throws.Nothing);
            Assert.That(delegate() { reader.Dispose(); }, Throws.Nothing); // 2nd Dispose doesn't access any cleared members.
            Assert.That(delegate() { reader.Close(); }, Throws.Nothing);

            // Act
            StaMa.BinaryReader reader2 = new StaMa.BinaryReader(new MemoryStream(new Byte[] { }), System.Text.Encoding.UTF8);

            // Assert
            Assert.That(delegate() { reader2.Close(); }, Throws.Nothing);
            Assert.That(delegate() { reader2.ReadByte(); }, Throws.TypeOf(typeof(ObjectDisposedException)));
        }
Esempio n. 4
0
        public void BinaryWriterWriteBinaryReaderRead_SomeData_ReaderCanReadWhatWriterWrites()
        {
            // Arrange
            ArrayList data = new ArrayList();

            data.Add((Byte)255);
            data.Add("asdf\0ka sdfk jla ssd");
            data.Add("\u2302");
            data.Add(new String('y', 0x3FFF + 1)); // One more than 2 * 7 bit
            data.Add((Byte)45);
            data.Add((Int16)9876);

            MemoryStream stream = new MemoryStream();

            StaMa.BinaryWriter writer = new StaMa.BinaryWriter(stream, System.Text.Encoding.UTF8);

            // Act
            foreach (Object item in data)
            {
                if (item.GetType() == typeof(String))
                {
                    writer.Write((String)item);
                }
                else if (item.GetType() == typeof(Int16))
                {
                    writer.Write((Int16)item);
                }
                else if (item.GetType() == typeof(Byte))
                {
                    writer.Write((Byte)item);
                }
                else
                {
                    throw new NotSupportedException(item.GetType() + " data not supported by BinaryWriter and BinaryReader");
                }
            }
            writer.Flush();
            stream.Position = 0;

            StaMa.BinaryReader reader = new StaMa.BinaryReader(stream, System.Text.Encoding.UTF8);
            foreach (Object item in data)
            {
                Object result;
                if (item.GetType() == typeof(String))
                {
                    result = reader.ReadString();
                }
                else if (item.GetType() == typeof(Int16))
                {
                    result = reader.ReadInt16();
                }
                else if (item.GetType() == typeof(Byte))
                {
                    result = reader.ReadByte();
                }
                else
                {
                    throw new InvalidOperationException("Unexpected data encountered.");
                }

                // Assert
                Assert.That(result, Is.EqualTo(item));
            }
        }