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