Read() public method

public Read ( ) : int
return int
Exemplo n.º 1
0
        public virtual void Test()
        {
            char[] buf = new char[4];

            using (ReusableStringReader reader = new ReusableStringReader())
            {
                Assert.AreEqual(-1, reader.Read());
                Assert.AreEqual(-1, reader.Read(new char[1], 0, 1));
                Assert.AreEqual(-1, reader.Read(new char[2], 1, 1));
                //Assert.AreEqual(-1, reader.Read(CharBuffer.wrap(new char[2])));

                reader.SetValue("foobar");
                Assert.AreEqual(4, reader.Read(buf, 0, 4));
                Assert.AreEqual("foob", new string(buf));
                Assert.AreEqual(2, reader.Read(buf, 0, 2));
                Assert.AreEqual("ar", new string(buf, 0, 2));
                Assert.AreEqual(-1, reader.Read(buf, 2, 0));
            }

            using (ReusableStringReader reader = new ReusableStringReader())
            {
                reader.SetValue("foobar");
                Assert.AreEqual(0, reader.Read(buf, 1, 0));
                Assert.AreEqual(3, reader.Read(buf, 1, 3));
                Assert.AreEqual("foo", new string(buf, 1, 3));
                Assert.AreEqual(2, reader.Read(buf, 2, 2));
                Assert.AreEqual("ba", new string(buf, 2, 2));
                Assert.AreEqual('r', (char)reader.Read());
                Assert.AreEqual(-1, reader.Read(buf, 2, 0));
                reader.Dispose();

                reader.SetValue("foobar");
                StringBuilder sb = new StringBuilder();
                int           ch;
                while ((ch = reader.Read()) != -1)
                {
                    sb.Append((char)ch);
                }
                Assert.AreEqual("foobar", sb.ToString());
            }
        }
        public virtual void Test()
        {
            ReusableStringReader reader = new ReusableStringReader();
            Assert.AreEqual(-1, reader.Read());
            Assert.AreEqual(-1, reader.Read(new char[1], 0, 1));
            Assert.AreEqual(-1, reader.Read(new char[2], 1, 1));
            //Assert.AreEqual(-1, reader.Read(CharBuffer.wrap(new char[2])));

            reader.Value = "foobar";
            char[] buf = new char[4];
            Assert.AreEqual(4, reader.Read(buf, 0, 4));
            Assert.AreEqual("foob", new string(buf));
            Assert.AreEqual(2, reader.Read(buf, 0, 2));
            Assert.AreEqual("ar", new string(buf, 0, 2));
            Assert.AreEqual(-1, reader.Read(buf, 2, 0));
            reader.Close();

            reader.Value = "foobar";
            Assert.AreEqual(0, reader.Read(buf, 1, 0));
            Assert.AreEqual(3, reader.Read(buf, 1, 3));
            Assert.AreEqual("foo", new string(buf, 1, 3));
            Assert.AreEqual(2, reader.Read(buf, 2, 2));
            Assert.AreEqual("ba", new string(buf, 2, 2));
            Assert.AreEqual('r', (char)reader.Read());
            Assert.AreEqual(-1, reader.Read(buf, 2, 0));
            reader.Close();

            reader.Value = "foobar";
            StringBuilder sb = new StringBuilder();
            int ch;
            while ((ch = reader.Read()) != -1)
            {
                sb.Append((char)ch);
            }
            reader.Close();
            Assert.AreEqual("foobar", sb.ToString());
        }