Created
April 15, 2016 13:11
-
-
Save RyotaMurohoshi/eb5164768916ddd7fa89095260ba22f9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NUnit.Framework; | |
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace EnumeratorExample | |
{ | |
[TestFixture ()] | |
public class CurrentWhenMoveNextFalseTest | |
{ | |
[Test ()] | |
public void TestEmptyArray () | |
{ | |
IEnumerable<int> enumerable = new int[0]; | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.Throws<InvalidOperationException> (() => { | |
var current = enumerator.Current; | |
}); | |
} | |
[Test ()] | |
public void TestArray () | |
{ | |
IEnumerable<int> enumerable = new int[]{ 0, 1, 2 }; | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.Throws<InvalidOperationException> (() => { | |
var current = enumerator.Current; | |
}); | |
} | |
[Test ()] | |
public void TestEmptyList () | |
{ | |
IEnumerable<int> enumerable = new List<int> (); | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (default(int), enumerator.Current); | |
} | |
[Test ()] | |
public void TestList () | |
{ | |
IEnumerable<int> enumerable = new List<int> { 0, 1, 2 }; | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (default(int), enumerator.Current); | |
} | |
[Test ()] | |
public void TestEmptyDictionary () | |
{ | |
IEnumerable<KeyValuePair<string, int>> enumerable = new Dictionary<string, int> (); | |
IEnumerator<KeyValuePair<string, int>> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (default(KeyValuePair<string, int>), enumerator.Current); | |
} | |
[Test ()] | |
public void TestDictionary () | |
{ | |
IEnumerable<KeyValuePair<string, int>> enumerable = new Dictionary<string, int> { | |
{ "a", 0 }, | |
{ "b", 1 }, | |
{ "c", 2 } | |
}; | |
IEnumerator<KeyValuePair<string, int>> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (default(KeyValuePair<string, int>), enumerator.Current); | |
} | |
[Test ()] | |
public void TestEmptySet () | |
{ | |
IEnumerable<int> enumerable = new HashSet<int> { }; | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (default(int), enumerator.Current); | |
} | |
[Test ()] | |
public void TestSet () | |
{ | |
IEnumerable<int> enumerable = new HashSet<int> { 0, 1, 2 }; | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (default(int), enumerator.Current); | |
} | |
[Test ()] | |
public void TestEmptyYield () | |
{ | |
IEnumerable<int> enumerable = YieldParams<int> (); | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (enumerable.LastOrDefault (), enumerator.Current); | |
} | |
[Test ()] | |
public void TestYield () | |
{ | |
IEnumerable<int> enumerable = YieldParams<int> (0, 1, 2); | |
IEnumerator<int> enumerator = enumerable.GetEnumerator (); | |
MoveEnumeratorToNoNext (enumerator); | |
Assert.AreEqual (enumerable.LastOrDefault (), enumerator.Current); | |
} | |
static void MoveEnumeratorToNoNext<T> (IEnumerator<T> enumerator) | |
{ | |
while (enumerator.MoveNext ()) { | |
} | |
} | |
static IEnumerable<T> YieldParams<T> (params T[] args) | |
{ | |
foreach (T it in args) { | |
yield return it; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment