gem5  v21.1.0.2
fiber.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 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  * Copyright 2018 Google, Inc.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are
18  * met: redistributions of source code must retain the above copyright
19  * notice, this list of conditions and the following disclaimer;
20  * redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in the
22  * documentation and/or other materials provided with the distribution;
23  * neither the name of the copyright holders nor the names of its
24  * contributors may be used to endorse or promote products derived from
25  * this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 #include <gtest/gtest.h>
41 
42 #include <initializer_list>
43 #include <iostream>
44 #include <vector>
45 
46 #include "base/fiber.hh"
47 
48 using namespace gem5;
49 
55 TEST(Fiber, Starting)
56 {
57  class StartingFiber : public Fiber
58  {
59  public:
60  StartingFiber(Fiber *link) : Fiber(link) {}
61  void main() { }
62  };
63 
64  StartingFiber fiber(Fiber::primaryFiber());
65 
66  ASSERT_FALSE(fiber.started());
67 
68  fiber.run();
69 
70  ASSERT_TRUE(fiber.started());
71 }
72 
73 class SwitchingFiber : public Fiber
74 {
75  public:
76  const char *name;
78 
79  SwitchingFiber(const char *name, std::initializer_list<Fiber *> l);
80 
81  void checkExpected();
82  void main();
83 };
84 
85 extern SwitchingFiber a;
86 extern SwitchingFiber b;
87 extern SwitchingFiber c;
88 
89 SwitchingFiber a("A", { &b, &a, Fiber::primaryFiber(), &b, &c });
90 SwitchingFiber b("B", { &a, &c });
92 
95  &a, &b, &a, &a, /* main Fiber, */
96  &a, &b, &c, &a, &c,
97  /* main Fiber, */ &c, &c
98 });
99 
101  const char *name, std::initializer_list<Fiber *> l) :
102  name(name), next(l)
103 {}
104 
105 void
107 {
108  ASSERT_NE(expectedIt, expected.end());
110  EXPECT_EQ(e, this) << "Expected " << e->name << ", got " << name;
111 }
112 
113 void
115 {
116  checkExpected();
117  for (auto &n : next) {
118  n->run();
119  checkExpected();
120  }
121 }
122 
123 TEST(Fiber, Switching)
124 {
125  expectedIt = expected.begin();
126 
127  a.run();
128  EXPECT_EQ(expectedIt - expected.begin(), 4);
129 
130  a.run();
131  EXPECT_EQ(expectedIt - expected.begin(), 9);
132 
133  c.run();
134  EXPECT_EQ(expectedIt - expected.begin(), 10);
135 
136  EXPECT_FALSE(a.finished());
137  EXPECT_FALSE(b.finished());
138  EXPECT_FALSE(c.finished());
139 
140  c.run();
141  EXPECT_EQ(expected.end(), expectedIt) <<
142  "Didn't exactly use up the expected Fiber sequence";
143 
144  EXPECT_TRUE(c.finished());
145 }
146 
147 int currentIndex = 0;
148 
149 class LinkedFiber : public Fiber
150 {
151  public:
152  const int index;
154 
155  void
157  {
158  EXPECT_EQ(currentIndex, index);
159  currentIndex++;
160  }
161 };
162 
163 TEST(Fiber, Linked)
164 {
165  currentIndex = 0;
166 
167  LinkedFiber lf3(Fiber::primaryFiber(), 3);
168  LinkedFiber lf2(&lf3, 2);
169  LinkedFiber lf1(&lf2, 1);
170  LinkedFiber lf0(&lf1, 0);
171 
172  lf0.run();
173 
174  EXPECT_EQ(currentIndex, 4);
175 }
LinkedFiber
Definition: fiber.test.cc:149
gem5::Fiber::run
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:166
b
SwitchingFiber b
gem5::ArmISA::e
Bitfield< 9 > e
Definition: misc_types.hh:64
LinkedFiber::LinkedFiber
LinkedFiber(Fiber *link, int index)
Definition: fiber.test.cc:153
std::vector< Fiber * >
SwitchingFiber::checkExpected
void checkExpected()
Definition: fiber.test.cc:106
gem5::Fiber::primaryFiber
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Definition: fiber.cc:186
LinkedFiber::main
void main()
This method is called when this fiber is first run.
Definition: fiber.test.cc:156
SwitchingFiber::name
const char * name
Definition: fiber.test.cc:76
SwitchingFiber::next
std::vector< Fiber * > next
Definition: fiber.test.cc:77
TEST
TEST(Fiber, Starting)
This test is checking if the "started" member has its expected value before and after the fiber runs.
Definition: fiber.test.cc:55
SwitchingFiber::main
void main()
This method is called when this fiber is first run.
Definition: fiber.test.cc:114
gem5::Fiber
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
Definition: fiber.hh:71
gem5::MipsISA::l
Bitfield< 5 > l
Definition: pra_constants.hh:323
gem5::main
int main()
Definition: cprintftime.cc:58
currentIndex
int currentIndex
Definition: fiber.test.cc:147
name
const std::string & name()
Definition: trace.cc:49
a
SwitchingFiber a
expectedIt
std::vector< SwitchingFiber * >::iterator expectedIt
Definition: fiber.test.cc:93
gem5::Fiber::finished
bool finished() const
Returns whether the "main" function of this fiber has finished.
Definition: fiber.hh:109
gem5::ArmISA::n
Bitfield< 31 > n
Definition: misc_types.hh:455
SwitchingFiber
Definition: fiber.test.cc:73
LinkedFiber::index
const int index
Definition: fiber.test.cc:152
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
c
SwitchingFiber c
fiber.hh
SwitchingFiber::SwitchingFiber
SwitchingFiber(const char *name, std::initializer_list< Fiber * > l)
Definition: fiber.test.cc:100
gem5::Fiber::link
Fiber * link
Definition: fiber.hh:153
expected
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })

Generated on Tue Sep 21 2021 12:24:56 for gem5 by doxygen 1.8.17