예제 #1
0
        public void TestGetSameChuckSeveralTimes()
        {
            var holder = new FileChunkHolder();

            holder.Add(0, new byte[0]);
            holder.Get(0);
            holder.Get(0);
        }
예제 #2
0
        public void TestFileChunksFromSomeByteArray()
        {
            var bytes  = Encoding.UTF8.GetBytes("Hello, World!");
            var holder = new FileChunkHolder();

            holder.Add(0, bytes.RangeSubset(0, 5));
            holder.Add(1, bytes.RangeSubset(5, 2));
            holder.Add(2, bytes.RangeSubset(7, 5));
            holder.Add(3, bytes.RangeSubset(12, 1));

            Assert.AreEqual(4, holder.Count());
            Assert.AreEqual("Hello", Encoding.UTF8.GetString(holder.Get(0)));
            Assert.AreEqual(", ", Encoding.UTF8.GetString(holder.Get(1)));
            Assert.AreEqual("World", Encoding.UTF8.GetString(holder.Get(2)));
            Assert.AreEqual("!", Encoding.UTF8.GetString(holder.Get(3)));
            Assert.AreEqual(0, holder.Count());
        }
예제 #3
0
        public void TestReadWholeFileToOneChunk()
        {
            var file        = new FileInfo("input.txt");
            var chunkHolder = new FileChunkHolder();

            var command = new ReadChunkCommand(file.Name, 0, (int)file.Length, 0, chunkHolder);

            command.Execute();

            var chunk = chunkHolder.Get(0);

            Assert.AreEqual(file.Length, chunk.Length);
            CollectionAssert.AreEqual(File.ReadAllBytes(file.Name), chunk);
        }
        public void TestDecompressSeveralChunks()
        {
            var originalByteChunks = new List <byte[]>(3)
            {
                new List <byte>().Concat(Constants.GZipDefaultHeader).Concat(new byte[] { 0x00, 0x01, 0x02 }).ToArray(),
                new List <byte>().Concat(Constants.GZipDefaultHeader).Concat(new byte[] { 0x05, 0x06, 0x07 }).ToArray(),
                new List <byte>().Concat(Constants.GZipDefaultHeader).Concat(new byte[] { 0x0A, 0x0B, 0x0C }).ToArray()
            };

            var compressor   = new GZipCompressor();
            var inputHolder  = new FileChunkHolder();
            var outputHolder = new FileChunkHolder();

            inputHolder.Add(0, originalByteChunks[0]);
            inputHolder.Add(1, originalByteChunks[1]);
            inputHolder.Add(2, originalByteChunks[2]);
            Assert.AreEqual(originalByteChunks.Count, inputHolder.Count());
            Assert.AreEqual(0, outputHolder.Count());

            var commands = new List <ICommand>(3)
            {
                new DecompressChunkCommand(0, inputHolder, outputHolder, compressor),
                new DecompressChunkCommand(1, inputHolder, outputHolder, compressor),
                new DecompressChunkCommand(2, inputHolder, outputHolder, compressor)
            };

            foreach (var command in commands)
            {
                command.Execute();
            }

            Assert.AreEqual(0, inputHolder.Count());
            Assert.AreEqual(originalByteChunks.Count, outputHolder.Count());
            CollectionAssert.AreEqual(new byte[0], outputHolder.Get(0));
            CollectionAssert.AreEqual(new byte[0], outputHolder.Get(1));
            CollectionAssert.AreEqual(new byte[0], outputHolder.Get(2));
        }
