public void TestCopyToBinaryWriterLargeMemoryStream()
        {
            var data0 = RandomBytes(6001001, -0x02F0B4EF);

            using (var bb = new TestByteBuffer(data0))
                using (var ms = new MemoryStream())
                    using (var bw = new BinaryWriter(ms))
                    {
                        bb.CopyTo(bw);

                        Assert.AreEqual(6001001, ms.Length, "CopyTo(BinaryWriter(MemoryStream)) - length");
                        Assert.AreEqual(0, bb.StreamPosition, "CopyTo(BinaryWriter(MemoryStream)) - StreamPosition - after Stream initialized");
                        AssertAreEqual(data0, ToArray(ms), "CopyTo(BinaryWriter(MemoryStream)) - destination data");
                        AssertAreEqual(data0, bb.DumpTestData(), "CopyTo(BinaryWriter(MemoryStream)) - source data");
                    }

            using (var bb = new TestByteBuffer(data0))
                using (var ms = new MemoryStream())
                    using (var bw = new BinaryWriter(ms))
                    {
                        bb.InitializeStream(position: 42);
                        bb.CopyTo(bw);

                        Assert.AreEqual(6001001, ms.Length, "CopyTo(BinaryWriter(MemoryStream)) - length - after Stream initialized");
                        Assert.AreEqual(42, bb.StreamPosition, "CopyTo(BinaryWriter(MemoryStream)) - StreamPosition - after Stream initialized");
                        AssertAreEqual(data0, ToArray(ms), "CopyTo(BinaryWriter(MemoryStream)) - destination data after Stream initialized");
                        AssertAreEqual(data0, bb.DumpTestData(), "CopyTo(BinaryWriter(MemoryStream)) - source data after Stream initialized");
                    }
        }
        public void TestCopyFrom()
        {
            var data0 = RandomBytes(9432, -0x02F0B4EF);
            var data1 = RandomBytes(8472, -0x7BFC1702);

            using (var bb = new TestByteBuffer(data0))
                using (var ms = new MemoryStream(TestByteBuffer.CopyBytes(data1)))
                {
                    var result = bb.CopyFrom(ms, 4195);
                    Assert.AreEqual(4195, bb.Length, "CopyFrom - length");
                    Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition");
                    Assert.AreEqual(4195, result, "CopyFrom - first batch return value");
                    AssertAreEqual(data1, 0, bb.DumpTestData(), 0, 4195, "CopyFrom - first batch data");

                    result = bb.CopyFrom(ms, 9000);
                    Assert.AreEqual(9000, bb.Length, "CopyFrom - length");             // apparently the length of the buffer after CopyFrom is the bytes requested, and not actual bytes read
                    Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition");
                    Assert.AreEqual(4277, result, "CopyFrom - second batch return value");
                    AssertAreEqual(data1, 4195, bb.DumpTestData(), 0, 4277, "CopyFrom - second batch data");
                }

            using (var bb = new TestByteBuffer(data0))
                using (var ms = new MemoryStream(TestByteBuffer.CopyBytes(data1)))
                {
                    bb.InitializeStream(position: 42);

                    var result = bb.CopyFrom(ms, 4195);
                    Assert.AreEqual(4195, bb.Length, "CopyFrom - length - after Stream initialized");
                    Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition - after Stream initialized");
                    Assert.AreEqual(4195, result, "CopyFrom - first batch return value - after Stream initialized");
                    AssertAreEqual(data1, 0, bb.DumpTestData(), 0, 4195, "CopyFrom - first batch data - after Stream initialized");

                    bb.InitializeStream(position: 42);

                    result = bb.CopyFrom(ms, 9000);
                    Assert.AreEqual(9000, bb.Length, "CopyFrom - length - after Stream initialized");             // apparently the length of the buffer after CopyFrom is the bytes requested, and not actual bytes read
                    Assert.AreEqual(0, bb.StreamPosition, "CopyFrom - StreamPosition - after Stream initialized");
                    Assert.AreEqual(4277, result, "CopyFrom - second batch return value - after Stream initialized");
                    AssertAreEqual(data1, 4195, bb.DumpTestData(), 0, 4277, "CopyFrom - second batch data - after Stream initialized");
                }
        }
        public void TestSetStringEvenLengthPadding()
        {
            var defCharSet = new byte[]
            {
                0x4D, 0x4F, 0x4E, 0x4F, 0x43, 0x48, 0x52, 0x4F,
                0x4D, 0x45, 0x32
            };

            var scsH31 = new byte[]
            {
                65,
                0x59, 0x61, 0x6D, 0x61, 0x64, 0x61, 0x5E, 0x54,
                0x61, 0x72, 0x6F, 0x75, 0x3D, 0x1B, 0x24, 0x42,
                0x3B, 0x33, 0x45, 0x44, 0x1B, 0x28, 0x42, 0x5E,
                0x1B, 0x24, 0x42, 0x42, 0x40, 0x4F, 0x3A, 0x1B,
                0x28, 0x42, 0x3D, 0x1B, 0x24, 0x42, 0x24, 0x64,
                0x24, 0x5E, 0x24, 0x40, 0x1B, 0x28, 0x42, 0x5E,
                0x1B, 0x24, 0x42, 0x24, 0x3F, 0x24, 0x6D, 0x24,
                0x26, 0x1B, 0x28, 0x42
            };

            using (var bb = new TestByteBuffer())
            {
                bb.SetString(@"MONOCHROME2", 0x20);
                Assert.AreEqual(0, bb.StreamPosition, @"SetString - StreamPosition - Default Character Set");
                AssertAreEqual(defCharSet, 0, bb.DumpTestData(), 0, defCharSet.Length, @"SetString - Default Character Set");
                AssertAreEqual(new byte[] { 0x20 }, 0, bb.DumpTestData(), defCharSet.Length, 1, @"SetString - Default Character Set - padding");
            }

            using (var bb = new TestByteBuffer {
                SpecificCharacterSet = @"\ISO 2022 IR 87"
            })
            {
                bb.SetString(@"AYamada^Tarou=山田^太郎=やまだ^たろう", 0x20);
                Assert.AreEqual(0, bb.StreamPosition, @"SetString - StreamPosition - H31 Example (\ISO 2022 IR 87)");
                AssertAreEqual(scsH31, 0, bb.DumpTestData(), 0, scsH31.Length, @"SetString - H31 Example (\ISO 2022 IR 87)");
                AssertAreEqual(new byte[] { 0x20 }, 0, bb.DumpTestData(), scsH31.Length, 1, @"SetString - H31 Example (\ISO 2022 IR 87) - padding");
            }
        }
        public void TestAppend()
        {
            var data0 = RandomBytes(8472, -0x02F0B4EF);
            var data1 = RandomBytes(8472, -0x7BFC1702);

            using (var bb = new TestByteBuffer(data0))
            {
                bb.Append(data1, 4195, 4194);
                Assert.AreEqual(8472 + 4194, bb.Length, "Append - length");
                Assert.AreEqual(0, bb.StreamPosition, "Append - StreamPosition");
                AssertAreEqual(data0, 0, bb.DumpTestData(), 0, 8472, "Append - original data");
                AssertAreEqual(data1, 4195, bb.DumpTestData(), 8472, 4194, "Append - appended data");
            }

            using (var bb = new TestByteBuffer(data0))
            {
                bb.InitializeStream(position: 42);
                bb.Append(data1, 4195, 4194);
                Assert.AreEqual(8472 + 4194, bb.Length, "Append - length - after Stream initialized");
                Assert.AreEqual(42, bb.StreamPosition, "Append - StreamPosition - after Stream initialized");
                AssertAreEqual(data0, 0, bb.DumpTestData(), 0, 8472, "Append - original data - after Stream initialized");
                AssertAreEqual(data1, 4195, bb.DumpTestData(), 8472, 4194, "Append - appended data - after Stream initialized");
            }
        }
        public void TestSwap1()
        {
            var data0 = RandomBytes(8472, -0x02F0B4EF);

            using (var bb = new TestByteBuffer(data0))
            {
                bb.Swap(1);
                Assert.AreEqual(0, bb.StreamPosition, "Swap(1) - StreamPosition");
                AssertAreEqual(data0, bb.DumpTestData(), "Swap(1)");
            }

            using (var bb = new TestByteBuffer(data0))
            {
                bb.InitializeStream(position: 42);
                bb.Swap(1);
                Assert.AreEqual(42, bb.StreamPosition, "Swap(1) - StreamPosition - after Stream initialized");
                AssertAreEqual(data0, bb.DumpTestData(), "Swap(1) - after Stream initialized");
            }
        }
        public void TestChop()
        {
            var data0 = RandomBytes(6000, -0x02F0B4EF);

            using (var bb = new TestByteBuffer(data0))
            {
                bb.Chop(6000);
                Assert.AreEqual(0, bb.Length, "Chop(6000) - Length");
                Assert.AreEqual(0, bb.StreamPosition, "Chop(6000) - StreamPosition");
                AssertAreEqual(new byte[0], bb.DumpTestData(), "Chop(6000)");
            }

            using (var bb = new TestByteBuffer(data0))
            {
                bb.Chop(401);
                Assert.AreEqual(6000 - 401, bb.Length, "Chop(401) - Length");
                Assert.AreEqual(0, bb.StreamPosition, "Chop(401) - StreamPosition");
                AssertAreEqual(data0, 401, bb.DumpTestData(), 0, 5599, "Chop(401)");
            }

            using (var bb = new TestByteBuffer(data0))
            {
                bb.InitializeStream(position: 42);
                bb.Chop(6000);
                Assert.AreEqual(0, bb.Length, "Chop(6000) - Length - after Stream initialized");
                Assert.AreEqual(0, bb.StreamPosition, "Chop(6000) - StreamPosition - after Stream initialized");
                AssertAreEqual(new byte[0], bb.DumpTestData(), "Chop(6000) - after Stream initialized");
            }

            using (var bb = new TestByteBuffer(data0))
            {
                bb.InitializeStream(position: 42);
                bb.Chop(401);
                Assert.AreEqual(6000 - 401, bb.Length, "Chop(401) - Length - after Stream initialized");
                Assert.AreEqual(0, bb.StreamPosition, "Chop(401) - StreamPosition - after Stream initialized");
                AssertAreEqual(data0, 401, bb.DumpTestData(), 0, 5599, "Chop(401) - after Stream initialized");
            }
        }
        public void TestFromBytes()
        {
            var data0 = RandomBytes(7101, -0x02F0B4EF);
            var data1 = RandomBytes(9431, -0x7BFC1702);

            using (var bb = new TestByteBuffer(data0))
            {
                bb.FromBytes(data1);

                Assert.AreEqual(9431, bb.Length, "FromBytes - length");
                Assert.AreEqual(0, bb.StreamPosition, "FromBytes - StreamPosition");
                AssertAreEqual(data1, bb.DumpTestData(), "FromBytes");
            }

            using (var bb = new TestByteBuffer(data0))
            {
                bb.InitializeStream(position: 42);
                bb.FromBytes(data1);

                Assert.AreEqual(9431, bb.Length, "FromBytes - length - after Stream initialized");
                Assert.AreEqual(0, bb.StreamPosition, "FromBytes - StreamPosition - after Stream initialized");
                AssertAreEqual(data1, bb.DumpTestData(), "FromBytes - after Stream initialized");
            }
        }