Skip to content

Commit 5a6bf62

Browse files
author
DaZombieKiller
committed
Add RefEnumerable indexer exception tests
1 parent e4810fb commit 5a6bf62

File tree

2 files changed

+83
-25
lines changed

2 files changed

+83
-25
lines changed

UnitTests/UnitTests.HighPerformance.Shared/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int
5050

5151
[TestCategory("ReadOnlyRefEnumerable")]
5252
[TestMethod]
53-
[DataRow(1, 1, new[] { 1 })]
54-
[DataRow(4, 1, new[] { 1, 2, 3, 4 })]
55-
[DataRow(4, 4, new[] { 1, 5, 9, 13 })]
56-
[DataRow(4, 5, new[] { 1, 6, 11, 16 })]
57-
public void Test_ReadOnlyRefEnumerable_Indexer(int length, int step, int[] values)
53+
[DataRow(1, new[] { 1 })]
54+
[DataRow(1, new[] { 1, 2, 3, 4 })]
55+
[DataRow(4, new[] { 1, 5, 9, 13 })]
56+
[DataRow(5, new[] { 1, 6, 11, 16 })]
57+
public void Test_ReadOnlyRefEnumerable_Indexer(int step, int[] values)
5858
{
5959
Span<int> data = new[]
6060
{
@@ -64,22 +64,35 @@ public void Test_ReadOnlyRefEnumerable_Indexer(int length, int step, int[] value
6464
13, 14, 15, 16
6565
};
6666

67-
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], length, step);
67+
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], values.Length, step);
6868

6969
for (int i = 0; i < enumerable.Length; i++)
7070
{
7171
Assert.AreEqual(enumerable[i], values[i]);
7272
}
7373
}
7474

75+
[TestCategory("ReadOnlyRefEnumerable")]
76+
[TestMethod]
77+
public void Test_ReadOnlyRefEnumerable_Indexer_ThrowsIndexOutOfRange()
78+
{
79+
int[] array = new[]
80+
{
81+
0, 0, 0, 0
82+
};
83+
84+
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[-1]);
85+
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[array.Length]);
86+
}
87+
7588
#if NETCOREAPP3_1_OR_GREATER
7689
[TestCategory("ReadOnlyRefEnumerable")]
7790
[TestMethod]
78-
[DataRow(1, 1, new[] { 1 })]
79-
[DataRow(4, 1, new[] { 1, 2, 3, 4 })]
80-
[DataRow(4, 4, new[] { 1, 5, 9, 13 })]
81-
[DataRow(4, 5, new[] { 1, 6, 11, 16 })]
82-
public void Test_ReadOnlyRefEnumerable_Index_Indexer(int length, int step, int[] values)
91+
[DataRow(1, new[] { 1 })]
92+
[DataRow(1, new[] { 1, 2, 3, 4 })]
93+
[DataRow(4, new[] { 1, 5, 9, 13 })]
94+
[DataRow(5, new[] { 1, 6, 11, 16 })]
95+
public void Test_ReadOnlyRefEnumerable_Index_Indexer(int step, int[] values)
8396
{
8497
Span<int> data = new[]
8598
{
@@ -89,9 +102,25 @@ public void Test_ReadOnlyRefEnumerable_Index_Indexer(int length, int step, int[]
89102
13, 14, 15, 16
90103
};
91104

92-
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], length, step);
105+
ReadOnlyRefEnumerable<int> enumerable = ReadOnlyRefEnumerable<int>.DangerousCreate(in data[0], values.Length, step);
106+
107+
for (int i = 1; i <= enumerable.Length; i++)
108+
{
109+
Assert.AreEqual(values[^i], enumerable[^i]);
110+
}
111+
}
112+
113+
[TestCategory("ReadOnlyRefEnumerable")]
114+
[TestMethod]
115+
public void Test_ReadOnlyRefEnumerable_Index_Indexer_ThrowsIndexOutOfRange()
116+
{
117+
int[] array = new[]
118+
{
119+
0, 0, 0, 0
120+
};
93121

94-
Assert.AreEqual(values[^1], enumerable[^1]);
122+
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[new Index(array.Length)]);
123+
Assert.ThrowsException<IndexOutOfRangeException>(() => ReadOnlyRefEnumerable<int>.DangerousCreate(in array[0], array.Length, 1)[^0]);
95124
}
96125
#endif
97126
}

