11using HeapSort ;
22
3- namespace HeapSortTests
3+ namespace HeapSortTests ;
4+
5+ [ TestClass ]
6+ public class HeapSortUnitTests
47{
5- [ TestClass ]
6- public class HeapSortUnitTests
8+ [ TestMethod ]
9+ public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray ( )
710 {
8- [ TestMethod ]
9- public void GivenUnsortedArray_WhenArrayIsNotEmpty_ThenReturnSortedArray ( )
10- {
11- var array = new int [ ] { 73 , 57 , 49 , 99 , 133 , 20 , 1 } ;
12- var expected = new int [ ] { 1 , 20 , 49 , 57 , 73 , 99 , 133 } ;
13- var sortFunction = new HeapSortMethods ( ) ;
11+ var array = new int [ ] { 73 , 57 , 49 , 99 , 133 , 20 , 1 } ;
12+ var expected = new int [ ] { 1 , 20 , 49 , 57 , 73 , 99 , 133 } ;
13+ var sortFunction = new HeapSortMethods ( ) ;
1414
15- var sortedArray = sortFunction . SortArray ( array , array . Length , string . Empty ) ;
15+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
1616
17- Assert . IsNotNull ( sortedArray ) ;
18- CollectionAssert . AreEqual ( sortedArray , expected ) ;
19- }
17+ Assert . IsNotNull ( sortedArray ) ;
18+ CollectionAssert . AreEqual ( expected , sortedArray ) ;
19+ }
2020
21- [ TestMethod ]
22- public void GivenUnsortedArray_WhenArrayIsRandomized_ThenCheckInstanceType ( )
23- {
24- var sortFunction = new HeapSortMethods ( ) ;
25- var array = HeapSortMethods . CreateRandomArray ( 200 , 0 , 200 ) ;
21+ [ TestMethod ]
22+ public void GivenArticleExample_WhenSorted_ThenNegativeValueComesFirst ( )
23+ {
24+ var array = new int [ ] { 12 , 2 , 24 , 51 , 8 , - 5 } ;
25+ var expected = new int [ ] { - 5 , 2 , 8 , 12 , 24 , 51 } ;
26+ var sortFunction = new HeapSortMethods ( ) ;
2627
27- var sortedArray = sortFunction . SortArray ( array , array . Length , string . Empty ) ;
28+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
2829
29- Assert . IsInstanceOfType ( sortedArray , typeof ( int [ ] ) ) ;
30- }
30+ CollectionAssert . AreEqual ( expected , sortedArray ) ;
31+ }
3132
32- [ TestMethod ]
33- public void GivenUnsortedArray_WhenArrayIsSorted_ThenResultingArrayIsNotNull ( )
34- {
35- var sortFunction = new HeapSortMethods ( ) ; ;
36- var array = HeapSortMethods . CreateRandomArray ( 200 , 0 , 200 ) ;
33+ [ TestMethod ]
34+ public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray ( )
35+ {
36+ var array = new int [ ] { 73 } ;
37+ var expected = new int [ ] { 73 } ;
38+ var sortFunction = new HeapSortMethods ( ) ;
3739
38- var sortedArray = sortFunction . SortArray ( array , array . Length , string . Empty ) ;
40+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
3941
40- Assert . IsNotNull ( sortedArray ) ;
41- }
42+ Assert . IsNotNull ( sortedArray ) ;
43+ CollectionAssert . AreEqual ( expected , sortedArray ) ;
44+ }
4245
43- [ TestMethod ]
44- public void GivenArray_WhenArrayHasSingleElement_ThenReturnSortedArray ( )
45- {
46- var array = new int [ ] { 73 } ;
47- var expected = new int [ ] { 73 } ;
48- var sortFunction = new HeapSortMethods ( ) ;
46+ [ TestMethod ]
47+ public void GivenEmptyArray_WhenSorted_ThenReturnEmptyArray ( )
48+ {
49+ var array = Array . Empty < int > ( ) ;
50+ var sortFunction = new HeapSortMethods ( ) ;
51+
52+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
4953
50- var sortedArray = sortFunction . SortArray ( array , array . Length , string . Empty ) ;
54+ Assert . AreEqual ( 0 , sortedArray . Length ) ;
55+ }
56+
57+ [ TestMethod ]
58+ public void GivenSortedArray_WhenSorted_ThenArrayStaysSorted ( )
59+ {
60+ var array = HeapSortMethods . CreateSortedArray ( 1_000 ) ;
61+ var expected = HeapSortMethods . CreateSortedArray ( 1_000 ) ;
62+ var sortFunction = new HeapSortMethods ( ) ;
63+
64+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
65+
66+ CollectionAssert . AreEqual ( expected , sortedArray ) ;
67+ }
68+
69+ [ TestMethod ]
70+ public void GivenIdenticalValues_WhenSorted_ThenValuesAreUnchanged ( )
71+ {
72+ var array = HeapSortMethods . CreateIdenticalArray ( 1_000 , 1 , 100 ) ;
73+ var expected = ( int [ ] ) array . Clone ( ) ;
74+ var sortFunction = new HeapSortMethods ( ) ;
75+
76+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
77+
78+ CollectionAssert . AreEqual ( expected , sortedArray ) ;
79+ }
80+
81+ [ TestMethod ]
82+ public void GivenRandomArrayWithDuplicates_WhenSorted_ThenMatchesArraySort ( )
83+ {
84+ var array = HeapSortMethods . CreateRandomArray ( 5_000 , 0 , 500 ) ;
85+ var expected = ( int [ ] ) array . Clone ( ) ;
86+ Array . Sort ( expected ) ;
87+ var sortFunction = new HeapSortMethods ( ) ;
5188
52- Assert . IsNotNull ( sortedArray ) ;
53- CollectionAssert . AreEqual ( sortedArray , expected ) ;
54- }
89+ var sortedArray = sortFunction . SortArray ( array , array . Length ) ;
5590
91+ CollectionAssert . AreEqual ( expected , sortedArray ) ;
5692 }
57- }
93+ }
0 commit comments