暫無描述
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.

FixedStringTests.cs 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #if !NET_DOTS
  2. using System;
  3. using System.Globalization;
  4. using System.Threading;
  5. using NUnit.Framework;
  6. using Unity.Collections;
  7. using Unity.Collections.LowLevel.Unsafe;
  8. using System.Text;
  9. #if !UNITY_DOTSRUNTIME
  10. using UnityEditor.VersionControl;
  11. #endif
  12. // change this to change the core type under test
  13. using FixedStringN = Unity.Collections.FixedString128Bytes;
  14. namespace FixedStringTests
  15. {
  16. internal static class FixedStringTestUtils
  17. {
  18. internal unsafe static void Junk<T>(ref this T fs)
  19. where T : struct, INativeList<byte>, IUTF8Bytes
  20. {
  21. var bytes = fs.GetUnsafePtr();
  22. var cap = fs.Capacity;
  23. // Match MSVC stack init pattern
  24. UnsafeUtility.MemSet(bytes, 0xcc, cap);
  25. }
  26. internal unsafe static void AssertNullTerminated<T>(this T fs)
  27. where T : struct, INativeList<byte>, IUTF8Bytes
  28. {
  29. Assert.AreEqual(0, fs.GetUnsafePtr()[fs.Length]);
  30. }
  31. }
  32. internal class FixedStringTests
  33. {
  34. [Test]
  35. public void FixedStringFormat()
  36. {
  37. Assert.AreEqual("1 0", FixedString.Format("{0} {1}", 1, 0));
  38. Assert.AreEqual("0.1 1.2", FixedString.Format("{0} {1}", 0.1f, 1.2f));
  39. Assert.AreEqual("error 500 in line 350: bubbly", FixedString.Format("error {0} in line {1}: {2}", 500, 350, "bubbly"));
  40. }
  41. [Test]
  42. public void FixedStringNFormatExtension1Params()
  43. {
  44. FixedStringN aa = default;
  45. aa.Junk();
  46. FixedStringN format = "{0}";
  47. FixedString32Bytes arg0 = "a";
  48. aa.AppendFormat(format, arg0);
  49. Assert.AreEqual("a", aa);
  50. aa.AssertNullTerminated();
  51. }
  52. [Test]
  53. public void FixedStringNFormatExtension2Params()
  54. {
  55. FixedStringN aa = default;
  56. aa.Junk();
  57. FixedStringN format = "{0} {1}";
  58. FixedString32Bytes arg0 = "a";
  59. FixedString32Bytes arg1 = "b";
  60. aa.AppendFormat(format, arg0, arg1);
  61. Assert.AreEqual("a b", aa);
  62. aa.AssertNullTerminated();
  63. }
  64. [Test]
  65. public void FixedStringNFormatExtension3Params()
  66. {
  67. FixedStringN aa = default;
  68. aa.Junk();
  69. FixedStringN format = "{0} {1} {2}";
  70. FixedString32Bytes arg0 = "a";
  71. FixedString32Bytes arg1 = "b";
  72. FixedString32Bytes arg2 = "c";
  73. aa.AppendFormat(format, arg0, arg1, arg2);
  74. Assert.AreEqual("a b c", aa);
  75. aa.AssertNullTerminated();
  76. }
  77. [Test]
  78. public void FixedStringNFormatExtension4Params()
  79. {
  80. FixedStringN aa = default;
  81. aa.Junk();
  82. FixedStringN format = "{0} {1} {2} {3}";
  83. FixedString32Bytes arg0 = "a";
  84. FixedString32Bytes arg1 = "b";
  85. FixedString32Bytes arg2 = "c";
  86. FixedString32Bytes arg3 = "d";
  87. aa.AppendFormat(format, arg0, arg1, arg2, arg3);
  88. Assert.AreEqual("a b c d", aa);
  89. aa.AssertNullTerminated();
  90. }
  91. [Test]
  92. public void FixedStringNFormatExtension5Params()
  93. {
  94. FixedStringN aa = default;
  95. aa.Junk();
  96. FixedStringN format = "{0} {1} {2} {3} {4}";
  97. FixedString32Bytes arg0 = "a";
  98. FixedString32Bytes arg1 = "b";
  99. FixedString32Bytes arg2 = "c";
  100. FixedString32Bytes arg3 = "d";
  101. FixedString32Bytes arg4 = "e";
  102. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4);
  103. Assert.AreEqual("a b c d e", aa);
  104. aa.AssertNullTerminated();
  105. }
  106. [Test]
  107. public void FixedStringNFormatExtension6Params()
  108. {
  109. FixedStringN aa = default;
  110. aa.Junk();
  111. FixedStringN format = "{0} {1} {2} {3} {4} {5}";
  112. FixedString32Bytes arg0 = "a";
  113. FixedString32Bytes arg1 = "b";
  114. FixedString32Bytes arg2 = "c";
  115. FixedString32Bytes arg3 = "d";
  116. FixedString32Bytes arg4 = "e";
  117. FixedString32Bytes arg5 = "f";
  118. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5);
  119. Assert.AreEqual("a b c d e f", aa);
  120. aa.AssertNullTerminated();
  121. }
  122. [Test]
  123. public void FixedStringNFormatExtension7Params()
  124. {
  125. FixedStringN aa = default;
  126. aa.Junk();
  127. FixedStringN format = "{0} {1} {2} {3} {4} {5} {6}";
  128. FixedString32Bytes arg0 = "a";
  129. FixedString32Bytes arg1 = "b";
  130. FixedString32Bytes arg2 = "c";
  131. FixedString32Bytes arg3 = "d";
  132. FixedString32Bytes arg4 = "e";
  133. FixedString32Bytes arg5 = "f";
  134. FixedString32Bytes arg6 = "g";
  135. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6);
  136. Assert.AreEqual("a b c d e f g", aa);
  137. aa.AssertNullTerminated();
  138. }
  139. [Test]
  140. public void FixedStringNFormatExtension8Params()
  141. {
  142. FixedStringN aa = default;
  143. aa.Junk();
  144. FixedStringN format = "{0} {1} {2} {3} {4} {5} {6} {7}";
  145. FixedString32Bytes arg0 = "a";
  146. FixedString32Bytes arg1 = "b";
  147. FixedString32Bytes arg2 = "c";
  148. FixedString32Bytes arg3 = "d";
  149. FixedString32Bytes arg4 = "e";
  150. FixedString32Bytes arg5 = "f";
  151. FixedString32Bytes arg6 = "g";
  152. FixedString32Bytes arg7 = "h";
  153. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
  154. Assert.AreEqual("a b c d e f g h", aa);
  155. aa.AssertNullTerminated();
  156. }
  157. [Test]
  158. public void FixedStringNFormatExtension9Params()
  159. {
  160. FixedStringN aa = default;
  161. aa.Junk();
  162. FixedStringN format = "{0} {1} {2} {3} {4} {5} {6} {7} {8}";
  163. FixedString32Bytes arg0 = "a";
  164. FixedString32Bytes arg1 = "b";
  165. FixedString32Bytes arg2 = "c";
  166. FixedString32Bytes arg3 = "d";
  167. FixedString32Bytes arg4 = "e";
  168. FixedString32Bytes arg5 = "f";
  169. FixedString32Bytes arg6 = "g";
  170. FixedString32Bytes arg7 = "h";
  171. FixedString32Bytes arg8 = "i";
  172. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  173. Assert.AreEqual("a b c d e f g h i", aa);
  174. aa.AssertNullTerminated();
  175. }
  176. [Test]
  177. public void FixedStringNFormatExtension10Params()
  178. {
  179. FixedStringN aa = default;
  180. aa.Junk();
  181. FixedStringN format = "{0} {1} {2} {3} {4} {5} {6} {7} {8} {9}";
  182. FixedString32Bytes arg0 = "a";
  183. FixedString32Bytes arg1 = "b";
  184. FixedString32Bytes arg2 = "c";
  185. FixedString32Bytes arg3 = "d";
  186. FixedString32Bytes arg4 = "e";
  187. FixedString32Bytes arg5 = "f";
  188. FixedString32Bytes arg6 = "g";
  189. FixedString32Bytes arg7 = "h";
  190. FixedString32Bytes arg8 = "i";
  191. FixedString32Bytes arg9 = "j";
  192. aa.AppendFormat(format, arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9);
  193. Assert.AreEqual("a b c d e f g h i j", aa);
  194. aa.AssertNullTerminated();
  195. }
  196. [Test]
  197. public void FixedStringNAppendString()
  198. {
  199. FixedStringN aa = default;
  200. Assert.AreEqual(CopyError.None, aa.CopyFrom(new FixedString32Bytes("aa")));
  201. Assert.AreEqual("aa", aa.ToString());
  202. Assert.AreEqual(FormatError.None, aa.Append("bb"));
  203. Assert.AreEqual("aabb", aa.ToString());
  204. }
  205. [Test]
  206. public void FixedStringRuneWorks()
  207. {
  208. var rune = new Unicode.Rune(0xfbad);
  209. FixedStringN a = new FixedStringN(rune, 3);
  210. FixedStringN b = default;
  211. Assert.AreEqual(FormatError.None, b.Append(rune));
  212. Assert.AreEqual(FormatError.None, b.Append(rune, 2));
  213. Assert.AreEqual(a.ToString(), b.ToString());
  214. }
  215. [TestCase("Antidisestablishmentarianism")]
  216. [TestCase("⁣🌹🌻🌷🌿🌵🌾⁣")]
  217. public void FixedStringNCopyFromBytesWorks(String a)
  218. {
  219. FixedStringN aa = default;
  220. aa.Junk();
  221. Assert.AreEqual(CopyError.None, aa.CopyFrom(a));
  222. Assert.AreEqual(a, aa.ToString());
  223. aa.AssertNullTerminated();
  224. Assert.AreEqual(FormatError.None, aa.Append("tail"));
  225. Assert.AreEqual(a + "tail", aa.ToString());
  226. aa.AssertNullTerminated();
  227. }
  228. [TestCase("red")]
  229. [TestCase("紅色", TestName="{m}(Chinese-Red)")]
  230. [TestCase("George Washington")]
  231. [TestCase("村上春樹", TestName="{m}(HarukiMurakami)")]
  232. public void FixedStringNToStringWorks(String a)
  233. {
  234. FixedStringN aa = new FixedStringN(a);
  235. Assert.AreEqual(a, aa.ToString());
  236. aa.AssertNullTerminated();
  237. }
  238. [TestCase("monkey", "monkey")]
  239. [TestCase("yellow","green")]
  240. [TestCase("violet","紅色", TestName="{m}(Violet-Chinese-Red")]
  241. [TestCase("绿色","蓝色", TestName="{m}(Chinese-Green-Blue")]
  242. [TestCase("靛蓝色","紫罗兰色", TestName="{m}(Chinese-Indigo-Violet")]
  243. [TestCase("James Monroe","John Quincy Adams")]
  244. [TestCase("Andrew Jackson","村上春樹", TestName="{m}(AndrewJackson-HarukiMurakami")]
  245. [TestCase("三島 由紀夫","吉本ばなな", TestName="{m}(MishimaYukio-YoshimotoBanana")]
  246. public void FixedStringNEqualsWorks(String a, String b)
  247. {
  248. FixedStringN aa = new FixedStringN(a);
  249. FixedStringN bb = new FixedStringN(b);
  250. Assert.AreEqual(aa.Equals(bb), a.Equals(b));
  251. aa.AssertNullTerminated();
  252. bb.AssertNullTerminated();
  253. }
  254. [Test]
  255. public void FixedStringNForEach()
  256. {
  257. FixedStringN actual = "A🌕Z🌑";
  258. FixedList32Bytes<int> expected = default;
  259. expected.Add('A');
  260. expected.Add(0x1F315);
  261. expected.Add('Z');
  262. expected.Add(0x1F311);
  263. int index = 0;
  264. foreach(var rune in actual)
  265. {
  266. Assert.AreEqual(expected[index], rune.value);
  267. ++index;
  268. }
  269. }
  270. [Test]
  271. public void FixedStringNIndexOf()
  272. {
  273. FixedStringN a = "bookkeeper bookkeeper";
  274. FixedStringN b = "ookkee";
  275. Assert.AreEqual(1, a.IndexOf(b));
  276. Assert.AreEqual(-1, b.IndexOf(a));
  277. }
  278. [Test]
  279. public void FixedStringNLastIndexOf()
  280. {
  281. FixedStringN a = "bookkeeper bookkeeper";
  282. FixedStringN b = "ookkee";
  283. Assert.AreEqual(12, a.LastIndexOf(b));
  284. Assert.AreEqual(-1, b.LastIndexOf(a));
  285. }
  286. [Test]
  287. public void FixedStringNContains()
  288. {
  289. FixedStringN a = "bookkeeper";
  290. FixedStringN b = "ookkee";
  291. Assert.AreEqual(true, a.Contains(b));
  292. }
  293. [Test]
  294. public void FixedStringNComparisons()
  295. {
  296. FixedStringN a = "apple";
  297. FixedStringN b = "banana";
  298. Assert.AreEqual(false, a.Equals(b));
  299. Assert.AreEqual(true, !b.Equals(a));
  300. }
  301. [Test]
  302. public void FixedStringNSizeOf()
  303. {
  304. Assert.AreEqual(UnsafeUtility.SizeOf<FixedStringN>(), 128);
  305. }
  306. [TestCase("red", new byte[]{3, 0, 114, 101, 100, 0}, TestName="{m}(red)")]
  307. [TestCase("紅色", new byte[]{6, 0, 231, 180, 133, 232, 137, 178, 0}, TestName="{m}(Chinese-Red)")]
  308. [TestCase("црвена", new byte[]{12, 0, 209, 134, 209, 128, 208, 178, 208, 181, 208, 189, 208, 176, 0}, TestName="{m}(Serbian-Red)")]
  309. [TestCase("George Washington", new byte[]{17, 0, 71, 101, 111, 114, 103, 101, 32, 87, 97, 115, 104, 105, 110, 103, 116, 111, 110, 0}, TestName="{m}(George Washington)")]
  310. [TestCase("村上春樹", new byte[]{12, 0, 230, 157, 145, 228, 184, 138, 230, 152, 165, 230, 168, 185, 0}, TestName="{m}(HarukiMurakami)")]
  311. [TestCase("🌕🌖🌗🌘🌑🌒🌓🌔", new byte[]{32, 0, 240, 159, 140, 149, 240, 159, 140, 150, 240, 159, 140, 151, 240, 159, 140, 152, 240, 159, 140, 145, 240, 159, 140, 146, 240, 159, 140, 147, 240, 159, 140, 148, 0}, TestName="{m}(MoonPhases)")]
  312. [TestCase("𝒞𝒯𝒮𝒟𝒳𝒩𝒫𝒢", new byte[]{32, 0, 240, 157, 146, 158, 240, 157, 146, 175, 240, 157, 146, 174, 240, 157, 146, 159, 240, 157, 146, 179, 240, 157, 146, 169, 240, 157, 146, 171, 240, 157, 146, 162, 0}, TestName="{m}(Cursive)")]
  313. [TestCase("로마는 하루아침에 이루어진 것이 아니다", new byte[]{55, 0, 235, 161, 156, 235, 167, 136, 235, 138, 148, 32, 237, 149, 152, 235, 163, 168, 236, 149, 132, 236, 185, 168, 236, 151, 144, 32, 236, 157, 180, 235, 163, 168, 236, 150, 180, 236, 167, 132, 32, 234, 178, 131, 236, 157, 180, 32, 236, 149, 132, 235, 139, 136, 235, 139, 164, 0}, TestName="{m}(Korean - Rome was not made overnight)")]
  314. [TestCase("Лако ти је плитку воду замутити и будалу наљутити", new byte[]{90, 0, 208, 155, 208, 176, 208, 186, 208, 190, 32, 209, 130, 208, 184, 32, 209, 152, 208, 181, 32, 208, 191, 208, 187, 208, 184, 209, 130, 208, 186, 209, 131, 32, 208, 178, 208, 190, 208, 180, 209, 131, 32, 208, 183, 208, 176, 208, 188, 209, 131, 209, 130, 208, 184, 209, 130, 208, 184, 32, 208, 184, 32, 208, 177, 209, 131, 208, 180, 208, 176, 208, 187, 209, 131, 32, 208, 189, 208, 176, 209, 153, 209, 131, 209, 130, 208, 184, 209, 130, 208, 184, 0}, TestName="{m}(Serbian-Proverb)")]
  315. [TestCase("Үнэн үг хэлсэн хүнд ноёд өстэй, үхэр унасан хүнд ноход өстэй.", new byte[]{110, 0, 210, 174, 208, 189, 209, 141, 208, 189, 32, 210, 175, 208, 179, 32, 209, 133, 209, 141, 208, 187, 209, 129, 209, 141, 208, 189, 32, 209, 133, 210, 175, 208, 189, 208, 180, 32, 208, 189, 208, 190, 209, 145, 208, 180, 32, 211, 169, 209, 129, 209, 130, 209, 141, 208, 185, 44, 32, 210, 175, 209, 133, 209, 141, 209, 128, 32, 209, 131, 208, 189, 208, 176, 209, 129, 208, 176, 208, 189, 32, 209, 133, 210, 175, 208, 189, 208, 180, 32, 208, 189, 208, 190, 209, 133, 208, 190, 208, 180, 32, 211, 169, 209, 129, 209, 130, 209, 141, 208, 185, 46, 0}, TestName="{m}(Mongolian-Proverb1)")]
  316. unsafe public void FixedStringNLayout(String a, byte[] expected)
  317. {
  318. fixed(byte* expectedBytes = expected)
  319. {
  320. FixedStringN actual = a;
  321. byte* actualBytes = (byte*)&actual;
  322. Assert.AreEqual(0, UnsafeUtility.MemCmp(expectedBytes, actualBytes, expected.Length));
  323. }
  324. }
  325. }
  326. }
  327. #endif