Example #1
0
        public void FunctionMemoizationExtensions_Memoize_NoMemoizer_NoArgs_CacheError()
        {
            var n   = 0;
            var res = FunctionMemoizationExtensions.Memoize <int>(() => { n++; throw new InvalidOperationException(); }, MemoizationOptions.CacheException);

            Assert.AreEqual(0, n);
            Assert.AreEqual(0, res.Cache.Count);

            Assert.ThrowsException <InvalidOperationException>(() => res.Delegate());
            Assert.AreEqual(1, n);
            Assert.AreEqual(1, res.Cache.Count);

            Assert.ThrowsException <InvalidOperationException>(() => res.Delegate());
            Assert.AreEqual(1, n);
            Assert.AreEqual(1, res.Cache.Count);

            res.Cache.Clear();

            Assert.AreEqual(1, n);
            Assert.AreEqual(0, res.Cache.Count);

            Assert.ThrowsException <InvalidOperationException>(() => res.Delegate());
            Assert.AreEqual(2, n);
            Assert.AreEqual(1, res.Cache.Count);
        }
Example #2
0
        public void FunctionMemoizationExtensions_Memoize_NoMemoizer_NoArgs_Simple()
        {
            foreach (var options in new[] { MemoizationOptions.None, MemoizationOptions.CacheException })
            {
                var n   = 0;
                var res = FunctionMemoizationExtensions.Memoize(() => { n++; return(42); }, options);

                Assert.AreEqual(0, n);
                Assert.AreEqual(0, res.Cache.Count);

                Assert.AreEqual(42, res.Delegate());
                Assert.AreEqual(1, n);
                Assert.AreEqual(1, res.Cache.Count);

                Assert.AreEqual(42, res.Delegate());
                Assert.AreEqual(1, n);
                Assert.AreEqual(1, res.Cache.Count);

                res.Cache.Clear();

                Assert.AreEqual(1, n);
                Assert.AreEqual(0, res.Cache.Count);

                Assert.AreEqual(42, res.Delegate());
                Assert.AreEqual(2, n);
                Assert.AreEqual(1, res.Cache.Count);

                Assert.IsTrue(!string.IsNullOrEmpty(res.Cache.DebugView));
            }
        }
Example #3
0
        public void FunctionMemoizationExtensions_Memoize_NoArgs_ArgumentChecking()
        {
            var m = Memoizer.Create(MemoizationCacheFactory.Unbounded);

#pragma warning disable IDE0034 // Simplify 'default' expression (illustrative of method signature)
            Assert.ThrowsException <ArgumentNullException>(() => FunctionMemoizationExtensions.Memoize(default(Func <int>)));

            Assert.ThrowsException <ArgumentNullException>(() => FunctionMemoizationExtensions.Memoize(default(IMemoizer), () => 42));
            Assert.ThrowsException <ArgumentNullException>(() => FunctionMemoizationExtensions.Memoize(m, default(Func <int>)));
#pragma warning restore IDE0034 // Simplify 'default' expression
        }
Example #4
0
        public void FunctionMemoizationExtensions_MemoizeWeak_TDelegate_ArgumentChecking()
        {
            var mem = Memoizer.CreateWeak(WeakMemoizationCacheFactory.Unbounded);
            var a   = new A(() => { });

#pragma warning disable IDE0034 // Simplify 'default' expression (illustrative of method signature)
            Assert.ThrowsException <ArgumentNullException>(() => FunctionMemoizationExtensions.MemoizeWeak <A>(default(IWeakMemoizer), a, MemoizationOptions.None));
            Assert.ThrowsException <ArgumentNullException>(() => FunctionMemoizationExtensions.MemoizeWeak <A>(mem, default(A), MemoizationOptions.None));

            Assert.ThrowsException <ArgumentException>(() => FunctionMemoizationExtensions.MemoizeWeak <int>(mem, 42, MemoizationOptions.None));
            Assert.ThrowsException <NotSupportedException>(() => FunctionMemoizationExtensions.MemoizeWeak <Q>(mem, (ref string x) => { }, MemoizationOptions.None));
#pragma warning restore IDE0034 // Simplify 'default' expression
        }