Ejemplo n.º 1
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
        }
Ejemplo n.º 2
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);
        }