public void ChunkSourceLazily() { using IEnumerator <int[]> chunks = new FastInfiniteEnumerator <int>().Chunk(5).GetEnumerator(); chunks.MoveNext(); Assert.Equal(new[] { 0, 0, 0, 0, 0 }, chunks.Current); Assert.True(chunks.MoveNext()); }
public void IteratorStateShouldNotChangeIfNumberOfElementsIsUnbounded() { // With https://github.com/dotnet/corefx/pull/13628, Skip and Take return // the same type of iterator. For Take, there is a limit, or upper bound, // on how many items can be returned from the iterator. An integer field, // _state, is incremented to keep track of this and to stop enumerating once // we pass that limit. However, for Skip, there is no such limit and the // iterator can contain an unlimited number of items (including past int.MaxValue). // This test makes sure that, in Skip, _state is not incorrectly incremented, // so that it does not overflow to a negative number and enumeration does not // stop prematurely. var iterator = new FastInfiniteEnumerator <int>().Skip(1).GetEnumerator(); iterator.MoveNext(); // Make sure the underlying enumerator has been initialized. FieldInfo state = iterator.GetType().GetTypeInfo() .GetField("_state", BindingFlags.Instance | BindingFlags.NonPublic); // On platforms that do not have this change, the optimization may not be present // and the iterator may not have a field named _state. In that case, nop. if (state != null) { state.SetValue(iterator, int.MaxValue); for (int i = 0; i < 10; i++) { Assert.True(iterator.MoveNext()); } } }