Example #1
0
        public void BasicEncodingWithLineBreaksMime()
        {
            var format = new StandardFormat('M');

            for (int numBytes = 58; numBytes < 1000; numBytes++)
            {
                Span <byte> source = new byte[numBytes];
                Base64TestHelper.InitalizeBytes(source, numBytes);

                char[]      charArray    = new char[numBytes * 10];
                Span <byte> encodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)];
                Span <byte> encodedBytesWithLineBreaks = new byte[Base64Experimental.GetMaxEncodedToUtf8Length(source.Length, format)];
                Assert.Equal(OperationStatus.Done, Base64.EncodeToUtf8(source, encodedBytes, out int consumed, out int encodedBytesCount));
                Assert.Equal(encodedBytes.Length, encodedBytesCount);
                Assert.True(OperationStatus.Done == Base64Experimental.EncodeToUtf8(source, encodedBytesWithLineBreaks, out consumed, out encodedBytesCount, format), "At index: " + numBytes);
                Assert.Equal(encodedBytesWithLineBreaks.Length, encodedBytesCount);

                string encodedText = Text.Encoding.ASCII.GetString(encodedBytes.ToArray());
                string encodedTextWithLineBreaks = Text.Encoding.ASCII.GetString(encodedBytesWithLineBreaks.ToArray());
                string expectedText = Convert.ToBase64String(source.ToArray());
                string expectedTextWithLineBreaks = Convert.ToBase64String(source.ToArray(), Base64FormattingOptions.InsertLineBreaks);
                Assert.Equal(expectedText, encodedText);
                Assert.Equal(expectedTextWithLineBreaks, encodedTextWithLineBreaks);
            }
        }
Example #2
0
        public void BasicEncodingWithLineBreaks()
        {
            var format = new ParsedFormat('N', 64);

            for (int numBytes = 0; numBytes < 1000; numBytes++)
            {
                Span <byte> source = new byte[numBytes];
                Base64TestHelper.InitalizeBytes(source, numBytes);

                char[]      charArray    = new char[numBytes * 10];
                Span <byte> encodedBytes = new byte[Base64.GetMaxEncodedToUtf8Length(source.Length)];
                Span <byte> encodedBytesWithLineBreaks = new byte[Base64Experimental.GetMaxEncodedToUtf8Length(source.Length, format)];
                Assert.True(OperationStatus.Done == Base64Experimental.EncodeToUtf8(source, encodedBytesWithLineBreaks, out int consumed, out int encodedBytesCount, format), "At index: " + numBytes);
                Assert.Equal(encodedBytesWithLineBreaks.Length, encodedBytesCount);

                string encodedTextWithLineBreaks = Text.Encoding.ASCII.GetString(encodedBytesWithLineBreaks.ToArray());
                Assert.Equal(encodedTextWithLineBreaks, encodedTextWithLineBreaks);
            }
        }
Example #3
0
        private static void Base64EncodeWithLineBreaks(int numberOfBytes)
        {
            Span <byte> source = new byte[numberOfBytes];

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

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

            string encodedText  = Text.Encoding.ASCII.GetString(destination.ToArray());
            string expectedText = Convert.ToBase64String(source.ToArray(), Base64FormattingOptions.InsertLineBreaks);

            Assert.Equal(expectedText, encodedText);
        }