Skip to content
This repository was archived by the owner on Mar 8, 2025. It is now read-only.

Commit b8bdee0

Browse files
author
J Burnett
committed
Improve tests by using both Random methods
1 parent 7b271f4 commit b8bdee0

2 files changed

Lines changed: 51 additions & 14 deletions

File tree

test/IntListRandomizerTests.cs

Lines changed: 50 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,16 @@ public IntListRandomizerTests(IntListTestFixture testData)
2626
[Fact]
2727
public void random_element_from_int_list()
2828
{
29+
IEnumerable<int> enr = _testData.TestList as IEnumerable<int>;
30+
2931
for (int i = 0; i < _testData.TestList.Count; ++i)
3032
{
33+
// Test via the method taking ICollection
3134
int curr = _testData.TestList.Random();
3235
curr.Should().BeGreaterOrEqualTo(IntListTestFixture.MinTestValue);
33-
}
34-
}
35-
3636

37-
[Fact]
38-
public void random_element_from_int_list_by_IEnumerable()
39-
{
40-
IEnumerable<int> enumr = _testData.TestList;
41-
foreach (var itr in enumr)
42-
{
43-
int curr = enumr.Random();
37+
// Test via the method taking IEnumerable
38+
curr = enr.Random();
4439
curr.Should().BeGreaterOrEqualTo(IntListTestFixture.MinTestValue);
4540
}
4641
}
@@ -53,43 +48,85 @@ public void random_element_from_single_item_list()
5348
int knownInt = 7466;
5449
List<int> list = new List<int>();
5550
list.Add(knownInt);
51+
IEnumerable<int> enr = list as IEnumerable<int>;
5652

5753
// Get random from the list several times. Should only receive the single element.
5854
for (int i = 0; i < 5; ++i)
5955
{
56+
// Test via the method taking ICollection
6057
(list.Random()).Should().Be(knownInt);
58+
59+
// Test via the method taking IEnumerable
60+
(enr.Random()).Should().Be(knownInt);
6161
}
6262
}
6363

6464

6565
[Fact]
66-
public void random_element_from_()
66+
public void random_element_from_Dictionary()
6767
{
68+
// Create a collection with asso'd enumerator, and populate it.
69+
Dictionary<int, string> dict = new Dictionary<int, string>();
70+
IEnumerable<KeyValuePair<int, string>> enr = dict as IEnumerable<KeyValuePair<int, string>>;
71+
for (int i = 0; i < 250; ++i) { dict.Add(i, i.ToString()); }
72+
73+
74+
for (int i = 0; i < dict.Count; ++i)
75+
{
76+
// Test via the method taking ICollection
77+
var curr = dict.Random();
78+
curr.Key.Should().BeGreaterOrEqualTo(IntListTestFixture.MinTestValue);
79+
curr.Value.Should().BeEquivalentTo(curr.Key.ToString());
80+
81+
// Test via the method taking IEnumerable
82+
curr = enr.Random();
83+
curr.Key.Should().BeGreaterOrEqualTo(IntListTestFixture.MinTestValue);
84+
curr.Value.Should().BeEquivalentTo(curr.Key.ToString());
85+
}
86+
}
87+
88+
89+
[Fact]
90+
public void random_element_from_SortedList()
91+
{
92+
// Create a collection with asso'd enumerator, and populate it.
6893
SortedList<int, string> list = new SortedList<int, string>();
94+
IEnumerable<KeyValuePair<int, string>> enr = list as IEnumerable<KeyValuePair<int, string>>;
6995
for (int i = 0; i < 250; ++i) { list.Add(i, i.ToString()); }
7096

71-
// Get random elements from ~1/10 of the list. The probability of sequential duplicates
72-
// increases when covering more of the available elements.
7397
for (int i = 0; i < list.Count; ++i)
7498
{
99+
// Test via the method taking ICollection
75100
var curr = list.Random();
76101
curr.Key.Should().BeGreaterOrEqualTo(IntListTestFixture.MinTestValue);
77102
curr.Value.Should().BeEquivalentTo(curr.Key.ToString());
103+
104+
// Test via the method taking IEnumerable
105+
curr = enr.Random();
106+
curr.Key.Should().BeGreaterOrEqualTo(IntListTestFixture.MinTestValue);
107+
curr.Value.Should().BeEquivalentTo(curr.Key.ToString());
78108
}
79109
}
80110

81111
#endregion Positive tests
82112

83113

114+
84115
#region Negative tests
85116

86117
// Verify the correct exception occurs when passed an empty list
87118
[Fact]
88119
public void verify_excp_on_empty_list()
89120
{
90121
List<string> list = new List<string>();
122+
123+
// Test via the method taking ICollection
91124
Action act = () => list.Random();
92125
act.ShouldThrow<ArgumentOutOfRangeException>();
126+
127+
// Test via the method taking IEnumerable
128+
act = () => (list as IEnumerable<string>).Random();
129+
act.ShouldThrow<ArgumentOutOfRangeException>();
93130
}
94131

95132
#endregion Negative tests

test/IntListTestFixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class IntListTestFixture : IDisposable
1717
public IntListTestFixture()
1818
{
1919
TestList = new List<int>();
20-
for (int i = MinTestValue; i < MaxTestValue; ++i)
20+
for (int i = MinTestValue; i <= MaxTestValue; ++i)
2121
{
2222
TestList.Add(i);
2323
}

0 commit comments

Comments
 (0)