-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
GitHub21379.cs
116 lines (100 loc) · 4.97 KB
/
GitHub21379.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Reflection;
using CoreFXTestLibrary;
// Regression test for https://github.com/dotnet/corefx/issues/21379
public class GitHub21379
{
public static IEnumerable<object[]> BinarySearch_SZArray_TestData()
{
// SByte
sbyte[] sbyteArray = new sbyte[] { sbyte.MinValue, 0, 0, sbyte.MaxValue };
yield return new object[] { sbyteArray, 0, 4, sbyte.MinValue, null, 0 };
yield return new object[] { sbyteArray, 0, 4, (sbyte)0, null, 1 };
yield return new object[] { sbyteArray, 0, 4, sbyte.MaxValue, null, 3 };
yield return new object[] { sbyteArray, 0, 4, (sbyte)1, null, -4 };
yield return new object[] { sbyteArray, 0, 1, sbyte.MinValue, null, 0 };
yield return new object[] { sbyteArray, 1, 3, sbyte.MaxValue, null, 3 };
yield return new object[] { sbyteArray, 1, 3, sbyte.MinValue, null, -2 };
yield return new object[] { sbyteArray, 1, 0, (sbyte)0, null, -2 };
yield return new object[] { new sbyte[0], 0, 0, (sbyte)0, null, -1 };
// Int16
short[] shortArray = new short[] { short.MinValue, 0, 0, short.MaxValue };
yield return new object[] { shortArray, 0, 4, short.MinValue, null, 0 };
yield return new object[] { shortArray, 0, 4, (short)0, null, 1 };
yield return new object[] { shortArray, 0, 4, short.MaxValue, null, 3 };
yield return new object[] { shortArray, 0, 4, (short)1, null, -4 };
yield return new object[] { shortArray, 0, 1, short.MinValue, null, 0 };
yield return new object[] { shortArray, 1, 3, short.MaxValue, null, 3 };
yield return new object[] { shortArray, 1, 3, short.MinValue, null, -2 };
yield return new object[] { shortArray, 1, 0, (short)0, null, -2 };
yield return new object[] { new short[0], 0, 0, (short)0, null, -1 };
}
public static void BinarySearch_Array(Array array, int index, int length, object value, IComparer comparer, int expected)
{
bool isDefaultComparer = comparer == null || comparer == Comparer.Default;
if (index == array.GetLowerBound(0) && length == array.Length)
{
if (isDefaultComparer)
{
// Use BinarySearch(Array, object)
Assert.AreEqual(expected, Array.BinarySearch(array, value));
Assert.AreEqual(expected, Array.BinarySearch(array, value, Comparer.Default));
}
// Use BinarySearch(Array, object, IComparer)
Assert.AreEqual(expected, Array.BinarySearch(array, value, comparer));
}
if (isDefaultComparer)
{
// Use BinarySearch(Array, int, int, object)
Assert.AreEqual(expected, Array.BinarySearch(array, index, length, value));
}
// Use BinarySearch(Array, int, int, object, IComparer)
Assert.AreEqual(expected, Array.BinarySearch(array, index, length, value, comparer));
}
public static void BinarySearch_SZArray<T>(T[] array, int index, int length, T value, IComparer<T> comparer, int expected)
{
// Forward to the non-generic overload if we can.
bool isDefaultComparer = comparer == null || comparer == Comparer<T>.Default;
if (isDefaultComparer || comparer is IComparer)
{
// Basic: forward SZArray
BinarySearch_Array(array, index, length, value, (IComparer)comparer, expected);
}
if (index == 0 && length == array.Length)
{
if (isDefaultComparer)
{
// Use BinarySearch<T>(T[], T)
Assert.AreEqual(expected, Array.BinarySearch(array, value));
Assert.AreEqual(expected, Array.BinarySearch(array, value, Comparer<T>.Default));
}
// Use BinarySearch<T>(T[], T, IComparer)
Assert.AreEqual(expected, Array.BinarySearch(array, value, comparer));
}
if (isDefaultComparer)
{
// Use BinarySearch<T>(T, int, int, T)
Assert.AreEqual(expected, Array.BinarySearch(array, index, length, value));
}
// Use BinarySearch<T>(T[], int, int, T, IComparer)
Assert.AreEqual(expected, Array.BinarySearch(array, index, length, value, comparer));
}
[TestMethod]
public static void RunTest()
{
MethodInfo testMethodDefinition = typeof(GitHub21379).GetTypeInfo().GetDeclaredMethod("BinarySearch_SZArray");
foreach (object[] testData in BinarySearch_SZArray_TestData())
{
Type elementType = testData[0].GetType().GetElementType();
MethodInfo testMethod = testMethodDefinition.MakeGenericMethod(new Type[] { elementType });
testMethod.Invoke(null, testData);
}
}
}