暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UTF8ArrayUnsafeUtilityTests.cs 13KB

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