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

Commit d95a76c

Browse files
committed
Merge pull request #2872 from Sridhar-MS/system-runtime-tests
Add tests for System.Runtime based on mscorlib code coverage data
2 parents 83a5b06 + 392378c commit d95a76c

File tree

10 files changed

+500
-93
lines changed

10 files changed

+500
-93
lines changed

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

Lines changed: 8 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" />
@@ -98,6 +100,12 @@
98100
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
99101
<Targets>Build;DebugSymbolsProjectOutputGroup</Targets>
100102
</ProjectReference>
103+
<ProjectReference Include="..\..\System.Private.Uri\src\System.Private.Uri.CoreCLR.csproj">
104+
<ReferenceOutputAssembly>false</ReferenceOutputAssembly>
105+
<OutputItemType>Content</OutputItemType>
106+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
107+
<Targets>Build;DebugSymbolsProjectOutputGroup</Targets>
108+
</ProjectReference>
101109
</ItemGroup>
102110
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
103111
</Project>

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

Lines changed: 66 additions & 58 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);
@@ -222,78 +223,85 @@ public static void TestTrivials()
222223
Assert.Throws<RankException>(() => il4.IndexOf(0));
223224
}
224225

225-
[Fact]
226-
public static void TestBinarySearch()
226+
public static IEnumerable<object[]> BinarySearchTestData
227227
{
228-
//@todo: Must pass in a non-null comparer for now (due to incomplete support in underlying Compare class)
229-
//
230-
IComparer comparer = new IntegerComparer();
231-
IComparer<int> icomparer = new IntegerComparer();
232-
int idx;
233-
int[] ia = { 1, 3, 6, 6, 8, 10, 12, 16 };
234-
idx = Array.BinarySearch((Array)ia, 8, comparer);
235-
Assert.Equal(idx, 4);
236-
237-
//Return value semantics: Quoted from MSDN without comment.
238-
//
239-
//The index of the specified value in the specified array,
240-
//if value is found. If value is not found and value is
241-
//less than one or more elements in array, a negative
242-
//number which is the bitwise complement of the index of
243-
//the first element that is larger than value.
244-
//If value is not found and value is greater than any
245-
//of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
246-
idx = Array.BinarySearch((Array)ia, 99, comparer);
247-
Assert.Equal(idx, ~(ia.Length));
248-
249-
idx = Array.BinarySearch<int>(ia, 0, 8, 99, icomparer);
250-
Assert.Equal(idx, ~(ia.Length));
251-
252-
idx = Array.BinarySearch((Array)ia, 6, comparer);
253-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
228+
get
229+
{
230+
int[] intArray = { 1, 3, 6, 6, 8, 10, 12, 16 };
231+
IComparer intComparer = new IntegerComparer();
232+
IComparer<int> intGenericComparer = new IntegerComparer();
254233

255-
idx = Array.BinarySearch<int>(ia, 0, 8, 6, icomparer);
256-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
234+
string[] strArray = { null, "aa", "bb", "bb", "cc", "dd", "ee" };
235+
IComparer strComparer = new StringComparer();
236+
IComparer<string> strGenericComparer = new StringComparer();
257237

258-
idx = Array.BinarySearch((Array)ia, 6, comparer);
259-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
238+
return new[]
239+
{
240+
new object[] { intArray, 8, intComparer, intGenericComparer, new Func<int, bool>(i => i == 4) },
241+
new object[] { intArray, 99, intComparer, intGenericComparer, new Func<int, bool>(i => i == ~(intArray.Length)) },
242+
new object[] { intArray, 6, intComparer, intGenericComparer, new Func<int, bool>(i => i == 2 || i == 3) },
243+
new object[] { strArray, "bb", strComparer, strGenericComparer, new Func<int, bool>(i => i == 2 || i == 3) },
244+
new object[] { strArray, null, strComparer, null, new Func<int, bool>(i => i == 0) },
245+
};
246+
}
247+
}
260248

