public void TestIllegalTypeConversionFailureDoesNotIncrementPosition1() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 0, 255, 78 }; streamMessage.WriteBytes(bytes); streamMessage.Reset(); AssertGetStreamEntryThrowsMessageFormatException <bool>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <byte>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <short>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <char>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <int>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <long>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <float>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <double>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <string>(streamMessage); byte[] retrievedByteArray = new byte[bytes.Length]; int readBytesLength = streamMessage.ReadBytes(retrievedByteArray); Assert.AreEqual(bytes.Length, readBytesLength, "Number of bytes read did not match original array length"); CollectionAssert.AreEqual(bytes, retrievedByteArray, "Expected array to equal retrieved bytes"); Assert.AreEqual(-1, streamMessage.ReadBytes(retrievedByteArray), "Expected completion return value"); }
public void TestReadObjectAfterPartialReadBytesThrowsMFE() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 4, 44, 99 }; streamMessage.WriteBytes(bytes); streamMessage.Reset(); // start reading via readBytes int partialLength = 2; byte[] retrievedByteArray = new byte[partialLength]; int readBytesLength = streamMessage.ReadBytes(retrievedByteArray); Assert.AreEqual(partialLength, readBytesLength); CollectionAssert.AreEqual(bytes.Take(partialLength), retrievedByteArray, "Expected array subset to equal retrieved bytes"); // check that using readObject does not return the full/remaining bytes as a new array Assert.Throws <MessageFormatException>(() => streamMessage.ReadObject()); // finish reading via reaBytes to ensure it can be completed readBytesLength = streamMessage.ReadBytes(retrievedByteArray); Assert.AreEqual(bytes.Length - partialLength, readBytesLength); CollectionAssert.AreEqual(bytes.Skip(partialLength).Take(bytes.Length), retrievedByteArray.Take(readBytesLength), "Expected array subset to equal retrieved bytes"); }
public void TestReadBytesFullWithUndersizedDestinationArrayUsingMultipleReads() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 3, 78, 253, 26, 8 }; Assert.AreEqual(1, bytes.Length % 2, "bytes should be odd length"); int undersizedLength = 2; int remaining = 1; streamMessage.WriteBytes(bytes); streamMessage.Reset(); byte[] undersizedDestination = new byte[undersizedLength]; byte[] fullRetrievedBytes = new byte[bytes.Length]; Assert.AreEqual(undersizedLength, streamMessage.ReadBytes(undersizedDestination), "Number of bytes read did not match destination array length"); int read = undersizedLength; Array.Copy(undersizedDestination, 0, fullRetrievedBytes, 0, undersizedLength); Assert.AreEqual(undersizedLength, streamMessage.ReadBytes(undersizedDestination), "Number of bytes read did not match destination array length"); Array.Copy(undersizedDestination, 0, fullRetrievedBytes, read, undersizedLength); read += undersizedLength; Assert.AreEqual(remaining, streamMessage.ReadBytes(undersizedDestination), "Number of bytes read did not match expectation"); Array.Copy(undersizedDestination, 0, fullRetrievedBytes, read, remaining); read += remaining; CollectionAssert.AreEqual(bytes, fullRetrievedBytes, "Expected array to equal retrieved bytes"); }
public void TestReadBytesWithZeroLengthSource() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); streamMessage.WriteBytes(new byte[0]); streamMessage.Reset(); byte[] fullRetrievedBytes = new byte[1]; Assert.AreEqual(0, streamMessage.ReadBytes(fullRetrievedBytes), "Expected no bytes to be read, as none were written"); }
public void TestReadObjectForBytesReturnsNewArray() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 11, 44, 99 }; streamMessage.WriteBytes(bytes); streamMessage.Reset(); byte[] retrievedBytes = (byte[])streamMessage.ReadObject(); Assert.AreNotSame(bytes, retrievedBytes, "Expected different array objects"); CollectionAssert.AreEqual(bytes, retrievedBytes, "Expected arrays to be equal"); }
public void TestReadBytesFullWithOversizedDestinationArray() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 4, 115, 255 }; streamMessage.WriteBytes(bytes); streamMessage.Reset(); byte[] oversizedDestination = new byte[bytes.Length + 1]; int readBytesLength = streamMessage.ReadBytes(oversizedDestination); Assert.AreEqual(bytes.Length, readBytesLength, "Number of bytes read did not match original array length"); CollectionAssert.AreEqual(bytes, oversizedDestination.Take(readBytesLength), "Expected array subset to equal retrieved bytes"); }
public void TestReadBytesFullWithPreciselySizedDestinationArray() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 11, 44, 99 }; streamMessage.WriteBytes(bytes); streamMessage.Reset(); byte[] retrievedByteArray = new byte[bytes.Length]; int readBytesLength = streamMessage.ReadBytes(retrievedByteArray); Assert.AreEqual(bytes.Length, readBytesLength, "Number of bytes read did not match original array length"); CollectionAssert.AreEqual(bytes, retrievedByteArray, "Expected array to equal retrieved bytes"); Assert.AreEqual(-1, streamMessage.ReadBytes(retrievedByteArray), "Expected completion return value"); }
public void TestWriteBytesReadLegal() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] value = { 0, 255, 78 }; streamMessage.WriteBytes(value); streamMessage.Reset(); byte[] dest = new byte[value.Length]; int readBytesLength = streamMessage.ReadBytes(dest); Assert.AreEqual(value.Length, readBytesLength, "Number of bytes read did not match expectation"); CollectionAssert.AreEqual(value, dest, "value not as expected"); }
public void TestReadBytesWithZeroLengthDestination() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] bytes = { 11, 44, 99 }; streamMessage.WriteBytes(bytes); streamMessage.Reset(); byte[] zeroDestination = new byte[0]; byte[] fullRetrievedBytes = new byte[bytes.Length]; Assert.AreEqual(0, streamMessage.ReadBytes(zeroDestination), "Expected no bytes to be read"); Assert.AreEqual(bytes.Length, streamMessage.ReadBytes(fullRetrievedBytes), "Expected all bytes to be read"); CollectionAssert.AreEqual(bytes, fullRetrievedBytes, "Expected arrays to be equal"); Assert.AreEqual(-1, streamMessage.ReadBytes(zeroDestination), "Expected completion signal"); }
public void TestWriteBytesReadIllegal() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] value = { 0, 255, 78 }; streamMessage.WriteBytes(value); streamMessage.Reset(); AssertGetStreamEntryThrowsMessageFormatException <bool>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <byte>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <short>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <char>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <int>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <long>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <float>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <double>(streamMessage); AssertGetStreamEntryThrowsMessageFormatException <string>(streamMessage); }
public void TestWriteBytesWithOffsetAndLength() { NmsStreamMessage streamMessage = factory.CreateStreamMessage(); byte[] orig = Encoding.ASCII.GetBytes("myBytesAll"); // extract the segment containing 'Bytes' int offset = 2; int length = 5; byte[] segment = orig.Skip(offset).Take(length).ToArray(); // set the same section from the original bytes streamMessage.WriteBytes(orig, offset, length); streamMessage.Reset(); byte[] retrieved = (byte[])streamMessage.ReadObject(); // verify the retrieved bytes from the stream equal the segment but are not the same Assert.AreNotSame(orig, retrieved); Assert.AreNotSame(segment, retrieved); CollectionAssert.AreEqual(segment, retrieved); }
public void WriteBytes(byte[] value) { message.WriteBytes(value); }