Skip to content

Commit ee77252

Browse files
committed
Don't use '[ExpectedException]' in HighPerformance tests
1 parent 0892002 commit ee77252

12 files changed

Lines changed: 67 additions & 62 deletions

tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_ArrayPoolBufferWriter{T}.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,11 @@ public void Test_ArrayPoolBufferWriterOfT_AllocateFromCustomPoolAndGetMemoryAndS
137137
}
138138

139139
[TestMethod]
140-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
141140
public void Test_ArrayPoolBufferWriterOfT_InvalidRequestedSize()
142141
{
143142
_ = new ArrayPoolBufferWriter<byte>(-1);
144143

145-
Assert.Fail("You shouldn't be here");
144+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Assert.Fail("You shouldn't be here"));
146145
}
147146

148147
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_MemoryOwner{T}.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,34 +51,31 @@ public void Test_MemoryOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
5151
}
5252

5353
[TestMethod]
54-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
5554
public void Test_MemoryOwnerOfT_InvalidRequestedSize()
5655
{
5756
using MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(-1);
5857

59-
Assert.Fail("You shouldn't be here");
58+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Assert.Fail("You shouldn't be here"));
6059
}
6160

6261
[TestMethod]
63-
[ExpectedException(typeof(ObjectDisposedException))]
6462
public void Test_MemoryOwnerOfT_DisposedMemory()
6563
{
6664
MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(127);
6765

6866
buffer.Dispose();
6967

70-
_ = buffer.Memory;
68+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => _ = buffer.Memory);
7169
}
7270

7371
[TestMethod]
74-
[ExpectedException(typeof(ObjectDisposedException))]
7572
public void Test_MemoryOwnerOfT_DisposedSpan()
7673
{
7774
MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(127);
7875

7976
buffer.Dispose();
8077

81-
_ = buffer.Span;
78+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => _ = buffer.Span);
8279
}
8380

8481
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Buffers/Test_SpanOwner{T}.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ public void Test_SpanOwnerOfT_AllocateFromCustomPoolAndGetMemoryAndSpan()
4747
}
4848

4949
[TestMethod]
50-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
5150
public void Test_SpanOwnerOfT_InvalidRequestedSize()
5251
{
5352
using SpanOwner<int> buffer = SpanOwner<int>.Allocate(-1);
5453

55-
Assert.Fail("You shouldn't be here");
54+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Assert.Fail("You shouldn't be here"));
5655
}
5756

5857
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_ReadOnlyRefEnumerable{T}.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ public void Test_ReadOnlyRefEnumerable_DangerousCreate_Ok(int length, int step,
3939
[DataRow(-44, 10)]
4040
[DataRow(10, -14)]
4141
[DataRow(-32, -1)]
42-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
4342
public unsafe void Test_ReadOnlyRefEnumerable_DangerousCreate_BelowZero(int length, int step)
4443
{
45-
_ = ReadOnlyRefEnumerable<int>.DangerousCreate(in *(int*)null, length, step);
44+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _ = ReadOnlyRefEnumerable<int>.DangerousCreate(in *(int*)null, length, step));
4645
}
4746

4847
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Enumerables/Test_RefEnumerable{T}.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,9 @@ public void Test_RefEnumerable_DangerousCreate_Ok(int length, int step, int[] va
3939
[DataRow(-44, 10)]
4040
[DataRow(10, -14)]
4141
[DataRow(-32, -1)]
42-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
4342
public unsafe void Test_RefEnumerable_DangerousCreate_BelowZero(int length, int step)
4443
{
45-
_ = RefEnumerable<int>.DangerousCreate(ref *(int*)null, length, step);
44+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => _ = RefEnumerable<int>.DangerousCreate(ref *(int*)null, length, step));
4645
}
4746

4847
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Extensions/Test_ArrayPoolExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,11 @@ namespace CommunityToolkit.HighPerformance.UnitTests.Extensions;
1414
public class Test_ArrayPoolExtensions
1515
{
1616
[TestMethod]
17-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
1817
public void Test_ArrayPoolExtensions_Resize_InvalidSize()
1918
{
2019
int[]? array = null;
2120

22-
ArrayPool<int>.Shared.Resize(ref array, -1);
21+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => ArrayPool<int>.Shared.Resize(ref array, -1));
2322
}
2423

