A game about forced loneliness, made by TACStudios
at master 215 lines 13 kB view raw
1using NUnit.Framework; 2using Unity.Collections; 3using Unity.Collections.LowLevel.Unsafe; 4using Unity.Collections.Tests; 5 6[TestFixture] 7internal class UTF8ArrayUnsafeUtilityTests : CollectionsTestCommonBase 8{ 9 [TestCase("The Quick Brown Fox Jumps Over The Lazy Dog")] 10 [TestCase("Albert osti fagotin ja töräytti puhkuvan melodian.", TestName = "{m}(Finnish)")] 11 [TestCase("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.", TestName = "{m}(German)")] 12 [TestCase("איך בלש תפס גמד רוצח עז קטנה?", TestName = "{m}(Hebrew)")] 13 [TestCase("PORTEZ CE VIEUX WHISKY AU JUGE BLOND QUI FUME.", TestName = "{m}(French)")] 14 [TestCase("いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす", TestName = "{m}(Japanese)")] 15 [TestCase("키스의 고유조건은 입술끼리 만나야 하고 특별한 기술은 필요치 않다.", TestName = "{m}(Korean)")] 16 public void CopyTest(string text) 17 { 18 unsafe 19 { 20 int bytes = text.Length*4; 21 int chars = text.Length*2; 22 var destByte = stackalloc byte[bytes]; 23 var destChar = stackalloc char[chars]; 24 var srcByte = stackalloc byte[bytes]; 25 var srcChar = stackalloc char[chars]; 26 int byteLength = 0; 27 int charLength = text.Length; 28 fixed(char *t = text) 29 { 30 Unicode.Utf16ToUtf8(t, text.Length, srcByte, out byteLength, bytes); 31 UnsafeUtility.MemCpy(srcChar, t, charLength*sizeof(char)); 32 } 33 CopyError shouldPass, shouldFail; 34 int destLengthInt = default; 35 ushort destLengthUshort = default; 36 37 shouldPass = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthInt, bytes, srcChar, charLength); 38 shouldFail = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthInt, destLengthInt-1, srcChar, charLength); 39 Assert.AreEqual(CopyError.None, shouldPass); 40 Assert.AreEqual(CopyError.Truncation, shouldFail); 41 42 shouldPass = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthUshort, (ushort)bytes, srcChar, charLength); 43 shouldFail = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthUshort, (ushort)(destLengthUshort-1), srcChar, charLength); 44 Assert.AreEqual(CopyError.None, shouldPass); 45 Assert.AreEqual(CopyError.Truncation, shouldFail); 46 47 shouldPass = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthInt, bytes, srcByte, byteLength); 48 shouldFail = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthInt, destLengthInt-1, srcByte, byteLength); 49 Assert.AreEqual(CopyError.None, shouldPass); 50 Assert.AreEqual(CopyError.Truncation, shouldFail); 51 52 shouldPass = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthUshort, (ushort)bytes, srcByte, (ushort)byteLength); 53 shouldFail = UTF8ArrayUnsafeUtility.Copy(destByte, out destLengthUshort, (ushort)(destLengthUshort-1), srcByte, (ushort)byteLength); 54 Assert.AreEqual(CopyError.None, shouldPass); 55 Assert.AreEqual(CopyError.Truncation, shouldFail); 56 57 shouldPass = UTF8ArrayUnsafeUtility.Copy(destChar, out destLengthInt, chars, srcByte, byteLength); 58 shouldFail = UTF8ArrayUnsafeUtility.Copy(destChar, out destLengthInt, destLengthInt-1, srcByte, byteLength); 59 Assert.AreEqual(CopyError.None, shouldPass); 60 Assert.AreEqual(CopyError.Truncation, shouldFail); 61 62 shouldPass = UTF8ArrayUnsafeUtility.Copy(destChar, out destLengthUshort, (ushort)chars, srcByte, (ushort)byteLength); 63 shouldFail = UTF8ArrayUnsafeUtility.Copy(destChar, out destLengthUshort, (ushort)(destLengthUshort-1), srcByte, (ushort)byteLength); 64 Assert.AreEqual(CopyError.None, shouldPass); 65 Assert.AreEqual(CopyError.Truncation, shouldFail); 66 } 67 } 68 69 // public static FormatError AppendUTF8Bytes(byte* dest, ref int destLength, int destCapacity, byte* src, int srcLength) 70 [TestCase("The Quick Brown Fox Jumps Over The Lazy Dog")] 71 [TestCase("Albert osti fagotin ja töräytti puhkuvan melodian.", TestName = "{m}(Finnish)")] 72 [TestCase("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.", TestName = "{m}(German)")] 73 [TestCase("איך בלש תפס גמד רוצח עז קטנה?", TestName = "{m}(Hebrew)")] 74 [TestCase("PORTEZ CE VIEUX WHISKY AU JUGE BLOND QUI FUME.", TestName = "{m}(French)")] 75 [TestCase("いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす", TestName = "{m}(Japanese)")] 76 [TestCase("키스의 고유조건은 입술끼리 만나야 하고 특별한 기술은 필요치 않다.", TestName = "{m}(Korean)")] 77 public void AppendUTF8BytesTest(string text) 78 { 79 unsafe 80 { 81 ushort bytes = (ushort)(text.Length*4); 82 var destByte = stackalloc byte[bytes]; 83 var srcByte = stackalloc byte[bytes]; 84 int byteLength = 0; 85 fixed(char *t = text) 86 { 87 Unicode.Utf16ToUtf8(t, text.Length, srcByte, out byteLength, bytes); 88 } 89 FormatError shouldPass, shouldFail; 90 int destLength; 91 destLength = 0; 92 shouldPass = UTF8ArrayUnsafeUtility.AppendUTF8Bytes(destByte, ref destLength, bytes, srcByte, byteLength); 93 destLength = 0; 94 shouldFail = UTF8ArrayUnsafeUtility.AppendUTF8Bytes(destByte, ref destLength, byteLength-1, srcByte, byteLength); 95 Assert.AreEqual(FormatError.None, shouldPass); 96 Assert.AreEqual(FormatError.Overflow, shouldFail); 97 } 98 } 99 100 // public static CopyError Append(byte *dest, ref ushort destLength, ushort destUTF8MaxLengthInBytes, byte *src, ushort srcLength) 101 // public static CopyError Append(byte *dest, ref ushort destLength, ushort destUTF8MaxLengthInBytes, char *src, int srcLength) 102 // public static CopyError Append(char *dest, ref ushort destLength, ushort destUCS2MaxLengthInChars, byte *src, ushort srcLength) 103 [TestCase("The Quick Brown Fox Jumps Over The Lazy Dog")] 104 [TestCase("Albert osti fagotin ja töräytti puhkuvan melodian.", TestName = "{m}(Finnish)")] 105 [TestCase("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.", TestName = "{m}(German)")] 106 [TestCase("איך בלש תפס גמד רוצח עז קטנה?", TestName = "{m}(Hebrew)")] 107 [TestCase("PORTEZ CE VIEUX WHISKY AU JUGE BLOND QUI FUME.", TestName = "{m}(French)")] 108 [TestCase("いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす", TestName = "{m}(Japanese)")] 109 [TestCase("키스의 고유조건은 입술끼리 만나야 하고 특별한 기술은 필요치 않다.", TestName = "{m}(Korean)")] 110 public void AppendTest(string text) 111 { 112 unsafe 113 { 114 ushort bytes = (ushort)(text.Length*4); 115 ushort chars = (ushort)(text.Length*2); 116 var destByte = stackalloc byte[bytes]; 117 var destChar = stackalloc char[chars]; 118 var srcByte = stackalloc byte[bytes]; 119 var srcChar = stackalloc char[chars]; 120 int byteLength = 0; 121 int charLength = text.Length; 122 fixed(char *t = text) 123 { 124 Unicode.Utf16ToUtf8(t, text.Length, srcByte, out byteLength, bytes); 125 UnsafeUtility.MemCpy(srcChar, t, charLength*sizeof(char)); 126 } 127 CopyError shouldPass, shouldFail; 128 ushort destLength = default; 129 ushort tooFewBytes = 0; 130 ushort tooFewChars = 0; 131 132 destLength = 0; 133 shouldPass = UTF8ArrayUnsafeUtility.Append(destByte, ref destLength, bytes, srcByte, (ushort)charLength); 134 tooFewBytes = (ushort)(destLength - 1); 135 destLength = 0; 136 shouldFail = UTF8ArrayUnsafeUtility.Append(destByte, ref destLength, tooFewBytes, srcByte, (ushort)charLength); 137 Assert.AreEqual(CopyError.None, shouldPass); 138 Assert.AreEqual(CopyError.Truncation, shouldFail); 139 140 destLength = 0; 141 shouldPass = UTF8ArrayUnsafeUtility.Append(destByte, ref destLength, bytes, srcChar, charLength); 142 tooFewBytes = (ushort)(destLength - 1); 143 destLength = 0; 144 shouldFail = UTF8ArrayUnsafeUtility.Append(destByte, ref destLength, tooFewBytes, srcChar, charLength); 145 Assert.AreEqual(CopyError.None, shouldPass); 146 Assert.AreEqual(CopyError.Truncation, shouldFail); 147 148 destLength = 0; 149 shouldPass = UTF8ArrayUnsafeUtility.Append(destChar, ref destLength, chars, srcByte, (ushort)charLength); 150 tooFewChars = (ushort)(destLength - 1); 151 destLength = 0; 152 shouldFail = UTF8ArrayUnsafeUtility.Append(destChar, ref destLength, tooFewChars, srcByte, (ushort)charLength); 153 Assert.AreEqual(CopyError.None, shouldPass); 154 Assert.AreEqual(CopyError.Truncation, shouldFail); 155 } 156 } 157 158 [TestCase("The Quick Brown Fox Jumps Over The Lazy Dog")] 159 [TestCase("Albert osti fagotin ja töräytti puhkuvan melodian.", TestName = "{m}(Finnish)")] 160 [TestCase("Franz jagt im komplett verwahrlosten Taxi quer durch Bayern.", TestName = "{m}(German)")] 161 [TestCase("איך בלש תפס גמד רוצח עז קטנה?", TestName = "{m}(Hebrew)")] 162 [TestCase("PORTEZ CE VIEUX WHISKY AU JUGE BLOND QUI FUME.", TestName = "{m}(French)")] 163 [TestCase("いろはにほへとちりぬるをわかよたれそつねならむうゐのおくやまけふこえてあさきゆめみしゑひもせす", TestName = "{m}(Japanese)")] 164 [TestCase("키스의 고유조건은 입술끼리 만나야 하고 특별한 기술은 필요치 않다.", TestName = "{m}(Korean)")] 165 public void EqualsUTF8BytesTest(string text) 166 { 167 unsafe 168 { 169 ushort bytes = (ushort)(text.Length*4); 170 var srcByte0 = stackalloc byte[bytes]; 171 var srcByte1 = stackalloc byte[bytes]; 172 int byteLength = 0; 173 fixed(char *t = text) 174 { 175 Unicode.Utf16ToUtf8(t, text.Length, srcByte0, out byteLength, bytes); 176 Unicode.Utf16ToUtf8(t, text.Length, srcByte1, out byteLength, bytes); 177 } 178 bool shouldPass, shouldFail; 179 shouldPass = UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(srcByte0, byteLength, srcByte1, byteLength); 180 shouldFail = UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(srcByte0, byteLength, srcByte1, byteLength-1); 181 Assert.AreEqual(true, shouldPass); 182 Assert.AreEqual(false, shouldFail); 183 } 184 } 185 186 [TestCase("PORTEZ", "VIEUX", TestName = "{m}(French)")] 187 [TestCase("いろは", "にほへ", TestName = "{m}(Japanese)")] 188 [TestCase("고유조건은", "키스의", TestName = "{m}(Korean)")] 189 public unsafe void StrCmpTest(string utf16A, string utf16B) 190 { 191 FixedString32Bytes utf8A = utf16A; 192 FixedString32Bytes utf8B = utf16B; 193 byte *utf8BufferA = utf8A.GetUnsafePtr(); 194 byte *utf8BufferB = utf8B.GetUnsafePtr(); 195 fixed(char *utf16BufferA = utf16A) 196 fixed(char *utf16BufferB = utf16B) 197 { 198 Assert.IsTrue(0 > UTF8ArrayUnsafeUtility.StrCmp(utf8BufferA, utf8A.Length, utf8BufferB, utf8B.Length)); 199 Assert.IsTrue(0 > UTF8ArrayUnsafeUtility.StrCmp(utf16BufferA, utf16A.Length, utf8BufferB, utf8B.Length)); 200 Assert.IsTrue(0 > UTF8ArrayUnsafeUtility.StrCmp(utf8BufferA, utf8A.Length, utf16BufferB, utf16B.Length)); 201 Assert.IsTrue(0 > UTF8ArrayUnsafeUtility.StrCmp(utf16BufferA, utf16A.Length, utf16BufferB, utf16B.Length)); 202 203 Assert.AreEqual(0, UTF8ArrayUnsafeUtility.StrCmp(utf8BufferA, utf8A.Length, utf8BufferA, utf8A.Length)); 204 Assert.AreEqual(0, UTF8ArrayUnsafeUtility.StrCmp(utf16BufferA, utf16A.Length, utf8BufferA, utf8A.Length)); 205 Assert.AreEqual(0, UTF8ArrayUnsafeUtility.StrCmp(utf8BufferA, utf8A.Length, utf16BufferA, utf16A.Length)); 206 Assert.AreEqual(0, UTF8ArrayUnsafeUtility.StrCmp(utf16BufferA, utf16A.Length, utf16BufferA, utf16A.Length)); 207 208 Assert.IsTrue(0 < UTF8ArrayUnsafeUtility.StrCmp(utf8BufferB, utf8B.Length, utf8BufferA, utf8A.Length)); 209 Assert.IsTrue(0 < UTF8ArrayUnsafeUtility.StrCmp(utf16BufferB, utf16B.Length, utf8BufferA, utf8A.Length)); 210 Assert.IsTrue(0 < UTF8ArrayUnsafeUtility.StrCmp(utf8BufferB, utf8B.Length, utf16BufferA, utf16A.Length)); 211 Assert.IsTrue(0 < UTF8ArrayUnsafeUtility.StrCmp(utf16BufferB, utf16B.Length, utf16BufferA, utf16A.Length)); 212 } 213 } 214} 215