コード例 #1
0
        public void Memoize_with_default_dict_should_call_handler_once()
        {
            // Arrange
            int value     = 0;
            var memoized1 = FlowUtils.Memoize(() => ++ value);

            // Act & Assert
            Assert.Equal(1, memoized1());
            Assert.Equal(1, memoized1());
        }
コード例 #2
0
        public void Cache_with_max_count_should_save_within_count()
        {
            // Arrange
            int value     = 0;
            var memoized1 = FlowUtils.Memoize(
                (a) => ++ value,
                FlowUtils.CreateMaxCountCacheStrategy <int, int>(maxCount: 3, removeCount: 2)
                );

            // Act & assert
            Assert.Equal(1, memoized1(1));
            Assert.Equal(2, memoized1(2));
            Assert.Equal(3, memoized1(3));
            Assert.Equal(1, memoized1(1)); // cached here
            Assert.Equal(4, memoized1(4)); // cache reset here
            Assert.Equal(5, memoized1(1)); // not cached now
            Assert.Equal(3, memoized1(3)); // but this one is cached
        }
コード例 #3
0
        public void Cache_with_max_age_should_cache_within_period()
        {
            // Arrange
            int value     = 0;
            var memoized1 = FlowUtils.Memoize(
                () => ++ value,
                FlowUtils.CreateMaxAgeCacheStrategy <int>(TimeSpan.FromSeconds(1))
                );

            // Act & assert
            Assert.Equal(1, memoized1());
            Assert.Equal(1, memoized1());
#if PORTABLE || NETSTANDARD1_6
            Task.Delay(1300).Wait();
#else
            Thread.Sleep(1300);
#endif
            Assert.Equal(2, memoized1());
        }
コード例 #4
0
        public void Cache_with_composite_key_should_cache()
        {
            // Arrange
            int totalCalls = 0;
            Func <int, string, int> func = (a, b) => ++ totalCalls;
            var memoizedFunc             = FlowUtils.Memoize(
                func, FlowUtils.CreateMaxCountCacheStrategy <int, string, int>(100));

            // Act
            var val1 = memoizedFunc(10, "string");
            var val2 = memoizedFunc(10, "string");
            var val3 = memoizedFunc(10, "string2");
            var val4 = memoizedFunc(11, "string");
            var val5 = memoizedFunc(10, "string");
            var val6 = memoizedFunc(11, "string");

            // Assert
            Assert.Equal(1, val1);
            Assert.Equal(1, val2);
            Assert.Equal(2, val3);
            Assert.Equal(3, val4);
            Assert.Equal(1, val5);
            Assert.Equal(3, val6);
        }