Ejemplo n.º 1
0
        public void ResetWhenReadAheadLimitExceededAfterMarkAndResetThrowsException()
        {
            // given
            const string input  = "abcdefgh";
            var          target = new DefaultResettableReader(input);

            // when
            target.Mark(5);

            // then
            for (var i = 0; i < 5; i++)
            {
                var chr = target.Read();
                Assert.That(chr, Is.EqualTo(input[i]));
            }

            // and when
            target.Reset();
            target.Mark(3);

            // then
            for (var i = 0; i < 4; i++)
            {
                var chr = target.Read();
                Assert.That(chr, Is.EqualTo(input[i]));
            }

            var ex = Assert.Throws <InvalidOperationException>(() => target.Reset());

            Assert.That(ex.Message, Is.EqualTo("Cannot reset beyond 3 positions. Tried to reset 4 positions"));
        }
Ejemplo n.º 2
0
        public void MarkWithHigherReadAheadLimitThanPreviousMarkOnOddPosition()
        {
            // given
            const string input  = "abcde";
            var          target = new DefaultResettableReader(input);

            // when
            target.Mark(1);
            var obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            target.Mark(1);
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('b'));

            // and when reading from reset reader at odd position
            target.Reset();
            target.Mark(2);
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('b'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('c'));

            // and when
            target.Reset();

            // then
            for (var i = 1; i < input.Length; i++)
            {
                obtained = target.Read();
                Assert.That(obtained, Is.EqualTo(input[i]));
            }

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo(-1));
        }
Ejemplo n.º 3
0
        public void MarkWithLowerReadAheadLimitThanPreviousMark()
        {
            // given
            const string input  = "abcd";
            var          target = new DefaultResettableReader(input);

            // when
            target.Mark(2);

            // then
            for (var i = 0; i < 2; i++)
            {
                var chr = target.Read();
                Assert.That(chr, Is.EqualTo(input[i]));
            }

            // and when
            target.Reset();

            for (var i = 0; i < 3; i++)
            {
                // and when
                target.Mark(1);
                var obtained = target.Read();

                // then
                Assert.That(obtained, Is.EqualTo(input[i]));

                // and when
                target.Reset();
                obtained = target.Read();

                // then
                Assert.That(obtained, Is.EqualTo(input[i]));
            }

            // and when reading to end, then
            for (var i = 3; i < input.Length; i++)
            {
                var obtained = target.Read();
                Assert.That(obtained, Is.EqualTo(input[i]));
            }

            // and finally when
            var lastReadChr = target.Read();

            // then
            Assert.That(lastReadChr, Is.EqualTo(-1));
        }
Ejemplo n.º 4
0
        public void ResetOnUnmarkedReaderThrowsAnException()
        {
            // given
            var target = new DefaultResettableReader("abcd");

            // when, then
            var ex = Assert.Throws <InvalidOperationException>(() => target.Reset());

            Assert.That(ex.Message, Is.EqualTo("No position has been marked"));
        }
Ejemplo n.º 5
0
        public void MarkWithHigherReadAheadLimitThanPreviousMarkOnEvenPosition()
        {
            // given
            const string input  = "abcde";
            var          target = new DefaultResettableReader(input);

            // when
            target.Mark(1);
            var obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when reading from reset reader at even position
            target.Reset();
            target.Mark(2);
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('b'));

            // and when
            target.Reset();

            // then
            foreach (var c in input)
            {
                obtained = target.Read();
                Assert.That(obtained, Is.EqualTo(c));
            }

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo(-1));
        }
Ejemplo n.º 6
0
        public void ResetAfterReadAheadLimitExceededThrowsException()
        {
            // given
            var target = new DefaultResettableReader("abcd");

            // when
            target.Mark(1);

            target.Read();
            target.Read();

            // then
            var ex = Assert.Throws <InvalidOperationException>(() => target.Reset());

            Assert.That(ex.Message, Is.EqualTo("Cannot reset beyond 1 positions. Tried to reset 2 positions"));
        }
Ejemplo n.º 7
0
        public void ReadAfterMarkAndResettingTwoCharactersWithOddReadOffset()
        {
            // given
            const string input  = "abcd";
            var          target = new DefaultResettableReader(input);

            // when
            var obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            target.Mark(2);
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('b'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('c'));

            // and when
            target.Reset();

            for (var i = 1; i < input.Length; i++)
            {
                // when
                obtained = target.Read();

                // then
                Assert.That(obtained, Is.EqualTo(input[i]));
            }

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo(-1));
        }
Ejemplo n.º 8
0
        public void ReadAfterMarkAndResettingTwoCharactersWithEvenReadOffset()
        {
            // given
            const string input  = "abc";
            var          target = new DefaultResettableReader(input);

            // when
            target.Mark(2);
            var obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('b'));

            // and when
            target.Reset();

            foreach (var chr in input)
            {
                // when
                obtained = target.Read();

                // then
                Assert.That(obtained, Is.EqualTo(chr));
            }

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo(-1));
        }
Ejemplo n.º 9
0
        public void ReadAfterMarkAndResettingSingleCharacter()
        {
            // given
            var target = new DefaultResettableReader("abc");

            // when
            target.Mark(1);
            var obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            target.Reset();
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('b'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('c'));

            // and when
            obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo(-1));
        }
Ejemplo n.º 10
0
        public void MarkAndReadAfterNotReadingAllPreviouslyMarkedCharacters()
        {
            // given
            const string input  = "abcdef";
            var          target = new DefaultResettableReader(input);

            // when
            target.Mark(2);
            var obtained = target.Read();

            // then
            Assert.That(obtained, Is.EqualTo('a'));

            // and when
            target.Mark(3);

            // then
            for (var i = 1; i < 4; i++)
            {
                obtained = target.Read();
                Assert.That(obtained, Is.EqualTo(input[i]));
            }

            // and when
            target.Reset();

            // then
            for (var i = 1; i < input.Length; i++)
            {
                obtained = target.Read();
                Assert.That(obtained, Is.EqualTo(input[i]));
            }

            obtained = target.Read();
            Assert.That(obtained, Is.EqualTo(-1));
        }