public void TestOptionSearchInvalidSpecial(SpecialTestCases inputType) { var request = GetOptionSearchParameters(InvalidSpecialMap[inputType]()); var optionSearchHandler = new OptionSearchManager(); var response = optionSearchHandler.OptionSearch <OptionSearchAliasResponse>(request, System.Net.Http.HttpMethod.Post, inputType == SpecialTestCases.WrongContentType ? "text/html" : "application/json"); PrAssert.That(response, PrIs.ErrorResponse().And.HttpCode(System.Net.HttpStatusCode.BadRequest)); }
public static string GenerateStringData(int length, int minCodePoint, int maxCodePoint, SpecialTestCases special = SpecialTestCases.None) { if (special != SpecialTestCases.None) { if (special == SpecialTestCases.AlternatingASCIIAndNonASCII) { return(TextEncoderTestHelper.GenerateStringAlternatingASCIIAndNonASCII(length)); } if (special == SpecialTestCases.MostlyASCIIAndSomeNonASCII) { return(TextEncoderTestHelper.GenerateStringWithMostlyASCIIAndSomeNonASCII(length)); } return(""); } else { return(TextEncoderTestHelper.GenerateValidString(length, minCodePoint, maxCodePoint)); } }
public void EncodeFromUtf8toUtf16UsingEncoding(int length, int minCodePoint, int maxCodePoint, SpecialTestCases special = SpecialTestCases.None) { string inputString = GenerateStringData(length, minCodePoint, maxCodePoint, special); char[] characters = inputString.AsSpan().ToArray(); Text.Encoding utf8 = Text.Encoding.UTF8; int utf8Length = utf8.GetByteCount(characters); var utf8Buffer = new byte[utf8Length]; utf8.GetBytes(characters, 0, characters.Length, utf8Buffer, 0); var utf16Chars = new char[characters.Length]; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) for (int i = 0; i < Benchmark.InnerIterationCount; i++) { utf8.GetChars(utf8Buffer, 0, utf8Buffer.Length, utf16Chars, 0); } } }
public void EncodeFromUtf32toUtf8UsingTextEncoder(int length, int minCodePoint, int maxCodePoint, SpecialTestCases special = SpecialTestCases.None) { string inputString = GenerateStringData(length, minCodePoint, maxCodePoint, special); ReadOnlySpan <byte> utf32 = Text.Encoding.UTF32.GetBytes(inputString); var status = Encoders.Utf32.ToUtf8Length(utf32, out int needed); Assert.Equal(TransformationStatus.Done, status); Span <byte> utf8 = new byte[needed]; foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) { for (int i = 0; i < Benchmark.InnerIterationCount; i++) { status = Encoders.Utf32.ToUtf8(utf32, utf8, out int consumed, out int written); if (status != TransformationStatus.Done) { throw new Exception(); } } } } }
public void EncodeFromUtf16toUtf8UsingTextEncoder(int length, int minCodePoint, int maxCodePoint, SpecialTestCases special = SpecialTestCases.None) { string inputString = GenerateStringData(length, minCodePoint, maxCodePoint, special); ReadOnlySpan <char> characters = inputString.AsSpan(); TextEncoder utf8 = TextEncoder.Utf8; Assert.True(utf8.TryComputeEncodedBytes(characters, out int encodedBytes)); var utf8Buffer = new byte[encodedBytes]; var span = new Span <byte>(utf8Buffer); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) for (int i = 0; i < Benchmark.InnerIterationCount; i++) { if (!utf8.TryEncode(characters, span, out int consumed, out int written)) { throw new Exception(); } } } }
public void EncodeFromUtf8toUtf8UsingTextEncoder(int length, int minCodePoint, int maxCodePoint, SpecialTestCases special = SpecialTestCases.None) { string inputString = GenerateStringData(length, minCodePoint, maxCodePoint, special); char[] characters = inputString.AsSpan().ToArray(); Text.Encoding utf8Encoder = Text.Encoding.UTF8; int utf8Length = utf8Encoder.GetByteCount(characters); var utf8TempBuffer = new byte[utf8Length]; utf8Encoder.GetBytes(characters, 0, characters.Length, utf8TempBuffer, 0); Span <byte> utf8Span = new Span <byte>(utf8TempBuffer); TextEncoder utf8 = TextEncoder.Utf8; var utf8Buffer = new byte[utf8Length]; var span = new Span <byte>(utf8Buffer); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) for (int i = 0; i < Benchmark.InnerIterationCount; i++) { if (!utf8.TryEncode(utf8Span, span, out int consumed, out int written)) { throw new Exception(); } } } }
public void EncodeFromUtf32toUtf16UsingTextEncoder(int length, int minCodePoint, int maxCodePoint, SpecialTestCases special = SpecialTestCases.None) { string inputString = GenerateStringData(length, minCodePoint, maxCodePoint, special); char[] characters = inputString.AsSpan().ToArray(); Text.Encoding utf32 = Text.Encoding.UTF32; int utf32Length = utf32.GetByteCount(characters); var utf32Buffer = new byte[utf32Length]; utf32.GetBytes(characters, 0, characters.Length, utf32Buffer, 0); Span <byte> utf32ByteSpan = new Span <byte>(utf32Buffer); ReadOnlySpan <uint> utf32Span = utf32ByteSpan.NonPortableCast <byte, uint>(); TextEncoder utf16 = TextEncoder.Utf16; Assert.True(utf16.TryComputeEncodedBytes(utf32Span, out int encodedBytes)); var utf16Buffer = new byte[encodedBytes]; var span = new Span <byte>(utf16Buffer); foreach (var iteration in Benchmark.Iterations) { using (iteration.StartMeasurement()) for (int i = 0; i < Benchmark.InnerIterationCount; i++) { if (!utf16.TryEncode(utf32Span, span, out int consumed, out int written)) { throw new Exception(); } } } }