Beispiel #1
0
        public void should_be_able_to_read_a_string_from_multiple_segments()
        {
            string   expected = "The quick brown fox jumps over the lazy dog";
            Encoding encoding = Encoding.ASCII;

            // create n-segments of each word and space
            Random random = new Random(0);

            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();
            var words = expected.Split(' ');

            for (int i = 0; i < words.Length; i++)
            {
                if (i != 0)
                {
                    segments.Add(GetSegment(encoding.GetBytes(" "), random.Next(0, 256), random.Next(1, 4) * 1024));
                }

                segments.Add(GetSegment(encoding.GetBytes(words[i]), random.Next(0, 256), random.Next(1, 4) * 1024));
            }

            BufferListStream sut = new BufferListStream();

            sut.Initialize(segments);

            // pre-condition
            Assert.AreEqual(expected.Length, sut.Length);

            string actual = sut.ReadString(expected.Length, encoding);

            Assert.AreEqual(expected, actual);
            Assert.AreEqual(expected.Length, sut.Position);
        }
Beispiel #2
0
        public void should_be_able_to_take_a_buffer_from_the_current_position()
        {
            BufferListStream            sut      = new BufferListStream();
            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();

            for (int i = 0; i < 10; i++)
            {
                segments.Add(GetSegment(new byte[16], 512, 1024));
            }
            sut.Initialize(segments);

            // pre-condition
            Assert.AreEqual(0, sut.Position);

            sut.Position += 24;

            // act
            var actual = sut.Take(36);

            var totalLength = actual.Sum(segment => segment.Count);

            // assert
            Assert.IsNotNull(actual);
            Assert.AreEqual(36, totalLength);
            // TODO: check if the data is correct
        }
Beispiel #3
0
        public void should_not_be_able_to_skip_backwards()
        {
            BufferListStream sut = new BufferListStream();

            sut.Initialize(new[] { GetSegment(new byte[] { 0x01, 0x02, 0x03, 0x04 }, 123, 1024) });
            sut.Position = 1;
            sut.Skip(-1);
        }
Beispiel #4
0
        public void should_be_able_to_skip_to_the_end_of_all_the_buffers()
        {
            int segmentSize = 16;
            BufferListStream            sut      = new BufferListStream();
            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();

            segments.Add(GetSegment(new byte[segmentSize], 512, 1024));
            segments.Add(GetSegment(new byte[segmentSize], 512, 1024));
            sut.Initialize(segments);

            sut.Skip(segmentSize * 2);
        }
Beispiel #5
0
        public void should_be_able_to_advance_the_position_into_the_next_segment()
        {
            BufferListStream            sut      = new BufferListStream();
            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();

            segments.Add(GetSegment(new byte[16], 512, 1024));
            segments.Add(GetSegment(new byte[16], 512, 1024));
            sut.Initialize(segments);

            Assert.AreEqual(0, sut.Position);

            sut.Position += 24;

            Assert.AreEqual(24, sut.Position);
        }
Beispiel #6
0
        public void should_be_able_to_advance_the_position_within_current_segment()
        {
            int segmentLength    = 16;
            int expectedPosition = segmentLength / 2;

            BufferListStream sut = new BufferListStream();

            sut.Initialize(new[] { GetSegment(new byte[segmentLength], 512, 1024) });

            Assert.AreEqual(0, sut.Position);

            sut.Position += expectedPosition;

            Assert.AreEqual(expectedPosition, sut.Position);
        }
Beispiel #7
0
        public void should_be_able_to_read_an_unsigned_integer(bool littleEndian, byte[] bytes, ulong expected)
        {
            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();

            segments.Add(new ArraySegment <byte>(bytes));
            BufferListStream sut = new BufferListStream();

            sut.Initialize(segments);

            // act
            var actual = sut.ReadUInt32(littleEndian);

            // assert
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(4, sut.Position);
        }
Beispiel #8
0
        public void should_be_able_to_call_skip_to_advance_the_position()
        {
            BufferListStream sut = new BufferListStream();

            sut.Initialize(new[] { GetSegment(new byte[] { 0x01 }, 123, 1024) });

            // pre-condition
            Assert.AreEqual(1, sut.Length);

            // act
            var actual = sut.Skip(1);

            // assert
            Assert.NotNull(actual);
            Assert.AreEqual(1, sut.Position);
        }
