38#include <gtest/gtest.h>
53 return fibonacci(
n-1) + fibonacci(
n-2);
56using FibonacciMemoizer =
decltype(
Memoizer(fibonacci));
58class FibonacciMemoizerFixture :
public FibonacciMemoizer,
59 public ::testing::Test
62 FibonacciMemoizerFixture()
63 : FibonacciMemoizer(fibonacci)
73TEST_F(FibonacciMemoizerFixture, Uncached)
75 const auto res10 = fibonacci(10);
78 ASSERT_FALSE(cached(10));
82 EXPECT_EQ((*
this)(10), res10);
85 ASSERT_TRUE(cached(10));
91TEST_F(FibonacciMemoizerFixture, MultipleValues)
93 const auto res0 = fibonacci(0);
94 const auto res10 = fibonacci(10);
95 const auto res20 = fibonacci(20);
97 EXPECT_EQ((*
this)(0),
res0);
98 EXPECT_EQ((*
this)(10), res10);
99 EXPECT_EQ((*
this)(20), res20);
101 EXPECT_EQ(cacheSize(), 3);
103 EXPECT_TRUE(cached(0));
104 EXPECT_TRUE(cached(10));
105 EXPECT_TRUE(cached(20));
108 EXPECT_FALSE(cached(30));
114TEST_F(FibonacciMemoizerFixture, CacheFlush)
116 const auto res10 = fibonacci(10);
118 ASSERT_EQ(cacheSize(), 0);
121 EXPECT_EQ((*
this)(10), res10);
122 ASSERT_EQ(cacheSize(), 1);
128 ASSERT_EQ(cacheSize(), 0);
This class takes a function as a constructor argument and memoizes it: every time the function gets i...
TEST_F(FibonacciMemoizerFixture, Uncached)
Testing result cache before and after a memoized call.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.