public void MemoizeLast() { var calls = 0; var f = Memoization.MemoizeLast(delegate(int i) { calls++; return(i * 2); }); Assert.Equal(4, f(2)); Assert.Equal(1, calls); Assert.Equal(4, f(2)); Assert.Equal(1, calls); Assert.Equal(8, f(4)); Assert.Equal(2, calls); }
public void MemoizeLastWithCustomComparer() { var calls = 0; var comparer = StringComparer.OrdinalIgnoreCase; var f = Memoization.MemoizeLast(delegate(string s) { calls++; return(s.ToUpperInvariant()); }, comparer); Assert.Equal("FOOBAR", f("foobar")); Assert.Equal(1, calls); Assert.Equal("FOOBAR", f("FOOBAR")); Assert.Equal(1, calls); Assert.Equal("FOOBAR", f("FoObAr")); Assert.Equal(1, calls); }
public void MemoizeLastWithNullInputComparer() { var f = Memoization.MemoizeLast <object, object>(_ => null, null); Assert.Null(f(new object())); }
public void MemoizeLastThrowsWithNullFunction() { var e = Assert.Throws <ArgumentNullException>(() => Memoization.MemoizeLast <object, object>(null)); Assert.Equal("function", e.ParamName); }