gem5 v24.0.0.0
Loading...
Searching...
No Matches
port.test.cc
Go to the documentation of this file.
1/*
2 * Copyright 2021 Daniel R. Carvalho
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met: redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer;
8 * redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution;
11 * neither the name of the copyright holders nor the names of its
12 * contributors may be used to endorse or promote products derived from
13 * this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <gmock/gmock.h>
29#include <gtest/gtest.h>
30
31#include <iostream>
32#include <sstream>
33#include <streambuf>
34
35#include "base/gtest/logging.hh"
36#include "sim/port.hh"
37
38using namespace gem5;
39
40class TestPort : public Port
41{
42 public:
43 TestPort(PortID _id) : Port("TestPort", _id) {}
44};
45
47TEST(PortTest, GetId)
48{
49 TestPort port(0), port2(2);
50 ASSERT_EQ(port.getId(), 0);
51 ASSERT_EQ(port2.getId(), 2);
52}
53
55TEST(PortTest, OneSidedConnection)
56{
57 TestPort port(0), port2(2);
58
59 // Both ports start unconnected
60 ASSERT_FALSE(port.isConnected());
61 ASSERT_FALSE(port2.isConnected());
62
63 // Bind one of the ports to the other
64 port.bind(port2);
65
66 // Binding a single side does not bind both ports automatically
67 ASSERT_TRUE(port.isConnected());
68 ASSERT_EQ(&port.getPeer(), &port2);
69 ASSERT_FALSE(port2.isConnected());
70
71 // Unbind the port
72 port.unbind();
73 ASSERT_FALSE(port.isConnected());
74 ASSERT_FALSE(port2.isConnected());
75}
76
78TEST(PortTest, TwoSidedConnection)
79{
80 TestPort port(0), port2(2);
81
82 // Both ports start unconnected
83 ASSERT_FALSE(port.isConnected());
84 ASSERT_FALSE(port2.isConnected());
85
86 // Bind the ports
87 port.bind(port2);
88 port2.bind(port);
89 ASSERT_TRUE(port.isConnected());
90 ASSERT_EQ(&port.getPeer(), &port2);
91 ASSERT_TRUE(port2.isConnected());
92 ASSERT_EQ(&port2.getPeer(), &port);
93
94 // Unbinding one port does not automatically unbind the other
95 port.unbind();
96 ASSERT_FALSE(port.isConnected());
97 ASSERT_TRUE(port2.isConnected());
98 ASSERT_EQ(&port2.getPeer(), &port);
99
100 // Finish unbinding
101 port2.unbind();
102 ASSERT_FALSE(port.isConnected());
103 ASSERT_FALSE(port2.isConnected());
104}
105
107TEST(PortTest, OverwriteConnection)
108{
109 TestPort port(0), port2(2), port3(6);
110
111 // All ports start unconnected
112 ASSERT_FALSE(port.isConnected());
113 ASSERT_FALSE(port2.isConnected());
114 ASSERT_FALSE(port3.isConnected());
115
116 // Bind one of the ports to the other
117 port.bind(port2);
118 ASSERT_TRUE(port.isConnected());
119 ASSERT_EQ(&port.getPeer(), &port2);
120 ASSERT_FALSE(port2.isConnected());
121 ASSERT_FALSE(port3.isConnected());
122
123 // Bind one of the ports to a third
124 port.bind(port3);
125 ASSERT_TRUE(port.isConnected());
126 ASSERT_EQ(&port.getPeer(), &port3);
127 ASSERT_FALSE(port2.isConnected());
128 ASSERT_FALSE(port3.isConnected());
129}
130
132TEST(PortDeathTest, TakeOverNoPort)
133{
134#ifdef NDEBUG
135 GTEST_SKIP() << "Skipping as assertions are "
136 "stripped out of fast builds";
137#endif
138 TestPort port(0);
139 ASSERT_DEATH(port.takeOverFrom(nullptr), "");
140}
141
143TEST(PortDeathTest, TakeOverDisconnected)
144{
145#ifdef NDEBUG
146 GTEST_SKIP() << "Skipping as assertions are "
147 "stripped out of fast builds";
148#endif
149 TestPort port(0), port2(2);
150 ASSERT_DEATH(port.takeOverFrom(&port2), "");
151}
152
158TEST(PortDeathTest, TakeOverConnected)
159{
160#ifdef NDEBUG
161 GTEST_SKIP() << "Skipping as assertions are "
162 "stripped out of fast builds";
163#endif
164 TestPort port(0), port2(2);
165 port.bind(port2);
166 ASSERT_DEATH(port.takeOverFrom(&port2), "");
167}
168
174TEST(PortDeathTest, TakeOverOneSided)
175{
176#ifdef NDEBUG
177 GTEST_SKIP() << "Skipping as assertions are "
178 "stripped out of fast builds";
179#endif
180 TestPort port(0), port2(2), port3(6);
181 port2.bind(port3);
182 ASSERT_DEATH(port.takeOverFrom(&port2), "");
183}
184
192TEST(PortTest, TakeOverOneSided)
193{
194 TestPort port(0), port2(2), port3(6), port4(10);
195 port2.bind(port3);
196 port3.bind(port4);
197 port.takeOverFrom(&port2);
198
199 ASSERT_TRUE(port.isConnected());
200 ASSERT_EQ(&port.getPeer(), &port3);
201 ASSERT_FALSE(port2.isConnected());
202 ASSERT_TRUE(port3.isConnected());
203 ASSERT_EQ(&port3.getPeer(), &port);
204 ASSERT_FALSE(port4.isConnected());
205}
206
213TEST(PortTest, TakeOver)
214{
215 TestPort port(0), port2(2), port3(6);
216 port2.bind(port3);
217 port3.bind(port2);
218 port.takeOverFrom(&port2);
219
220 ASSERT_TRUE(port.isConnected());
221 ASSERT_EQ(&port.getPeer(), &port3);
222 ASSERT_TRUE(port3.isConnected());
223 ASSERT_EQ(&port3.getPeer(), &port);
224}
225
227TEST(PortTest, Print)
228{
229 // Temporarily redirect cout
230 std::streambuf *old;
231 std::ostringstream buffer;
232 old = std::cout.rdbuf(buffer.rdbuf());
233
234 // Must use EXPECT, so that cout is restored
235 TestPort port(0);
236 std::cout << port << std::endl;
237 EXPECT_EQ(buffer.str(), "TestPort\n");
238
239 // Restore cout's streambuf
240 std::cout.rdbuf(old);
241}
TestPort(PortID _id)
Definition port.test.cc:43
Ports are used to interface objects to each other.
Definition port.hh:62
Port & getPeer()
Return a reference to this port's peer.
Definition port.hh:108
bool isConnected() const
Is this port currently connected to a peer?
Definition port.hh:133
void takeOverFrom(Port *old)
A utility function to make it easier to swap out ports.
Definition port.hh:137
virtual void unbind()
Dettach from a peer port.
Definition port.hh:126
virtual void bind(Port &peer)
Attach to a peer port.
Definition port.hh:118
PortID getId() const
Get the port id.
Definition port.hh:114
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
int16_t PortID
Port index/ID type, and a symbolic name for an invalid port id.
Definition types.hh:245
TEST(PortTest, GetId)
Test getting the port id.
Definition port.test.cc:47
Port Object Declaration.

Generated on Tue Jun 18 2024 16:24:06 for gem5 by doxygen 1.11.0