private void BinaryReaderReadByte_ValidStream_ReturnsExpectedValues(Byte[] expectedBytes) #endif { // Arrange MemoryStream stream = new MemoryStream(); stream.Write(expectedBytes, 0, expectedBytes.Length); stream.Position = 0; StaMa.BinaryReader reader = new StaMa.BinaryReader(stream, System.Text.Encoding.UTF8); for (int i = 0; i < expectedBytes.Length; i++) { // Act Byte result = reader.ReadByte(); // Assert Assert.That(result, Is.EqualTo(expectedBytes[i])); } // Assert Assert.That(delegate() { reader.ReadByte(); }, Throws.TypeOf(typeof(IOException))); }
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))); }
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)); } }