private void DoUploadFromByteArrayTest(ICloudBlob blob, int bufferSize, int bufferOffset, int count, bool isAsync)
        {
            byte[] buffer = GetRandomBuffer(bufferSize);
            byte[] downloadedBuffer = new byte[bufferSize];
            int downloadLength;

            if (isAsync)
            {
                IAsyncResult result;
                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    result = blob.BeginUploadFromByteArray(buffer, bufferOffset, count,
                                                                    ar => waitHandle.Set(),
                                                                    null);
                    waitHandle.WaitOne();
                    blob.EndUploadFromByteArray(result);

                    result = blob.BeginDownloadToByteArray(downloadedBuffer, 0,
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    downloadLength = blob.EndDownloadToByteArray(result);
                }
            }
            else
            {
                blob.UploadFromByteArray(buffer, bufferOffset, count);
                downloadLength = blob.DownloadToByteArray(downloadedBuffer, 0);
            }

            Assert.AreEqual(count, downloadLength);

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(buffer[i + bufferOffset], downloadedBuffer[i]);
            }
        }
        /// <summary>
        /// Single put blob and get blob
        /// </summary>
        /// <param name="blobSize">The blob size.</param>
        /// <param name="bufferOffset">The blob offset.</param>
        /// <param name="option"> 0 - Sunc, 1 - APM and 2 - APM overload.</param>
        private void DoDownloadToByteArrayTest(ICloudBlob blob, int blobSize, int bufferSize, int bufferOffset, int option)
        {
            int downloadLength;
            byte[] buffer = GetRandomBuffer(blobSize);
            byte[] resultBuffer = new byte[bufferSize];
            byte[] resultBuffer2 = new byte[bufferSize];

            using (MemoryStream originalBlob = new MemoryStream(buffer))
            {
                blob.UploadFromStream(originalBlob);
            }

            if (option == 0)
            {
                downloadLength = blob.DownloadToByteArray(resultBuffer, bufferOffset);
            }
            else if (option == 1)
            {
                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    ICancellableAsyncResult result = blob.BeginDownloadToByteArray(resultBuffer,
                        bufferOffset,
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    downloadLength = blob.EndDownloadToByteArray(result);
                }
            }
            else
            {
                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    OperationContext context = new OperationContext();
                    ICancellableAsyncResult result = blob.BeginDownloadToByteArray(resultBuffer,
                        bufferOffset, /* offset */
                        null, /* accessCondition */
                        null, /* options */
                        context, /* operationContext */
                        ar => waitHandle.Set(),
                        null);
                    waitHandle.WaitOne();
                    downloadLength = blob.EndDownloadToByteArray(result);
                }
            }

            int downloadSize = Math.Min(blobSize, bufferSize - bufferOffset);
            Assert.AreEqual(downloadSize, downloadLength);

            for (int i = 0; i < blob.Properties.Length; i++)
            {
                Assert.AreEqual(buffer[i], resultBuffer[bufferOffset + i]);
            }

            for (int j = 0; j < bufferOffset; j++)
            {
                Assert.AreEqual(0, resultBuffer2[j]);
            }

            if (bufferOffset + blobSize < bufferSize)
            {
                for (int k = bufferOffset + blobSize; k < bufferSize; k++)
                {
                    Assert.AreEqual(0, resultBuffer2[k]);
                }
            }
        }