private static void BlockStreamWriterOnceTest(IBlockTransformer transformer, IBlockCache cache) { using var ms = new KeepOpenMemoryStream(); const int COUNT = 100_000; long expectedLength; using (var writer = new BlockWriteOnceStream(ms, transformer)) { using var binaryWriter = new BinaryWriter(writer, Encoding.ASCII, true); for (var i = 0; i < COUNT; ++i) { binaryWriter.Write(i); } writer.Write(new byte[1 << 22]); expectedLength = writer.Length; } using (var writer = new BlockReadOnlyStream(ms, transformer, cache: cache)) { Assert.AreEqual(expectedLength, writer.Length); using var binaryReader = new BinaryReader(writer, Encoding.ASCII, true); using var binaryCursorReader = new BinaryReader(writer.CreateCursor(), Encoding.ASCII, true); for (var i = 0; i < COUNT; ++i) { Assert.AreEqual(i, binaryReader.ReadInt32()); Assert.AreEqual(i, binaryCursorReader.ReadInt32()); } var buf = new byte[1 << 22]; Assert.AreEqual(buf.Length, writer.Read(buf)); Assert.IsTrue(buf.All(i => i == 0)); } }
private static void BlockStreamSequentialTestRunner(IBlockTransformer transformer) { var buf = new byte[1 << 20]; var outbuf = new byte[1 << 20]; buf.AsSpan().Fill(0x03); using var ms = new KeepOpenMemoryStream(); using (var writer = new SequentialBlockWriteOnceStream(ms, transformer)) { for (var i = 0; i < buf.Length; i += (int)Math.Floor(buf.Length / 20.0) + 1) { buf[i] = (byte)(i % byte.MaxValue); writer.Write(buf.AsSpan(0, i)); writer.Flush(true); } } ms.Seek(0, SeekOrigin.Begin); using (var reader = new SequentialBlockReadonlyStream(ms, transformer)) { for (var i = 0; i < buf.Length; i += (int)Math.Floor(buf.Length / 20.0) + 1) { outbuf.AsSpan(0, i).Clear(); var read = reader.Read(outbuf.AsSpan(0, i)); Assert.AreEqual(i, read); Assert.IsTrue(buf.AsSpan(0, i).SequenceEqual(outbuf.AsSpan(0, i))); } } }
private static void BlockStreamWriterSizeTestRunner(IBlockTransformer transformer, [CanBeNull] IBlockCache cache) { BlockStreamWriterOnceTest(transformer, cache); using var ms = new KeepOpenMemoryStream(); var items = 10_000; using (var writer = new BlockRandomAccessStream(ms, transformer, cache: cache)) { using (var binaryWriter = new BinaryWriter(writer, Encoding.ASCII, true)) { for (var i = 0; i < items; ++i) { binaryWriter.Write(i); } } Assert.AreEqual(writer.Position, writer.Length); writer.Seek(0, SeekOrigin.Begin); Assert.AreEqual(writer.Position, 0); Assert.AreEqual(writer.Length, sizeof(int) * items); using var binaryReader = new BinaryReader(writer, Encoding.ASCII, true); for (var i = 0; i < items; ++i) { Assert.AreEqual(i, binaryReader.ReadInt32()); } } using (var writer = new BlockRandomAccessStream(ms, transformer, cache: cache)) { Assert.AreEqual(writer.Position, 0); Assert.AreEqual(writer.Length, sizeof(int) * items); using (var binaryReader = new BinaryReader(writer, Encoding.ASCII, true)) { for (var i = 0; i < items; ++i) { Assert.AreEqual(i, binaryReader.ReadInt32()); } } Assert.AreEqual(writer.Position, writer.Length); Assert.AreEqual(writer.Length, sizeof(int) * items); if (transformer.MayChangeSize) { Assert.ThrowsException <IOException>(() => { writer.Seek(sizeof(int), SeekOrigin.Begin); using var binaryWriter = new BinaryWriter(writer, Encoding.ASCII, true); for (var i = 0; i < items; ++i) { binaryWriter.Write(i); } }); Assert.AreEqual(sizeof(int), writer.Position); } writer.Seek(0, SeekOrigin.Begin); Assert.AreEqual(writer.Position, 0); Assert.AreEqual(writer.Length, sizeof(int) * items); using (var binaryReader = new BinaryReader(writer, Encoding.ASCII, true)) { for (var i = 0; i < items; ++i) { Assert.AreEqual(i, binaryReader.ReadInt32()); } } Assert.AreEqual(writer.Position, writer.Length); writer.Seek(-4, SeekOrigin.End); Assert.AreEqual(writer.Position + 4, writer.Length); Assert.AreEqual(4, writer.Read(new byte[5])); Assert.AreEqual(writer.Position, writer.Length); Assert.AreEqual(0, writer.Read(new byte[5])); writer.SetLength(0); Assert.AreEqual(writer.Length, 0); Assert.AreEqual(writer.Position, 0); writer.Flush(true); } }
public void BlockStreamWriterNone2Test() { using var ms = new KeepOpenMemoryStream(); const int COUNT = 100_000; using (var writer = new BlockRandomAccessStream(ms)) { using (var binaryWriter = new BinaryWriter(writer, Encoding.ASCII, true)) { for (var i = 0; i < COUNT; ++i) { binaryWriter.Write(i); } } Assert.AreEqual(writer.Position, writer.Length); writer.Seek(0, SeekOrigin.Begin); Assert.AreEqual(writer.Position, 0); Assert.AreEqual(writer.Length, sizeof(int) * COUNT); using var binaryReader = new BinaryReader(writer, Encoding.ASCII, true); for (var i = 0; i < COUNT; ++i) { Assert.AreEqual(i, binaryReader.ReadInt32()); } } using (var writer = new BlockRandomAccessStream(ms)) { Assert.AreEqual(writer.Position, 0); Assert.AreEqual(writer.Length, sizeof(int) * COUNT); using (var binaryReader = new BinaryReader(writer, Encoding.ASCII, true)) { for (var i = 0; i < COUNT; ++i) { Assert.AreEqual(i, binaryReader.ReadInt32()); } } writer.Seek(sizeof(int), SeekOrigin.Begin); using (var binaryWriter = new BinaryWriter(writer, Encoding.ASCII, true)) { for (var i = 0; i < COUNT; ++i) { binaryWriter.Write(i); } } Assert.AreEqual(writer.Position, writer.Length); writer.Seek(0, SeekOrigin.Begin); Assert.AreEqual(writer.Position, 0); Assert.AreEqual(writer.Length, sizeof(int) * (COUNT + 1)); using (var binaryReader = new BinaryReader(writer, Encoding.ASCII, false)) { for (var i = 0; i < COUNT + 1; ++i) { Assert.AreEqual(Math.Max(i - 1, 0), binaryReader.ReadInt32()); } } Assert.AreEqual(writer.Position, writer.Length); writer.Seek(-4, SeekOrigin.End); Assert.AreEqual(writer.Position + 4, writer.Length); Assert.AreEqual(4, writer.Read(new byte[5])); Assert.AreEqual(writer.Position, writer.Length); Assert.AreEqual(0, writer.Read(new byte[5])); writer.SetLength(0); Assert.AreEqual(writer.Length, 0); Assert.AreEqual(writer.Position, 0); } }