gem5  v19.0.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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  * Authors: Gabe Black
40  * Giacomo Travaglini
41  */
42 
43 #include <gtest/gtest.h>
44 
45 #include <initializer_list>
46 #include <iostream>
47 #include <vector>
48 
49 #include "base/fiber.hh"
50 
56 TEST(Fiber, Starting)
57 {
58  class StartingFiber : public Fiber
59  {
60  public:
61  StartingFiber(Fiber *link) : Fiber(link) {}
62  void main() { }
63  };
64 
65  StartingFiber fiber(Fiber::primaryFiber());
66 
67  ASSERT_FALSE(fiber.started());
68 
69  fiber.run();
70 
71  ASSERT_TRUE(fiber.started());
72 }
73 
74 class SwitchingFiber : public Fiber
75 {
76  public:
77  const char *name;
79 
80  SwitchingFiber(const char *name, std::initializer_list<Fiber *> l);
81 
82  void checkExpected();
83  void main();
84 };
85 
86 extern SwitchingFiber a;
87 extern SwitchingFiber b;
88 extern SwitchingFiber c;
89 
90 SwitchingFiber a("A", { &b, &a, Fiber::primaryFiber(), &b, &c });
91 SwitchingFiber b("B", { &a, &c });
93 
96  &a, &b, &a, &a, /* main Fiber, */
97  &a, &b, &c, &a, &c,
98  /* main Fiber, */ &c, &c
99 });
100 
102  const char *name, std::initializer_list<Fiber *> l) :
103  name(name), next(l)
104 {}
105 
106 void
108 {
109  ASSERT_NE(expectedIt, expected.end());
110  SwitchingFiber *e = *expectedIt++;
111  EXPECT_EQ(e, this) << "Expected " << e->name << ", got " << name;
112 }
113 
114 void
116 {
117  checkExpected();
118  for (auto &n : next) {
119  n->run();
120  checkExpected();
121  }
122 }
123 
124 TEST(Fiber, Switching)
125 {
126  expectedIt = expected.begin();
127 
128  a.run();
129  EXPECT_EQ(expectedIt - expected.begin(), 4);
130 
131  a.run();
132  EXPECT_EQ(expectedIt - expected.begin(), 9);
133 
134  c.run();
135  EXPECT_EQ(expectedIt - expected.begin(), 10);
136 
137  EXPECT_FALSE(a.finished());
138  EXPECT_FALSE(b.finished());
139  EXPECT_FALSE(c.finished());
140 
141  c.run();
142  EXPECT_EQ(expected.end(), expectedIt) <<
143  "Didn't exactly use up the expected Fiber sequence";
144 
145  EXPECT_TRUE(c.finished());
146 }
147 
148 int currentIndex = 0;
149 
150 class LinkedFiber : public Fiber
151 {
152  public:
153  const int index;
154  LinkedFiber(Fiber *link, int index) : Fiber(link), index(index) {}
155 
156  void
158  {
159  EXPECT_EQ(currentIndex, index);
160  currentIndex++;
161  }
162 };
163 
164 TEST(Fiber, Linked)
165 {
166  currentIndex = 0;
167 
169  LinkedFiber lf2(&lf3, 2);
170  LinkedFiber lf1(&lf2, 1);
171  LinkedFiber lf0(&lf1, 0);
172 
173  lf0.run();
174 
175  EXPECT_EQ(currentIndex, 4);
176 }
void run()
Start executing the fiber represented by this object.
Definition: fiber.cc:165
bool finished() const
Returns whether the "main" function of this fiber has finished.
Definition: fiber.hh:83
std::vector< SwitchingFiber * >::iterator expectedIt
Definition: fiber.test.cc:94
const std::string & name()
Definition: trace.cc:54
static Fiber * primaryFiber()
Get a pointer to the primary Fiber.
Definition: fiber.cc:184
int currentIndex
Definition: fiber.test.cc:148
std::vector< Fiber * > next
Definition: fiber.test.cc:78
SwitchingFiber a
SwitchingFiber c
STL vector class.
Definition: stl.hh:40
int main(int argc, char **argv)
Definition: marshal.cc:48
SwitchingFiber(const char *name, std::initializer_list< Fiber *> l)
Definition: fiber.test.cc:101
Bitfield< 31 > n
void checkExpected()
Definition: fiber.test.cc:107
#define EXPECT_TRUE(expr)
A macro which verifies that expr evaluates to true.
Definition: unittest.hh:105
SwitchingFiber b
std::vector< SwitchingFiber * > expected({ &a, &b, &a, &a, &a, &b, &c, &a, &c, &c, &c })
void main()
This method is called when this fiber is first run.
Definition: fiber.test.cc:157
#define EXPECT_FALSE(expr)
A macro which verifies that expr evaluates to false.
Definition: unittest.hh:108
const char * name
Definition: fiber.test.cc:77
Bitfield< 9 > e
This class represents a fiber, which is a light weight sort of thread which is cooperatively schedule...
Definition: fiber.hh:64
void main()
This method is called when this fiber is first run.
Definition: fiber.test.cc:115
Fiber(size_t stack_size=DefaultStackSize)
stack_size is the size of the stack available to this fiber.
Definition: fiber.cc:86
virtual void main()=0
This method is called when this fiber is first run.
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:56
Bitfield< 5 > l
#define EXPECT_EQ(lhs, rhs)
A macro which verifies that lhs and rhs are equal to each other.
Definition: unittest.hh:112
Fiber * link
Definition: fiber.hh:110
const int index
Definition: fiber.test.cc:153
LinkedFiber(Fiber *link, int index)
Definition: fiber.test.cc:154

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