Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        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);
            }
        }