Ejemplo n.º 1
0
        public void Data_Written_Can_Be_Read_Back()
        {
            var  sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
            var  pool  = new BufferManagerAllocator(1024, 1024 * 1024);
            byte i     = 1;

            foreach (var size in sizes)
            {
                using (var stream = new PooledMemoryStream(pool))
                {
                    var data = new byte[size];
                    for (var j = 0; j < data.Length; j++)
                    {
                        data[j] = i;
                    }

                    stream.Write(data, 0, data.Length);
                    stream.Position = 0;

                    Assert.Equal(0, stream.Position);
                    Assert.Equal(data.Length, stream.Length);

                    var copy = new byte[size];
                    stream.Read(copy, 0, copy.Length);
                    Assert.Equal(data, copy);
                }

                i++;
            }
        }
		public void Data_Written_Can_Be_Read_Back()
		{
			var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			byte i = 1;

			foreach (var size in sizes)
			{
				using (var stream = new PooledMemoryStream(pool))
				{
					var data = new byte[size];
					for (var j = 0; j < data.Length; j++) data[j] = i;

					stream.Write(data, 0, data.Length);
					stream.Position = 0;

					Assert.Equal(0, stream.Position);
					Assert.Equal(data.Length, stream.Length);

					var copy = new byte[size];
					stream.Read(copy, 0, copy.Length);
					Assert.Equal(data, copy);
				}

				i++;
			}
		}
Ejemplo n.º 3
0
        public void Segmented_Data_Written_Can_Be_Read_Back_In_Segments()
        {
            var       sizes      = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024 };
            var       pool       = new BufferManagerAllocator(1024, 1024 * 1024);
            const int STEP_WRITE = 37;

            foreach (var size in sizes)
            {
                using (var stream = new PooledMemoryStream(pool))
                {
                    var  remaining = size;
                    var  data      = new byte[STEP_WRITE];
                    byte value     = 1;

                    for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
                    {
                        Fill(data, value);
                        stream.Write(data, 0, data.Length);
                        value++;
                        remaining -= STEP_WRITE;
                    }

                    if (remaining > 0)
                    {
                        Fill(data, value);
                        stream.Write(data, 0, remaining);
                    }

                    Assert.Equal(size, stream.Position);
                    Assert.Equal(size, stream.Length);

                    stream.Position = 0;
                    Assert.Equal(0, stream.Position);
                    var copy = new byte[STEP_WRITE];

                    value = 1;

                    for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
                    {
                        Fill(data, value);
                        Fill(copy, (byte)(value - 1));
                        stream.Read(copy, 0, copy.Length);
                        value++;
                        remaining -= STEP_WRITE;

                        Assert.Equal(data, copy);
                    }

                    if (remaining > 0)
                    {
                        Fill(data, value);
                        stream.Write(data, 0, remaining);
                        Assert.Equal(data.Take(remaining), copy.Take(remaining));
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void Can_Rewrite_Stream()
        {
            var pool   = new BufferManagerAllocator(1024, 1024 * 1024);
            var stream = new PooledMemoryStream(pool);

            var chunk = new byte[60];

            for (var i = 0; i < 10; i++)
            {
                Fill(chunk, (byte)(i + 1));
                stream.Write(chunk, 0, chunk.Length);

                Assert.Equal((i + 1) * 60, stream.Position);
                Assert.Equal((i + 1) * 60, stream.Length);
            }

            stream.Position = 120;
            Fill(chunk, 200);
            stream.Write(chunk, 0, chunk.Length);

            stream.Position = 370;
            Fill(chunk, 100);
            stream.Write(chunk, 0, chunk.Length);

            Assert.Equal(430, stream.Position);
            Assert.Equal(600, stream.Length);

            var all = new byte[stream.Length];

            stream.Position = 0;
            stream.Read(all, 0, all.Length);

            Assert.Equal(600, stream.Position);
            Assert.Equal(600, stream.Length);

            var expected = new[]
            {
                Enumerable.Repeat(1, 60),                 // 0
                Enumerable.Repeat(2, 60),                 // 60
                Enumerable.Repeat(200, 60),               // 120
                Enumerable.Repeat(4, 60),                 // 180
                Enumerable.Repeat(5, 60),                 // 240
                Enumerable.Repeat(6, 60),                 // 300
                Enumerable.Repeat(7, 10),                 // 360
                Enumerable.Repeat(100, 60),               // 370
                Enumerable.Repeat(8, 50),                 // 430
                Enumerable.Repeat(9, 60),                 // 480
                Enumerable.Repeat(10, 60),                // 540
            };

            var expectedValue = expected.SelectMany(_ => _.Select(v => (byte)v)).ToArray();

            Assert.Equal(expectedValue.Length, all.Length);
            Assert.Equal(stream.Length, all.Length);

            AssertArray(expectedValue, all);
        }
Ejemplo n.º 5
0
        public void Read_Must_Indicate_End_Of_Stream()
        {
            var pool   = new BufferManagerAllocator(1024, 1024 * 1024);
            var stream = new PooledMemoryStream(pool);
            var buffer = new byte[129];

            Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));

            stream.Write(buffer, 0, buffer.Length);
            Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));
        }
		public void Read_Must_Indicate_End_Of_Stream()
		{
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			var stream = new PooledMemoryStream(pool);
			var buffer = new byte[129];

			Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));

			stream.Write(buffer, 0, buffer.Length);
			Assert.Equal(-1, stream.Read(buffer, 0, buffer.Length));
		}
        public static PooledMemoryStream From(BufferManagerAllocator pool, byte[] buffer, int length)
        {
            var retval = new PooledMemoryStream(pool);

            retval.chunks.Add(buffer);
            retval.lengths.Add(buffer.Length);
            retval.currentChunk = buffer;
            retval.currentIndex = 0;
            retval.length       = length;

            return(retval);
        }
