forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInt128Tests.cs
More file actions
472 lines (410 loc) · 22.8 KB
/
Int128Tests.cs
File metadata and controls
472 lines (410 loc) · 22.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Globalization;
using System.Numerics;
using Xunit;
namespace System.Tests
{
public class Int128Tests
{
[Fact]
public static void Ctor_Empty()
{
var i = new Int128();
Assert.Equal(0, i);
}
[Fact]
public static void Ctor_Value()
{
Int128 i = 41;
Assert.Equal(41, i);
}
[Fact]
public static void MaxValue()
{
Assert.Equal(new Int128(0x7FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF), Int128.MaxValue);
}
[Fact]
public static void MinValue()
{
Assert.Equal(new Int128(0x8000_0000_0000_0000, 0x0000_0000_0000_0000), Int128.MinValue);
}
public static IEnumerable<object[]> CompareTo_Other_ReturnsExpected_TestData()
{
yield return new object[] { (Int128)234, (Int128)234, 0 };
yield return new object[] { (Int128)234, Int128.MinValue, 1 };
yield return new object[] { (Int128)(-234), Int128.MinValue, 1 };
yield return new object[] { Int128.MinValue, Int128.MinValue, 0 };
yield return new object[] { (Int128)234, (Int128)(-123), 1 };
yield return new object[] { (Int128)234, (Int128)0, 1 };
yield return new object[] { (Int128)234, (Int128)123, 1 };
yield return new object[] { (Int128)234, (Int128)456, -1 };
yield return new object[] { (Int128)234, Int128.MaxValue, -1 };
yield return new object[] { (Int128)(-234), Int128.MaxValue, -1 };
yield return new object[] { Int128.MaxValue, Int128.MaxValue, 0 };
yield return new object[] { (Int128)(-234), (Int128)(-234), 0 };
yield return new object[] { (Int128)(-234), (Int128)234, -1 };
yield return new object[] { (Int128)(-234), (Int128)(-432), 1 };
yield return new object[] { (Int128)234, null, 1 };
}
[Theory]
[MemberData(nameof(CompareTo_Other_ReturnsExpected_TestData))]
public void CompareTo_Other_ReturnsExpected(Int128 i, object value, int expected)
{
if (value is Int128 int128Value)
{
Assert.Equal(expected, Int128.Sign(i.CompareTo(int128Value)));
Assert.Equal(-expected, Int128.Sign(int128Value.CompareTo(i)));
}
Assert.Equal(expected, Int128.Sign(i.CompareTo(value)));
}
public static IEnumerable<object[]> CompareTo_ObjectNotInt128_ThrowsArgumentException_TestData()
{
yield return new object[] { "a" };
yield return new object[] { 234 };
}
[Theory]
[MemberData(nameof(CompareTo_ObjectNotInt128_ThrowsArgumentException_TestData))]
public void CompareTo_ObjectNotInt128_ThrowsArgumentException(object value)
{
AssertExtensions.Throws<ArgumentException>(null, () => ((Int128)123).CompareTo(value));
}
public static IEnumerable<object[]> EqualsTest_TestData()
{
yield return new object[] { (Int128)789, (Int128)789, true };
yield return new object[] { (Int128)789, (Int128)(-789), false };
yield return new object[] { (Int128)789, (Int128)0, false };
yield return new object[] { (Int128)0, (Int128)0, true };
yield return new object[] { (Int128)(-789), (Int128)(-789), true };
yield return new object[] { (Int128)(-789), (Int128)789, false };
yield return new object[] { (Int128)789, null, false };
yield return new object[] { (Int128)789, "789", false };
yield return new object[] { (Int128)789, 789, false };
}
[Theory]
[MemberData(nameof(EqualsTest_TestData))]
public static void EqualsTest(Int128 i1, object obj, bool expected)
{
if (obj is Int128 i2)
{
Assert.Equal(expected, i1.Equals(i2));
Assert.Equal(expected, i1.GetHashCode().Equals(i2.GetHashCode()));
}
Assert.Equal(expected, i1.Equals(obj));
}
public static IEnumerable<object[]> ToString_TestData()
{
foreach (NumberFormatInfo defaultFormat in new[] { null, NumberFormatInfo.CurrentInfo })
{
foreach (string defaultSpecifier in new[] { "G", "G\0", "\0N222", "\0", "", "R" })
{
yield return new object[] { Int128.MinValue, defaultSpecifier, defaultFormat, "-170141183460469231731687303715884105728" };
yield return new object[] { (Int128)(-4567), defaultSpecifier, defaultFormat, "-4567" };
yield return new object[] { (Int128)0, defaultSpecifier, defaultFormat, "0" };
yield return new object[] { (Int128)4567, defaultSpecifier, defaultFormat, "4567" };
yield return new object[] { new Int128(0x0000_0000_0000_0001, 0x0000_0000_0000_0003), defaultSpecifier, defaultFormat, "18446744073709551619" };
yield return new object[] { new Int128(0x0000_0000_0000_0001, 0x0000_0000_0000_000A), defaultSpecifier, defaultFormat, "18446744073709551626" };
yield return new object[] { new Int128(0x0000_0000_0000_0005, 0x0000_0000_0000_0001), defaultSpecifier, defaultFormat, "92233720368547758081" };
yield return new object[] { new Int128(0x0000_0000_0000_0005, 0x6BC7_5E2D_6310_0000), defaultSpecifier, defaultFormat, "100000000000000000000" };
yield return new object[] { new Int128(0x0000_0000_0000_0036, 0x35C9_ADC5_DEA0_0000), defaultSpecifier, defaultFormat, "1000000000000000000000" };
yield return new object[] { new Int128(0x0013_4261_72C7_4D82, 0x2B87_8FE8_0000_0000), defaultSpecifier, defaultFormat, "100000000000000000000000000000000000" };
yield return new object[] { Int128.MaxValue, defaultSpecifier, defaultFormat, "170141183460469231731687303715884105727" };
}
yield return new object[] { (Int128)4567, "D", defaultFormat, "4567" };
yield return new object[] { (Int128)4567, "D99", defaultFormat, "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004567" };
yield return new object[] { (Int128)4567, "D99\09", defaultFormat, "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004567" };
yield return new object[] { (Int128)(-4567), "D99", defaultFormat, "-000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004567" };
yield return new object[] { (Int128)0x2468, "x", defaultFormat, "2468" };
yield return new object[] { (Int128)(-0x2468), "x", defaultFormat, "ffffffffffffffffffffffffffffdb98" };
yield return new object[] { (Int128)2468, "N", defaultFormat, string.Format("{0:N}", 2468.00) };
}
NumberFormatInfo invariantFormat = NumberFormatInfo.InvariantInfo;
yield return new object[] { (Int128)32, "C100", invariantFormat, "¤32.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" };
yield return new object[] { (Int128)32, "P100", invariantFormat, "3,200.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 %" };
yield return new object[] { (Int128)32, "D100", invariantFormat, "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000032" };
yield return new object[] { (Int128)32, "E100", invariantFormat, "3.2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000E+001" };
yield return new object[] { (Int128)32, "F100", invariantFormat, "32.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" };
yield return new object[] { (Int128)32, "N100", invariantFormat, "32.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" };
yield return new object[] { (Int128)32, "X100", invariantFormat, "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000020" };
var customFormat = new NumberFormatInfo()
{
NegativeSign = "#",
NumberDecimalSeparator = "~",
NumberGroupSeparator = "*",
PositiveSign = "&",
NumberDecimalDigits = 2,
PercentSymbol = "@",
PercentGroupSeparator = ",",
PercentDecimalSeparator = ".",
PercentDecimalDigits = 5
};
yield return new object[] { (Int128)(-2468), "N", customFormat, "#2*468~00" };
yield return new object[] { (Int128)2468, "N", customFormat, "2*468~00" };
yield return new object[] { (Int128)123, "E", customFormat, "1~230000E&002" };
yield return new object[] { (Int128)123, "F", customFormat, "123~00" };
yield return new object[] { (Int128)123, "P", customFormat, "12,300.00000 @" };
}
[Theory]
[MemberData(nameof(ToString_TestData))]
public static void ToStringTest(Int128 i, string format, IFormatProvider provider, string expected)
{
// Format is case insensitive
string upperFormat = format.ToUpperInvariant();
string lowerFormat = format.ToLowerInvariant();
string upperExpected = expected.ToUpperInvariant();
string lowerExpected = expected.ToLowerInvariant();
bool isDefaultProvider = (provider is null) || (provider == NumberFormatInfo.CurrentInfo);
if (string.IsNullOrEmpty(format) || (format.ToUpperInvariant() is "G" or "R"))
{
if (isDefaultProvider)
{
Assert.Equal(upperExpected, i.ToString());
Assert.Equal(upperExpected, i.ToString((IFormatProvider)null));
}
Assert.Equal(upperExpected, i.ToString(provider));
}
if (isDefaultProvider)
{
Assert.Equal(upperExpected, i.ToString(upperFormat));
Assert.Equal(lowerExpected, i.ToString(lowerFormat));
Assert.Equal(upperExpected, i.ToString(upperFormat, null));
Assert.Equal(lowerExpected, i.ToString(lowerFormat, null));
}
Assert.Equal(upperExpected, i.ToString(upperFormat, provider));
Assert.Equal(lowerExpected, i.ToString(lowerFormat, provider));
}
[Fact]
public static void ToString_InvalidFormat_ThrowsFormatException()
{
Int128 i = 123;
Assert.Throws<FormatException>(() => i.ToString("Y")); // Invalid format
Assert.Throws<FormatException>(() => i.ToString("Y", null)); // Invalid format
}
public static IEnumerable<object[]> Parse_Valid_TestData()
{
// Reuse all Int64 test data
foreach (object[] objs in Int64Tests.Parse_Valid_TestData())
{
bool unsigned = (((NumberStyles)objs[1]) & NumberStyles.HexNumber) == NumberStyles.HexNumber;
yield return new object[] { objs[0], objs[1], objs[2], unsigned ? (Int128)(ulong)(long)objs[3] : (Int128)(long)objs[3] };
}
// All lengths decimal
foreach (bool neg in new[] { false, true })
{
string s = neg ? "-" : "";
Int128 result = 0;
for (int i = 1; i <= 19; i++)
{
result = (result * 10) + (i % 10);
s += (i % 10).ToString();
yield return new object[] { s, NumberStyles.Integer, null, neg ? result * -1 : result };
}
}
// All lengths hexadecimal
{
string s = "";
Int128 result = 0;
for (int i = 1; i <= 16; i++)
{
result = (result * 16) + (i % 16);
s += (i % 16).ToString("X");
yield return new object[] { s, NumberStyles.HexNumber, null, result };
}
}
// And test boundary conditions for Int128
yield return new object[] { "-170141183460469231731687303715884105728", NumberStyles.Integer, null, Int128.MinValue };
yield return new object[] { "170141183460469231731687303715884105727", NumberStyles.Integer, null, Int128.MaxValue };
yield return new object[] { " -170141183460469231731687303715884105728 ", NumberStyles.Integer, null, Int128.MinValue };
yield return new object[] { " +170141183460469231731687303715884105727 ", NumberStyles.Integer, null, Int128.MaxValue };
yield return new object[] { "7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.HexNumber, null, Int128.MaxValue };
yield return new object[] { "80000000000000000000000000000000", NumberStyles.HexNumber, null, Int128.MinValue };
yield return new object[] { "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", NumberStyles.HexNumber, null, (Int128)(-1) };
yield return new object[] { " FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ", NumberStyles.HexNumber, null, (Int128)(-1) };
}
[Theory]
[MemberData(nameof(Parse_Valid_TestData))]
public static void Parse_Valid(string value, NumberStyles style, IFormatProvider provider, Int128 expected)
{
Int128 result;
// Default style and provider
if ((style == NumberStyles.Integer) && (provider is null))
{
Assert.True(Int128.TryParse(value, out result));
Assert.Equal(expected, result);
Assert.Equal(expected, Int128.Parse(value));
}
// Default provider
if (provider is null)
{
Assert.Equal(expected, Int128.Parse(value, style));
// Substitute default NumberFormatInfo
Assert.True(Int128.TryParse(value, style, new NumberFormatInfo(), out result));
Assert.Equal(expected, result);
Assert.Equal(expected, Int128.Parse(value, style, new NumberFormatInfo()));
}
// Default style
if (style == NumberStyles.Integer)
{
Assert.Equal(expected, Int128.Parse(value, provider));
}
// Full overloads
Assert.True(Int128.TryParse(value, style, provider, out result));
Assert.Equal(expected, result);
Assert.Equal(expected, Int128.Parse(value, style, provider));
}
public static IEnumerable<object[]> Parse_Invalid_TestData()
{
// Reuse all int test data, except for those that wouldn't overflow Int128.
foreach (object[] objs in Int32Tests.Parse_Invalid_TestData())
{
if ((Type)objs[3] == typeof(OverflowException) &&
(!BigInteger.TryParse((string)objs[0], out BigInteger bi) || (bi >= Int128.MinValue && bi <= Int128.MaxValue)))
{
continue;
}
yield return objs;
}
}
[Theory]
[MemberData(nameof(Parse_Invalid_TestData))]
public static void Parse_Invalid(string value, NumberStyles style, IFormatProvider provider, Type exceptionType)
{
Int128 result;
// Default style and provider
if ((style == NumberStyles.Integer) && (provider is null))
{
Assert.False(Int128.TryParse(value, out result));
Assert.Equal(default, result);
Assert.Throws(exceptionType, () => Int128.Parse(value));
}
// Default provider
if (provider is null)
{
Assert.Throws(exceptionType, () => Int128.Parse(value, style));
// Substitute default NumberFormatInfo
Assert.False(Int128.TryParse(value, style, new NumberFormatInfo(), out result));
Assert.Equal(default, result);
Assert.Throws(exceptionType, () => Int128.Parse(value, style, new NumberFormatInfo()));
}
// Default style
if (style == NumberStyles.Integer)
{
Assert.Throws(exceptionType, () => Int128.Parse(value, provider));
}
// Full overloads
Assert.False(Int128.TryParse(value, style, provider, out result));
Assert.Equal(default, result);
Assert.Throws(exceptionType, () => Int128.Parse(value, style, provider));
}
[Theory]
[InlineData(NumberStyles.HexNumber | NumberStyles.AllowParentheses, null)]
[InlineData(unchecked((NumberStyles)0xFFFFFC00), "style")]
public static void TryParse_InvalidNumberStyle_ThrowsArgumentException(NumberStyles style, string paramName)
{
Int128 result = 0;
AssertExtensions.Throws<ArgumentException>(paramName, () => Int128.TryParse("1", style, null, out result));
Assert.Equal(default(Int128), result);
AssertExtensions.Throws<ArgumentException>(paramName, () => Int128.Parse("1", style));
AssertExtensions.Throws<ArgumentException>(paramName, () => Int128.Parse("1", style, null));
}
public static IEnumerable<object[]> Parse_ValidWithOffsetCount_TestData()
{
foreach (object[] inputs in Parse_Valid_TestData())
{
yield return new object[] { inputs[0], 0, ((string)inputs[0]).Length, inputs[1], inputs[2], inputs[3] };
}
yield return new object[] { "-170141183460469231731687303715884105728", 0, 39, NumberStyles.Integer, null, new Int128(0xF333_3333_3333_3333, 0x3333_3333_3333_3334) };
yield return new object[] { "0170141183460469231731687303715884105727", 1, 39, NumberStyles.Integer, null, new Int128(0x7FFF_FFFF_FFFF_FFFF, 0xFFFF_FFFF_FFFF_FFFF) };
yield return new object[] { "170141183460469231731687303715884105727", 0, 1, NumberStyles.Integer, null, 1 };
yield return new object[] { "ABC", 0, 2, NumberStyles.HexNumber, null, (Int128)0xAB };
yield return new object[] { "(123)", 1, 3, NumberStyles.AllowParentheses, null, (Int128)123 };
yield return new object[] { "$1,000", 0, 2, NumberStyles.Currency, new NumberFormatInfo() { CurrencySymbol = "$" }, (Int128)1 };
}
[Theory]
[MemberData(nameof(Parse_ValidWithOffsetCount_TestData))]
public static void Parse_Span_Valid(string value, int offset, int count, NumberStyles style, IFormatProvider provider, Int128 expected)
{
Int128 result;
// Default style and provider
if ((style == NumberStyles.Integer) && (provider is null))
{
Assert.True(Int128.TryParse(value.AsSpan(offset, count), out result));
Assert.Equal(expected, result);
}
Assert.Equal(expected, Int128.Parse(value.AsSpan(offset, count), style, provider));
Assert.True(Int128.TryParse(value.AsSpan(offset, count), style, provider, out result));
Assert.Equal(expected, result);
}
[Theory]
[MemberData(nameof(Parse_Invalid_TestData))]
public static void Parse_Span_Invalid(string value, NumberStyles style, IFormatProvider provider, Type exceptionType)
{
if (value is not null)
{
Int128 result;
// Default style and provider
if ((style == NumberStyles.Integer) && (provider is null))
{
Assert.False(Int128.TryParse(value.AsSpan(), out result));
Assert.Equal(0, result);
}
Assert.Throws(exceptionType, () => Int128.Parse(value.AsSpan(), style, provider));
Assert.False(Int128.TryParse(value.AsSpan(), style, provider, out result));
Assert.Equal(0, result);
}
}
[Theory]
[MemberData(nameof(ToString_TestData))]
public static void TryFormat(Int128 i, string format, IFormatProvider provider, string expected)
{
char[] actual;
int charsWritten;
// Just right
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected, new string(actual));
// Longer than needed
actual = new char[expected.Length + 1];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected, new string(actual, 0, charsWritten));
// Too short
if (expected.Length > 0)
{
actual = new char[expected.Length - 1];
Assert.False(i.TryFormat(actual.AsSpan(), out charsWritten, format, provider));
Assert.Equal(0, charsWritten);
}
if (format is not null)
{
// Upper format
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToUpperInvariant(), provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected.ToUpperInvariant(), new string(actual));
// Lower format
actual = new char[expected.Length];
Assert.True(i.TryFormat(actual.AsSpan(), out charsWritten, format.ToLowerInvariant(), provider));
Assert.Equal(expected.Length, charsWritten);
Assert.Equal(expected.ToLowerInvariant(), new string(actual));
}
}
[Fact]
public static void TestNegativeNumberParsingWithHyphen()
{
// CLDR data for Swedish culture has negative sign U+2212. This test ensure parsing with the hyphen with such cultures will succeed.
CultureInfo ci = CultureInfo.GetCultureInfo("sv-SE");
Assert.Equal(-15868, Int128.Parse("-15868", NumberStyles.Number, ci));
}
[Fact]
public static void Runtime75416()
{
Int128 a = (Int128.MaxValue - 10) * +100;
Assert.Equal(a, -1100);
Int128 b = (Int128.MaxValue - 10) * -100;
Assert.Equal(b, +1100);
}
}
}