UnitTests/UnitTests.HighPerformance.Shared/Enumerables/Test_RefEnumerable{T}.cs

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ public void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step)
5050

5151
[TestCategory("RefEnumerable")]
5252
[TestMethod]
53-
[DataRow(1, 1, new[] { 1 })]
54-
[DataRow(4, 1, new[] { 1, 2, 3, 4 })]
55-
[DataRow(4, 4, new[] { 1, 5, 9, 13 })]
56-
[DataRow(4, 5, new[] { 1, 6, 11, 16 })]
57-
public void Test_RefEnumerable_Indexer(int length, int step, int[] values)
53+
[DataRow(1, new[] { 1 })]
54+
[DataRow(1, new[] { 1, 2, 3, 4 })]
55+
[DataRow(4, new[] { 1, 5, 9, 13 })]
56+
[DataRow(5, new[] { 1, 6, 11, 16 })]
57+
public void Test_RefEnumerable_Indexer(int step, int[] values)
5858
{
5959
Span<int> data = new[]
6060
{
@@ -64,21 +64,34 @@ public void Test_RefEnumerable_Indexer(int length, int step, int[] values)
6464
13, 14, 15, 16
6565
};
6666

67-
RefEnumerable<int> enumerable = RefEnumerable<int>.DangerousCreate(ref data[0], length, step);
67+
RefEnumerable<int> enumerable = RefEnumerable<int>.DangerousCreate(ref data[0], values.Length, step);
6868

6969
for (int i = 0; i < enumerable.Length; i++)
7070
{
7171
Assert.AreEqual(enumerable[i], values[i]);
7272
}
7373
}
7474

75+
[TestCategory("RefEnumerable")]
76+
[TestMethod]
77+
public void Test_RefEnumerable_Indexer_ThrowsIndexOutOfRange()
78+
{
79+
int[] array = new[]
80+
{
81+
0, 0, 0, 0
82+
};
83+
84+
Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[-1]);
85+
Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[array.Length]);
86+
}
87+
7588
#if NETCOREAPP3_1_OR_GREATER
7689
[TestCategory("RefEnumerable")]
7790
[TestMethod]
78-
[DataRow(1, 1, new[] { 1 })]
79-
[DataRow(4, 1, new[] { 1, 2, 3, 4 })]
80-
[DataRow(4, 4, new[] { 1, 5, 9, 13 })]
81-
[DataRow(4, 5, new[] { 1, 6, 11, 16 })]
91+
[DataRow(1, new[] { 1 })]
92+
[DataRow(1, new[] { 1, 2, 3, 4 })]
93+
[DataRow(4, new[] { 1, 5, 9, 13 })]
94+
[DataRow(5, new[] { 1, 6, 11, 16 })]
8295
public void Test_RefEnumerable_Index_Indexer(int length, int step, int[] values)
8396
{
8497
Span<int> data = new[]
@@ -89,9 +102,25 @@ public void Test_RefEnumerable_Index_Indexer(int length, int step, int[] values)
89102
13, 14, 15, 16
90103
};
91104

92-
RefEnumerable<int> enumerable = RefEnumerable<int>.DangerousCreate(ref data[0], length, step);
105+
RefEnumerable<int> enumerable = RefEnumerable<int>.DangerousCreate(ref data[0], values.Length, step);
106+
107+
for (int i = 1; i <= enumerable.Length; i++)
108+
{
109+
Assert.AreEqual(values[^i], enumerable[^i]);
110+
}
111+
}
112+
113+
[TestCategory("RefEnumerable")]
114+
[TestMethod]
115+
public void Test_RefEnumerable_Index_Indexer_ThrowsIndexOutOfRange()
116+
{
117+
int[] array = new[]
118+
{
119+
0, 0, 0, 0
120+
};
93121

94-
Assert.AreEqual(values[^1], enumerable[^1]);
122+
Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[new Index(array.Length)]);
123+
Assert.ThrowsException<IndexOutOfRangeException>(() => RefEnumerable<int>.DangerousCreate(ref array[0], array.Length, 1)[^0]);
95124
}
96125
#endif
97126
}

0 commit comments

Comments
 (0)