Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit aed0765

Browse files
committed
Add tests for System.Runtime based on mscorlib code coverage data
System.Array - add 'BinarySearch' overload tests System.DateTime - add 'GetDateTimeFormats' tests System.Decimal - add 'GetBits' tests System.GC - add 'ReRegisterForFinalize', 'GetTotalMemory' and 'GetGeneration' tests System.Runtime.CompilerServices.ConditionalWeakTable - add new tests System.TimeSpan - add 'ParseExact' tests System.Type - add 'GetType' by name tests System.Uri - fix the C# project to include Uri tests.
1 parent 139df71 commit aed0765

File tree

10 files changed

+621
-29
lines changed

10 files changed

+621
-29
lines changed

src/System.Runtime/tests/System.Runtime.Tests.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
<Compile Include="System\MulticastDelegate.cs" />
6868
<Compile Include="System\Nullable.cs" />
6969
<Compile Include="System\Object.cs" />
70+
<Compile Include="System\Runtime\CompilerServices\ConditionalWeakTable.cs" />
7071
<Compile Include="System\Runtime\CompilerServices\StrongBox.cs" />
7172
<Compile Include="System\Runtime\CompilerServices\RuntimeHelpers.cs" />
7273
<Compile Include="System\SByte.cs" />
@@ -83,6 +84,7 @@
8384
<Compile Include="System\UInt32.cs" />
8485
<Compile Include="System\UInt64.cs" />
8586
<Compile Include="System\UIntPtr.cs" />
87+
<Compile Include="System\Uri.cs" />
8688
<Compile Include="System\ValueType.cs" />
8789
<Compile Include="System\Version.cs" />
8890
<Compile Include="System\WeakReference.cs" />

src/System.Runtime/tests/System/Array.cs

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ public static void TestTrivials()
9494
Assert.Equal(a.Rank, 1);
9595
IList il = (IList)a;
9696
Assert.Equal(il.Count, 3);
97+
Assert.Equal(il.SyncRoot, a);
9798
Assert.False(il.IsSynchronized);
9899
Assert.True(il.IsFixedSize);
99100
Assert.False(il.IsReadOnly);
@@ -252,10 +253,10 @@ public static void TestBinarySearch()
252253
idx = Array.BinarySearch((Array)ia, 6, comparer);
253254
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
254255

255-
idx = Array.BinarySearch<int>(ia, 0, 8, 6, icomparer);
256+
idx = Array.BinarySearch((Array)ia, 0, 8, 6, comparer);
256257
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
257258

258-
idx = Array.BinarySearch((Array)ia, 6, comparer);
259+
idx = Array.BinarySearch<int>(ia, 6, icomparer);
259260
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
260261

261262
idx = Array.BinarySearch<int>(ia, 0, 8, 6, icomparer);
@@ -296,6 +297,73 @@ public static void TestBinarySearch()
296297
Assert.Equal(idx, 0);
297298
}
298299

300+
[Fact]
301+
public static void TestBinarySearch_NoIComparer()
302+
{
303+
int idx;
304+
int[] ia = { 1, 3, 6, 6, 8, 10, 12, 16 };
305+
idx = Array.BinarySearch((Array)ia, 8);
306+
Assert.Equal(idx, 4);
307+
308+
//Return value semantics: Quoted from MSDN without comment.
309+
//
310+
//The index of the specified value in the specified array,
311+
//if value is found. If value is not found and value is
312+
//less than one or more elements in array, a negative
313+
//number which is the bitwise complement of the index of
314+
//the first element that is larger than value.
315+
//If value is not found and value is greater than any
316+
//of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
317+
idx = Array.BinarySearch((Array)ia, 99);
318+
Assert.Equal(idx, ~(ia.Length));
319+
320+
idx = Array.BinarySearch<int>(ia, 0, 8, 99);
321+
Assert.Equal(idx, ~(ia.Length));
322+
323+
idx = Array.BinarySearch((Array)ia, 6);
324+
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
325+
326+
idx = Array.BinarySearch((Array)ia, 0, 8, 6);
327+
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
328+
329+
idx = Array.BinarySearch<int>(ia, 6);
330+
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
331+
332+
idx = Array.BinarySearch<int>(ia, 0, 8, 6);
333+
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
334+
335+
String[] sa = { null, "aa", "bb", "bb", "cc", "dd", "ee" };
336+
idx = Array.BinarySearch((Array)sa, "bb");
337+
Assert.Equal(idx, 3);
338+
339+
idx = Array.BinarySearch<String>(sa, 0, sa.Length, "bb");
340+
Assert.Equal(idx, 3);
341+
342+
idx = Array.BinarySearch((Array)sa, 3, 4, "bb");
343+
Assert.Equal(idx, 3);
344+
345+
idx = Array.BinarySearch<String>(sa, 3, 4, "bb");
346+
Assert.Equal(idx, 3);
347+
348+
idx = Array.BinarySearch((Array)sa, 4, 3, "bb");
349+
Assert.Equal(idx, -5);
350+
351+
idx = Array.BinarySearch<String>(sa, 4, 3, "bb");
352+
Assert.Equal(idx, -5);
353+
354+
idx = Array.BinarySearch((Array)sa, 4, 0, "bb");
355+
Assert.Equal(idx, -5);
356+
357+
idx = Array.BinarySearch<String>(sa, 4, 0, "bb");
358+
Assert.Equal(idx, -5);
359+
360+
idx = Array.BinarySearch((Array)sa, 0, 7, null);
361+
Assert.Equal(idx, 0);
362+
363+
idx = Array.BinarySearch<String>(sa, 0, 7, null);
364+
Assert.Equal(idx, 0);
365+
}
366+
299367
[Fact]
300368
public static void TestGetAndSetValue()
301369
{

src/System.Runtime/tests/System/Buffer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ public static void TestByteLength()
7979

8080
i = Buffer.ByteLength(new double[33]);
8181
Assert.Equal(i, 33 * sizeof(double));
82+
83+
Assert.Throws<ArgumentException>(() => Buffer.ByteLength(new DateTime[10]));
8284
}
8385