2524
[TestMethod]
@@ -89,12 +88,11 @@ public void Test_ArrayPoolExtensions_Resize_Clear()
8988
}
9089

9190
[TestMethod]
92-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
9391
public void Test_ArrayPoolExtensions_EnsureCapacity_InvalidCapacity()
9492
{
9593
int[]? array = null;
9694

97-
ArrayPool<int>.Shared.EnsureCapacity(ref array, -1);
95+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => ArrayPool<int>.Shared.EnsureCapacity(ref array, -1));
9896
}
9997

10098
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,15 @@ public unsafe void Test_ParallelHelper_ForWithIndices()
4040

4141
#if NET6_0_OR_GREATER
4242
[TestMethod]
43-
[ExpectedException(typeof(ArgumentException))]
4443
public void Test_ParallelHelper_ForInvalidRange_FromEnd()
4544
{
46-
ParallelHelper.For<Assigner>(..^1);
45+
_ = Assert.ThrowsExactly<ArgumentException>(() => ParallelHelper.For<Assigner>(..^1));
4746
}
4847

4948
[TestMethod]
50-
[ExpectedException(typeof(ArgumentException))]
5149
public void Test_ParallelHelper_ForInvalidRange_RangeAll()
5250
{
53-
ParallelHelper.For<Assigner>(..);
51+
_ = Assert.ThrowsExactly<ArgumentException>(() => ParallelHelper.For<Assigner>(..));
5452
}
5553

5654
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Helpers/Test_ParallelHelper.For2D.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,15 @@ public unsafe void Test_ParallelHelper_For2DWithIndices()
5252

5353
#if NET6_0_OR_GREATER
5454
[TestMethod]
55-
[ExpectedException(typeof(ArgumentException))]
5655
public void Test_ParallelHelper_For2DInvalidRange_FromEnd()
5756
{
58-
ParallelHelper.For2D<Assigner2D>(..^1, ..4);
57+
_ = Assert.ThrowsExactly<ArgumentException>(() => ParallelHelper.For2D<Assigner2D>(..^1, ..4));
5958
}
6059

6160
[TestMethod]
62-
[ExpectedException(typeof(ArgumentException))]
6361
public void Test_ParallelHelper_For2DInvalidRange_RangeAll()
6462
{
65-
ParallelHelper.For2D<Assigner2D>(..5, ..);
63+
_ = Assert.ThrowsExactly<ArgumentException>(() => ParallelHelper.For2D<Assigner2D>(..5, ..));
6664
}
6765

6866
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_ReadOnlySpan2D{T}.cs

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,16 @@ public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_2()
417417
}
418418

419419
[TestMethod]
420-
[ExpectedException(typeof(IndexOutOfRangeException))]
421420
public unsafe void Test_ReadOnlySpan2DT_Index_Indexer_Fail()
422421
{
423422
int[,] array = new int[4, 4];
424423

425-
ReadOnlySpan2D<int> span2d = new(array);
424+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() =>
425+
{
426+
ReadOnlySpan2D<int> span2d = new(array);
426427

427-
ref readonly int span2dRef = ref span2d[^6, 2];
428+
ref readonly int span2dRef = ref span2d[^6, 2];
429+
});
428430
}
429431

430432
[TestMethod]
@@ -454,15 +456,14 @@ public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_2()
454456
}
455457

456458
[TestMethod]
457-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
458459
public unsafe void Test_ReadOnlySpan2DT_Range_Indexer_Fail()
459460
{
460461
int[,] array = new int[4, 4];
461462

462463
ReadOnlySpan2D<int> span2d = new(array);
463464
_ = span2d[0..6, 2..^1];
464465

465-
Assert.Fail();
466+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Assert.Fail());
466467
}
467468
#endif
468469

@@ -690,7 +691,6 @@ public void Test_ReadOnlySpan2DT_ToArray_2()
690691
}
691692

692693
[TestMethod]
693-
[ExpectedException(typeof(NotSupportedException))]
694694
public void Test_ReadOnlySpan2DT_Equals()
695695
{
696696
int[,] array =
@@ -699,13 +699,15 @@ public void Test_ReadOnlySpan2DT_Equals()
699699
{ 4, 5, 6 }
700700
};
701701

