コード例 #1
0
ファイル: TestStreams.cs プロジェクト: radiaku/csharptest-net
        public void TestCombinedStream()
        {
            DisposableFlag f1 = new DisposableFlag();
            DisposableFlag f2 = new DisposableFlag();

            Assert.IsFalse(f1.Disposed || f2.Disposed);

            Stream ms1 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("Hello"))).WithDisposeOf(f1);
            Stream ms2 = new DisposingStream(new MemoryStream(Encoding.ASCII.GetBytes("There"))).WithDisposeOf(f2);

            Assert.AreEqual(ms1.Length, ms2.Length);

            int size = (int)ms1.Length;

            byte[] bytes = new byte[size * 2];

            using (Stream cs = new CombinedStream(ms1, ms2))
            {
                Assert.IsTrue(cs.CanRead);

                Assert.IsFalse(f1.Disposed);
                Assert.AreEqual(size, cs.Read(bytes, 0, size));
                Assert.IsFalse(f1.Disposed);                 //still not disposed util read of 0 bytes
                Assert.AreEqual(1, cs.Read(bytes, size, 1)); //read 1 more byte
                Assert.IsTrue(f1.Disposed);
                //now finish the second one...
                Assert.IsFalse(f2.Disposed);
                Assert.AreEqual(size - 1, cs.Read(bytes, size + 1, size - 1));
                Assert.IsFalse(f2.Disposed);//still not done
                Assert.AreEqual(-1, cs.ReadByte());
                Assert.IsTrue(f2.Disposed);
            }

            Assert.AreEqual("HelloThere", Encoding.ASCII.GetString(bytes));

            //both were disposed
            Assert.IsTrue(f1.Disposed && f2.Disposed);
        }