Beispiel #9
0
        public void should_be_able_to_read_a_byte_and_the_position_should_be_advanced_by_one()
        {
            BufferListStream sut = new BufferListStream();

            sut.Initialize(new[] { GetSegment(new byte[] { 0x01, 0x02 }, 512, 1024) });

            // pre-condition
            Assert.AreEqual(2, sut.Length);
            Assert.AreEqual(0, sut.Position);

            // act
            var first = sut.ReadByte();

            // assert
            Assert.AreEqual(1, first);
            Assert.AreEqual(1, sut.Position);
        }
Beispiel #10
0
        public void should_be_able_to_read_a_long_integer(bool littleEndian, byte[] bytes, long expected)
        {
            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();

            segments.Add(new ArraySegment <byte>(bytes));
            BufferListStream sut = new BufferListStream();

            sut.Initialize(segments);

            // act
            long actual = sut.ReadInt64(littleEndian);

            // assert
            Assert.AreEqual(8, bytes.Length);
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(8, sut.Position);
        }
Beispiel #11
0
        public void should_be_able_to_read_a_string_from_segments()
        {
            string expected = "The quick brown fox jumps over the lazy dog";

            BufferListStream sut = new BufferListStream();

            sut.Initialize(GetSegments(expected, Encoding.ASCII, 2));

            // pre-condition
            Assert.AreEqual(expected.Length, sut.Length);

            var actual = sut.ReadString(expected.Length, Encoding.ASCII);

            // assert
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(expected.Length, sut.Position);
        }
Beispiel #12
0
        public void should_be_able_to_read_a_single_byte()
        {
            byte expected = 0x01;

            BufferListStream sut = new BufferListStream();

            sut.Initialize(new[] { GetSegment(new byte[] { expected }, 123, 1024) });

            // pre-condition
            Assert.AreEqual(1, sut.Length);
            Assert.AreEqual(0, sut.Position);

            // act
            var actual = sut.ReadByte();

            // assert
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(1, sut.Position);
        }
Beispiel #13
0
        public void should_be_able_to_read_a_string_from_one_segment()
        {
            string expected = "The quick brown fox jumps over the lazy dog";
            var    encoding = Encoding.ASCII;

            var bytes = encoding.GetBytes(expected);

            ArraySegment <byte> segment = new ArraySegment <byte>(bytes);

            BufferListStream sut = new BufferListStream();

            sut.Initialize(new[] { segment });

            // act
            var actual = sut.ReadString(expected.Length, encoding);

            // assert
            Assert.AreEqual(expected, actual);
            Assert.AreEqual(expected.Length, sut.Position);
        }
Beispiel #14
0
        public void should_be_able_to_rewind_the_position_into_the_previous_segment()
        {
            int segmentLength    = 16;
            int expectedPosition = segmentLength * 3 / 2;

            BufferListStream            sut      = new BufferListStream();
            List <ArraySegment <byte> > segments = new List <ArraySegment <byte> >();

            segments.Add(GetSegment(new byte[segmentLength], 512, 1024));
            segments.Add(GetSegment(new byte[segmentLength], 512, 1024));
            sut.Initialize(segments);

            Assert.AreEqual(0, sut.Position);

            sut.Position += expectedPosition;

            Assert.AreEqual(expectedPosition, sut.Position);

            sut.Position -= segmentLength;
            Assert.AreEqual(segmentLength / 2, sut.Position);
        }
Beispiel #15
0
        public void should_be_able_to_get_correct_position()
        {
            var data     = Encoding.UTF8.GetBytes("SuperSocket rocks!");
            var segments = new List <ArraySegment <byte> >();

            segments.Add(new ArraySegment <byte>(data));

            using (var bufferListStream = new BufferListStream())
            {
                bufferListStream.Initialize(segments);

                Assert.AreEqual(data.Length, bufferListStream.Length);
                Assert.AreEqual(0, bufferListStream.Position);

                bufferListStream.Seek(1, SeekOrigin.Begin);
                Assert.AreEqual(1, bufferListStream.Position);

                bufferListStream.Seek(1, SeekOrigin.Current);
                Assert.AreEqual(2, bufferListStream.Position);

                bufferListStream.Seek(data.Length - 1, SeekOrigin.Begin);
                Assert.AreEqual(data.Length - 1, bufferListStream.Position);
            }
        }