private void BinaryWriterWriteInt16_ValidStream_ReturnsExpectedValues(Int16[] expectedInt16s) #endif { // Arrange Byte[] expectedBytes = new Byte[2 * expectedInt16s.Length]; int pos = 0; for (int i = 0; i < expectedInt16s.Length; i++) { expectedBytes[pos] = (Byte)expectedInt16s[i]; pos += 1; expectedBytes[pos] = (Byte)(expectedInt16s[i] >> 8); pos += 1; } MemoryStream stream = new MemoryStream(); //stream.Position = 0; StaMa.BinaryWriter writer = new StaMa.BinaryWriter(stream, System.Text.Encoding.UTF8); // Act for (int i = 0; i < expectedInt16s.Length; i++) { writer.Write(expectedInt16s[i]); } writer.Flush(); // Assert Assert.That(stream.ToArray(), Is.EqualTo(expectedBytes)); }
private void BinaryWriterWriteByte_ValidStream_ReturnsExpectedValues(Byte[] expectedBytes) #endif { // Arrange MemoryStream stream = new MemoryStream(); //stream.Write(expectedBytes, 0, expectedBytes.Length); //stream.Position = 0; StaMa.BinaryWriter writer = new StaMa.BinaryWriter(stream, System.Text.Encoding.UTF8); // Act for (int i = 0; i < expectedBytes.Length; i++) { writer.Write(expectedBytes[i]); } writer.Flush(); // Assert Assert.That(stream.ToArray(), Is.EqualTo(expectedBytes)); }
public void BinaryWriterConstructor_ValidArguments_IsProperlyInitialized() { // Act StaMa.BinaryWriter writer = new StaMa.BinaryWriter(new MemoryStream(), System.Text.Encoding.UTF8); // Assert Assert.That(delegate() { writer.Flush(); }, Throws.Nothing); Assert.That(delegate() { writer.Dispose(); }, Throws.Nothing); Assert.That(delegate() { writer.Write((Byte)0); }, Throws.TypeOf(typeof(ObjectDisposedException))); Assert.That(delegate() { writer.Write((Int16)0); }, Throws.TypeOf(typeof(ObjectDisposedException))); Assert.That(delegate() { writer.Write(String.Empty); }, Throws.TypeOf(typeof(ObjectDisposedException))); Assert.That(delegate() { writer.Flush(); }, Throws.TypeOf(typeof(ObjectDisposedException))); Assert.That(delegate() { writer.ToString(); }, Throws.Nothing); Assert.That(delegate() { writer.Dispose(); }, Throws.Nothing); // 2nd Dispose doesn't access any cleared members. Assert.That(delegate() { writer.Close(); }, Throws.Nothing); // Act StaMa.BinaryWriter writer2 = new StaMa.BinaryWriter(new MemoryStream(new Byte[] { }), System.Text.Encoding.UTF8); // Assert Assert.That(delegate() { writer2.Close(); }, Throws.Nothing); Assert.That(delegate() { writer2.Write((Byte)0); }, Throws.TypeOf(typeof(ObjectDisposedException))); }
private void BinaryWriterWriteString_ValidStream_ReturnsExpectedValues(Object[] expectedStrings) #endif { // Arrange MemoryStream expectedData = new MemoryStream(); foreach (String expectedString in expectedStrings) { Byte[] expectedStringBytes = System.Text.Encoding.UTF8.GetBytes(expectedString); UInt32 val = (UInt32)expectedStringBytes.Length; while (val >= 0x80) { expectedData.WriteByte((Byte)(val | 0x80)); val = val >> 7; } expectedData.WriteByte((Byte)val); expectedData.Write(expectedStringBytes, 0, expectedStringBytes.Length); } expectedData.Flush(); Byte[] expectedBytes = expectedData.ToArray(); MemoryStream stream = new MemoryStream(); StaMa.BinaryWriter writer = new StaMa.BinaryWriter(stream, System.Text.Encoding.UTF8); // Act foreach (String expectedString in expectedStrings) { writer.Write(expectedString); } writer.Flush(); // Assert Assert.That(stream.ToArray(), Is.EqualTo(expectedBytes)); }
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)); } }