Нема описа
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.

UnsafeTextTests.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. #if !UNITY_DOTSRUNTIME
  2. using System;
  3. using NUnit.Framework;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using System.Text;
  7. using Unity.Burst;
  8. using Unity.Jobs;
  9. namespace FixedStringTests
  10. {
  11. internal class UnsafeTextTests
  12. {
  13. void AssertAreEqualInTest(string expected, in UnsafeText actual)
  14. {
  15. var actualString = actual.ToString();
  16. Assert.AreEqual(expected, actualString);
  17. }
  18. // NOTE: If you call this function from Mono and T is not marshalable - your app (Editor or the player built with Mono scripting backend) could/will crash.
  19. bool IsMarshalable<T>() where T : struct
  20. {
  21. try
  22. {
  23. unsafe
  24. {
  25. var size = System.Runtime.InteropServices.Marshal.SizeOf(typeof(T));
  26. IntPtr memoryIntPtr = System.Runtime.InteropServices.Marshal.AllocHGlobal(size);
  27. try
  28. {
  29. var obj = new T();
  30. System.Runtime.InteropServices.Marshal.StructureToPtr(obj, memoryIntPtr, false);
  31. System.Runtime.InteropServices.Marshal.DestroyStructure<T>(memoryIntPtr);
  32. }
  33. finally
  34. {
  35. System.Runtime.InteropServices.Marshal.FreeHGlobal(memoryIntPtr);
  36. }
  37. return true;
  38. }
  39. }
  40. catch (Exception e)
  41. {
  42. UnityEngine.Debug.LogError("ERROR in IsMarshalable<" + typeof(T).FullName + "> " + e);
  43. return false;
  44. }
  45. }
  46. [Test]
  47. public void UnsafeTextIsMarshalable()
  48. {
  49. var result = IsMarshalable<UnsafeText>();
  50. Assert.IsTrue(result);
  51. }
  52. [Test]
  53. public unsafe void UnsafeTextCorrectBinaryHeader()
  54. {
  55. var text = new UnsafeText(42, Allocator.Persistent);
  56. var ptr = text.GetUnsafePtr();
  57. Assert.AreEqual(0 + 1, text.m_UntypedListData.m_length);
  58. Assert.AreEqual(Allocator.Persistent, text.m_UntypedListData.Allocator.ToAllocator);
  59. Assert.IsTrue(ptr == text.m_UntypedListData.Ptr, "ptr != text.m_UntypedListData.Ptr");
  60. var listOfBytesCast = text.AsUnsafeListOfBytes();
  61. Assert.AreEqual(0 + 1, listOfBytesCast.Length);
  62. Assert.AreEqual(Allocator.Persistent, listOfBytesCast.Allocator.ToAllocator);
  63. Assert.IsTrue(ptr == listOfBytesCast.Ptr, "ptr != listOfBytesCast.Ptr");
  64. Assert.AreEqual(text.m_UntypedListData.m_capacity, listOfBytesCast.Capacity);
  65. text.Dispose();
  66. }
  67. [Test]
  68. public void UnsafeTextCorrectLengthAfterClear()
  69. {
  70. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  71. Assert.True(aa.IsCreated);
  72. Assert.AreEqual(0, aa.Length, "Length after creation is not 0");
  73. aa.AssertNullTerminated();
  74. aa.Junk();
  75. aa.Clear();
  76. Assert.AreEqual(0, aa.Length, "Length after clear is not 0");
  77. aa.AssertNullTerminated();
  78. aa.Dispose();
  79. }
  80. [Test]
  81. public void UnsafeTextFormatExtension1Params()
  82. {
  83. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  84. Assert.True(aa.IsCreated);
  85. aa.Junk();
  86. FixedString32Bytes format = "{0}";
  87. FixedString32Bytes arg0 = "a";
  88. aa.AppendFormat(format, arg0);
  89. aa.Append('a');
  90. aa.AssertNullTerminated();
  91. AssertAreEqualInTest("aa", aa);
  92. aa.Dispose();
  93. }
  94. [Test]
  95. public void UnsafeTextFormatExtension2Params()
  96. {
  97. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  98. aa.Junk();
  99. FixedString32Bytes format = "{0} {1}";
  100. FixedString32Bytes arg0 = "a";
  101. FixedString32Bytes arg1 = "b";
  102. aa.AppendFormat(format, arg0, arg1);
  103. AssertAreEqualInTest("a b", aa);
  104. aa.AssertNullTerminated();
  105. aa.Dispose();
  106. }
  107. [Test]
  108. public void UnsafeTextFormatExtension3Params()
  109. {
  110. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  111. aa.Junk();
  112. FixedString32Bytes format = "{0} {1} {2}";
  113. FixedString32Bytes arg0 = "a";
  114. FixedString32Bytes arg1 = "b";
  115. FixedString32Bytes arg2 = "c";
  116. aa.AppendFormat(format, arg0, arg1, arg2);
  117. AssertAreEqualInTest("a b c", aa);
  118. aa.AssertNullTerminated();
  119. aa.Dispose();
  120. }
  121. [Test]
  122. public void UnsafeTextFormatExtension4Params()
  123. {
  124. UnsafeText aa = new UnsafeText(512, Allocator.Temp);
  125. aa.Junk();
  126. FixedString32Bytes format = "{0} {1} {2} {3}";
  127. FixedString32Bytes arg0 = "a";
  128. FixedString32Bytes arg1 = "b";
  129. FixedString32Bytes arg2 = "c";
  130. FixedString32Bytes arg3 = "d";
  131. aa.AppendFormat(format, arg0, arg1, arg2, arg3);
  132. AssertAreEqualInTest("a b c d", aa);
  133. aa.AssertNullTerminated();
  134. aa.Dispose();
  135. }
  136. [Test]
  137. public void UnsafeTextFormatExtension5Params()
  138. {
  139. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  140. aa.Junk();
  141. FixedString32Bytes format = "{0} {1} {2} {3} {4}";
  142. FixedString32Bytes arg0 = "a";
  143. FixedString32Bytes arg1 = "b";
  144. FixedString32Bytes arg2 = "c";
  145. FixedString32Bytes arg3 = "d";
  146. FixedString32Bytes arg4 = "e";
  147. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4);
  148. AssertAreEqualInTest("a b c d e", aa);
  149. aa.AssertNullTerminated();
  150. aa.Dispose();
  151. }
  152. [Test]
  153. public void UnsafeTextFormatExtension6Params()
  154. {
  155. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  156. aa.Junk();
  157. FixedString32Bytes format = "{0} {1} {2} {3} {4} {5}";
  158. FixedString32Bytes arg0 = "a";
  159. FixedString32Bytes arg1 = "b";
  160. FixedString32Bytes arg2 = "c";
  161. FixedString32Bytes arg3 = "d";
  162. FixedString32Bytes arg4 = "e";
  163. FixedString32Bytes arg5 = "f";
  164. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5);
  165. AssertAreEqualInTest("a b c d e f", aa);
  166. aa.AssertNullTerminated();
  167. aa.Dispose();
  168. }
  169. [Test]
  170. public void UnsafeTextFormatExtension7Params()
  171. {
  172. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  173. aa.Junk();
  174. FixedString32Bytes format = "{0} {1} {2} {3} {4} {5} {6}";
  175. FixedString32Bytes arg0 = "a";
  176. FixedString32Bytes arg1 = "b";
  177. FixedString32Bytes arg2 = "c";
  178. FixedString32Bytes arg3 = "d";
  179. FixedString32Bytes arg4 = "e";
  180. FixedString32Bytes arg5 = "f";
  181. FixedString32Bytes arg6 = "g";
  182. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
  183. AssertAreEqualInTest("a b c d e f g", aa);
  184. aa.AssertNullTerminated();
  185. aa.Dispose();
  186. }
  187. [Test]
  188. public void UnsafeTextFormatExtension8Params()
  189. {
  190. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  191. aa.Junk();
  192. FixedString128Bytes format = "{0} {1} {2} {3} {4} {5} {6} {7}";
  193. FixedString32Bytes arg0 = "a";
  194. FixedString32Bytes arg1 = "b";
  195. FixedString32Bytes arg2 = "c";
  196. FixedString32Bytes arg3 = "d";
  197. FixedString32Bytes arg4 = "e";
  198. FixedString32Bytes arg5 = "f";
  199. FixedString32Bytes arg6 = "g";
  200. FixedString32Bytes arg7 = "h";
  201. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
  202. AssertAreEqualInTest("a b c d e f g h", aa);
  203. aa.AssertNullTerminated();
  204. aa.Dispose();
  205. }
  206. [Test]
  207. public void UnsafeTextFormatExtension9Params()
  208. {
  209. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  210. aa.Junk();
  211. FixedString128Bytes format = "{0} {1} {2} {3} {4} {5} {6} {7} {8}";
  212. FixedString32Bytes arg0 = "a";
  213. FixedString32Bytes arg1 = "b";
  214. FixedString32Bytes arg2 = "c";
  215. FixedString32Bytes arg3 = "d";
  216. FixedString32Bytes arg4 = "e";
  217. FixedString32Bytes arg5 = "f";
  218. FixedString32Bytes arg6 = "g";
  219. FixedString32Bytes arg7 = "h";
  220. FixedString32Bytes arg8 = "i";
  221. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  222. AssertAreEqualInTest("a b c d e f g h i", aa);
  223. aa.AssertNullTerminated();
  224. aa.Dispose();
  225. }
  226. [Test]
  227. public void UnsafeTextFormatExtension10Params()
  228. {
  229. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  230. aa.Junk();
  231. FixedString128Bytes format = "{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}";
  232. FixedString32Bytes arg0 = "a";
  233. FixedString32Bytes arg1 = "b";
  234. FixedString32Bytes arg2 = "c";
  235. FixedString32Bytes arg3 = "d";
  236. FixedString32Bytes arg4 = "e";
  237. FixedString32Bytes arg5 = "f";
  238. FixedString32Bytes arg6 = "g";
  239. FixedString32Bytes arg7 = "h";
  240. FixedString32Bytes arg8 = "i";
  241. FixedString32Bytes arg9 = "j";
  242. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
  243. AssertAreEqualInTest("a b c d e f g h i j", aa);
  244. aa.AssertNullTerminated();
  245. aa.Dispose();
  246. }
  247. [Test]
  248. public void UnsafeTextAppendGrows()
  249. {
  250. UnsafeText aa = new UnsafeText(1, Allocator.Temp);
  251. var origCapacity = aa.Capacity;
  252. for (int i = 0; i < origCapacity; ++i)
  253. aa.Append('a');
  254. Assert.AreEqual(origCapacity, aa.Capacity);
  255. aa.Append('b');
  256. Assert.GreaterOrEqual(aa.Capacity, origCapacity);
  257. Assert.AreEqual(new String('a', origCapacity) + "b", aa.ToString());
  258. aa.Dispose();
  259. }
  260. [Test]
  261. public void UnsafeTextAppendString()
  262. {
  263. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  264. aa.Append("aa");
  265. Assert.AreEqual("aa", aa.ToString());
  266. aa.Append("bb");
  267. Assert.AreEqual("aabb", aa.ToString());
  268. aa.Dispose();
  269. }
  270. [TestCase("Antidisestablishmentarianism")]
  271. [TestCase("⁣🌹🌻🌷🌿🌵🌾⁣")]
  272. public void UnsafeTextCopyFromBytesWorks(String a)
  273. {
  274. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  275. aa.Junk();
  276. var utf8 = Encoding.UTF8.GetBytes(a);
  277. unsafe
  278. {
  279. fixed (byte* b = utf8)
  280. aa.Append(b, (ushort) utf8.Length);
  281. }
  282. Assert.AreEqual(a, aa.ToString());
  283. aa.AssertNullTerminated();
  284. aa.Append("tail");
  285. Assert.AreEqual(a + "tail", aa.ToString());
  286. aa.AssertNullTerminated();
  287. aa.Dispose();
  288. }
  289. [TestCase("red")]
  290. [TestCase("紅色", TestName = "{m}(Chinese-Red)")]
  291. [TestCase("George Washington")]
  292. [TestCase("村上春樹", TestName = "{m}(HarukiMurakami)")]
  293. public void UnsafeTextToStringWorks(String a)
  294. {
  295. UnsafeText aa = new UnsafeText(4, Allocator.Temp);
  296. aa.Append(new FixedString128Bytes(a));
  297. Assert.AreEqual(a, aa.ToString());
  298. aa.AssertNullTerminated();
  299. aa.Dispose();
  300. }
  301. [Test]
  302. public void UnsafeTextIndexOf()
  303. {
  304. UnsafeText a = new UnsafeText(16, Allocator.Temp);
  305. a.Append((FixedString64Bytes) "bookkeeper bookkeeper");
  306. UnsafeText b = new UnsafeText(8, Allocator.Temp);
  307. b.Append((FixedString32Bytes) "ookkee");
  308. Assert.AreEqual(1, a.IndexOf(b));
  309. Assert.AreEqual(-1, b.IndexOf(a));
  310. a.Dispose();
  311. b.Dispose();
  312. }
  313. [Test]
  314. public void UnsafeTextLastIndexOf()
  315. {
  316. UnsafeText a = new UnsafeText(16, Allocator.Temp);
  317. a.Append((FixedString64Bytes) "bookkeeper bookkeeper");
  318. UnsafeText b = new UnsafeText(8, Allocator.Temp);
  319. b.Append((FixedString32Bytes) "ookkee");
  320. Assert.AreEqual(12, a.LastIndexOf(b));
  321. Assert.AreEqual(-1, b.LastIndexOf(a));
  322. a.Dispose();
  323. b.Dispose();
  324. }
  325. [Test]
  326. public void UnsafeTextContains()
  327. {
  328. UnsafeText a = new UnsafeText(16, Allocator.Temp);
  329. a.Append((FixedString64Bytes) "bookkeeper bookkeeper");
  330. UnsafeText b = new UnsafeText(8, Allocator.Temp);
  331. b.Append((FixedString32Bytes) "ookkee");
  332. Assert.AreEqual(true, a.Contains(b));
  333. a.Dispose();
  334. b.Dispose();
  335. }
  336. [Test]
  337. public void UnsafeTextComparisons()
  338. {
  339. UnsafeText a = new UnsafeText(16, Allocator.Temp);
  340. a.Append((FixedString64Bytes) "apple");
  341. UnsafeText b = new UnsafeText(8, Allocator.Temp);
  342. b.Append((FixedString32Bytes) "banana");
  343. Assert.AreEqual(false, a.Equals(b));
  344. Assert.AreEqual(true, !b.Equals(a));
  345. a.Dispose();
  346. b.Dispose();
  347. }
  348. [Test]
  349. public void UnsafeText_CustomAllocatorTest()
  350. {
  351. AllocatorManager.Initialize();
  352. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  353. ref var allocator = ref allocatorHelper.Allocator;
  354. allocator.Initialize();
  355. using (var container = new UnsafeText(1, allocator.Handle))
  356. {
  357. }
  358. Assert.IsTrue(allocator.WasUsed);
  359. allocator.Dispose();
  360. allocatorHelper.Dispose();
  361. AllocatorManager.Shutdown();
  362. }
  363. [BurstCompile]
  364. struct BurstedCustomAllocatorJob : IJob
  365. {
  366. [NativeDisableUnsafePtrRestriction]
  367. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  368. public void Execute()
  369. {
  370. unsafe
  371. {
  372. using (var container = new UnsafeText(1, Allocator->Handle))
  373. {
  374. }
  375. }
  376. }
  377. }
  378. [Test]
  379. public unsafe void UnsafeText_BurstedCustomAllocatorTest()
  380. {
  381. AllocatorManager.Initialize();
  382. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  383. ref var allocator = ref allocatorHelper.Allocator;
  384. allocator.Initialize();
  385. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  386. unsafe
  387. {
  388. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  389. handle.Complete();
  390. }
  391. Assert.IsTrue(allocator.WasUsed);
  392. allocator.Dispose();
  393. allocatorHelper.Dispose();
  394. AllocatorManager.Shutdown();
  395. }
  396. }
  397. }
  398. #endif