702-
ReadOnlySpan2D<int> span2d = new(array);
702+
_ = Assert.ThrowsExactly<NotSupportedException>(() =>
703+
{
704+
ReadOnlySpan2D<int> span2d = new(array);
703705

704-
_ = span2d.Equals(null);
706+
_ = span2d.Equals(null);
707+
});
705708
}
706709

707710
[TestMethod]
708-
[ExpectedException(typeof(NotSupportedException))]
709711
public void Test_ReadOnlySpan2DT_GetHashCode()
710712
{
711713
int[,] array =
@@ -714,9 +716,12 @@ public void Test_ReadOnlySpan2DT_GetHashCode()
714716
{ 4, 5, 6 }
715717
};
716718

717-
ReadOnlySpan2D<int> span2d = new(array);
719+
_ = Assert.ThrowsExactly<NotSupportedException>(() =>
720+
{
721+
ReadOnlySpan2D<int> span2d = new(array);
718722

719-
_ = span2d.GetHashCode();
723+
_ = span2d.GetHashCode();
724+
});
720725
}
721726

722727
[TestMethod]

tests/CommunityToolkit.HighPerformance.UnitTests/Memory/Test_Span2D{T}.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,14 +574,16 @@ public unsafe void Test_Span2DT_Index_Indexer_2()
574574
}
575575

576576
[TestMethod]
577-
[ExpectedException(typeof(IndexOutOfRangeException))]
578577
public unsafe void Test_Span2DT_Index_Indexer_Fail()
579578
{
580579
int[,] array = new int[4, 4];
581580

582-
Span2D<int> span2d = new(array);
581+
_ = Assert.ThrowsExactly<IndexOutOfRangeException>(() =>
582+
{
583+
Span2D<int> span2d = new(array);
583584

584-
ref int span2dRef = ref span2d[^6, 2];
585+
ref int span2dRef = ref span2d[^6, 2];
586+
});
585587
}
586588

587589
[TestMethod]
@@ -611,15 +613,14 @@ public unsafe void Test_Span2DT_Range_Indexer_2()
611613
}
612614

613615
[TestMethod]
614-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
615616
public unsafe void Test_Span2DT_Range_Indexer_Fail()
616617
{
617618
int[,] array = new int[4, 4];
618619

619620
Span2D<int> span2d = new(array);
620621
_ = span2d[0..6, 2..^1];
621622

622-
Assert.Fail();
623+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => Assert.Fail());
623624
}
624625
#endif
625626

@@ -882,7 +883,6 @@ public void Test_Span2DT_ToArray_2()
882883
}
883884

884885
[TestMethod]
885-
[ExpectedException(typeof(NotSupportedException))]
886886
public void Test_Span2DT_Equals()
887887
{
888888
int[,] array =
@@ -891,14 +891,16 @@ public void Test_Span2DT_Equals()
891891
{ 4, 5, 6 }
892892
};
893893

894-
Span2D<int> span2d = new(array);
895-
896894
// Span2D<T>.Equals always throw (this mirrors the behavior of Span<T>.Equals)
897-
_ = span2d.Equals(null);
895+
_ = Assert.ThrowsExactly<NotSupportedException>(() =>
896+
{
897+
Span2D<int> span2d = new(array);
898+
899+
_ = span2d.Equals(null);
900+
});
898901
}
899902

900903
[TestMethod]
901-
[ExpectedException(typeof(NotSupportedException))]
902904
public void Test_Span2DT_GetHashCode()
903905
{
904906
int[,] array =
@@ -907,10 +909,13 @@ public void Test_Span2DT_GetHashCode()
907909
{ 4, 5, 6 }
908910
};
909911

910-
Span2D<int> span2d = new(array);
911-
912912
// Same as above, this always throws
913-
_ = span2d.GetHashCode();
913+
_ = Assert.ThrowsExactly<NotSupportedException>(() =>
914+
{
915+
Span2D<int> span2d = new(array);
916+
917+
_ = span2d.GetHashCode();
918+
});
914919
}
915920

916921
[TestMethod]

0 commit comments

Comments
 (0)