261-
idx = Array.BinarySearch<int>(ia, 0, 8, 6, icomparer);
262-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
249+
[Theory, MemberData("BinarySearchTestData")]
250+
public static void TestBinarySearch<T>(T[] array, T value, IComparer comparer, IComparer<T> genericComparer, Func<int, bool> verifier)
251+
{
252+
int idx = Array.BinarySearch((Array)array, value, comparer);
253+
Assert.True(verifier(idx));
263254

264-
comparer = new StringComparer();
265-
IComparer<String> scomparer = new StringComparer();
255+
idx = Array.BinarySearch<T>(array, value, genericComparer);
256+
Assert.True(verifier(idx));
266257

267-
String[] sa = { null, "aa", "bb", "bb", "cc", "dd", "ee" };
268-
idx = Array.BinarySearch((Array)sa, "bb", comparer);
269-
Assert.Equal(idx, 3);
258+
idx = Array.BinarySearch((Array)array, value);
259+
Assert.True(verifier(idx));
270260

271-
idx = Array.BinarySearch<String>(sa, 0, sa.Length, "bb", scomparer);
272-
Assert.Equal(idx, 3);
273-
274-
idx = Array.BinarySearch((Array)sa, 3, 4, "bb", comparer);
275-
Assert.Equal(idx, 3);
261+
idx = Array.BinarySearch<T>(array, value);
262+
Assert.True(verifier(idx));
263+
}
276264

277-
idx = Array.BinarySearch<String>(sa, 3, 4, "bb", scomparer);
278-
Assert.Equal(idx, 3);
265+
public static IEnumerable<object[]> BinarySearchTestDataInRange
266+
{
267+
get
268+
{
269+
int[] intArray = { 1, 3, 6, 6, 8, 10, 12, 16 };
270+
IComparer intComparer = new IntegerComparer();
271+
IComparer<int> intGenericComparer = new IntegerComparer();
279272

280-
idx = Array.BinarySearch((Array)sa, 4, 3, "bb", comparer);
281-
Assert.Equal(idx, -5);
273+
string[] strArray = { null, "aa", "bb", "bb", "cc", "dd", "ee" };
274+
IComparer strComparer = new StringComparer();
275+
IComparer<string> strGenericComparer = new StringComparer();
282276

283-
idx = Array.BinarySearch<String>(sa, 4, 3, "bb", scomparer);
284-
Assert.Equal(idx, -5);
277+
return new[]
278+
{
279+
new object[] { intArray, 0, 8, 99, intComparer, intGenericComparer, new Func<int, bool>(i => i == ~(intArray.Length)) },
280+
new object[] { intArray, 0, 8, 6, intComparer, intGenericComparer, new Func<int, bool>(i => i == 2 || i == 3) },
281+
new object[] { intArray, 1, 5, 16, intComparer, intGenericComparer, new Func<int, bool>(i => i == -7) },
282+
new object[] { strArray, 0, strArray.Length, "bb", strComparer, strGenericComparer, new Func<int, bool>(i => i == 2 || i == 3) },
283+
new object[] { strArray, 3, 4, "bb", strComparer, strGenericComparer, new Func<int, bool>(i => i == 3) },
284+
new object[] { strArray, 4, 3, "bb", strComparer, strGenericComparer, new Func<int, bool>(i => i == -5) },
285+
new object[] { strArray, 4, 0, "bb", strComparer, strGenericComparer, new Func<int, bool>(i => i == -5) },
286+
new object[] { strArray, 0, 7, null, strComparer, null, new Func<int, bool>(i => i == 0) },
287+
};
288+
}
289+
}
285290

286-
idx = Array.BinarySearch((Array)sa, 4, 0, "bb", comparer);
287-
Assert.Equal(idx, -5);
291+
[Theory, MemberData("BinarySearchTestDataInRange")]
292+
public static void TestBinarySearchInRange<T>(T[] array, int index, int length, T value, IComparer comparer, IComparer<T> genericComparer, Func<int, bool> verifier)
293+
{
294+
int idx = Array.BinarySearch((Array)array, index, length, value, comparer);
295+
Assert.True(verifier(idx));
288296

289-
idx = Array.BinarySearch<String>(sa, 4, 0, "bb", scomparer);
290-
Assert.Equal(idx, -5);
297+
idx = Array.BinarySearch<T>(array, index, length, value, genericComparer);
298+
Assert.True(verifier(idx));
291299

292-
idx = Array.BinarySearch((Array)sa, 0, 7, null, comparer);
293-
Assert.Equal(idx, 0);
300+
idx = Array.BinarySearch((Array)array, index, length, value);
301+
Assert.True(verifier(idx));
294302

295-
idx = Array.BinarySearch<String>(sa, 0, 7, null, scomparer);
296-
Assert.Equal(idx, 0);
303+
idx = Array.BinarySearch<T>(array, index, length, value);
304+
Assert.True(verifier(idx));
297305
}
298306

