Beispiel #1
0
        public void testReadSingleBytes()
        {
            var u = new UnionInputStream();

            Assert.IsTrue(u.isEmpty());
            u.add(new MemoryStream(new byte[] { 1, 0, 2 }));
            u.add(new MemoryStream(new byte[] { 3 }));
            u.add(new MemoryStream(new byte[] { 4, 5 }));

            Assert.IsFalse(u.isEmpty());
            Assert.AreEqual(3, u.available());
            Assert.AreEqual(1, u.read());
            Assert.AreEqual(0, u.read());
            Assert.AreEqual(2, u.read());
            Assert.AreEqual(0, u.available());

            Assert.AreEqual(3, u.read());
            Assert.AreEqual(0, u.available());

            Assert.AreEqual(4, u.read());
            Assert.AreEqual(1, u.available());
            Assert.AreEqual(5, u.read());
            Assert.AreEqual(0, u.available());
            Assert.AreEqual(-1, u.read());

            Assert.IsTrue(u.isEmpty());
            u.add(new MemoryStream(new byte[] { (byte)255 }));
            Assert.AreEqual(255, u.read());
            Assert.AreEqual(-1, u.read());
            Assert.IsTrue(u.isEmpty());
        }
Beispiel #2
0
        public void testReadByteBlocks()
        {
            var u = new UnionInputStream();

            u.add(new MemoryStream(new byte[] { 1, 0, 2 }));
            u.add(new MemoryStream(new byte[] { 3 }));
            u.add(new MemoryStream(new byte[] { 4, 5 }));

            var r = new byte[5];

            Assert.AreEqual(5, u.Read(r, 0, 5));
            Assert.IsTrue(r.SequenceEqual(new byte[] { 1, 0, 2, 3, 4 }));
            Assert.AreEqual(1, u.Read(r, 0, 5));
            Assert.AreEqual(5, r[0]);
            Assert.AreEqual(-1, u.Read(r, 0, 5));
        }
Beispiel #3
0
        public void testCloseDuringClose()
        {
            var u      = new UnionInputStream();
            var closed = new bool[2];

            u.add(new MockMemoryStream(new byte[] { 1 }, () => { closed[0] = true; }));
            u.add(new MockMemoryStream(new byte[] { 2 }, () => { closed[1] = true; }));

            Assert.IsFalse(closed[0]);
            Assert.IsFalse(closed[1]);

            u.Close();

            Assert.IsTrue(closed[0]);
            Assert.IsTrue(closed[1]);
        }
Beispiel #4
0
        public void testMarkSupported()
        {
            var u = new UnionInputStream();

            Assert.IsFalse(u.markSupported());
            u.add(new MemoryStream(new byte[] { 1, 0, 2 }));
            Assert.IsFalse(u.markSupported());
        }
Beispiel #5
0
        public void testSkip()
        {
            var u = new UnionInputStream();

            u.add(new MemoryStream(new byte[] { 1, 0, 2 }));
            u.add(new MemoryStream(new byte[] { 3 }));
            u.add(new MemoryStream(new byte[] { 4, 5 }));
            Assert.AreEqual(0, u.skip(0));
            Assert.AreEqual(4, u.skip(4));
            Assert.AreEqual(4, u.read());
            Assert.AreEqual(1, u.skip(5));
            Assert.AreEqual(0, u.skip(5));
            Assert.AreEqual(-1, u.read());

            u.add(new MockMemoryStream(new byte[] { 20, 30 }, null)); // can't mock skip behavior :-(
            Assert.AreEqual(2, u.skip(8));
            Assert.AreEqual(-1, u.read());
        }
Beispiel #6
0
        public void testAutoCloseDuringRead()
        {
            var u      = new UnionInputStream();
            var closed = new bool[2];

            u.add(new MockMemoryStream(new byte[] { 1 }, () => { closed[0] = true; }));
            u.add(new MockMemoryStream(new byte[] { 2 }, () => { closed[1] = true; }));

            Assert.IsFalse(closed[0]);
            Assert.IsFalse(closed[1]);

            Assert.AreEqual(1, u.read());
            Assert.IsFalse(closed[0]);
            Assert.IsFalse(closed[1]);

            Assert.AreEqual(2, u.read());
            Assert.IsTrue(closed[0]);
            Assert.IsFalse(closed[1]);

            Assert.AreEqual(-1, u.read());
            Assert.IsTrue(closed[0]);
            Assert.IsTrue(closed[1]);
        }
Beispiel #7
0
        public void testExceptionDuringClose()
        {
            var u = new UnionInputStream();

            u.add(new MockMemoryStream(new byte[] { 1 }, () => { throw new IOException("I AM A TEST"); }));

            try
            {
                u.Close();
                Assert.Fail("close ignored inner stream exception");
            }
            catch (IOException e)
            {
                Assert.AreEqual("I AM A TEST", e.Message);
            }
        }