Skip to content

Commit 5eb6a68

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

12 files changed

Lines changed: 81 additions & 72 deletions

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

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

139139
[TestMethod]
140-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
141140
public void Test_ArrayPoolBufferWriterOfT_InvalidRequestedSize()
142141
{
143-
_ = new ArrayPoolBufferWriter<byte>(-1);
144-
145-
Assert.Fail("You shouldn't be here");
142+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => new ArrayPoolBufferWriter<byte>(-1));
146143
}
147144

148145
[TestMethod]

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

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

5353
[TestMethod]
54-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
5554
public void Test_MemoryOwnerOfT_InvalidRequestedSize()
5655
{
57-
using MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(-1);
58-
59-
Assert.Fail("You shouldn't be here");
56+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
57+
{
58+
using MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(-1);
59+
});
6060
}
6161

6262
[TestMethod]
63-
[ExpectedException(typeof(ObjectDisposedException))]
6463
public void Test_MemoryOwnerOfT_DisposedMemory()
6564
{
6665
MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(127);
6766

6867
buffer.Dispose();
6968

70-
_ = buffer.Memory;
69+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => _ = buffer.Memory);
7170
}
7271

7372
[TestMethod]
74-
[ExpectedException(typeof(ObjectDisposedException))]
7573
public void Test_MemoryOwnerOfT_DisposedSpan()
7674
{
7775
MemoryOwner<int>? buffer = MemoryOwner<int>.Allocate(127);
7876

7977
buffer.Dispose();
8078

81-
_ = buffer.Span;
79+
_ = Assert.ThrowsExactly<ObjectDisposedException>(() => _ = buffer.Span);
8280
}
8381

8482
[TestMethod]

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

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

4949
[TestMethod]
50-
[ExpectedException(typeof(ArgumentOutOfRangeException))]
5150
public void Test_SpanOwnerOfT_InvalidRequestedSize()
5251
{
53-
using SpanOwner<int> buffer = SpanOwner<int>.Allocate(-1);
54-
55-
Assert.Fail("You shouldn't be here");
52+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
53+
{
54+
using SpanOwner<int> buffer = SpanOwner<int>.Allocate(-1);
55+
});
5656
}
5757

5858
[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: 20 additions & 13 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,16 @@ 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

462-
ReadOnlySpan2D<int> span2d = new(array);
463-
_ = span2d[0..6, 2..^1];
463+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
464+
{
465+
ReadOnlySpan2D<int> span2d = new(array);
464466

465-
Assert.Fail();
467+
_ = span2d[0..6, 2..^1];
468+
});
466469
}
467470
#endif
468471

@@ -690,7 +693,6 @@ public void Test_ReadOnlySpan2DT_ToArray_2()
690693
}
691694

692695
[TestMethod]
693-
[ExpectedException(typeof(NotSupportedException))]
694696
public void Test_ReadOnlySpan2DT_Equals()
695697
{
696698
int[,] array =
@@ -699,13 +701,15 @@ public void Test_ReadOnlySpan2DT_Equals()
699701
{ 4, 5, 6 }
700702
};
701703

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

704-
_ = span2d.Equals(null);
708+
_ = span2d.Equals(null);
709+
});
705710
}
706711

707712
[TestMethod]
708-
[ExpectedException(typeof(NotSupportedException))]
709713
public void Test_ReadOnlySpan2DT_GetHashCode()
710714
{
711715
int[,] array =
@@ -714,9 +718,12 @@ public void Test_ReadOnlySpan2DT_GetHashCode()
714718
{ 4, 5, 6 }
715719
};
716720

717-
ReadOnlySpan2D<int> span2d = new(array);
721+
_ = Assert.ThrowsExactly<NotSupportedException>(() =>
722+
{
723+
ReadOnlySpan2D<int> span2d = new(array);
718724

719-
_ = span2d.GetHashCode();
725+
_ = span2d.GetHashCode();
726+
});
720727
}
721728

722729
[TestMethod]

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

Lines changed: 22 additions & 15 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,16 @@ 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

619-
Span2D<int> span2d = new(array);
620-
_ = span2d[0..6, 2..^1];
620+
_ = Assert.ThrowsExactly<ArgumentOutOfRangeException>(() =>
621+
{
622+
Span2D<int> span2d = new(array);
621623

622-
Assert.Fail();
624+
_ = span2d[0..6, 2..^1];
625+
});
623626
}
624627
#endif
625628

@@ -882,7 +885,6 @@ public void Test_Span2DT_ToArray_2()
882885
}
883886

884887
[TestMethod]
885-
[ExpectedException(typeof(NotSupportedException))]
886888
public void Test_Span2DT_Equals()
887889
{
888890
int[,] array =
@@ -891,14 +893,16 @@ public void Test_Span2DT_Equals()
891893
{ 4, 5, 6 }
892894
};
893895

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

900905
[TestMethod]
901-
[ExpectedException(typeof(NotSupportedException))]
902906
public void Test_Span2DT_GetHashCode()
903907
{
904908
int[,] array =
@@ -907,10 +911,13 @@ public void Test_Span2DT_GetHashCode()
907911
{ 4, 5, 6 }
908912
};
909913

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

916923
[TestMethod]

0 commit comments

Comments
 (0)