Ejemplo n.º 8
0
        public void Write_Sets_Properties_Properly()
        {
            var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
            var pool  = new BufferManagerAllocator(1024, 1024 * 1024);

            foreach (var size in sizes)
            {
                using (var stream = new PooledMemoryStream(pool))
                {
                    var data = new byte[size];

                    stream.Write(data, 0, data.Length);

                    Assert.Equal(data.Length, stream.Position);
                    Assert.Equal(data.Length, stream.Length);
                }
            }
        }
		public void Write_Sets_Properties_Properly()
		{
			var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024, 4 };
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);

			foreach (var size in sizes)
			{
				using (var stream = new PooledMemoryStream(pool))
				{
					var data = new byte[size];

					stream.Write(data, 0, data.Length);

					Assert.Equal(data.Length, stream.Position);
					Assert.Equal(data.Length, stream.Length);
				}
			}
		}
		public void Segmented_Data_Written_Can_Be_Read_Back_In_Segments()
		{
			var sizes = new int[] { 128 + 256 + 512 + 1024 + 1024 + 2, 200, 1000, 1024 * 1024 };
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			const int STEP_WRITE = 37;

			foreach (var size in sizes)
			{
				using (var stream = new PooledMemoryStream(pool))
				{
					var remaining = size;
					var data = new byte[STEP_WRITE];
					byte value = 1;

					for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
					{
						Fill(data, value);
						stream.Write(data, 0, data.Length);
						value++;
						remaining -= STEP_WRITE;
					}

					if (remaining > 0)
					{
						Fill(data, value);
						stream.Write(data, 0, remaining);
					}

					Assert.Equal(size, stream.Position);
					Assert.Equal(size, stream.Length);

					stream.Position = 0;
					Assert.Equal(0, stream.Position);
					var copy = new byte[STEP_WRITE];

					value = 1;

					for (var i = 0; i < size - (size % STEP_WRITE); i += STEP_WRITE)
					{
						Fill(data, value);
						Fill(copy, (byte)(value - 1));
						stream.Read(copy, 0, copy.Length);
						value++;
						remaining -= STEP_WRITE;

						Assert.Equal(data, copy);
					}

					if (remaining > 0)
					{
						Fill(data, value);
						stream.Write(data, 0, remaining);
						Assert.Equal(data.Take(remaining), copy.Take(remaining));
					}
				}
			}
		}
		public void Can_Rewrite_Stream()
		{
			var pool = new BufferManagerAllocator(1024, 1024 * 1024);
			var stream = new PooledMemoryStream(pool);

			var chunk = new byte[60];
			for (var i = 0; i < 10; i++)
			{
				Fill(chunk, (byte)(i + 1));
				stream.Write(chunk, 0, chunk.Length);

				Assert.Equal((i + 1) * 60, stream.Position);
				Assert.Equal((i + 1) * 60, stream.Length);
			}

			stream.Position = 120;
			Fill(chunk, 200);
			stream.Write(chunk, 0, chunk.Length);

			stream.Position = 370;
			Fill(chunk, 100);
			stream.Write(chunk, 0, chunk.Length);

			Assert.Equal(430, stream.Position);
			Assert.Equal(600, stream.Length);

			var all = new byte[stream.Length];
			stream.Position = 0;
			stream.Read(all, 0, all.Length);

			Assert.Equal(600, stream.Position);
			Assert.Equal(600, stream.Length);

			var expected = new[]
			{
				Enumerable.Repeat(1, 60), // 0
				Enumerable.Repeat(2, 60), // 60
				Enumerable.Repeat(200, 60),	// 120
				Enumerable.Repeat(4, 60), // 180
				Enumerable.Repeat(5, 60), // 240
				Enumerable.Repeat(6, 60), // 300
				Enumerable.Repeat(7, 10), // 360
				Enumerable.Repeat(100, 60),	// 370
				Enumerable.Repeat(8, 50), // 430
				Enumerable.Repeat(9, 60), // 480
				Enumerable.Repeat(10, 60), // 540
			};

			var expectedValue = expected.SelectMany(_ => _.Select(v => (byte)v)).ToArray();

			Assert.Equal(expectedValue.Length, all.Length);
			Assert.Equal(stream.Length, all.Length);

			AssertArray(expectedValue, all);
		}