private static void Base64EncodeInPlaceOnce(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            int         length      = Base64.GetMaxEncodedToUtf8Length(numberOfBytes);
            Span <byte> decodedSpan = new byte[length];

            source.CopyTo(decodedSpan);
            Span <byte> backupSpan = decodedSpan.ToArray();

            int bytesWritten = 0;

            foreach (var iteration in Benchmark.Iterations)
            {
                backupSpan.CopyTo(decodedSpan);
                using (iteration.StartMeasurement())
                {
                    Base64.EncodeToUtf8InPlace(decodedSpan, numberOfBytes, out bytesWritten);
                }
            }

            Span <byte> backToSource = new byte[numberOfBytes];

            Base64.DecodeFromUtf8(decodedSpan, backToSource, out _, out _);
            Assert.True(backupSpan.Slice(0, numberOfBytes).SequenceEqual(backToSource));
        }
Exemplo n.º 2
0
        public void EncodingOutputTooSmallRetry()
        {
            Span <byte> source = new byte[750];

            Base64TestHelper.InitalizeBytes(source);

            int outputSize   = 320;
            int requiredSize = Base64.GetMaxEncodedToUtf8Length(source.Length);

            Span <byte> encodedBytes = new byte[outputSize];

            Assert.Equal(OperationStatus.DestinationTooSmall,
                         Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int written));
            int expectedConsumed = encodedBytes.Length / 4 * 3;

            Assert.Equal(expectedConsumed, consumed);
            Assert.Equal(encodedBytes.Length, written);
            Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes));

            encodedBytes = new byte[requiredSize - outputSize];
            source       = source.Slice(consumed);
            Assert.Equal(OperationStatus.Done,
                         Base64.EncodeToUtf8(source, encodedBytes, out consumed, out written));
            expectedConsumed = encodedBytes.Length / 4 * 3;
            Assert.Equal(expectedConsumed, consumed);
            Assert.Equal(encodedBytes.Length, written);
            Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes));
        }
        private static void Base64DecodeInPlace(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            int         length      = Base64.GetMaxEncodedToUtf8Length(numberOfBytes);
            Span <byte> encodedSpan = new byte[length];

            Base64.EncodeToUtf8(source, encodedSpan, out _, out _);

            Span <byte> backupSpan = encodedSpan.ToArray();

            int bytesWritten = 0;

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        backupSpan.CopyTo(encodedSpan);
                        Base64.DecodeFromUtf8InPlace(encodedSpan, out bytesWritten);
                    }
                }
            }

            Assert.True(source.SequenceEqual(encodedSpan.Slice(0, bytesWritten)));
        }
Exemplo n.º 4
0
        private static void Base64Decode(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            Span <byte> encoded = new byte[Base64.GetMaxEncodedToUtf8Length(numberOfBytes)];

            Base64.EncodeToUtf8(source, encoded, out _, out _);

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64.DecodeFromUtf8(encoded, source, out int bytesConsumed, out int bytesWritten);
                    }
                }
            }

            Span <byte> backToEncoded = encoded.ToArray();

            Base64.EncodeToUtf8(source, encoded, out _, out _);
            Assert.True(backToEncoded.SequenceEqual(encoded));
        }
        public void EncodingOutputTooSmall(bool isFinalBlock)
        {
            for (int numBytes = 4; numBytes < 20; numBytes++)
            {
                Span <byte> source = new byte[numBytes];
                Base64TestHelper.InitalizeBytes(source, numBytes);

                Span <byte> encodedBytes = new byte[4];
                Assert.Equal(OperationStatus.DestinationTooSmall, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int written, isFinalBlock));
                int expectedConsumed = 3;
                Assert.Equal(expectedConsumed, consumed);
                Assert.Equal(encodedBytes.Length, written);
                Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, encodedBytes.Length, source, encodedBytes));
            }
        }
