Пример #1
0
        private static void StichingTestNoStichingNeeded(int inputBufferSize)
        {
            Span <byte> source = new byte[inputBufferSize];

            Base64TestHelper.InitalizeDecodableBytes(source);
            Span <byte> expected = new byte[inputBufferSize];

            Base64.Decoder.Transform(source, expected, out int expectedConsumed, out int expectedWritten);

            Base64TestHelper.SplitSourceIntoSpans(source, false, out ReadOnlySpan <byte> source1, out ReadOnlySpan <byte> source2);

            Span <byte> destination = new byte[inputBufferSize]; // Plenty of space

            int bytesConsumed = 0;
            int bytesWritten  = 0;

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64TestHelper.DecodeNoNeedToStich(source1, source2, destination, out bytesConsumed, out bytesWritten);
                    }
                }
            }

            Assert.Equal(expectedConsumed, bytesConsumed);
            Assert.Equal(expectedWritten, bytesWritten);
            Assert.True(expected.SequenceEqual(destination));
        }
Пример #2
0
        [InlineData(30000, 50000)]  // No Third Call
        private static void StichingTestStichingRequired(int stackSize, int inputBufferSize)
        {
            Span <byte> source = new byte[inputBufferSize];

            Base64TestHelper.InitalizeDecodableBytes(source);
            Span <byte> expected = new byte[inputBufferSize];

            Base64.Utf8ToBytes(source, expected, out int expectedConsumed, out int expectedWritten);

            Base64TestHelper.SplitSourceIntoSpans(source, true, out ReadOnlySpan <byte> source1, out ReadOnlySpan <byte> source2);

            Span <byte> destination = new byte[inputBufferSize]; // Plenty of space
            Span <byte> stackSpan   = stackalloc byte[stackSize];

            int bytesConsumed = 0;
            int bytesWritten  = 0;

            foreach (var iteration in Benchmark.Iterations)
            {
                using (iteration.StartMeasurement())
                {
                    for (int i = 0; i < Benchmark.InnerIterationCount; i++)
                    {
                        Base64TestHelper.DecodeStichUsingStack(source1, source2, destination, stackSpan, out bytesConsumed, out bytesWritten);
                    }
                }
            }

            Assert.Equal(expectedConsumed, bytesConsumed);
            Assert.Equal(expectedWritten, bytesWritten);
            Assert.True(expected.SequenceEqual(destination));
        }
Пример #3
0
        public void ValidInputOnlyMultiByte()
        {
            Span <byte> inputSpan = new byte[1000];

            Base64TestHelper.InitalizeDecodableBytes(inputSpan);
            int         requiredLength = Base64.ComputeDecodedLength(inputSpan);
            Span <byte> expected       = new byte[requiredLength];

            Assert.Equal(TransformationStatus.Done, Base64.Decoder.Transform(inputSpan, expected, out int bytesConsumed, out int bytesWritten));

            byte[][] input = new byte[10][];

            int[] split = { 100, 102, 98, 101, 2, 97, 101, 1, 2, 396 };

            int sum = 0;

            for (int i = 0; i < split.Length; i++)
            {
                int splitter = split[i];
                input[i] = inputSpan.Slice(sum, splitter).ToArray();
                sum     += splitter;
            }
            Assert.Equal(1000, sum);

            var output = new TestOutput();

            Base64.Decoder.Pipe(ReadOnlyBytes.Create(input), output);

            Assert.True(expected.SequenceEqual(output.GetBuffer.Slice(0, requiredLength)));
        }
Пример #4
0
        public void DecodingOutputTooSmall()
        {
            Span <byte> source = new byte[1000];

            Base64TestHelper.InitalizeDecodableBytes(source);

            int outputSize   = 240;
            int requiredSize = Base64.ComputeDecodedLength(source);

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

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

            string expectedStr = Text.Encoding.ASCII.GetString(source.ToArray());

            byte[] expectedText = Convert.FromBase64String(expectedStr);
            Assert.True(expectedText.AsSpan().Slice(outputSize).SequenceEqual(decodedBytes));
        }
Пример #5
0
        public void BasicDecoding()
        {
            var rnd = new Random(42);

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

                Span <byte> decodedBytes = new byte[Base64.ComputeDecodedLength(source)];
                Assert.Equal(TransformationStatus.Done,
                             Base64.Decoder.Transform(source, decodedBytes, out int consumed, out int decodedByteCount));
                Assert.Equal(decodedBytes.Length, decodedByteCount);

                string expectedStr  = Text.Encoding.ASCII.GetString(source.ToArray());
                byte[] expectedText = Convert.FromBase64String(expectedStr);
                Assert.True(expectedText.AsSpan().SequenceEqual(decodedBytes));
            }
        }