예제 #5
0
        public void TestCompressSeveralChunks()
        {
            var originalByteChunks = new List <byte[]>(3)
            {
                Encoding.UTF8.GetBytes("High-Speed Recovery"),
                Encoding.UTF8.GetBytes("Verified Recoverability"),
                Encoding.UTF8.GetBytes("Complete Visibility")
            };

            var compressor   = new GZipCompressor();
            var inputHolder  = new FileChunkHolder();
            var outputHolder = new FileChunkHolder();

            inputHolder.Add(0, originalByteChunks[0]);
            inputHolder.Add(1, originalByteChunks[1]);
            inputHolder.Add(2, originalByteChunks[2]);
            Assert.AreEqual(originalByteChunks.Count, inputHolder.Count());
            Assert.AreEqual(0, outputHolder.Count());

            var commands = new List <ICommand>(3)
            {
                new CompressChunkCommand(0, inputHolder, outputHolder, compressor),
                new CompressChunkCommand(1, inputHolder, outputHolder, compressor),
                new CompressChunkCommand(2, inputHolder, outputHolder, compressor)
            };

            foreach (var command in commands)
            {
                command.Execute();
            }

            Assert.AreEqual(0, inputHolder.Count());
            Assert.AreEqual(originalByteChunks.Count, outputHolder.Count());
            CollectionAssert.AreEqual(compressor.Compress(originalByteChunks[0]), outputHolder.Get(0));
            CollectionAssert.AreEqual(compressor.Compress(originalByteChunks[1]), outputHolder.Get(1));
            CollectionAssert.AreEqual(compressor.Compress(originalByteChunks[2]), outputHolder.Get(2));
        }
예제 #6
0
        public void TestReadSomeFileToSeveralChunks()
        {
            var file        = new FileInfo("input.txt");
            var chunkHolder = new FileChunkHolder();
            var commands    = new List <ICommand>(0);

            var bytesAvailable = file.Length;
            var readPosition   = 0;
            var chunkIndex     = 0;

            while (bytesAvailable > 0)
            {
                var readCount = bytesAvailable < Constants.DefaultByteBufferSize
                    ? (int)bytesAvailable
                    : Constants.DefaultByteBufferSize;

                commands.Add(new ReadChunkCommand(file.Name, readPosition, readCount, chunkIndex, chunkHolder));

                bytesAvailable -= readCount;
                readPosition   += readCount;
                chunkIndex++;
            }

            foreach (var command in commands)
            {
                command.Execute();
            }

            var chunksCount = chunkHolder.Count();

            Assert.AreEqual(file.Length / Constants.DefaultByteBufferSize + 1, chunksCount);

            var chunkHolderBytes = new List <byte>(0);

            for (var i = 0; i < chunksCount; i++)
            {
                chunkHolderBytes.AddRange(chunkHolder.Get(i));
            }
            Assert.AreEqual(0, chunkHolder.Count());
            Assert.AreEqual(file.Length, chunkHolderBytes.Count);
            CollectionAssert.AreEqual(File.ReadAllBytes(file.Name), chunkHolderBytes);
        }
예제 #7
0
        public void TestCompressOneChunk()
        {
            var originalBytes = Encoding.UTF8.GetBytes("AVAILABILITY for the Always-On Enterprise");

            var compressor   = new GZipCompressor();
            var inputHolder  = new FileChunkHolder();
            var outputHolder = new FileChunkHolder();

            inputHolder.Add(0, originalBytes);
            Assert.AreEqual(1, inputHolder.Count());
            Assert.AreEqual(0, outputHolder.Count());

            var command = new CompressChunkCommand(0, inputHolder, outputHolder, compressor);

            command.Execute();

            Assert.AreEqual(0, inputHolder.Count());
            Assert.AreEqual(1, outputHolder.Count());
            CollectionAssert.AreEqual(compressor.Compress(originalBytes), outputHolder.Get(0));
        }
        public void TestDecompressOneChunk()
        {
            var originalBytes = new List <byte>()
                                .Concat(Constants.GZipDefaultHeader)
                                .Concat(new byte[] { 0x00, 0x01, 0x02 }).ToArray();

            var compressor   = new GZipCompressor();
            var inputHolder  = new FileChunkHolder();
            var outputHolder = new FileChunkHolder();

            inputHolder.Add(0, originalBytes);
            Assert.AreEqual(1, inputHolder.Count());
            Assert.AreEqual(0, outputHolder.Count());

            var command = new DecompressChunkCommand(0, inputHolder, outputHolder, compressor);

            command.Execute();

            Assert.AreEqual(0, inputHolder.Count());
            Assert.AreEqual(1, outputHolder.Count());
            CollectionAssert.AreEqual(new byte[0], outputHolder.Get(0));
        }