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

FixedStringCultureTests.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #if !UNITY_DOTSRUNTIME
  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. // change this to change the core type under test
  10. using FixedStringN = Unity.Collections.FixedString128Bytes;
  11. namespace FixedStringTests
  12. {
  13. [TestFixture("en-US")]
  14. [TestFixture("da-DK")]
  15. internal class FixedStringCultureTests
  16. {
  17. CultureInfo testCulture;
  18. CultureInfo backupCulture;
  19. public FixedStringCultureTests(string culture)
  20. {
  21. testCulture = CultureInfo.CreateSpecificCulture(culture);
  22. }
  23. [SetUp]
  24. public virtual void Setup()
  25. {
  26. backupCulture = Thread.CurrentThread.CurrentCulture;
  27. Thread.CurrentThread.CurrentCulture = testCulture;
  28. }
  29. [TearDown]
  30. public virtual void TearDown()
  31. {
  32. Thread.CurrentThread.CurrentCulture = backupCulture;
  33. }
  34. [TestCase("red", 0, 0, ParseError.Syntax)]
  35. [TestCase("-red", 0, 0, ParseError.Syntax)]
  36. [TestCase("+red", 0, 0, ParseError.Syntax)]
  37. [TestCase("0", 1, 0, ParseError.None)]
  38. [TestCase("+0", 2, 0, ParseError.None)]
  39. [TestCase("-0", 2, 0, ParseError.None)]
  40. [TestCase("-1", 2, -1, ParseError.None)]
  41. [TestCase("100", 3, 100, ParseError.None)]
  42. [TestCase("+100", 4, 100, ParseError.None)]
  43. [TestCase("-100", 4, -100, ParseError.None)]
  44. [TestCase("100.50", 3, 100, ParseError.None)]
  45. [TestCase("-100ab", 4, -100, ParseError.None)]
  46. [TestCase("2147483647", 10, 2147483647, ParseError.None)]
  47. [TestCase("+2147483647", 11, 2147483647, ParseError.None)]
  48. [TestCase("-2147483648", 11, -2147483648, ParseError.None)]
  49. [TestCase("2147483648", 10, 0, ParseError.Overflow)]
  50. [TestCase("-2147483649", 11, 0, ParseError.Overflow)]
  51. [TestCase("2147483648000", 13, 0, ParseError.Overflow)]
  52. [TestCase("-2147483649000", 14, 0, ParseError.Overflow)]
  53. public void FixedStringNParseIntWorks(String a, int expectedOffset, int expectedOutput, ParseError expectedResult)
  54. {
  55. FixedStringN aa = new FixedStringN(a);
  56. int offset = 0;
  57. int output = 0;
  58. var result = aa.Parse(ref offset, ref output);
  59. Assert.AreEqual(expectedResult, result);
  60. Assert.AreEqual(expectedOffset, offset);
  61. if (result == ParseError.None)
  62. {
  63. Assert.AreEqual(expectedOutput, output);
  64. }
  65. }
  66. [TestCase("red", 0, 0u, ParseError.Syntax)]
  67. [TestCase("-red", 0, 0u, ParseError.Syntax)]
  68. [TestCase("+red", 0, 0u, ParseError.Syntax)]
  69. [TestCase("0", 1, 0u, ParseError.None)]
  70. [TestCase("+0", 2, 0u, ParseError.None)]
  71. [TestCase("-0", 2, 0u, ParseError.None)]
  72. [TestCase("-1", 2, 0u, ParseError.Overflow)]
  73. [TestCase("100", 3, 100u, ParseError.None)]
  74. [TestCase("+100", 4, 100u, ParseError.None)]
  75. [TestCase("-100", 4, 0u, ParseError.Overflow)]
  76. [TestCase("100.50", 3, 100u, ParseError.None)]
  77. [TestCase("-100.50", 4, 0u, ParseError.Overflow)]
  78. [TestCase("100ab", 3, 100u, ParseError.None)]
  79. [TestCase("-100ab", 4, 0u, ParseError.Overflow)]
  80. [TestCase("2147483647", 10, 2147483647u, ParseError.None)]
  81. [TestCase("-2147483648", 11, 0u, ParseError.Overflow)]
  82. [TestCase("2147483648", 10, 2147483648u, ParseError.None)]
  83. [TestCase("-2147483649", 11, 0u, ParseError.Overflow)]
  84. [TestCase("4294967295", 10, 4294967295u, ParseError.None)]
  85. [TestCase("+4294967295", 11, 4294967295u, ParseError.None)]
  86. [TestCase("4294967296", 10, 0u, ParseError.Overflow)]
  87. [TestCase("2147483648000", 13, 0u, ParseError.Overflow)]
  88. [TestCase("-2147483649000", 14, 0u, ParseError.Overflow)]
  89. public void FixedStringNParseUIntWorks(String a, int expectedOffset, uint expectedOutput, ParseError expectedResult)
  90. {
  91. FixedStringN aa = new FixedStringN(a);
  92. int offset = 0;
  93. uint output = 0;
  94. var result = aa.Parse(ref offset, ref output);
  95. Assert.AreEqual(expectedResult, result);
  96. Assert.AreEqual(expectedOffset, offset);
  97. if (result == ParseError.None)
  98. {
  99. Assert.AreEqual(expectedOutput, output);
  100. }
  101. }
  102. [TestCase("red", 0, ParseError.Syntax)]
  103. [TestCase("-red", 0, ParseError.Syntax)]
  104. [TestCase("+red", 0, ParseError.Syntax)]
  105. [TestCase("0", 1, ParseError.None)]
  106. [TestCase("-1", 2, ParseError.None)]
  107. [TestCase("-0", 2, ParseError.None)]
  108. [TestCase("100", 3, ParseError.None)]
  109. [TestCase("+100", 4, ParseError.None)]
  110. [TestCase("-100", 4, ParseError.None)]
  111. [TestCase("100.50", 6, ParseError.None)]
  112. [TestCase("2147483648", 10, ParseError.None)]
  113. [TestCase("-2147483649", 11, ParseError.None)]
  114. [TestCase("-10E10", 6, ParseError.None)]
  115. [TestCase("-10E-10", 7, ParseError.None)]
  116. [TestCase("-10E+10", 7, ParseError.None)]
  117. [TestCase("+10E10", 6, ParseError.None)]
  118. [TestCase("+10E-10", 7, ParseError.None)]
  119. [TestCase("+10E+10", 7, ParseError.None)]
  120. [TestCase("10E-40", 6, ParseError.Underflow)]
  121. [TestCase("10E+40", 6, ParseError.Overflow)]
  122. // These tests are highly inconsistent among .NET versions. All 6 cases below parse correctly in
  123. // .NET 5 however, so once we have updated they should be tried again.
  124. //[TestCase("-nan", 4, ParseError.None)]
  125. //[TestCase("+nan", 4, ParseError.None)]
  126. //[TestCase("nan", 3, ParseError.None)]
  127. //[TestCase("-infinity", 9, ParseError.None)]
  128. //[TestCase("+infinity", 9, ParseError.None)]
  129. //[TestCase("infinity", 8, ParseError.None)]
  130. [TestCase("1000001", 7, ParseError.None)]
  131. [TestCase("10000001", 8, ParseError.None)]
  132. [TestCase("100000001", 9, ParseError.None)]
  133. [TestCase("1000000001", 10, ParseError.None)]
  134. [TestCase("10000000001", 11, ParseError.None)]
  135. [TestCase("100000000001", 12, ParseError.None)]
  136. public void FixedStringNParseFloat(String unlocalizedString, int expectedOffset, ParseError expectedResult)
  137. {
  138. var localizedDecimalSeparator = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  139. var localizedString = unlocalizedString.Replace('.', localizedDecimalSeparator);
  140. float expectedOutput = 0;
  141. try { expectedOutput = Single.Parse(localizedString); } catch {}
  142. FixedStringN nativeLocalizedString = new FixedStringN(localizedString);
  143. int offset = 0;
  144. float output = 0;
  145. var result = nativeLocalizedString.Parse(ref offset, ref output, localizedDecimalSeparator);
  146. Assert.AreEqual(expectedResult, result);
  147. Assert.AreEqual(expectedOffset, offset);
  148. if (result == ParseError.None)
  149. {
  150. Assert.AreEqual(expectedOutput, output);
  151. }
  152. }
  153. // While .NET 5 supports all combinations and capitalizations of "nan", "-nan", and "+nan",
  154. // the implementation in Unity currently only supports "NaN" specifically
  155. [Test]
  156. public void FixedStringNParseFloatNan()
  157. {
  158. FixedStringN aa = new FixedStringN("NaN");
  159. int offset = 0;
  160. float output = 0;
  161. var result = aa.Parse(ref offset, ref output);
  162. Assert.AreEqual(ParseError.None, result);
  163. Assert.IsTrue(Single.IsNaN(output));
  164. }
  165. // While .NET 5 supports all combinations and capitalizations of "infinity", "-infinity", and "+infinity",
  166. // the mono implementation in Unity currently only supports "Infinity" and "-Infinity" specifically
  167. // (not even "+Infinity")
  168. [Test]
  169. public void FixedStringNParseFloatInfinity()
  170. {
  171. FixedStringN aa = new FixedStringN("Infinity");
  172. int offset = 0;
  173. float output = 0;
  174. var result = aa.Parse(ref offset, ref output);
  175. Assert.AreEqual(ParseError.None, result);
  176. Assert.IsTrue(Single.IsPositiveInfinity(output));
  177. }
  178. [Test]
  179. public void FixedStringNParseFloatNegativeInfinity()
  180. {
  181. FixedStringN aa = new FixedStringN("-Infinity");
  182. int offset = 0;
  183. float output = 0;
  184. var result = aa.Parse(ref offset, ref output);
  185. Assert.AreEqual(ParseError.None, result);
  186. Assert.IsTrue(Single.IsNegativeInfinity(output));
  187. }
  188. [TestCase(-2147483648)]
  189. [TestCase(-100)]
  190. [TestCase(-1)]
  191. [TestCase(0)]
  192. [TestCase(1)]
  193. [TestCase(100)]
  194. [TestCase(2147483647)]
  195. public void FixedStringNFormatInt(int input)
  196. {
  197. var expectedOutput = input.ToString();
  198. FixedStringN aa = new FixedStringN();
  199. var result = aa.Append(input);
  200. Assert.AreEqual(FormatError.None, result);
  201. var actualOutput = aa.ToString();
  202. Assert.AreEqual(expectedOutput, actualOutput);
  203. }
  204. [TestCase(-9223372036854775808L)]
  205. [TestCase(-100L)]
  206. [TestCase(-1L)]
  207. [TestCase(0L)]
  208. [TestCase(1L)]
  209. [TestCase(100L)]
  210. [TestCase(9223372036854775807L)]
  211. public void FixedStringNFormatLong(long input)
  212. {
  213. var expectedOutput = input.ToString();
  214. FixedStringN aa = new FixedStringN();
  215. var result = aa.Append(input);
  216. Assert.AreEqual(FormatError.None, result);
  217. var actualOutput = aa.ToString();
  218. Assert.AreEqual(expectedOutput, actualOutput);
  219. }
  220. [TestCase(0U)]
  221. [TestCase(1U)]
  222. [TestCase(100U)]
  223. [TestCase(4294967295U)]
  224. public void FixedStringNFormatUInt(uint input)
  225. {
  226. var expectedOutput = input.ToString();
  227. FixedStringN aa = new FixedStringN();
  228. var result = aa.Append(input);
  229. Assert.AreEqual(FormatError.None, result);
  230. var actualOutput = aa.ToString();
  231. Assert.AreEqual(expectedOutput, actualOutput);
  232. }
  233. [TestCase(0UL)]
  234. [TestCase(1UL)]
  235. [TestCase(100UL)]
  236. [TestCase(18446744073709551615UL)]
  237. public void FixedStringNFormatULong(ulong input)
  238. {
  239. var expectedOutput = input.ToString();
  240. FixedStringN aa = new FixedStringN();
  241. var result = aa.Append(input);
  242. Assert.AreEqual(FormatError.None, result);
  243. var actualOutput = aa.ToString();
  244. Assert.AreEqual(expectedOutput, actualOutput);
  245. }
  246. [TestCase(Single.NaN, FormatError.None)]
  247. [TestCase(Single.PositiveInfinity, FormatError.None)]
  248. [TestCase(Single.NegativeInfinity, FormatError.None)]
  249. [TestCase(0.0f, FormatError.None)]
  250. [TestCase(-1.0f, FormatError.None)]
  251. [TestCase(100.0f, FormatError.None)]
  252. [TestCase(-100.0f, FormatError.None)]
  253. [TestCase(100.5f, FormatError.None)]
  254. [TestCase(0.001005f, FormatError.None)]
  255. [TestCase(0.0001f, FormatError.None)]
  256. [TestCase(0.00001f, FormatError.None)]
  257. [TestCase(0.000001f, FormatError.None)]
  258. [TestCase(-1E10f, FormatError.None)]
  259. [TestCase(-1E-10f, FormatError.None)]
  260. [TestCase(3.402823E+38f, FormatError.None)]
  261. public void FixedStringNFormatFloat(float input, FormatError expectedResult)
  262. {
  263. var localizedDecimalSeparator = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  264. var expectedOutput = input.ToString();
  265. FixedStringN aa = new FixedStringN();
  266. var result = aa.Append(input, localizedDecimalSeparator);
  267. Assert.AreEqual(expectedResult, result);
  268. if (result == FormatError.None)
  269. {
  270. var actualOutput = aa.ToString();
  271. Assert.AreEqual(expectedOutput, actualOutput);
  272. }
  273. }
  274. [TestCase(-2147483648)]
  275. [TestCase(-100)]
  276. [TestCase(-1)]
  277. [TestCase(0)]
  278. [TestCase(1)]
  279. [TestCase(100)]
  280. [TestCase(2147483647)]
  281. public void FixedStringNAppendInt(int input)
  282. {
  283. var expectedOutput = "foo" + input.ToString();
  284. FixedStringN aa = "foo";
  285. var result = aa.Append(input);
  286. Assert.AreEqual(FormatError.None, result);
  287. var actualOutput = aa.ToString();
  288. Assert.AreEqual(expectedOutput, actualOutput);
  289. }
  290. [TestCase(-9223372036854775808L)]
  291. [TestCase(-100L)]
  292. [TestCase(-1L)]
  293. [TestCase(0L)]
  294. [TestCase(1L)]
  295. [TestCase(100L)]
  296. [TestCase(9223372036854775807L)]
  297. public void FixedStringNAppendLong(long input)
  298. {
  299. var expectedOutput = "foo" + input.ToString();
  300. FixedStringN aa = "foo";
  301. var result = aa.Append(input);
  302. Assert.AreEqual(FormatError.None, result);
  303. var actualOutput = aa.ToString();
  304. Assert.AreEqual(expectedOutput, actualOutput);
  305. }
  306. [TestCase(0U)]
  307. [TestCase(1U)]
  308. [TestCase(100U)]
  309. [TestCase(4294967295U)]
  310. public void FixedStringNAppendUInt(uint input)
  311. {
  312. var expectedOutput = "foo" + input.ToString();
  313. FixedStringN aa = "foo";
  314. var result = aa.Append(input);
  315. Assert.AreEqual(FormatError.None, result);
  316. var actualOutput = aa.ToString();
  317. Assert.AreEqual(expectedOutput, actualOutput);
  318. }
  319. [TestCase(0UL)]
  320. [TestCase(1UL)]
  321. [TestCase(100UL)]
  322. [TestCase(18446744073709551615UL)]
  323. public void FixedStringNAppendULong(ulong input)
  324. {
  325. var expectedOutput = "foo" + input.ToString();
  326. FixedStringN aa = "foo";
  327. var result = aa.Append(input);
  328. Assert.AreEqual(FormatError.None, result);
  329. var actualOutput = aa.ToString();
  330. Assert.AreEqual(expectedOutput, actualOutput);
  331. }
  332. [TestCase(Single.NaN, FormatError.None)]
  333. [TestCase(Single.PositiveInfinity, FormatError.None)]
  334. [TestCase(Single.NegativeInfinity, FormatError.None)]
  335. [TestCase(0.0f, FormatError.None)]
  336. [TestCase(-1.0f, FormatError.None)]
  337. [TestCase(100.0f, FormatError.None)]
  338. [TestCase(-100.0f, FormatError.None)]
  339. [TestCase(100.5f, FormatError.None)]
  340. [TestCase(0.001005f, FormatError.None)]
  341. [TestCase(0.0001f, FormatError.None)]
  342. [TestCase(0.00001f, FormatError.None)]
  343. [TestCase(0.000001f, FormatError.None)]
  344. [TestCase(-1E10f, FormatError.None)]
  345. [TestCase(-1E-10f, FormatError.None)]
  346. public void FixedStringNAppendFloat(float input, FormatError expectedResult)
  347. {
  348. var localizedDecimalSeparator = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  349. var expectedOutput = "foo" + input.ToString();
  350. FixedStringN aa = "foo";
  351. var result = aa.Append(input, localizedDecimalSeparator);
  352. Assert.AreEqual(expectedResult, result);
  353. if (result == FormatError.None)
  354. {
  355. var actualOutput = aa.ToString();
  356. Assert.AreEqual(expectedOutput, actualOutput);
  357. }
  358. }
  359. [Test]
  360. public void FixedStringNFormatNegativeZero()
  361. {
  362. float input = -0.0f;
  363. var expectedOutput = input.ToString(CultureInfo.InvariantCulture);
  364. FixedStringN aa = new FixedStringN();
  365. var result = aa.Append(input);
  366. Assert.AreEqual(FormatError.None, result);
  367. var actualOutput = aa.ToString();
  368. Assert.AreEqual(expectedOutput, actualOutput);
  369. }
  370. [TestCase("en-US")]
  371. [TestCase("da-DK")]
  372. public void FixedStringNParseFloatLocale(String locale)
  373. {
  374. var original = CultureInfo.CurrentCulture;
  375. try
  376. {
  377. Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
  378. var localizedDecimalSeparator = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);
  379. float value = 1.5f;
  380. FixedStringN native = new FixedStringN();
  381. native.Append(value, localizedDecimalSeparator);
  382. var nativeResult = native.ToString();
  383. var managedResult = value.ToString();
  384. Assert.AreEqual(managedResult, nativeResult);
  385. }
  386. finally
  387. {
  388. Thread.CurrentThread.CurrentCulture = original;
  389. }
  390. }
  391. }
  392. }
  393. #endif