public void BasicEncodingDecoding() { var bytes = new byte[byte.MaxValue + 1]; for (int i = 0; i < byte.MaxValue + 1; i++) { bytes[i] = (byte)i; } for (int value = 0; value < 256; value++) { Span <byte> sourceBytes = bytes.AsSpan().Slice(0, value + 1); Span <byte> encodedBytes = new byte[Base64Encoder.ComputeEncodedLength(sourceBytes.Length)]; Assert.True(Base64Encoder.TryEncode(sourceBytes, encodedBytes, out int consumed, out int encodedBytesCount)); Assert.Equal(encodedBytes.Length, encodedBytesCount); string encodedText = Text.Encoding.ASCII.GetString(encodedBytes.ToArray()); string expectedText = Convert.ToBase64String(bytes, 0, value + 1); Assert.Equal(expectedText, encodedText); if (encodedBytes.Length % 4 == 0) { Span <byte> decodedBytes = new byte[Base64Encoder.ComputeDecodedLength(encodedBytes)]; Assert.True(Base64Encoder.TryDecode(encodedBytes, decodedBytes, out consumed, out int decodedByteCount)); Assert.Equal(sourceBytes.Length, decodedByteCount); Assert.True(sourceBytes.SequenceEqual(decodedBytes)); } } }
public void DecodeInPlace() { var list = new List <byte>(); for (int value = 0; value < 256; value++) { list.Add((byte)value); } var testBytes = list.ToArray(); for (int value = 0; value < 256; value++) { var sourceBytes = testBytes.AsSpan().Slice(0, value + 1); var buffer = new byte[Base64Encoder.ComputeEncodedLength(sourceBytes.Length)]; var bufferSlice = buffer.AsSpan(); Base64Encoder.TryEncode(sourceBytes, bufferSlice, out int consumed, out int written); var encodedText = Text.Encoding.ASCII.GetString(bufferSlice.ToArray()); var expectedText = Convert.ToBase64String(testBytes, 0, value + 1); Assert.Equal(expectedText, encodedText); var decodedByteCount = Base64Encoder.DecodeInPlace(bufferSlice); Assert.Equal(sourceBytes.Length, decodedByteCount); for (int i = 0; i < decodedByteCount; i++) { Assert.Equal(sourceBytes[i], buffer[i]); } } }
public void ComputeEncodedLength() { // (int.MaxValue - 4)/(4/3) => 1610612733, otherwise integer overflow int[] input = { 0, 1, 2, 3, 4, 5, 6, 1610612728, 1610612729, 1610612730, 1610612731, 1610612732, 1610612733 }; int[] expected = { 0, 4, 4, 4, 8, 8, 8, 2147483640, 2147483640, 2147483640, 2147483644, 2147483644, 2147483644 }; for (int i = 0; i < input.Length; i++) { Assert.Equal(expected[i], Base64Encoder.ComputeEncodedLength(input[i])); } Assert.True(Base64Encoder.ComputeEncodedLength(1610612734) < 0); // integer overflow }
private static void Base64EncodeBaseline(int numberOfBytes) { var source = new byte[numberOfBytes]; InitalizeBytes(source.AsSpan()); var destination = new char[Base64Encoder.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); } } } }
private static void Base64Encode(int numberOfBytes) { Span <byte> source = new byte[numberOfBytes]; InitalizeBytes(source); Span <byte> destination = new byte[Base64Encoder.ComputeEncodedLength(numberOfBytes)]; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { Base64Encoder.TryEncode(source, destination, out int consumed, out int written); } } } }
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]; InitalizeBytes(source, numBytes); Span <byte> encodedBytes = new byte[Base64Encoder.ComputeEncodedLength(source.Length)]; Assert.True(Base64Encoder.TryEncode(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); } }
public void EncodingOutputTooSmall() { Span <byte> source = new byte[750]; InitalizeBytes(source); int outputSize = 320; int requiredSize = Base64Encoder.ComputeEncodedLength(source.Length); Span <byte> encodedBytes = new byte[outputSize]; Assert.False(Base64Encoder.TryEncode(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.True(Base64Encoder.TryEncode(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); }