gem5  v22.1.0.0
memoizer.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 Arm Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include <gtest/gtest.h>
39 
40 #include "base/memoizer.hh"
41 
42 using namespace gem5;
43 
44 namespace
45 {
46 
47 uint32_t
48 fibonacci(uint32_t n)
49 {
50  if (n == 0) return 0;
51  if (n == 1) return 1;
52 
53  return fibonacci(n-1) + fibonacci(n-2);
54 }
55 
56 using FibonacciMemoizer = decltype(Memoizer(fibonacci));
57 
58 class FibonacciMemoizerFixture : public FibonacciMemoizer,
59  public ::testing::Test
60 {
61  public:
62  FibonacciMemoizerFixture()
63  : FibonacciMemoizer(fibonacci)
64  {}
65 
66 };
67 
68 }
69 
73 TEST_F(FibonacciMemoizerFixture, Uncached)
74 {
75  const auto res10 = fibonacci(10);
76 
77  // Fresh memoizer, input = 10 shouldn't be present
78  ASSERT_FALSE(cached(10));
79 
80  // We are now memoizing the result and making sure
81  // it provides the same value
82  EXPECT_EQ((*this)(10), res10);
83 
84  // Now the fibonacci output for input = 10 should be cached
85  ASSERT_TRUE(cached(10));
86 }
87 
91 TEST_F(FibonacciMemoizerFixture, MultipleValues)
92 {
93  const auto res0 = fibonacci(0);
94  const auto res10 = fibonacci(10);
95  const auto res20 = fibonacci(20);
96 
97  EXPECT_EQ((*this)(0), res0);
98  EXPECT_EQ((*this)(10), res10);
99  EXPECT_EQ((*this)(20), res20);
100 
101  EXPECT_EQ(cacheSize(), 3);
102 
103  EXPECT_TRUE(cached(0));
104  EXPECT_TRUE(cached(10));
105  EXPECT_TRUE(cached(20));
106 
107  // fibonacci(30) shouldn't be cached
108  EXPECT_FALSE(cached(30));
109 }
110 
114 TEST_F(FibonacciMemoizerFixture, CacheFlush)
115 {
116  const auto res10 = fibonacci(10);
117 
118  ASSERT_EQ(cacheSize(), 0);
119 
120  // Memoizing fibonacci(10)
121  EXPECT_EQ((*this)(10), res10);
122  ASSERT_EQ(cacheSize(), 1);
123 
124  // Flushing the cache
125  flush();
126 
127  // Cache should be empty now
128  ASSERT_EQ(cacheSize(), 0);
129 }
This class takes a function as a constructor argument and memoizes it: every time the function gets i...
Definition: memoizer.hh:83
TEST_F(FibonacciMemoizerFixture, Uncached)
Testing result cache before and after a memoized call.
Bitfield< 31 > n
Definition: misc_types.hh:462
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1