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

Commit fd5d0d9

Browse files
committed
Address Pull Request feedback for System.Runtime tests
And also fix CI failure related to System.Private.Uri. Similar issue is tracked by #2817.
1 parent aed0765 commit fd5d0d9

File tree

5 files changed

+140
-155
lines changed

5 files changed

+140
-155
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
101101
<Targets>Build;DebugSymbolsProjectOutputGroup</Targets>
102102
</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>
103109
</ItemGroup>
104110
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
105111
</Project>

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

Lines changed: 63 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -223,145 +223,85 @@ public static void TestTrivials()
223223
Assert.Throws<RankException>(() => il4.IndexOf(0));
224224
}
225225

226-
[Fact]
227-
public static void TestBinarySearch()
226+
public static IEnumerable<object[]> BinarySearchTestData
228227
{
229-
//@todo: Must pass in a non-null comparer for now (due to incomplete support in underlying Compare class)
230-
//
231-
IComparer comparer = new IntegerComparer();
232-
IComparer<int> icomparer = new IntegerComparer();
233-
int idx;
234-
int[] ia = { 1, 3, 6, 6, 8, 10, 12, 16 };
235-
idx = Array.BinarySearch((Array)ia, 8, comparer);
236-
Assert.Equal(idx, 4);
237-
238-
//Return value semantics: Quoted from MSDN without comment.
239-
//
240-
//The index of the specified value in the specified array,
241-
//if value is found. If value is not found and value is
242-
//less than one or more elements in array, a negative
243-
//number which is the bitwise complement of the index of
244-
//the first element that is larger than value.
245-
//If value is not found and value is greater than any
246-
//of the elements in array, a negative number which is the bitwise complement of (the index of the last element plus 1).
247-
idx = Array.BinarySearch((Array)ia, 99, comparer);
248-
Assert.Equal(idx, ~(ia.Length));
249-
250-
idx = Array.BinarySearch<int>(ia, 0, 8, 99, icomparer);
251-
Assert.Equal(idx, ~(ia.Length));
252-
253-
idx = Array.BinarySearch((Array)ia, 6, comparer);
254-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
255-
256-
idx = Array.BinarySearch((Array)ia, 0, 8, 6, comparer);
257-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
258-
259-
idx = Array.BinarySearch<int>(ia, 6, icomparer);
260-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
261-
262-
idx = Array.BinarySearch<int>(ia, 0, 8, 6, icomparer);
263-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
264-
265-
comparer = new StringComparer();
266-
IComparer<String> scomparer = new StringComparer();
267-
268-
String[] sa = { null, "aa", "bb", "bb", "cc", "dd", "ee" };
269-
idx = Array.BinarySearch((Array)sa, "bb", comparer);
270-
Assert.Equal(idx, 3);
271-
272-
idx = Array.BinarySearch<String>(sa, 0, sa.Length, "bb", scomparer);
273-
Assert.Equal(idx, 3);
274-
275-
idx = Array.BinarySearch((Array)sa, 3, 4, "bb", comparer);
276-
Assert.Equal(idx, 3);
277-
278-
idx = Array.BinarySearch<String>(sa, 3, 4, "bb", scomparer);
279-
Assert.Equal(idx, 3);
280-
281-
idx = Array.BinarySearch((Array)sa, 4, 3, "bb", comparer);
282-
Assert.Equal(idx, -5);
283-
284-
idx = Array.BinarySearch<String>(sa, 4, 3, "bb", scomparer);
285-
Assert.Equal(idx, -5);
286-
287-
idx = Array.BinarySearch((Array)sa, 4, 0, "bb", comparer);
288-
Assert.Equal(idx, -5);
289-
290-
idx = Array.BinarySearch<String>(sa, 4, 0, "bb", scomparer);
291-
Assert.Equal(idx, -5);
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();
292233

293-
idx = Array.BinarySearch((Array)sa, 0, 7, null, comparer);
294-
Assert.Equal(idx, 0);
234+
string[] strArray = { null, "aa", "bb", "bb", "cc", "dd", "ee" };
235+
IComparer strComparer = new StringComparer();
236+
IComparer<string> strGenericComparer = new StringComparer();
295237

296-
idx = Array.BinarySearch<String>(sa, 0, 7, null, scomparer);
297-
Assert.Equal(idx, 0);
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+
}
298247
}
299248

300-
[Fact]
301-
public static void TestBinarySearch_NoIComparer()
249+
[Theory, MemberData("BinarySearchTestData")]
250+
public static void TestBinarySearch<T>(T[] array, T value, IComparer comparer, IComparer<T> genericComparer, Func<int, bool> verifier)
302251
{
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));
252+
int idx = Array.BinarySearch((Array)array, value, comparer);
253+
Assert.True(verifier(idx));
319254

320-
idx = Array.BinarySearch<int>(ia, 0, 8, 99);
321-
Assert.Equal(idx, ~(ia.Length));
255+
idx = Array.BinarySearch<T>(array, value, genericComparer);
256+
Assert.True(verifier(idx));
322257

323-
idx = Array.BinarySearch((Array)ia, 6);
324-
Assert.True(idx == 2 || idx == 3); // Duplicates are allowed but no promises on which one
258+
idx = Array.BinarySearch((Array)array, value);
259+
Assert.True(verifier(idx));
325260

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);
261+
idx = Array.BinarySearch<T>(array, value);
262+
Assert.True(verifier(idx));
263+
}
344264

