Пример #1
0
        public void EncodingOutputTooSmall()
        {
            Span <byte> source = new byte[750];

            Base64TestHelper.InitalizeBytes(source);

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

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

            Assert.Equal(TransformationStatus.DestinationTooSmall,
                         Base64.Encoder.Transform(source, encodedBytes, out int consumed, out int written));
            Assert.Equal(encodedBytes.Length, written);
            Assert.Equal(encodedBytes.Length / 4 * 3, consumed);
            encodedBytes = new byte[requiredSize - outputSize];
            Assert.Equal(TransformationStatus.Done,
                         Base64.Encoder.Transform(source.Slice(consumed), encodedBytes, out consumed, out written));
            Assert.Equal(encodedBytes.Length, written);
            Assert.Equal(encodedBytes.Length / 4 * 3, consumed);

            string encodedText  = Text.Encoding.ASCII.GetString(encodedBytes.ToArray());
            string expectedText = Convert.ToBase64String(source.ToArray()).Substring(outputSize);

            Assert.Equal(expectedText, encodedText);
        }
Пример #2
0
        private static void Base64DecodeBaseline(int numberOfBytes)
        {
            var source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source.AsSpan());
            char[] encoded = Convert.ToBase64String(source).ToCharArray();

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Convert.FromBase64CharArray(encoded, 0, encoded.Length);
                    }
                }
            }
        }
Пример #3
0
        private static void Base64EncodeBaseline(int numberOfBytes)
        {
            var source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source.AsSpan());
            var destination = new char[Base64.ComputeEncodedLength(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);
                    }
                }
            }
        }
Пример #4
0
        private static void Base64Encode(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

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

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64.Encoder.Transform(source, destination, out int consumed, out int written);
                    }
                }
            }
        }
Пример #5
0
        private static void Base64Decode(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

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

            Base64.BytesToUtf8(source, encoded, out int consumed, out int written);

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement()) {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64.Utf8ToBytes(encoded, source, out int bytesConsumed, out int bytesWritten);
                    }
                }
            }
        }
Пример #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.ComputeEncodedLength(source.Length)];
                Assert.Equal(TransformationStatus.Done, Base64.Encoder.Transform(source, encodedBytes, out int consumed, out int encodedBytesCount));
                Assert.Equal(encodedBytes.Length, encodedBytesCount);

                string encodedText  = Text.Encoding.ASCII.GetString(encodedBytes.ToArray());
                string expectedText = Convert.ToBase64String(source.ToArray());
                Assert.Equal(expectedText, encodedText);
            }
        }
Пример #7
0
        private static void Base64DecodeInPlace(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

            Base64TestHelper.InitalizeBytes(source);
            int length = Base64.ComputeEncodedLength(numberOfBytes);

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Span <byte> encodedSpan = new byte[length];
                        Base64.Encoder.Transform(source, encodedSpan, out int consumed, out int written);
                        Base64.DecodeInPlace(encodedSpan, out int bytesConsumed, out int bytesWritten);
                    }
                }
            }
        }