1 // Licensed to the .NET Foundation under one or more agreements. 2 // The .NET Foundation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Xunit; 6 7 namespace System.Reflection.Metadata.Tests 8 { 9 public unsafe class BlobUtilitiesTests 10 { TestGetUTF8ByteCount(int expectedCount, string expectedRemainder, string str, int charCount, int byteLimit)11 private void TestGetUTF8ByteCount(int expectedCount, string expectedRemainder, string str, int charCount, int byteLimit) 12 { 13 fixed (char* ptr = str) 14 { 15 char* remainderPtr; 16 Assert.Equal(expectedCount, BlobUtilities.GetUTF8ByteCount(ptr, charCount, byteLimit, out remainderPtr)); 17 18 string remainder = new string(remainderPtr); 19 Assert.Equal(expectedRemainder, remainder); 20 } 21 } 22 23 [Fact] GetUTF8ByteCount()24 public void GetUTF8ByteCount() 25 { 26 TestGetUTF8ByteCount(0, "", str: "", charCount: 0, byteLimit: int.MaxValue); 27 TestGetUTF8ByteCount(2, "c", str: "abc", charCount: 2, byteLimit: int.MaxValue); 28 TestGetUTF8ByteCount(2, "", str: "\u0123", charCount: 1, byteLimit: int.MaxValue); 29 TestGetUTF8ByteCount(3, "", str: "\u1234", charCount: 1, byteLimit: int.MaxValue); 30 TestGetUTF8ByteCount(3, "", str: "\ud800", charCount: 1, byteLimit: int.MaxValue); 31 TestGetUTF8ByteCount(3, "", str: "\udc00", charCount: 1, byteLimit: int.MaxValue); 32 TestGetUTF8ByteCount(3, "\udc00", str: "\ud800\udc00", charCount: 1, byteLimit: int.MaxValue); // surrogate pair 33 TestGetUTF8ByteCount(4, "", str: "\ud800\udc00", charCount: 2, byteLimit: int.MaxValue); // surrogate pair 34 35 TestGetUTF8ByteCount(0, "", str: "", charCount: 0, byteLimit: 0); 36 TestGetUTF8ByteCount(0, "abc", str: "abc", charCount: 2, byteLimit: 0); 37 TestGetUTF8ByteCount(0, "\u0123", str: "\u0123", charCount: 1, byteLimit: 0); 38 TestGetUTF8ByteCount(0, "\u1234", str: "\u1234", charCount: 1, byteLimit: 0); 39 TestGetUTF8ByteCount(0, "\ud800", str: "\ud800", charCount: 1, byteLimit: 0); 40 TestGetUTF8ByteCount(0, "\udc00", str: "\udc00", charCount: 1, byteLimit: 0); 41 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 1, byteLimit: 0); 42 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 2, byteLimit: 0); // surrogate pair 43 44 TestGetUTF8ByteCount(0, "", str: "", charCount: 0, byteLimit: 1); 45 TestGetUTF8ByteCount(1, "bc", str: "abc", charCount: 2, byteLimit: 1); 46 TestGetUTF8ByteCount(0, "\u0123", str: "\u0123", charCount: 1, byteLimit: 1); 47 TestGetUTF8ByteCount(0, "\u1234", str: "\u1234", charCount: 1, byteLimit: 1); 48 TestGetUTF8ByteCount(0, "\ud800", str: "\ud800", charCount: 1, byteLimit: 1); 49 TestGetUTF8ByteCount(0, "\udc00", str: "\udc00", charCount: 1, byteLimit: 1); 50 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 1, byteLimit: 1); 51 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 2, byteLimit: 1); // surrogate pair 52 53 TestGetUTF8ByteCount(0, "", str: "", charCount: 0, byteLimit: 2); 54 TestGetUTF8ByteCount(2, "c", str: "abc", charCount: 2, byteLimit: 2); 55 TestGetUTF8ByteCount(2, "", str: "\u0123", charCount: 1, byteLimit: 2); 56 TestGetUTF8ByteCount(0, "\u1234", str: "\u1234", charCount: 1, byteLimit: 2); 57 TestGetUTF8ByteCount(0, "\ud800", str: "\ud800", charCount: 1, byteLimit: 2); 58 TestGetUTF8ByteCount(0, "\udc00", str: "\udc00", charCount: 1, byteLimit: 2); 59 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 1, byteLimit: 2); 60 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 2, byteLimit: 2); // surrogate pair 61 62 TestGetUTF8ByteCount(0, "", str: "", charCount: 0, byteLimit: 3); 63 TestGetUTF8ByteCount(2, "c", str: "abc", charCount: 2, byteLimit: 3); 64 TestGetUTF8ByteCount(2, "", str: "\u0123", charCount: 1, byteLimit: 3); 65 TestGetUTF8ByteCount(3, "", str: "\u1234", charCount: 1, byteLimit: 3); 66 TestGetUTF8ByteCount(3, "", str: "\ud800", charCount: 1, byteLimit: 3); 67 TestGetUTF8ByteCount(3, "", str: "\udc00", charCount: 1, byteLimit: 3); 68 TestGetUTF8ByteCount(3, "\udc00", str: "\ud800\udc00", charCount: 1, byteLimit: 3); 69 TestGetUTF8ByteCount(0, "\ud800\udc00", str: "\ud800\udc00", charCount: 2, byteLimit: 3); // surrogate pair 70 71 TestGetUTF8ByteCount(0, "", str: "", charCount: 0, byteLimit: 4); 72 TestGetUTF8ByteCount(2, "c", str: "abc", charCount: 2, byteLimit: 4); 73 TestGetUTF8ByteCount(2, "", str: "\u0123", charCount: 1, byteLimit: 4); 74 TestGetUTF8ByteCount(3, "", str: "\u1234", charCount: 1, byteLimit: 4); 75 TestGetUTF8ByteCount(3, "", str: "\ud800", charCount: 1, byteLimit: 4); 76 TestGetUTF8ByteCount(3, "", str: "\udc00", charCount: 1, byteLimit: 4); 77 TestGetUTF8ByteCount(3, "\udc00", str: "\ud800\udc00", charCount: 1, byteLimit: 4); 78 TestGetUTF8ByteCount(4, "", str: "\ud800\udc00", charCount: 2, byteLimit: 4); // surrogate pair 79 } 80 } 81 } 82