gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
callback.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 The Regents of the University of California
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  * Authors: Bobby R. Bruce
38  */
39 
40 #include <gtest/gtest.h>
41 
42 #include "base/callback.hh"
43 
44 class CallbackImpl : public Callback
45 {
46  public:
47  bool processed = false;
48  void process()
49  {
50  processed = true;
51  }
52 };
53 
54 class MockClass
55 {
56  public:
57  bool methodExecuted = false;
58  void method()
59  {
60  methodExecuted = true;
61  }
62 };
63 
64 TEST(CallbackQueueTest, GetName)
65 {
66  CallbackQueue callbackQueue;
67  EXPECT_EQ("CallbackQueue", callbackQueue.name());
68 }
69 
70 TEST(CallbackQueueTest, IsEmpty)
71 {
72  CallbackQueue callbackQueue;
73  EXPECT_TRUE(callbackQueue.empty());
74 }
75 
76 TEST(CallbackQueueTest, IsNotEmpty)
77 {
78  CallbackQueue callbackQueue;
80  callbackQueue.add(&impl);
81  EXPECT_FALSE(callbackQueue.empty());
82 }
83 
84 TEST(CallbackQueueTest, AddOneAndProcess)
85 {
86  CallbackQueue callbackQueue;
88  callbackQueue.add(&impl);
89  EXPECT_FALSE(impl.processed);
90  callbackQueue.process();
91  EXPECT_TRUE(impl.processed);
92  // Processing a queue does not clear it.
93  EXPECT_FALSE(callbackQueue.empty());
94 }
95 
96 TEST(CallbackQueueTest, AddManyAndProcess)
97 {
98  CallbackQueue callbackQueue;
99  CallbackImpl impl1;
100  CallbackImpl impl2;
101  CallbackImpl impl3;
102  CallbackImpl impl4;
103  callbackQueue.add(&impl1);
104  callbackQueue.add(&impl2);
105  callbackQueue.add(&impl3);
106  callbackQueue.add(&impl4);
107  EXPECT_FALSE(impl1.processed);
108  EXPECT_FALSE(impl2.processed);
109  EXPECT_FALSE(impl3.processed);
110  EXPECT_FALSE(impl4.processed);
111  callbackQueue.process();
112  EXPECT_TRUE(impl1.processed);
113  EXPECT_TRUE(impl2.processed);
114  EXPECT_TRUE(impl3.processed);
115  EXPECT_TRUE(impl4.processed);
116  EXPECT_FALSE(callbackQueue.empty());
117 }
118 
119 TEST(CallbackQueueTest, ClearQueue)
120 {
121  CallbackQueue callbackQueue;
122  CallbackImpl callbackImpl;
123  callbackQueue.add(&callbackImpl);
124  EXPECT_FALSE(callbackQueue.empty());
125  callbackQueue.clear();
126  EXPECT_TRUE(callbackQueue.empty());
127 }
128 
129 TEST(CallbackQueueTest, MakeCallbackAddByReference)
130 {
131  CallbackQueue callbackQueue;
132  MockClass mockClass;
133  EXPECT_FALSE(mockClass.methodExecuted);
134  callbackQueue.add<MockClass, &MockClass::method>(mockClass);
135  callbackQueue.process();
136  EXPECT_TRUE(mockClass.methodExecuted);
137  EXPECT_FALSE(callbackQueue.empty());
138 }
139 
140 TEST(CallbackQueueTest, MakeCallbackAddByPointer)
141 {
142  CallbackQueue callbackQueue;
143  MockClass mockClass;
144  EXPECT_FALSE(mockClass.methodExecuted);
145  callbackQueue.add<MockClass, &MockClass::method>(&mockClass);
146  callbackQueue.process();
147  EXPECT_TRUE(mockClass.methodExecuted);
148  EXPECT_FALSE(callbackQueue.empty());
149 }
Generic callback class.
Definition: callback.hh:41
void process()
process all callbacks
Definition: callback.hh:135
TEST(CallbackQueueTest, GetName)
std::string name() const
Definition: callback.hh:100
bool methodExecuted
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:105
Bitfield< 19, 16 > impl
Definition: mt_constants.hh:90
void add(Callback *callback)
Add a callback to the end of the queue.
Definition: callback.hh:107
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:108
void clear()
clear the callback queue
Definition: callback.hh:150
void process()
virtual process function that is invoked when the callback queue is executed.
bool empty() const
Find out if there are any callbacks in the queue.
Definition: callback.hh:129
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:112
void method()

Generated on Fri Feb 28 2020 16:26:58 for gem5 by doxygen 1.8.13