public void UnmanagedTest() { using var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream)) { writer.Write(1); writer.Dispose(); // double dispose is OK } stream.Seek(0, SeekOrigin.Begin); using var reader = new Infrastructure.BinaryReader(stream); Assert.AreEqual(1, reader.Read <int>()); Assert.Throws <EndOfStreamException>(() => reader.Read <int>()); Assert.Throws <EndOfStreamException>(() => reader.ReadString()); reader.Dispose(); // double dispose is OK }
public void ManyUnmanagedBoundaryTests(int bufferSize) { using var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream)) { for (var i = 0; i < bufferSize / sizeof(int); i++) { writer.Write(i); } } stream.Seek(0, SeekOrigin.Begin); using var reader = new Infrastructure.BinaryReader(stream); for (var i = 0; i < bufferSize / sizeof(int); i++) { Assert.AreEqual(i, reader.Read <int>()); } Assert.Throws <EndOfStreamException>(() => reader.Read <int>()); Assert.Throws <EndOfStreamException>(() => reader.ReadString()); }
public void EmptyStringTest() { var str = string.Empty; using var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream)) { writer.WriteString(str); } stream.Seek(0, SeekOrigin.Begin); using var reader = new Infrastructure.BinaryReader(stream); Assert.AreEqual(str, reader.ReadString()); Assert.Throws <EndOfStreamException>(() => reader.Read <int>()); Assert.Throws <EndOfStreamException>(() => reader.ReadString()); }
public void StringThatCannotFitBufferTests(int bufferSize) { var str = CreateString((bufferSize + sizeof(char)) / sizeof(char)); using var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream)) { writer.WriteString(str); } stream.Seek(0, SeekOrigin.Begin); using var reader = new Infrastructure.BinaryReader(stream); Assert.AreEqual(str, reader.ReadString()); Assert.Throws <EndOfStreamException>(() => reader.Read <int>()); Assert.Throws <EndOfStreamException>(() => reader.ReadString()); }
public void ManyStringBoundaryTests(int bufferSize) { const string str = "Test"; using var stream = new MemoryStream(); using (var writer = new BinaryWriter(stream)) { for (var i = 0; i < bufferSize / (sizeof(int) + sizeof(char) * str.Length); i++) { writer.WriteString(str); } } stream.Seek(0, SeekOrigin.Begin); using var reader = new Infrastructure.BinaryReader(stream); for (var i = 0; i < bufferSize / (sizeof(int) + sizeof(char) * str.Length); i++) { Assert.AreEqual(str, reader.ReadString()); } Assert.Throws <EndOfStreamException>(() => reader.Read <int>()); Assert.Throws <EndOfStreamException>(() => reader.ReadString()); }