public void TakeLastEntireSequence() { var sequence = new EnumerableMonitor <int>(Enumerable.Range(1, 3)); CollectionAssert.AreEqual(new[] { 1, 2, 3 }, sequence.TakeLast(5)); Assert.AreEqual(3, sequence.RequestCount); }
public void SplitIntoBinsEnumerable() { int[] coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 }; const int nBinCount = 5; int[] expectedBinSizes = { 3, 3, 2, 2, 2 }; EnumerableMonitor <int> seq = new EnumerableMonitor <int>(coll); int baseRequestCount = coll.Length; // SplitIntoBins calls Count(), which enumerates the sequence int nCurrentIndex = 0; int nCurrentBin = 0; foreach (IEnumerable <int> bin in EnumerableUtility.SplitIntoBins <int>(seq, nBinCount)) { int nBinSize = 0; foreach (int nValue in bin) { Assert.AreEqual(coll[nCurrentIndex], nValue); nCurrentIndex++; nBinSize++; } Assert.AreEqual(nCurrentIndex + baseRequestCount, seq.RequestCount); Assert.AreEqual(expectedBinSizes[nCurrentBin], nBinSize); nCurrentBin++; } Assert.AreEqual(nBinCount, nCurrentBin); Assert.AreEqual(coll.Length, nCurrentIndex); Assert.AreEqual(nCurrentIndex + baseRequestCount, seq.RequestCount); }
public void TakeLastPartialSequence() { var sequence = new EnumerableMonitor <int>(Enumerable.Range(1, 100)); CollectionAssert.AreEqual(new[] { 98, 99, 100 }, sequence.TakeLast(3)); Assert.AreEqual(100, sequence.RequestCount); }
public void EnumerateBatches() { const int nBatchSize = 3; int[] coll = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; EnumerableMonitor <int> seq = new EnumerableMonitor <int>(coll); int nCurrentIndex = 0; foreach (IEnumerable <int> batch in EnumerableUtility.EnumerateBatches(seq, nBatchSize)) { foreach (int nValue in batch) { Assert.AreEqual(coll[nCurrentIndex], nValue); int requestCount = Math.Min(((nCurrentIndex / nBatchSize) + 1) * nBatchSize, coll.Length); Assert.AreEqual(requestCount, seq.RequestCount); nCurrentIndex++; } // if we're not at the end, ensure that the batch was the right size if (nCurrentIndex != coll.Length) { Assert.AreEqual(0, nCurrentIndex % nBatchSize); } } Assert.AreEqual(nCurrentIndex, coll.Length); Assert.AreEqual(nCurrentIndex, seq.RequestCount); }