public void TailRecursivePatternReturnsResultTest3(int expected) { //expected = Math.Max(0,input), since we add 1 at each iteration. This is only to test against a stack overflow var p = Pattern .Match(((int n, int sum)x) => x.n <= 0, x => x.sum) .MatchTailRec(x => x.n >= 1, x => (x.n - 1, (1 + x.sum))); var task = Task.Run(() => p.Run((expected, 0))); bool isCompleted = Task.WaitAll(new[] { task }, 30000); if (!isCompleted) { throw new TimeoutException(); } Assert.Equal(expected, task.Result); }
public void TailRecursivePatternReturnsResultTest2(long expected, int input) { //Factorial var p = Pattern .Match(((int n, long sum)x) => x.n <= 1, x => x.sum) .MatchTailRec(x => x.n > 1, x => (x.n - 1, (x.n * x.sum))); var task = Task.Run(() => p.Run((input, 1))); bool isCompleted = Task.WaitAll(new[] { task }, 30000); if (!isCompleted) { throw new TimeoutException(); } Assert.Equal(expected, task.Result); }