345-
idx = Array.BinarySearch<String>(sa, 3, 4, "bb");
346-
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();
347272

348-
idx = Array.BinarySearch((Array)sa, 4, 3, "bb");
349-
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();
350276

351-
idx = Array.BinarySearch<String>(sa, 4, 3, "bb");
352-
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+
}
353290

354-
idx = Array.BinarySearch((Array)sa, 4, 0, "bb");
355-
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));
356296

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

360-
idx = Array.BinarySearch((Array)sa, 0, 7, null);
361-
Assert.Equal(idx, 0);
300+
idx = Array.BinarySearch((Array)array, index, length, value);
301+
Assert.True(verifier(idx));
362302

363-
idx = Array.BinarySearch<String>(sa, 0, 7, null);
364-
Assert.Equal(idx, 0);
303+
idx = Array.BinarySearch<T>(array, index, length, value);
304+
Assert.True(verifier(idx));
365305
}
366306

367307
[Fact]

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

Lines changed: 40 additions & 10 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,18 +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));
79-
80-
i = Buffer.ByteLength(new double[33]);
81-
Assert.Equal(i, 33 * sizeof(double));
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+
}
82100

83-
Assert.Throws<ArgumentException>(() => Buffer.ByteLength(new DateTime[10]));
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+
}
84114
}
85115

86116
[Fact]

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

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -298,24 +298,30 @@ public static void TestTryParseExactA()
298298
[ActiveIssue(846, PlatformID.AnyUnix)]
299299
public static void TestGetDateTimeFormats()
300300
{
301-
// Running in the new thread so that we do not interfere with the Main thread's culture
302-
Task.Run(() =>
301+
var savedCulture = CultureInfo.CurrentCulture;
302+
303+
try
303304
{
304305
CultureInfo.CurrentCulture = new CultureInfo("en-US");
305306
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
306307

307308
List<string> actualJuly28Formats = july28.GetDateTimeFormats().ToList();
308309

309310
Assert.Equal(expectedJuly28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
310-
}).Wait();
311+
}
312+
finally
313+
{
314+
CultureInfo.CurrentCulture = savedCulture;
315+
}
311316
}
312317

313318
[Fact]
314319
[ActiveIssue(846, PlatformID.AnyUnix)]
315320
public static void TestGetDateTimeFormats_FormatSpecifier()
316321
{
317-
// Running in the new thread so that we do not interfere with the Main thread's culture
318-
Task.Run(() =>
322+
var savedCulture = CultureInfo.CurrentCulture;
323+
324+
try
319325
{
320326
char[] allStandardFormats =
321327
{
@@ -334,7 +340,11 @@ public static void TestGetDateTimeFormats_FormatSpecifier()
334340
}
335341

336342
Assert.Equal(expectedJuly28Formats.OrderBy(t => t), actualJuly28Formats.OrderBy(t => t));
337-
}).Wait();
343+
}
344+
finally
345+
{
346+
CultureInfo.CurrentCulture = savedCulture;
347+
}
338348
}
339349

340350
[Fact]

src/System.Runtime/tests/System/GC.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -271,21 +271,27 @@ public void Dispose()
271271
}
272272

273273
[Fact]
274-
public static void GetTotalMemoryTest_ForceCollection()
274+
public static void GetTotalMemoryTest_NoForceCollection()
275275
{
276-
long garbageSize = MakeSomeGarbage(1024 * 1024);
277-
long garbageCollected = GC.GetTotalMemory(false) - GC.GetTotalMemory(true);
278-
Assert.True(garbageCollected >= garbageSize - 10000); // 10000 bytes threshold to stabilize the test
276+
GC.Collect();
277+
278+
byte[] bytes = new byte[50000];
279+
int genBefore = GC.GetGeneration(bytes);
280+
281+
Assert.True(GC.GetTotalMemory(false) >= bytes.Length);
282+
Assert.True(GC.GetGeneration(bytes) == genBefore);
279283
}
280284

281285
[Fact]
282-
public static void GetTotalMemoryTest_NoForceCollection()
286+
public static void GetTotalMemoryTest_ForceCollection()
283287
{
284-
long garbageSize = MakeSomeGarbage(1024 * 1024);
285-
long before = GC.GetTotalMemory(false);
286288
GC.Collect();
287-
long garbageCollected = before - GC.GetTotalMemory(false);
288-
Assert.True(garbageCollected >= garbageSize - 10000); // 10000 bytes threshold to stabilize the test
289+
290+
byte[] bytes = new byte[50000];
291+
int genBeforeGC = GC.GetGeneration(bytes);
292+
293+
Assert.True(GC.GetTotalMemory(true) >= bytes.Length);
294+
Assert.True(GC.GetGeneration(bytes) > genBeforeGC);
289295
}
290296

291297
[Fact]
@@ -300,11 +306,4 @@ public static void GetGenerationTest()
300306
GC.Collect();
301307
}
302308
}
303-
304-
private static long MakeSomeGarbage(int size)
305-
{
306-
byte[] bytes = new byte[size];
307-
GC.Collect();
308-
return bytes.Length;
309-
}
310309
}

0 commit comments

Comments
 (0)