public void TestInfiniteIterator() { try { var tokenSource = new CancellationTokenSource(); Exception caughtException = null; var task = Task.Run(() => { try { InfiniteRandomIntegerEnumerator it = new InfiniteRandomIntegerEnumerator(); IEnumerator[] iterators = new IEnumerator[] { it }; CompoundEnumerator ci = new CompoundEnumerator(iterators); for (int i = 0; i < 10 && ci.MoveNext(); i++) { var current = ci.Current; } } catch (Exception t) { caughtException = t; } }, tokenSource.Token); const int timeout = 10 * 1000; task.Wait(timeout, tokenSource.Token); if (task.Status != TaskStatus.RanToCompletion) { tokenSource.Cancel(); Console.WriteLine("Improper handling of infinite Iterator; had to wait more than " + timeout + "ms to construct and make 10 calls to MoveNext()."); } if (caughtException != null) { throw caughtException; } if (task.Exception != null) { throw task.Exception; } } catch (Exception e) { Console.WriteLine("Exception caught within test"); Console.WriteLine(e.ToString()); throw e; } }
private void doSequentialIntegerTest(int[][] testData, bool shouldCallHasNext) { IEnumerator[] iterators = new IEnumerator[testData.Length]; int numInts = 0; for (int i = 0; i < testData.Length; i++) { if (testData[i] == null) { iterators[i] = null; } else { var intList = intArrayToList(testData[i]); iterators[i] = intList.GetEnumerator(); numInts += intList.Count; } } Console.WriteLine(intMatrixToString(testData)); CompoundEnumerator iter = new CompoundEnumerator(iterators); int count = 0; while (iter.MoveNext()) { int integerFromIterator = (int)iter.Current; if (count++ != integerFromIterator) { throw new Exception("Unexpected value returned from CompoundIterator; " + "test data was: " + intMatrixToString(testData) + ". "); } } if (numInts != count) { throw new Exception("Expected number of elements in compound iterator " + "to be the sum of the number of elements " + "in the individual iterators"); } var ret = iter.MoveNext(); if (ret) { throw new Exception("Move Next should continue to return false"); } }