299307
[Fact]

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Reflection;
8+
using System.Runtime.InteropServices;
79
using Xunit;
810

911
public static unsafe class BufferTests
@@ -69,16 +71,46 @@ public static void TestBlockCopy()
6971
}
7072
}
7173

72-
[Fact]
73-
public static void TestByteLength()
74+
public static IEnumerable<object[]> ByteLengthTestData
7475
{
75-
int i;
76-
77-
i = Buffer.ByteLength(new int[7]);
78-
Assert.Equal(i, 7 * sizeof(int));
76+
get
77+
{
78+
return new[]
79+
{
80+
new object[] {typeof(byte), sizeof(byte) },
81+
new object[] {typeof(sbyte), sizeof(sbyte) },
82+
new object[] {typeof(short), sizeof(short) },
83+
new object[] {typeof(ushort), sizeof(ushort) },
84+
new object[] {typeof(int), sizeof(int) },
85+
new object[] {typeof(uint), sizeof(uint) },
86+
new object[] {typeof(long), sizeof(long) },
87+
new object[] {typeof(ulong), sizeof(ulong) },
88+
new object[] {typeof(IntPtr), sizeof(IntPtr) },
89+
new object[] {typeof(UIntPtr), sizeof(UIntPtr) },
90+
new object[] {typeof(double), sizeof(double) },
91+
new object[] {typeof(float), sizeof(float) },
92+
new object[] {typeof(bool), sizeof(bool) },
93+
new object[] {typeof(char), sizeof(char) },
94+
new object[] {typeof(decimal), sizeof(decimal) },
95+
new object[] {typeof(DateTime), sizeof(DateTime) },
96+
new object[] {typeof(string), -1 },
97+
};
98+
}
99+
}
79100

80-
i = Buffer.ByteLength(new double[33]);
81-
Assert.Equal(i, 33 * sizeof(double));
101+
[Theory, MemberData("ByteLengthTestData")]
102+
public static void TestByteLength(Type type, int size)
103+
{
104+
const int length = 25;
105+
Array array = Array.CreateInstance(type, length);
106+
if (type.GetTypeInfo().IsPrimitive)
107+
{
108+
Assert.Equal(length * size, Buffer.ByteLength(array));
109+
}
110+
else
111+
{
112+
Assert.Throws<ArgumentException>(() => Buffer.ByteLength(array));
113+
}
82114
}
83115

84116
[Fact]

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

Lines changed: 43 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,46 @@ public static void TestTryParseExactA()
291294
Assert.Equal(s, actual);
292295
}
293296

297+
[Fact]
298+
public static void TestGetDateTimeFormats()
299+
{
300+
char[] allStandardFormats =
301+
{
302+
'd', 'D', 'f', 'F', 'g', 'G',
303+
'm', 'M', 'o', 'O', 'r', 'R',
304+
's', 't', 'T', 'u', 'U', 'y', 'Y',
305+
};
306+
307+
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
308+
List<string> july28Formats = new List<string>();
309+
310+
foreach (char format in allStandardFormats)
311+
{
312+
string[] dates = july28.GetDateTimeFormats(format);
313+
314+
Assert.True(dates.Length > 0);
315+
316+
DateTime parsedDate;
317+
Assert.True(DateTime.TryParseExact(dates[0], format.ToString(), CultureInfo.CurrentCulture, DateTimeStyles.None, out parsedDate));
318+
319+
july28Formats.AddRange(dates);
320+
}
321+
322+
List<string> actualJuly28Formats = july28.GetDateTimeFormats().ToList();
323+
Assert.Equal(july28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
324+
325+
actualJuly28Formats = july28.GetDateTimeFormats(CultureInfo.CurrentCulture).ToList();
326+
Assert.Equal(july28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
327+
}
328+
329+
[Fact]
330+
public static void TestGetDateTimeFormats_FormatSpecifier_InvalidFormat()
331+
{
332+
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
333+
334+
Assert.Throws<FormatException>(() => july28.GetDateTimeFormats('x'));
335+
}
336+
294337
internal static void ValidateYearMonthDay(DateTime dt, int year, int month, int day)
295338
{
296339
Assert.Equal(dt.Year, year);

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)