Exemplo n.º 6
0
        public void BasicEncoding()
        {
            var rnd = new Random(42);

            for (int i = 0; i < 10; i++)
            {
                int         numBytes = rnd.Next(100, 1000 * 1000);
                Span <byte> source   = new byte[numBytes];
                Base64TestHelper.InitalizeBytes(source, numBytes);

                Span <byte> encodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)];
                Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount));
                Assert.Equal(source.Length, consumed);
                Assert.Equal(encodedBytes.Length, encodedBytesCount);
                Assert.True(Base64TestHelper.VerifyEncodingCorrectness(source.Length, encodedBytes.Length, source, encodedBytes));
            }
        }
        private static void Base64EncodeBaseline(int numberOfBytes)
        {
            var source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source.AsSpan());
            var destination = new char[Base64.GetMaxEncodedToUtf8Length(numberOfBytes)];

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Convert.ToBase64CharArray(source, 0, source.Length, destination, 0);
                    }
                }
            }
        }
        private static void Base64EncodeDestinationTooSmall(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            Span <byte> destination = new byte[Base64.GetMaxEncodedToUtf8Length(numberOfBytes) - 1];

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64.EncodeToUtf8(source, destination, out int consumed, out int written);
                    }
                }
            }
        }
        private static void Base64DecodeBaseline(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            ReadOnlySpan <char> encoded = Convert.ToBase64String(source.ToArray()).ToCharArray();

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Convert.TryFromBase64Chars(encoded, source, out int bytesWritten);
                    }
                }
            }
        }
Exemplo n.º 10
0
        public void EncodeInPlace()
        {
            const int   numberOfBytes = 15;
            Span <byte> testBytes     = new byte[numberOfBytes / 3 * 4]; // slack since encoding inflates the data

            Base64TestHelper.InitalizeBytes(testBytes);

            for (int numberOfBytesToTest = 0; numberOfBytesToTest <= numberOfBytes; numberOfBytesToTest++)
            {
                var expectedText = Convert.ToBase64String(testBytes.Slice(0, numberOfBytesToTest).ToArray());

                Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8InPlace(testBytes, numberOfBytesToTest, out int bytesWritten));
                Assert.Equal(Base64.GetMaxEncodedToUtf8Length(numberOfBytesToTest), bytesWritten);

                var encodedText = Encoding.ASCII.GetString(testBytes.Slice(0, bytesWritten).ToArray());
                Assert.Equal(expectedText, encodedText);
            }
        }
Exemplo n.º 11
0
        public void BasicEncodingWithFinalBlockFalse()
        {
            var rnd = new Random(42);

            for (int i = 0; i < 10; i++)
            {
                int         numBytes = rnd.Next(100, 1000 * 1000);
                Span <byte> source   = new byte[numBytes];
                Base64TestHelper.InitalizeBytes(source, numBytes);
                Span <byte> encodedBytes     = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)];
                int         expectedConsumed = source.Length / 3 * 3; // only consume closest multiple of three since isFinalBlock is false
                int         expectedWritten  = source.Length / 3 * 4;

                Assert.Equal(OperationStatus.NeedMoreData, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount, isFinalBlock: false));
                Assert.Equal(expectedConsumed, consumed);
                Assert.Equal(expectedWritten, encodedBytesCount);
                Assert.True(Base64TestHelper.VerifyEncodingCorrectness(expectedConsumed, expectedWritten, source, encodedBytes));
            }
        }
        private static void Base64DecodeDetinationTooSmall(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            Span <byte> encoded = new byte[Base64.GetMaxEncodedToUtf8Length(numberOfBytes)];

            Base64.EncodeToUtf8(source, encoded, out _, out _);

            source = source.Slice(0, source.Length - 1);

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64.DecodeFromUtf8(encoded, source, out int bytesConsumed, out int bytesWritten);
                    }
                }
            }
        }
Exemplo n.º 13
0
        private static void Base64DecodeInPlaceOnce(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            int         length      = Base64.GetMaxEncodedToUtf8Length(numberOfBytes);
            Span <byte> encodedSpan = new byte[length];

            int bytesWritten = 0;

            foreach (BenchmarkIteration iteration in Benchmark.Iterations)
            {
                Base64.EncodeToUtf8(source, encodedSpan, out _, out _);
                using (iteration.StartMeasurement())
                {
                    Base64.DecodeFromUtf8InPlace(encodedSpan, out bytesWritten);
                }
            }

            Assert.True(source.SequenceEqual(encodedSpan.Slice(0, bytesWritten)));
        }
        private static void Base64Encode(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            Span <byte> destination = new byte[Base64.GetMaxEncodedToUtf8Length(numberOfBytes)];

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64.EncodeToUtf8(source, destination, out int consumed, out int written);
                    }
                }
            }

            Span <byte> backToSource = new byte[numberOfBytes];

            Base64.DecodeFromUtf8(destination, backToSource, out _, out _);
            Assert.True(source.SequenceEqual(backToSource));
        }