8486
[Fact]

src/System.Runtime/tests/System/DateTime.cs

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
7+
using System.Linq;
8+
using System.Threading.Tasks;
69
using Xunit;
710

811
public static unsafe class DateTimeTests
@@ -291,6 +294,68 @@ public static void TestTryParseExactA()
291294
Assert.Equal(s, actual);
292295
}
293296

297+
[Fact]
298+
[ActiveIssue(846, PlatformID.AnyUnix)]
299+
public static void TestGetDateTimeFormats()
300+
{
301+
// Running in the new thread so that we do not interfere with the Main thread's culture
302+
Task.Run(() =>
303+
{
304+
CultureInfo.CurrentCulture = new CultureInfo("en-US");
305+
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
306+
307+
List<string> actualJuly28Formats = july28.GetDateTimeFormats().ToList();
308+
309+
Assert.Equal(expectedJuly28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
310+
}).Wait();
311+
}
312+
313+
[Fact]
314+
[ActiveIssue(846, PlatformID.AnyUnix)]
315+
public static void TestGetDateTimeFormats_FormatSpecifier()
316+
{
317+
// Running in the new thread so that we do not interfere with the Main thread's culture
318+
Task.Run(() =>
319+
{
320+
char[] allStandardFormats =
321+
{
322+
'd', 'D', 'f', 'F', 'g', 'G',
323+
'm', 'M', 'o', 'O', 'r', 'R',
324+
's', 't', 'T', 'u', 'U', 'y', 'Y',
325+
};
326+
327+
CultureInfo.CurrentCulture = new CultureInfo("en-US");
328+
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
329+
330+
List<string> actualJuly28Formats = new List<string>();
331+
foreach (char format in allStandardFormats)
332+
{
333+
actualJuly28Formats.AddRange(july28.GetDateTimeFormats(format));
334+
}
335+
336+
Assert.Equal(expectedJuly28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
337+
}).Wait();
338+
}
339+
340+
[Fact]
341+
public static void TestGetDateTimeFormats_FormatSpecifier_InvalidFormat()
342+
{
343+
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
344+
345+
Assert.Throws<FormatException>(() => july28.GetDateTimeFormats('x'));
346+
}
347+
348+
[Fact]
349+
[ActiveIssue(846, PlatformID.AnyUnix)]
350+
public static void TestGetDateTimeFormats_FormatProvider()
351+
{
352+
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
353+
354+
List<string> actualJuly28Formats = july28.GetDateTimeFormats(new CultureInfo("en-US")).ToList();
355+
356+
Assert.Equal(expectedJuly28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
357+
}
358+
294359
internal static void ValidateYearMonthDay(DateTime dt, int year, int month, int day)
295360
{
296361
Assert.Equal(dt.Year, year);
@@ -311,5 +376,143 @@ internal static void ValidateYearMonthDay(DateTime dt, int year, int month, int
311376
ValidateYearMonthDay(dt, year, month, day, hour, minute, second);
312377
Assert.Equal(dt.Millisecond, millisecond);
313378
}
379+
380+
381+
private static List<string> expectedJuly28Formats = new List<string>
382+
{
383+
"7/28/2009",
384+
"7/28/09",
385+
"07/28/09",
386+
"07/28/2009",
387+
"09/07/28",
388+
"2009-07-28",
389+
"28-Jul-09",
390+
"Tuesday, July 28, 2009",
391+
"July 28, 2009",
392+
"Tuesday, 28 July, 2009",
393+
"28 July, 2009",
394+
"Tuesday, July 28, 2009 5:23 AM",
395+
"Tuesday, July 28, 2009 05:23 AM",
396+
"Tuesday, July 28, 2009 5:23",
397+
"Tuesday, July 28, 2009 05:23",
398+
"July 28, 2009 5:23 AM",
399+
"July 28, 2009 05:23 AM",
400+
"July 28, 2009 5:23",
401+
"July 28, 2009 05:23",
402+
"Tuesday, 28 July, 2009 5:23 AM",
403+
"Tuesday, 28 July, 2009 05:23 AM",
404+
"Tuesday, 28 July, 2009 5:23",
405+
"Tuesday, 28 July, 2009 05:23",
406+
"28 July, 2009 5:23 AM",
407+
"28 July, 2009 05:23 AM",
408+
"28 July, 2009 5:23",
409+
"28 July, 2009 05:23",
410+
"Tuesday, July 28, 2009 5:23:15 AM",
411+
"Tuesday, July 28, 2009 05:23:15 AM",
412+
"Tuesday, July 28, 2009 5:23:15",
413+
"Tuesday, July 28, 2009 05:23:15",
414+
"July 28, 2009 5:23:15 AM",
415+
"July 28, 2009 05:23:15 AM",
416+
"July 28, 2009 5:23:15",
417+
"July 28, 2009 05:23:15",
418+
"Tuesday, 28 July, 2009 5:23:15 AM",
419+
"Tuesday, 28 July, 2009 05:23:15 AM",
420+
"Tuesday, 28 July, 2009 5:23:15",
421+
"Tuesday, 28 July, 2009 05:23:15",
422+
"28 July, 2009 5:23:15 AM",
423+
"28 July, 2009 05:23:15 AM",
424+
"28 July, 2009 5:23:15",
425+
"28 July, 2009 05:23:15",
426+
"7/28/2009 5:23 AM",
427+
"7/28/2009 05:23 AM",
428+
"7/28/2009 5:23",
429+
"7/28/2009 05:23",
430+
"7/28/09 5:23 AM",
431+
"7/28/09 05:23 AM",
432+
"7/28/09 5:23",
433+
"7/28/09 05:23",
434+
"07/28/09 5:23 AM",
435+
"07/28/09 05:23 AM",
436+
"07/28/09 5:23",
437+
"07/28/09 05:23",
438+
"07/28/2009 5:23 AM",
439+
"07/28/2009 05:23 AM",
440+
"07/28/2009 5:23",
441+
"07/28/2009 05:23",
442+
"09/07/28 5:23 AM",
443+
"09/07/28 05:23 AM",
444+
"09/07/28 5:23",
445+
"09/07/28 05:23",
446+
"2009-07-28 5:23 AM",
447+
"2009-07-28 05:23 AM",
448+
"2009-07-28 5:23",
449+
"2009-07-28 05:23",
450+
"28-Jul-09 5:23 AM",
451+
"28-Jul-09 05:23 AM",
452+
"28-Jul-09 5:23",
453+
"28-Jul-09 05:23",
454+
"7/28/2009 5:23:15 AM",
455+
"7/28/2009 05:23:15 AM",
456+
"7/28/2009 5:23:15",
457+
"7/28/2009 05:23:15",
458+
"7/28/09 5:23:15 AM",
459+
"7/28/09 05:23:15 AM",
460+
"7/28/09 5:23:15",
461+
"7/28/09 05:23:15",
462+
"07/28/09 5:23:15 AM",
463+
"07/28/09 05:23:15 AM",
464+
"07/28/09 5:23:15",
465+
"07/28/09 05:23:15",
466+
"07/28/2009 5:23:15 AM",
467+
"07/28/2009 05:23:15 AM",
468+
"07/28/2009 5:23:15",
469+
"07/28/2009 05:23:15",
470+
"09/07/28 5:23:15 AM",
471+
"09/07/28 05:23:15 AM",
472+
"09/07/28 5:23:15",
473+
"09/07/28 05:23:15",
474+
"2009-07-28 5:23:15 AM",
475+
"2009-07-28 05:23:15 AM",
476+
"2009-07-28 5:23:15",
477+
"2009-07-28 05:23:15",
478+
"28-Jul-09 5:23:15 AM",
479+
"28-Jul-09 05:23:15 AM",
480+
"28-Jul-09 5:23:15",
481+
"28-Jul-09 05:23:15",
482+
"July 28",
483+
"July 28",
484+
"2009-07-28T05:23:15.0000000",
485+
"2009-07-28T05:23:15.0000000",
486+
"Tue, 28 Jul 2009 05:23:15 GMT",
487+
"Tue, 28 Jul 2009 05:23:15 GMT",
488+
"2009-07-28T05:23:15",
489+
"5:23 AM",
490+
"05:23 AM",
491+
"5:23",
492+
"05:23",
493+
"5:23:15 AM",
494+
"05:23:15 AM",
495+
"5:23:15",
496+
"05:23:15",
497+
"2009-07-28 05:23:15Z",
498+
"Tuesday, July 28, 2009 12:23:15 PM",
499+
"Tuesday, July 28, 2009 12:23:15 PM",
500+
"Tuesday, July 28, 2009 12:23:15",
501+
"Tuesday, July 28, 2009 12:23:15",
502+
"July 28, 2009 12:23:15 PM",
503+
"July 28, 2009 12:23:15 PM",
504+
"July 28, 2009 12:23:15",
505+
"July 28, 2009 12:23:15",
506+
"Tuesday, 28 July, 2009 12:23:15 PM",
507+
"Tuesday, 28 July, 2009 12:23:15 PM",
508+
"Tuesday, 28 July, 2009 12:23:15",
509+
"Tuesday, 28 July, 2009 12:23:15",
510+
"28 July, 2009 12:23:15 PM",
511+
"28 July, 2009 12:23:15 PM",
512+
"28 July, 2009 12:23:15",
513+
"28 July, 2009 12:23:15",
514+
"July 2009",
515+
"July 2009",
516+
};
314517
}
315518

src/System.Runtime/tests/System/Decimal.cs

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.Globalization;
67
using Xunit;
78

@@ -751,10 +752,41 @@ public static void TestToInt32()
751752
Assert.Equal(Int32.MinValue, Decimal.ToInt32((Decimal)Int32.MinValue));
752753
}
753754

754-
[Fact]
755-
public static void TestGetBits()
755+
public static IEnumerable<object[]> DecimalTestData
756756
{
757-
// Int32[] Decimal.GetBits(Decimal)
757+
get
758+
{
759+
return new[]
760+
{
761+
new object[] {1M, new int[] { 0x00000001, 0x00000000, 0x00000000, 0x00000000 } },
762+
new object[] {100000000000000M, new int[] { 0x107A4000, 0x00005AF3, 0x00000000, 0x00000000 } },
763+
new object[] {100000000000000.00000000000000M, new int[] { 0x10000000, 0x3E250261, 0x204FCE5E, 0x000E0000 } },
764+
new object[] {1.0000000000000000000000000000M, new int[] { 0x10000000, 0x3E250261, 0x204FCE5E, 0x001C0000 } },
765+
new object[] {123456789M, new int[] { 0x075BCD15, 0x00000000, 0x00000000, 0x00000000 } },
766+
new object[] {0.123456789M, new int[] { 0x075BCD15, 0x00000000, 0x00000000, 0x00090000 } },
767+
new object[] {0.000000000123456789M, new int[] { 0x075BCD15, 0x00000000, 0x00000000, 0x00120000 } },
768+
new object[] {0.000000000000000000123456789M, new int[] { 0x075BCD15, 0x00000000, 0x00000000, 0x001B0000 } },
769+
new object[] {4294967295M, new int[] { unchecked((int)0xFFFFFFFF), 0x00000000, 0x00000000, 0x00000000 } },
770+
new object[] {18446744073709551615M, new int[] { unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), 0x00000000, 0x00000000 } },
771+
new object[] {Decimal.MaxValue, new int[] { unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), 0x00000000 } },
772+
new object[] {Decimal.MinValue, new int[] { unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), unchecked((int)0x80000000) } },
773+
new object[] {-7.9228162514264337593543950335M, new int[] { unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), unchecked((int)0xFFFFFFFF), unchecked((int)0x801C0000) } }
774+
};
775+
}
776+
}
777+
778+
[Theory, MemberData("DecimalTestData")]
779+
public static void TestGetBits(Decimal input, int[] expectedBits)
780+
{
781+
int[] actualsBits = Decimal.GetBits(input);
782+
783+
Assert.Equal(expectedBits, actualsBits);
784+
785+
bool sign = (actualsBits[3] & 0x80000000) != 0;
786+
byte scale = (byte)((actualsBits[3] >> 16) & 0x7F);
787+
Decimal newValue = new Decimal(actualsBits[0], actualsBits[1], actualsBits[2], sign, scale);
788+
789+
Assert.Equal(input, newValue);
758790
}
759791

760792
[Fact]

0 commit comments

Comments
 (0)